alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
Kshitij Sisodia | 26bc923 | 2023-03-10 16:33:23 +0000 | [diff] [blame] | 3 | # SPDX-FileCopyrightText: Copyright 2021-2023 Arm Limited and/or its affiliates <open-source-office@arm.com> |
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 | |
Kshitij Sisodia | 26bc923 | 2023-03-10 16:33:23 +0000 | [diff] [blame] | 27 | TF = "https://github.com/tensorflow/tflite-micro/archive/1d810cac73330d9bf156b38a111ffd8e86523362.zip" |
| 28 | CMSIS = "https://github.com/ARM-software/CMSIS_5/archive/41a6424ff4307bd52d94bfc2708874d4224848b6.zip" |
| 29 | CMSIS_DSP = "https://github.com/ARM-software/CMSIS-DSP/archive/refs/tags/v1.14.4.zip" |
| 30 | CMSIS_NN = "https://github.com/ARM-software/CMSIS-NN/archive/refs/tags/23.02.zip" |
| 31 | ETHOS_U_CORE_DRIVER = "https://git.mlplatform.org/ml/ethos-u/ethos-u-core-driver.git/snapshot/ethos-u-core-driver-23.02.tar.gz" |
| 32 | ETHOS_U_CORE_PLATFORM = "https://git.mlplatform.org/ml/ethos-u/ethos-u-core-platform.git/snapshot/ethos-u-core-platform-23.02.tar.gz" |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 33 | |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 34 | |
| 35 | def download(url_file: str, post_process=None): |
| 36 | with urlopen(url_file) as response, tempfile.NamedTemporaryFile() as temp: |
| 37 | logging.info(f"Downloading {url_file} ...") |
| 38 | temp.write(response.read()) |
| 39 | temp.seek(0) |
| 40 | logging.info(f"Finished downloading {url_file}.") |
| 41 | if post_process: |
| 42 | post_process(temp) |
| 43 | |
| 44 | |
| 45 | def unzip(file, to_path): |
| 46 | with ZipFile(file) as z: |
| 47 | for archive_path in z.infolist(): |
| 48 | archive_path.filename = archive_path.filename[archive_path.filename.find("/") + 1:] |
| 49 | if archive_path.filename: |
| 50 | z.extract(archive_path, to_path) |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 51 | target_path = to_path / archive_path.filename |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 52 | attr = archive_path.external_attr >> 16 |
| 53 | if attr != 0: |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 54 | target_path.chmod(attr) |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 55 | |
| 56 | |
| 57 | def untar(file, to_path): |
| 58 | with tarfile.open(file) as z: |
| 59 | for archive_path in z.getmembers(): |
| 60 | index = archive_path.name.find("/") |
| 61 | if index < 0: |
| 62 | continue |
| 63 | archive_path.name = archive_path.name[index + 1:] |
| 64 | if archive_path.name: |
| 65 | z.extract(archive_path, to_path) |
| 66 | |
| 67 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 68 | def main(dependencies_path: Path): |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 69 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 70 | download(CMSIS, |
| 71 | lambda file: unzip(file.name, to_path=dependencies_path / "cmsis")) |
Maksims Svecovs | 254853c | 2022-08-30 12:58:02 +0100 | [diff] [blame] | 72 | download(CMSIS_DSP, |
| 73 | lambda file: unzip(file.name, to_path=dependencies_path / "cmsis-dsp")) |
Richard Burton | 71f282e | 2022-12-01 12:31:23 +0000 | [diff] [blame] | 74 | download(CMSIS_NN, |
| 75 | lambda file: unzip(file.name, to_path=dependencies_path / "cmsis-nn")) |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 76 | download(ETHOS_U_CORE_DRIVER, |
| 77 | lambda file: untar(file.name, to_path=dependencies_path / "core-driver")) |
| 78 | download(ETHOS_U_CORE_PLATFORM, |
| 79 | lambda file: untar(file.name, to_path=dependencies_path / "core-platform")) |
| 80 | download(TF, |
| 81 | lambda file: unzip(file.name, to_path=dependencies_path / "tensorflow")) |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 82 | |
| 83 | |
| 84 | if __name__ == '__main__': |
Isabella Gottardi | 3149486 | 2021-08-13 16:29:39 +0100 | [diff] [blame] | 85 | logging.basicConfig(filename='download_dependencies.log', level=logging.DEBUG, filemode='w') |
Isabella Gottardi | c5d8bda | 2021-07-21 10:35:08 +0100 | [diff] [blame] | 86 | logging.getLogger().addHandler(logging.StreamHandler(sys.stdout)) |
| 87 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 88 | download_dir = Path(__file__).parent.resolve() / "dependencies" |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 89 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 90 | if download_dir.is_dir(): |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 91 | logging.info(f'{download_dir} exists. Skipping download.') |
| 92 | else: |
| 93 | main(download_dir) |