circuitpython/atmel-samd/tools/update_asf.py
Scott Shawcroft 0bbb7a8199 Add support for patching newer ASF4 code. (#292)
* atmel-samd: Support patching after updating ASF4.

This makes it possible to automatically fix newer code.

* atmel-samd: Update ASF4 to include flash APIs for SAMD51.

This is the first automatic update that has caused a few deletions
where code was previously copied instead of moved.

This is a prerequisite for #260.
2017-09-28 23:33:44 -04:00

55 lines
2.0 KiB
Python

import requests
import zipfile
import os.path
import shutil
import sys
import subprocess
for chip in ["samd21", "samd51"]:
r = None
filename = chip + ".zip"
if not os.path.isfile(filename):
with open("tools/" + chip + ".json", "r") as project_json:
headers = {"content-type": "text/plain"}
r = requests.post("http://start.atmel.com/api/v1/generate/?format=atzip&compilers=[make]&file_name_base=My%20Project", headers=headers, data=project_json)
if not r.ok:
# Double check that the JSON is minified. If its not, you'll get a 404.
print(r.text)
sys.exit(1)
with open(filename, "wb") as out:
out.write(r.content)
# Extract to a temporary location and patch it before replacing the existing location.
z = zipfile.ZipFile(filename)
tmp_dir = "asf4/" + chip + "_vanilla"
z.extractall(tmp_dir)
# Remove all carriage returns.
for dirpath, dirnames, filenames in os.walk(tmp_dir):
for fn in filenames:
fn = os.path.join(dirpath, fn)
subprocess.run(["sed", "-i", "s/\r//g", fn])
# Move files to match SAMD51 structure.
if chip == "samd21":
shutil.move("asf4/samd21_vanilla/samd21a/include", "asf4/samd21_vanilla")
shutil.move("asf4/samd21_vanilla/samd21a/gcc/gcc", "asf4/samd21_vanilla/gcc")
shutil.move("asf4/samd21_vanilla/samd21a/gcc/system_samd21.c", "asf4/samd21_vanilla/gcc")
ok = True
for patch in os.listdir("asf4/patches/" + chip):
patch = "patches/" + chip + "/" + patch
print(patch)
result = subprocess.run(["patch", "-l", "-F", "10", "-u", "-p", "1", "-d", tmp_dir, "-i", "../" + patch])
ok = ok and result.returncode == 0
print()
if ok:
real_dir = "asf4/" + chip
shutil.rmtree(real_dir)
shutil.move(tmp_dir, real_dir)
# delete the zip on success
os.remove(filename)
else:
print("A patch failed!")