blob: b22c63f7331d392a2088cf968a9aaff0ae1ac448 [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
28logging.basicConfig(filename='download_dependencies.log', level=logging.DEBUG)
29logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
30
31tf = "https://github.com/tensorflow/tensorflow/archive/6cff09aee1f832d495b3cae40cab0de58155a0af.zip"
32cmsis = "https://github.com/ARM-software/CMSIS_5/archive/0d7e4fa7131241a17e23dfae18140e0b2e77728f.zip"
33ethos_u_core_sw = "https://git.mlplatform.org/ml/ethos-u/ethos-u-core-software.git/snapshot/ethos-u-core-software-7f3c1c92732b611a53968b14e70a2b116e43b980.tar.gz"
34ethos_u_core_driver = "https://git.mlplatform.org/ml/ethos-u/ethos-u-core-driver.git/snapshot/ethos-u-core-driver-effc7aa8b9272fb20cdd1a7d1097818af70acc93.tar.gz"
35
36
37def download(url_file: str, post_process=None):
38 with urlopen(url_file) as response, tempfile.NamedTemporaryFile() as temp:
39 logging.info(f"Downloading {url_file} ...")
40 temp.write(response.read())
41 temp.seek(0)
42 logging.info(f"Finished downloading {url_file}.")
43 if post_process:
44 post_process(temp)
45
46
47def unzip(file, to_path):
48 with ZipFile(file) as z:
49 for archive_path in z.infolist():
50 archive_path.filename = archive_path.filename[archive_path.filename.find("/") + 1:]
51 if archive_path.filename:
52 z.extract(archive_path, to_path)
53 target_path = os.path.join(to_path, archive_path.filename)
54 attr = archive_path.external_attr >> 16
55 if attr != 0:
56 os.chmod(target_path, attr)
57
58
59def untar(file, to_path):
60 with tarfile.open(file) as z:
61 for archive_path in z.getmembers():
62 index = archive_path.name.find("/")
63 if index < 0:
64 continue
65 archive_path.name = archive_path.name[index + 1:]
66 if archive_path.name:
67 z.extract(archive_path, to_path)
68
69
70def main(dependencies_path: str):
71
72 download(cmsis,
73 lambda file: unzip(file.name,
74 to_path=os.path.join(dependencies_path, "cmsis")))
75 download(ethos_u_core_sw,
76 lambda file: untar(file.name,
77 to_path=os.path.join(dependencies_path, "core-software")))
78 download(ethos_u_core_driver,
79 lambda file: untar(file.name,
80 to_path=os.path.join(dependencies_path, "core-driver")))
81 download(tf,
82 lambda file: unzip(file.name,
83 to_path=os.path.join(dependencies_path, "tensorflow")))
84
85
86if __name__ == '__main__':
87 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)