blob: 9d2e6fa41a55c7ed469555bf2ff269e2a690590b [file] [log] [blame]
alexander3ef1fd42021-05-24 18:56:32 +01001#!/usr/bin/env python3
2
3# Copyright (c) 2021 Arm Limited. All rights reserved.
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
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
Isabella Gottardic5d8bda2021-07-21 10:35:08 +010028tf = "https://github.com/tensorflow/tflite-micro/archive/f510d38d0eaa3195ce3af66e3f32648740f08afb.zip"
29cmsis = "https://github.com/ARM-software/CMSIS_5/archive/e9637de2b4cfd99cd5c93893d88593262d8660c1.zip"
30ethos_u_core_sw = "https://git.mlplatform.org/ml/ethos-u/ethos-u-core-software.git/snapshot/ethos-u-core-software-8b1d9ded33ec59545897ff45019d05403dba7eee.tar.gz"
31ethos_u_core_driver = "https://git.mlplatform.org/ml/ethos-u/ethos-u-core-driver.git/snapshot/ethos-u-core-driver-d1ea036bd870663d737db9bfbb25e2aa04a389e0.tar.gz"
alexander3ef1fd42021-05-24 18:56:32 +010032
33
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)
50 target_path = os.path.join(to_path, archive_path.filename)
51 attr = archive_path.external_attr >> 16
52 if attr != 0:
53 os.chmod(target_path, attr)
54
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
67def main(dependencies_path: str):
68
69 download(cmsis,
70 lambda file: unzip(file.name,
71 to_path=os.path.join(dependencies_path, "cmsis")))
72 download(ethos_u_core_sw,
73 lambda file: untar(file.name,
74 to_path=os.path.join(dependencies_path, "core-software")))
75 download(ethos_u_core_driver,
76 lambda file: untar(file.name,
77 to_path=os.path.join(dependencies_path, "core-driver")))
78 download(tf,
79 lambda file: unzip(file.name,
80 to_path=os.path.join(dependencies_path, "tensorflow")))
81
82
83if __name__ == '__main__':
Isabella Gottardic5d8bda2021-07-21 10:35:08 +010084 logging.basicConfig(filename='download_dependencies.log', level=logging.DEBUG)
85 logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
86
alexander3ef1fd42021-05-24 18:56:32 +010087 download_dir = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "dependencies"))
88
89 if os.path.isdir(download_dir):
90 logging.info(f'{download_dir} exists. Skipping download.')
91 else:
92 main(download_dir)