alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
Richard Burton | b4123fd | 2022-03-04 09:19:09 +0000 | [diff] [blame] | 3 | # Copyright (c) 2021-2022 Arm Limited. All rights reserved. |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 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 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 18 | """This script does effectively the same as "git submodule update --init" command.""" |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 19 | import logging |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 20 | import sys |
| 21 | import tarfile |
| 22 | import tempfile |
| 23 | from urllib.request import urlopen |
| 24 | from zipfile import ZipFile |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 25 | from pathlib import Path |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 26 | |
Richard Burton | 5dd1838 | 2022-06-01 10:26:46 +0100 | [diff] [blame] | 27 | TF = "https://github.com/tensorflow/tflite-micro/archive/02715237c1fc0a23f465226364d206277f54ebce.zip" |
| 28 | CMSIS = "https://github.com/ARM-software/CMSIS_5/archive/29615088b12e3ba8ce50d316cf7f38c1bd7fc620.zip" |
| 29 | ETHOS_U_CORE_DRIVER = "https://git.mlplatform.org/ml/ethos-u/ethos-u-core-driver.git/snapshot/ethos-u-core-driver-22.05.tar.gz" |
| 30 | ETHOS_U_CORE_PLATFORM = "https://git.mlplatform.org/ml/ethos-u/ethos-u-core-platform.git/snapshot/ethos-u-core-platform-22.05.tar.gz" |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 31 | |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 32 | |
| 33 | def download(url_file: str, post_process=None): |
| 34 | with urlopen(url_file) as response, tempfile.NamedTemporaryFile() as temp: |
| 35 | logging.info(f"Downloading {url_file} ...") |
| 36 | temp.write(response.read()) |
| 37 | temp.seek(0) |
| 38 | logging.info(f"Finished downloading {url_file}.") |
| 39 | if post_process: |
| 40 | post_process(temp) |
| 41 | |
| 42 | |
| 43 | def unzip(file, to_path): |
| 44 | with ZipFile(file) as z: |
| 45 | for archive_path in z.infolist(): |
| 46 | archive_path.filename = archive_path.filename[archive_path.filename.find("/") + 1:] |
| 47 | if archive_path.filename: |
| 48 | z.extract(archive_path, to_path) |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 49 | target_path = to_path / archive_path.filename |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 50 | attr = archive_path.external_attr >> 16 |
| 51 | if attr != 0: |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 52 | target_path.chmod(attr) |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 53 | |
| 54 | |
| 55 | def untar(file, to_path): |
| 56 | with tarfile.open(file) as z: |
| 57 | for archive_path in z.getmembers(): |
| 58 | index = archive_path.name.find("/") |
| 59 | if index < 0: |
| 60 | continue |
| 61 | archive_path.name = archive_path.name[index + 1:] |
| 62 | if archive_path.name: |
| 63 | z.extract(archive_path, to_path) |
| 64 | |
| 65 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 66 | def main(dependencies_path: Path): |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 67 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 68 | download(CMSIS, |
| 69 | lambda file: unzip(file.name, to_path=dependencies_path / "cmsis")) |
| 70 | download(ETHOS_U_CORE_DRIVER, |
| 71 | lambda file: untar(file.name, to_path=dependencies_path / "core-driver")) |
| 72 | download(ETHOS_U_CORE_PLATFORM, |
| 73 | lambda file: untar(file.name, to_path=dependencies_path / "core-platform")) |
| 74 | download(TF, |
| 75 | lambda file: unzip(file.name, to_path=dependencies_path / "tensorflow")) |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 76 | |
| 77 | |
| 78 | if __name__ == '__main__': |
Isabella Gottardi | 3149486 | 2021-08-13 16:29:39 +0100 | [diff] [blame] | 79 | logging.basicConfig(filename='download_dependencies.log', level=logging.DEBUG, filemode='w') |
Isabella Gottardi | c5d8bda | 2021-07-21 10:35:08 +0100 | [diff] [blame] | 80 | logging.getLogger().addHandler(logging.StreamHandler(sys.stdout)) |
| 81 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 82 | download_dir = Path(__file__).parent.resolve() / "dependencies" |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 83 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 84 | if download_dir.is_dir(): |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 85 | logging.info(f'{download_dir} exists. Skipping download.') |
| 86 | else: |
| 87 | main(download_dir) |