blob: 280292f1f34a70850a439cb225bbe7079bff28cf [file] [log] [blame]
alexander3ef1fd42021-05-24 18:56:32 +01001#!/usr/bin/env python3
2
Richard Burtonf32a86a2022-11-15 11:46:11 +00003# SPDX-FileCopyrightText: Copyright 2021-2022 Arm Limited and/or its affiliates <open-source-office@arm.com>
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
Richard Burton71f282e2022-12-01 12:31:23 +000027TF = "https://github.com/tensorflow/tflite-micro/archive/28770e4cac1e5dae85b55a0d47d5315d35755d04.zip"
28CMSIS = "https://github.com/ARM-software/CMSIS_5/archive/81564cfb339ebf9def167a50693733f8a1e1471e.zip"
29CMSIS_DSP = "https://github.com/ARM-software/CMSIS-DSP/archive/refs/tags/v1.14.2.zip"
30CMSIS_NN = "https://github.com/ARM-software/CMSIS-NN/archive/refs/tags/v4.0.0.zip"
31ETHOS_U_CORE_DRIVER = "https://git.mlplatform.org/ml/ethos-u/ethos-u-core-driver.git/snapshot/ethos-u-core-driver-22.11.tar.gz"
32ETHOS_U_CORE_PLATFORM = "https://git.mlplatform.org/ml/ethos-u/ethos-u-core-platform.git/snapshot/ethos-u-core-platform-22.11.tar.gz"
Richard Burton17069622022-03-17 10:54:26 +000033
alexander3ef1fd42021-05-24 18:56:32 +010034
35def 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
45def 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 Burton17069622022-03-17 10:54:26 +000051 target_path = to_path / archive_path.filename
alexander3ef1fd42021-05-24 18:56:32 +010052 attr = archive_path.external_attr >> 16
53 if attr != 0:
Richard Burton17069622022-03-17 10:54:26 +000054 target_path.chmod(attr)
alexander3ef1fd42021-05-24 18:56:32 +010055
56
57def 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 Burton17069622022-03-17 10:54:26 +000068def main(dependencies_path: Path):
alexander3ef1fd42021-05-24 18:56:32 +010069
Richard Burton17069622022-03-17 10:54:26 +000070 download(CMSIS,
71 lambda file: unzip(file.name, to_path=dependencies_path / "cmsis"))
Maksims Svecovs254853c2022-08-30 12:58:02 +010072 download(CMSIS_DSP,
73 lambda file: unzip(file.name, to_path=dependencies_path / "cmsis-dsp"))
Richard Burton71f282e2022-12-01 12:31:23 +000074 download(CMSIS_NN,
75 lambda file: unzip(file.name, to_path=dependencies_path / "cmsis-nn"))
Richard Burton17069622022-03-17 10:54:26 +000076 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"))
alexander3ef1fd42021-05-24 18:56:32 +010082
83
84if __name__ == '__main__':
Isabella Gottardi31494862021-08-13 16:29:39 +010085 logging.basicConfig(filename='download_dependencies.log', level=logging.DEBUG, filemode='w')
Isabella Gottardic5d8bda2021-07-21 10:35:08 +010086 logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
87
Richard Burton17069622022-03-17 10:54:26 +000088 download_dir = Path(__file__).parent.resolve() / "dependencies"
alexander3ef1fd42021-05-24 18:56:32 +010089
Richard Burton17069622022-03-17 10:54:26 +000090 if download_dir.is_dir():
alexander3ef1fd42021-05-24 18:56:32 +010091 logging.info(f'{download_dir} exists. Skipping download.')
92 else:
93 main(download_dir)