blob: dccd38d14f30fa08b87fcf7d2e7da4d0e3412bcc [file] [log] [blame]
alexander3ef1fd42021-05-24 18:56:32 +01001#!/usr/bin/env python3
2
Richard Burtonb4123fd2022-03-04 09:19:09 +00003# Copyright (c) 2021-2022 Arm Limited. All rights reserved.
alexander3ef1fd42021-05-24 18:56:32 +01004# 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 Burton17069622022-03-17 10:54:26 +000018"""This script does effectively the same as "git submodule update --init" command."""
alexander3ef1fd42021-05-24 18:56:32 +010019import logging
alexander3ef1fd42021-05-24 18:56:32 +010020import sys
21import tarfile
22import tempfile
23from urllib.request import urlopen
24from zipfile import ZipFile
Richard Burton17069622022-03-17 10:54:26 +000025from pathlib import Path
alexander3ef1fd42021-05-24 18:56:32 +010026
Nina Drozd28224302022-09-06 17:00:34 +010027TF = "https://github.com/tensorflow/tflite-micro/archive/67e9d8f60d3e37ab02f94a93f016179ef5e96cb6.zip"
Maksims Svecovs254853c2022-08-30 12:58:02 +010028CMSIS = "https://github.com/ARM-software/CMSIS_5/archive/ed78d6b78766d71cfe0431149f2f261d1c7277a1.zip"
29CMSIS_DSP = "https://github.com/ARM-software/CMSIS-DSP/archive/refs/tags/v1.11.0.zip"
30ETHOS_U_CORE_DRIVER = "https://git.mlplatform.org/ml/ethos-u/ethos-u-core-driver.git/snapshot/ethos-u-core-driver-22.08.tar.gz"
31ETHOS_U_CORE_PLATFORM = "https://git.mlplatform.org/ml/ethos-u/ethos-u-core-platform.git/snapshot/ethos-u-core-platform-22.08.tar.gz"
Richard Burton17069622022-03-17 10:54:26 +000032
alexander3ef1fd42021-05-24 18:56:32 +010033
34def 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
44def 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)
Richard Burton17069622022-03-17 10:54:26 +000050 target_path = to_path / archive_path.filename
alexander3ef1fd42021-05-24 18:56:32 +010051 attr = archive_path.external_attr >> 16
52 if attr != 0:
Richard Burton17069622022-03-17 10:54:26 +000053 target_path.chmod(attr)
alexander3ef1fd42021-05-24 18:56:32 +010054
55
56def 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
Richard Burton17069622022-03-17 10:54:26 +000067def main(dependencies_path: Path):
alexander3ef1fd42021-05-24 18:56:32 +010068
Richard Burton17069622022-03-17 10:54:26 +000069 download(CMSIS,
70 lambda file: unzip(file.name, to_path=dependencies_path / "cmsis"))
Maksims Svecovs254853c2022-08-30 12:58:02 +010071 download(CMSIS_DSP,
72 lambda file: unzip(file.name, to_path=dependencies_path / "cmsis-dsp"))
Richard Burton17069622022-03-17 10:54:26 +000073 download(ETHOS_U_CORE_DRIVER,
74 lambda file: untar(file.name, to_path=dependencies_path / "core-driver"))
75 download(ETHOS_U_CORE_PLATFORM,
76 lambda file: untar(file.name, to_path=dependencies_path / "core-platform"))
77 download(TF,
78 lambda file: unzip(file.name, to_path=dependencies_path / "tensorflow"))
alexander3ef1fd42021-05-24 18:56:32 +010079
80
81if __name__ == '__main__':
Isabella Gottardi31494862021-08-13 16:29:39 +010082 logging.basicConfig(filename='download_dependencies.log', level=logging.DEBUG, filemode='w')
Isabella Gottardic5d8bda2021-07-21 10:35:08 +010083 logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
84
Richard Burton17069622022-03-17 10:54:26 +000085 download_dir = Path(__file__).parent.resolve() / "dependencies"
alexander3ef1fd42021-05-24 18:56:32 +010086
Richard Burton17069622022-03-17 10:54:26 +000087 if download_dir.is_dir():
alexander3ef1fd42021-05-24 18:56:32 +010088 logging.info(f'{download_dir} exists. Skipping download.')
89 else:
90 main(download_dir)