alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 2 | # SPDX-FileCopyrightText: Copyright 2021-2023 Arm Limited and/or its affiliates <open-source-office@arm.com> |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 3 | # 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 Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 17 | """This script does effectively the same as "git submodule update --init" command.""" |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 18 | import json |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 19 | import logging |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 20 | import sys |
| 21 | import tarfile |
| 22 | import tempfile |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 23 | import typing |
| 24 | from pathlib import Path |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 25 | from urllib.request import urlopen |
| 26 | from zipfile import ZipFile |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 27 | |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 28 | |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 29 | def 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 | """ |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 39 | with urlopen(url_file) as response, tempfile.NamedTemporaryFile() as temp: |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 40 | logging.info("Downloading %s ...", url_file) |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 41 | temp.write(response.read()) |
| 42 | temp.seek(0) |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 43 | logging.info("Finished downloading %s.", url_file) |
| 44 | if url_file.endswith(".tar.gz"): |
| 45 | untar(temp, to_path) |
| 46 | else: |
| 47 | unzip(temp, to_path) |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 48 | |
| 49 | |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 50 | def 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(): |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 62 | archive_path.filename = archive_path.filename[archive_path.filename.find("/") + 1:] |
| 63 | if archive_path.filename: |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 64 | f.extract(archive_path, to_path) |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 65 | target_path = to_path / archive_path.filename |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 66 | attr = archive_path.external_attr >> 16 |
| 67 | if attr != 0: |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 68 | target_path.chmod(attr) |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 69 | |
| 70 | |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 71 | def 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(): |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 83 | 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 Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 88 | f.extract(archive_path, to_path) |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 89 | |
| 90 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 91 | def main(dependencies_path: Path): |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 92 | """ |
| 93 | Download all dependencies |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 94 | |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 95 | @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) |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 104 | |
| 105 | |
| 106 | if __name__ == '__main__': |
Isabella Gottardi | 3149486 | 2021-08-13 16:29:39 +0100 | [diff] [blame] | 107 | logging.basicConfig(filename='download_dependencies.log', level=logging.DEBUG, filemode='w') |
Isabella Gottardi | c5d8bda | 2021-07-21 10:35:08 +0100 | [diff] [blame] | 108 | logging.getLogger().addHandler(logging.StreamHandler(sys.stdout)) |
| 109 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 110 | download_dir = Path(__file__).parent.resolve() / "dependencies" |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 111 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 112 | if download_dir.is_dir(): |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 113 | logging.info('%s exists. Skipping download.', download_dir) |
alexander | 3ef1fd4 | 2021-05-24 18:56:32 +0100 | [diff] [blame] | 114 | else: |
| 115 | main(download_dir) |