blob: 995c7aee70446f0e8179aac25aa6eab906afab08 [file] [log] [blame]
alexander3ef1fd42021-05-24 18:56:32 +01001#!/usr/bin/env python3
Alex Tawsedaba3cf2023-09-29 15:55:38 +01002# SPDX-FileCopyrightText: Copyright 2021-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
alexander3ef1fd42021-05-24 18:56:32 +01003# SPDX-License-Identifier: Apache-2.0
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Richard Burton17069622022-03-17 10:54:26 +000017"""This script does effectively the same as "git submodule update --init" command."""
Alex Tawsedaba3cf2023-09-29 15:55:38 +010018import json
alexander3ef1fd42021-05-24 18:56:32 +010019import logging
alexander3ef1fd42021-05-24 18:56:32 +010020import sys
21import tarfile
22import tempfile
Alex Tawsedaba3cf2023-09-29 15:55:38 +010023import typing
24from pathlib import Path
alexander3ef1fd42021-05-24 18:56:32 +010025from urllib.request import urlopen
26from zipfile import ZipFile
Richard Burton17069622022-03-17 10:54:26 +000027
alexander3ef1fd42021-05-24 18:56:32 +010028
Alex Tawsedaba3cf2023-09-29 15:55:38 +010029def download(
30 url_file: str,
31 to_path: Path,
32):
33 """
34 Download a file from the specified URL
35
36 @param url_file: The URL of the file to download
37 @param to_path: The location to download the file to
38 """
alexander3ef1fd42021-05-24 18:56:32 +010039 with urlopen(url_file) as response, tempfile.NamedTemporaryFile() as temp:
Alex Tawsedaba3cf2023-09-29 15:55:38 +010040 logging.info("Downloading %s ...", url_file)
alexander3ef1fd42021-05-24 18:56:32 +010041 temp.write(response.read())
42 temp.seek(0)
Alex Tawsedaba3cf2023-09-29 15:55:38 +010043 logging.info("Finished downloading %s.", url_file)
44 if url_file.endswith(".tar.gz"):
Richard Burton49482d52023-11-30 11:38:45 +000045 untar(temp.name, to_path)
Alex Tawsedaba3cf2023-09-29 15:55:38 +010046 else:
47 unzip(temp, to_path)
alexander3ef1fd42021-05-24 18:56:32 +010048
49
Alex Tawsedaba3cf2023-09-29 15:55:38 +010050def unzip(
51 file: typing.IO[bytes],
52 to_path: Path
53):
54 """
55 Unzip the specified file
56
57 @param file: The file to unzip
58 @param to_path: The location to extract to
59 """
60 with ZipFile(file) as f:
61 for archive_path in f.infolist():
alexander3ef1fd42021-05-24 18:56:32 +010062 archive_path.filename = archive_path.filename[archive_path.filename.find("/") + 1:]
63 if archive_path.filename:
Alex Tawsedaba3cf2023-09-29 15:55:38 +010064 f.extract(archive_path, to_path)
Richard Burton17069622022-03-17 10:54:26 +000065 target_path = to_path / archive_path.filename
alexander3ef1fd42021-05-24 18:56:32 +010066 attr = archive_path.external_attr >> 16
67 if attr != 0:
Richard Burton17069622022-03-17 10:54:26 +000068 target_path.chmod(attr)
alexander3ef1fd42021-05-24 18:56:32 +010069
70
Alex Tawsedaba3cf2023-09-29 15:55:38 +010071def untar(
72 file: bytes,
73 to_path: Path
74):
75 """
76 Untar the specified file
77
78 @param file: The file to untar
79 @param to_path: The location to extract to
80 """
81 with tarfile.open(file) as f:
82 for archive_path in f.getmembers():
alexander3ef1fd42021-05-24 18:56:32 +010083 index = archive_path.name.find("/")
84 if index < 0:
85 continue
86 archive_path.name = archive_path.name[index + 1:]
87 if archive_path.name:
Alex Tawsedaba3cf2023-09-29 15:55:38 +010088 f.extract(archive_path, to_path)
alexander3ef1fd42021-05-24 18:56:32 +010089
90
Richard Burton17069622022-03-17 10:54:26 +000091def main(dependencies_path: Path):
Alex Tawsedaba3cf2023-09-29 15:55:38 +010092 """
93 Download all dependencies
alexander3ef1fd42021-05-24 18:56:32 +010094
Alex Tawsedaba3cf2023-09-29 15:55:38 +010095 @param dependencies_path: The path to which the dependencies will be downloaded
96 """
97 dependency_urls_path = (
98 Path(__file__).parent.resolve() / "scripts" / "py" / "dependency_urls.json")
99 with open(dependency_urls_path, encoding="utf8") as f:
100 dependency_urls = json.load(f)
101
102 for name, url in dependency_urls.items():
103 download(url, dependencies_path / name)
alexander3ef1fd42021-05-24 18:56:32 +0100104
105
106if __name__ == '__main__':
Isabella Gottardi31494862021-08-13 16:29:39 +0100107 logging.basicConfig(filename='download_dependencies.log', level=logging.DEBUG, filemode='w')
Isabella Gottardic5d8bda2021-07-21 10:35:08 +0100108 logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
109
Richard Burton17069622022-03-17 10:54:26 +0000110 download_dir = Path(__file__).parent.resolve() / "dependencies"
alexander3ef1fd42021-05-24 18:56:32 +0100111
Richard Burton17069622022-03-17 10:54:26 +0000112 if download_dir.is_dir():
Alex Tawsedaba3cf2023-09-29 15:55:38 +0100113 logging.info('%s exists. Skipping download.', download_dir)
alexander3ef1fd42021-05-24 18:56:32 +0100114 else:
115 main(download_dir)