Debian Codegraph @

scripts/fetch-vendor.py

raw
1 import argparse
2 import logging
3 import json
4 import os
5 import platform
6 import subprocess
7 import time
8
9
10 def get_platform():
11 system = platform.system()
12 machine = platform.machine().lower()
13 is_arm64 = machine in {"arm64", "aarch64"}
14 if system == "Linux":
15 prefix = "manylinux-" if platform.libc_ver()[0] == "glibc" else "musllinux-"
16 return prefix + machine
17 elif system == "Darwin":
18 return "macos-arm64" if is_arm64 else "macos-x86_64"
19 elif system == "Windows":
20 return "windows-aarch64" if is_arm64 else "windows-x86_64"
21 else:
22 return "unknown"
23
24
25 parser = argparse.ArgumentParser(description="Fetch and extract tarballs")
26 parser.add_argument("destination_dir")
27 parser.add_argument("--cache-dir", default="tarballs")
28 parser.add_argument("--config-file", default=os.path.splitext(__file__)[0] + ".json")
29 args = parser.parse_args()
30 logging.basicConfig(level=logging.INFO)
31
32 with open(args.config_file) as fp:
33 config = json.load(fp)
34
35 # ensure destination directory exists
36 logging.info(f"Creating directory {args.destination_dir}")
37 if not os.path.exists(args.destination_dir):
38 os.makedirs(args.destination_dir)
39
40 tarball_url = config["url"].replace("{platform}", get_platform())
41
42 # download tarball
43 tarball_name = tarball_url.split("/")[-1]
44 tarball_file = os.path.join(args.cache_dir, tarball_name)
45 if not os.path.exists(tarball_file):
46 logging.info(f"Downloading {tarball_url}")
47 if not os.path.exists(args.cache_dir):
48 os.mkdir(args.cache_dir)
49 subprocess.check_call(
50 ["curl", "--location", "--output", tarball_file, "--silent", tarball_url]
51 )
52
53 logging.info(f"Extracting {tarball_name}")
54 subprocess.check_call(["tar", "-C", args.destination_dir, "-xf", tarball_file])
55
56 # Some tarball members carry pre-1980 mtimes, which the ZIP format (and thus
57 # delvewheel's wheel repackaging) cannot represent. Bump any such file to now.
58 ZIP_EPOCH = 315532800 # 1980-01-01 00:00:00 UTC
59 now = time.time()
60 for root, _, files in os.walk(args.destination_dir):
61 for name in files:
62 path = os.path.join(root, name)
63 if os.path.getmtime(path) < ZIP_EPOCH:
64 os.utime(path, (now, now))