alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | # Copyright (c) 2021 Arm Limited. All rights reserved. |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | |
| 18 | # The script does effectively the same as "git submodule update --init" command. |
| 19 | |
| 20 | import logging |
| 21 | import os |
| 22 | import sys |
| 23 | import tarfile |
| 24 | import tempfile |
| 25 | from urllib.request import urlopen |
| 26 | from zipfile import ZipFile |
| 27 | |
Cisco Cervellera | 414b1b9 | 2021-09-28 18:15:10 +0100 | [diff] [blame] | 28 | tf = "https://github.com/tensorflow/tflite-micro/archive/4e091a46e935afcb27035e538044e37892d4a983.zip" |
| 29 | cmsis = "https://github.com/ARM-software/CMSIS_5/archive/c43379be65dec9d808b72b8efbe52cad762909be.zip" |
| 30 | ethos_u_core_sw = "https://git.mlplatform.org/ml/ethos-u/ethos-u-core-software.git/snapshot/ethos-u-core-software-21.08.tar.gz" |
| 31 | ethos_u_core_driver = "https://git.mlplatform.org/ml/ethos-u/ethos-u-core-driver.git/snapshot/ethos-u-core-driver-21.08.tar.gz" |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 32 | |
| 33 | |
| 34 | def download(url_file: str, post_process=None): |
| 35 | with urlopen(url_file) as response, tempfile.NamedTemporaryFile() as temp: |
| 36 | logging.info(f"Downloading {url_file} ...") |
| 37 | temp.write(response.read()) |
| 38 | temp.seek(0) |
| 39 | logging.info(f"Finished downloading {url_file}.") |
| 40 | if post_process: |
| 41 | post_process(temp) |
| 42 | |
| 43 | |
| 44 | def unzip(file, to_path): |
| 45 | with ZipFile(file) as z: |
| 46 | for archive_path in z.infolist(): |
| 47 | archive_path.filename = archive_path.filename[archive_path.filename.find("/") + 1:] |
| 48 | if archive_path.filename: |
| 49 | z.extract(archive_path, to_path) |
| 50 | target_path = os.path.join(to_path, archive_path.filename) |
| 51 | attr = archive_path.external_attr >> 16 |
| 52 | if attr != 0: |
| 53 | os.chmod(target_path, attr) |
| 54 | |
| 55 | |
| 56 | def untar(file, to_path): |
| 57 | with tarfile.open(file) as z: |
| 58 | for archive_path in z.getmembers(): |
| 59 | index = archive_path.name.find("/") |
| 60 | if index < 0: |
| 61 | continue |
| 62 | archive_path.name = archive_path.name[index + 1:] |
| 63 | if archive_path.name: |
| 64 | z.extract(archive_path, to_path) |
| 65 | |
| 66 | |
| 67 | def main(dependencies_path: str): |
| 68 | |
| 69 | download(cmsis, |
| 70 | lambda file: unzip(file.name, |
| 71 | to_path=os.path.join(dependencies_path, "cmsis"))) |
| 72 | download(ethos_u_core_sw, |
| 73 | lambda file: untar(file.name, |
| 74 | to_path=os.path.join(dependencies_path, "core-software"))) |
| 75 | download(ethos_u_core_driver, |
| 76 | lambda file: untar(file.name, |
| 77 | to_path=os.path.join(dependencies_path, "core-driver"))) |
| 78 | download(tf, |
| 79 | lambda file: unzip(file.name, |
| 80 | to_path=os.path.join(dependencies_path, "tensorflow"))) |
| 81 | |
| 82 | |
| 83 | if __name__ == '__main__': |
Isabella Gottardi | 3149486 | 2021-08-13 16:29:39 +0100 | [diff] [blame] | 84 | logging.basicConfig(filename='download_dependencies.log', level=logging.DEBUG, filemode='w') |
Isabella Gottardi | c5d8bda | 2021-07-21 10:35:08 +0100 | [diff] [blame] | 85 | logging.getLogger().addHandler(logging.StreamHandler(sys.stdout)) |
| 86 | |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 87 | download_dir = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "dependencies")) |
| 88 | |
| 89 | if os.path.isdir(download_dir): |
| 90 | logging.info(f'{download_dir} exists. Skipping download.') |
| 91 | else: |
| 92 | main(download_dir) |