/ tools / update_from_atmel_start.py
update_from_atmel_start.py
 1  import requests
 2  import zipfile
 3  import os.path
 4  import shutil
 5  import os
 6  import os.path
 7  import sys
 8  import subprocess
 9  
10  if not subprocess.check_output(['git', 'rev-parse', '--show-toplevel'], universal_newlines=True).strip() == os.getcwd():
11      print('Please run "{}" from the top directory in the asf4 repo.'.format(sys.argv[0]))
12      sys.exit(1)
13  
14  # Change .gitignore if you change these dir names.
15  DOWNLOADED_ZIP_DIR = 'downloaded-zip'
16  DOWNLOADED_DIR = 'downloaded'
17  
18  for chip in ['samd21', 'samd51']:
19      r = None
20      os.makedirs(DOWNLOADED_ZIP_DIR, exist_ok=True)
21      filename = os.path.join(DOWNLOADED_ZIP_DIR, chip + '.zip')
22      if os.path.isfile(filename):
23          print('NOTE:', filename, 'already downloaded. Delete it and re-run if you want to re-download')
24      else:
25          print("Downloading", filename, "...")
26          with open('tools/' + chip + '.json', 'r') as project_json:
27              headers = {'content-type': 'text/plain'}
28              r = requests.post('http://start.atmel.com/api/v1/generate/?format=atzip&compilers=[atmel_studio,make]&file_name_base=My%20Project', headers=headers, data=project_json)
29          if not r.ok:
30              # Double check that the JSON is minified. If it's not, you'll get a 404.
31              print(r.text)
32              sys.exit(1)
33          with open(filename, 'wb') as out:
34              out.write(r.content)
35  
36      # Extract to a temporary location and normalize before replacing the existing location.
37      z = zipfile.ZipFile(filename)
38      downloaded_chip_dir = os.path.join(DOWNLOADED_DIR, chip)
39      # Clean up old zip extraction.
40      if os.path.isdir(downloaded_chip_dir):
41          shutil.rmtree(downloaded_chip_dir)
42      print("Unzipping ...")
43      z.extractall(downloaded_chip_dir)
44  
45      # Remove all carriage returns.
46      for dirpath, dirnames, filenames in os.walk(downloaded_chip_dir):
47          for fn in filenames:
48              fn = os.path.join(dirpath, fn)
49              subprocess.run(['sed', '-i', 's/\r//g', fn])
50  
51      # Move files to match SAMD51 structure.
52      if chip == 'samd21':
53          shutil.move(os.path.join(downloaded_chip_dir, 'samd21a/include'), downloaded_chip_dir)
54          shutil.move(os.path.join(downloaded_chip_dir, 'samd21a/gcc/gcc'), os.path.join(downloaded_chip_dir, 'gcc'))
55          shutil.move(os.path.join(downloaded_chip_dir, 'samd21a/gcc/system_samd21.c'), os.path.join(downloaded_chip_dir, 'gcc'))
56  
57      print("Updating",chip,"from",downloaded_chip_dir)
58      subprocess.run(['rsync', '-r', '--delete', downloaded_chip_dir + '/', chip], check=True)