blob: c02d98f48562da676f6b3efccd25877e7a81a73b [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
18# The script does effectively the same as "git submodule update --init" command.
19
20import logging
21import os
22import sys
23import tarfile
24import tempfile
25from urllib.request import urlopen
26from zipfile import ZipFile
27
Richard Burtonb4123fd2022-03-04 09:19:09 +000028tf = "https://github.com/tensorflow/tflite-micro/archive/1a0287fc5fa81fa6aa1dcfb0c5b2e01f74164393.zip"
29cmsis = "https://github.com/ARM-software/CMSIS_5/archive/9b5df640c777563919affb4e9201c96c657adbb2.zip"
30ethos_u_core_driver = "https://git.mlplatform.org/ml/ethos-u/ethos-u-core-driver.git/snapshot/ethos-u-core-driver-22.02.tar.gz"
31ethos_u_core_platform = "https://git.mlplatform.org/ml/ethos-u/ethos-u-core-platform.git/snapshot/ethos-u-core-platform-22.02.tar.gz"
alexander3ef1fd42021-05-24 18:56:32 +010032
33def 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
43def 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)
49 target_path = os.path.join(to_path, archive_path.filename)
50 attr = archive_path.external_attr >> 16
51 if attr != 0:
52 os.chmod(target_path, attr)
53
54
55def 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
66def main(dependencies_path: str):
67
68 download(cmsis,
69 lambda file: unzip(file.name,
70 to_path=os.path.join(dependencies_path, "cmsis")))
alexander3ef1fd42021-05-24 18:56:32 +010071 download(ethos_u_core_driver,
72 lambda file: untar(file.name,
73 to_path=os.path.join(dependencies_path, "core-driver")))
Richard Burtonb4123fd2022-03-04 09:19:09 +000074 download(ethos_u_core_platform,
75 lambda file: untar(file.name,
76 to_path=os.path.join(dependencies_path, "core-platform")))
alexander3ef1fd42021-05-24 18:56:32 +010077 download(tf,
78 lambda file: unzip(file.name,
79 to_path=os.path.join(dependencies_path, "tensorflow")))
80
81
82if __name__ == '__main__':
Isabella Gottardi31494862021-08-13 16:29:39 +010083 logging.basicConfig(filename='download_dependencies.log', level=logging.DEBUG, filemode='w')
Isabella Gottardic5d8bda2021-07-21 10:35:08 +010084 logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
85
alexander3ef1fd42021-05-24 18:56:32 +010086 download_dir = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "dependencies"))
87
88 if os.path.isdir(download_dir):
89 logging.info(f'{download_dir} exists. Skipping download.')
90 else:
91 main(download_dir)