From 7d3cad844316ad60c38b3a3ef1d8ca3c3f52a347 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Sun, 10 Mar 2024 17:15:17 -0700 Subject: [PATCH 1/2] Update script to download `latest` or a specific version. --- .../download_unicode_data_files.py | 40 ++++++++++++++----- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/tools/unicode_properties_parse/download_unicode_data_files.py b/tools/unicode_properties_parse/download_unicode_data_files.py index bf5c587fd18..207d2e769ba 100644 --- a/tools/unicode_properties_parse/download_unicode_data_files.py +++ b/tools/unicode_properties_parse/download_unicode_data_files.py @@ -1,21 +1,41 @@ # Copyright (c) Microsoft Corporation. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +from pathlib import PurePosixPath +import sys from urllib.request import urlretrieve -Unicode_data_files = { - "DerivedCoreProperties.txt": "https://www.unicode.org/Public/UCD/latest/ucd/DerivedCoreProperties.txt", - "DerivedGeneralCategory.txt": "https://www.unicode.org/Public/UCD/latest/ucd/extracted/DerivedGeneralCategory.txt", - "EastAsianWidth.txt": "https://www.unicode.org/Public/UCD/latest/ucd/EastAsianWidth.txt", - "GraphemeBreakProperty.txt": "https://www.unicode.org/Public/UCD/latest/ucd/auxiliary/GraphemeBreakProperty.txt", - "GraphemeBreakTest.txt": "https://www.unicode.org/Public/UCD/latest/ucd/auxiliary/GraphemeBreakTest.txt", - "emoji-data.txt": "https://www.unicode.org/Public/UCD/latest/ucd/emoji/emoji-data.txt", -} +def get_base_url(): + if len(sys.argv) != 2: + sys.exit(f"Usage: python {sys.argv[0]} [latest|]") + + version = sys.argv[1] + + if version == "latest": + return "https://unicode.org/Public/UCD/latest/" + + return f"https://unicode.org/Public/{version}/" + + +Unicode_data_files = [ + "ucd/DerivedCoreProperties.txt", + "ucd/extracted/DerivedGeneralCategory.txt", + "ucd/EastAsianWidth.txt", + "ucd/auxiliary/GraphemeBreakProperty.txt", + "ucd/auxiliary/GraphemeBreakTest.txt", + "ucd/emoji/emoji-data.txt", +] + def download_unicode_data_files(): - for filename, url in Unicode_data_files.items(): - print(f"downloading {filename} from {url}") + base_url = get_base_url() + print(f" Base URL: {base_url}") + + for data_file in Unicode_data_files: + url = base_url + data_file + filename = PurePosixPath(data_file).name + print(f"Downloading: {url}") urlretrieve(url, filename) From 1dd22beacb88e78c74031b8803f56a3d904e92eb Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Sun, 10 Mar 2024 17:25:16 -0700 Subject: [PATCH 2/2] Print HTTPError nicely. --- .../unicode_properties_parse/download_unicode_data_files.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tools/unicode_properties_parse/download_unicode_data_files.py b/tools/unicode_properties_parse/download_unicode_data_files.py index 207d2e769ba..7fada908688 100644 --- a/tools/unicode_properties_parse/download_unicode_data_files.py +++ b/tools/unicode_properties_parse/download_unicode_data_files.py @@ -3,6 +3,7 @@ from pathlib import PurePosixPath import sys +from urllib.error import HTTPError from urllib.request import urlretrieve @@ -36,7 +37,10 @@ def download_unicode_data_files(): url = base_url + data_file filename = PurePosixPath(data_file).name print(f"Downloading: {url}") - urlretrieve(url, filename) + try: + urlretrieve(url, filename) + except HTTPError as http_error: + sys.exit(f"{http_error}") if __name__ == "__main__":