Debian Codegraph @

tools/download-wheels.py

raw
1 #!/usr/bin/env python3
2 """
3 Script to download NumPy wheels from the Anaconda staging area.
4
5 Usage::
6
7 $ ./tools/download-wheels.py <version> -w <optional-wheelhouse>
8
9 The default wheelhouse is ``release/installers``.
10
11 Dependencies
12 ------------
13
14 - beautifulsoup4
15 - urllib3
16
17 Examples
18 --------
19
20 While in the repository root::
21
22 $ python tools/download-wheels.py 1.19.0
23 $ python tools/download-wheels.py 1.19.0 -w ~/wheelhouse
24
25 """
26 import argparse
27 import os
28 import re
29 import shutil
30
31 import urllib3
32 from bs4 import BeautifulSoup
33
34 __version__ = "0.2"
35
36 # Edit these for other projects.
37
38 # The first URL is used to get the file names as it avoids the need for paging
39 # when the number of files exceeds the page length. Note that files/page is not
40 # stable and can change when the page layout changes. The second URL is used to
41 # retrieve the files themselves. This workaround is copied from SciPy.
42 NAMES_URL = "https://pypi.anaconda.org/multibuild-wheels-staging/simple/numpy/"
43 FILES_URL = "https://anaconda.org/multibuild-wheels-staging/numpy"
44
45 # Name prefix of the files to download.
46 PREFIX = "numpy"
47
48 # Name endings of the files to download.
49 WHL = r"-.*\.whl$"
50 ZIP = r"\.zip$"
51 GZIP = r"\.tar\.gz$"
52 SUFFIX = rf"({WHL}|{GZIP}|{ZIP})"
53
54
55 def get_wheel_names(version):
56 """ Get wheel names from Anaconda HTML directory.
57
58 This looks in the Anaconda multibuild-wheels-staging page and
59 parses the HTML to get all the wheel names for a release version.
60
61 Parameters
62 ----------
63 version : str
64 The release version. For instance, "1.18.3".
65
66 """
67 http = urllib3.PoolManager(cert_reqs="CERT_REQUIRED")
68 tmpl = re.compile(rf"^.*{PREFIX}-{version}{SUFFIX}")
69 index_url = f"{NAMES_URL}"
70 index_html = http.request('GET', index_url)
71 soup = BeautifulSoup(index_html.data, 'html.parser')
72 return sorted(soup.find_all(string=tmpl))
73
74
75 def download_wheels(version, wheelhouse, test=False):
76 """Download release wheels.
77
78 The release wheels for the given NumPy version are downloaded
79 into the given directory.
80
81 Parameters
82 ----------
83 version : str
84 The release version. For instance, "1.18.3".
85 wheelhouse : str
86 Directory in which to download the wheels.
87
88 """
89 http = urllib3.PoolManager(cert_reqs="CERT_REQUIRED")
90 wheel_names = get_wheel_names(version)
91
92 for i, wheel_name in enumerate(wheel_names):
93 wheel_url = f"{FILES_URL}/{version}/download/{wheel_name}"
94 wheel_path = os.path.join(wheelhouse, wheel_name)
95 with open(wheel_path, "wb") as f:
96 with http.request("GET", wheel_url, preload_content=False,) as r:
97 info = r.info()
98 length = int(info.get('Content-Length', '0'))
99 if length == 0:
100 length = 'unknown size'
101 else:
102 length = f"{(length / 1024 / 1024):.2f}MB"
103 print(f"{i + 1:<4}{wheel_name} {length}")
104 if not test:
105 shutil.copyfileobj(r, f)
106 print(f"\nTotal files downloaded: {len(wheel_names)}")
107
108
109 if __name__ == "__main__":
110 parser = argparse.ArgumentParser()
111 parser.add_argument(
112 "version",
113 help="NumPy version to download.")
114 parser.add_argument(
115 "-w", "--wheelhouse",
116 default=os.path.join(os.getcwd(), "release", "installers"),
117 help="Directory in which to store downloaded wheels\n"
118 "[defaults to <cwd>/release/installers]")
119 parser.add_argument(
120 "-t", "--test",
121 action='store_true',
122 help="only list available wheels, do not download")
123
124 args = parser.parse_args()
125
126 wheelhouse = os.path.expanduser(args.wheelhouse)
127 if not os.path.isdir(wheelhouse):
128 raise RuntimeError(
129 f"{wheelhouse} wheelhouse directory is not present."
130 " Perhaps you need to use the '-w' flag to specify one.")
131
132 download_wheels(args.version, wheelhouse, test=args.test)