|
1 | | -import glob |
2 | 1 | import os |
3 | | -import re |
4 | | -import shutil |
5 | | -import subprocess |
6 | | -import sys |
7 | | -import urllib.error |
8 | | -import urllib.request |
| 2 | + |
| 3 | +import requests |
9 | 4 |
|
10 | 5 | # Output files |
11 | | -OUTPUT_WHL = "docs/wheels/" |
12 | 6 | OUTPUT_JS = "docs/_static/" |
13 | 7 |
|
14 | | -# Build message output |
15 | | -# BUILD_WHL_MESSAGE = f"{OUTPUT_WHL}(.*whl)" |
16 | | -# BUILD_WHL_COMMAND = [sys.executable, "-m", "hatch", "build", OUTPUT_WHL] |
17 | | -BUILD_WHL_MESSAGE = r"Successfully built ([-_0-9.a-zA-Z]+?\.whl)" |
18 | | -BUILD_WHL_COMMAND = [sys.executable, "-m", "build", "--wheel", "-o", OUTPUT_WHL] |
19 | | - |
20 | 8 | DEFAULT_COMMANDS = [""] |
21 | 9 |
|
22 | 10 | PLAYGROUND_WHEELS = [ |
23 | 11 | "https://files.pythonhosted.org/packages/97/9c/372fef8377a6e340b1704768d20daaded98bf13282b5327beb2e2fe2c7ef/pygments-2.17.2-py3-none-any.whl", |
24 | | - "https://files.pythonhosted.org/packages/d3/58/7d2dd906add6bd3481ee80beafb00af292866ed4ce3fa18cd168cb7bab74/bigtree-0.19.0-py3-none-any.whl", |
25 | 12 | ] |
26 | 13 |
|
27 | 14 | CONFIG = """\ |
|
33 | 20 | """ |
34 | 21 |
|
35 | 22 |
|
36 | | -def build_package(): |
37 | | - """Build wheel""" |
38 | | - |
39 | | - if sys.platform.startswith("win"): |
40 | | - startupinfo = subprocess.STARTUPINFO() |
41 | | - startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW |
42 | | - process = subprocess.Popen( |
43 | | - BUILD_WHL_COMMAND, |
44 | | - stdout=subprocess.PIPE, |
45 | | - startupinfo=startupinfo, |
46 | | - shell=False, |
47 | | - env=os.environ.copy(), |
48 | | - ) |
49 | | - else: |
50 | | - process = subprocess.Popen( |
51 | | - BUILD_WHL_COMMAND, |
52 | | - stdout=subprocess.PIPE, |
53 | | - shell=False, |
54 | | - env=os.environ.copy(), |
55 | | - ) |
56 | | - out, _ = process.communicate() |
57 | | - build_message = out.decode("utf-8") |
58 | | - m = re.compile(BUILD_WHL_MESSAGE).search(build_message) |
59 | | - print(build_message) |
60 | | - |
61 | | - return process.returncode, m.group(1) if m else "" |
62 | | - |
63 | | - |
64 | | -def download_wheel(url, dest): |
65 | | - """Download wheel""" |
66 | | - |
67 | | - print("Downloading: {}".format(url)) |
68 | | - try: |
69 | | - response = urllib.request.urlopen(url) |
70 | | - status = response.status |
71 | | - if status == 200: |
72 | | - status = 0 |
73 | | - with open(dest, "wb") as f: |
74 | | - print("Writing: {}".format(dest)) |
75 | | - f.write(response.read()) |
76 | | - except urllib.error.HTTPError as e: |
77 | | - status = e.status |
78 | | - |
79 | | - if status: |
80 | | - print("Failed to download, received status code {}".format(status)) |
81 | | - |
82 | | - return status |
83 | | - |
84 | | - |
85 | 23 | if __name__ == "__main__": |
86 | 24 |
|
87 | | - PLAYGROUND = {} |
88 | | - for url in PLAYGROUND_WHEELS: |
89 | | - PLAYGROUND[os.path.join(OUTPUT_WHL, url.split("/")[-1])] = url |
90 | | - |
91 | | - # Clean up all old wheels and js file |
92 | | - for file in glob.glob(OUTPUT_WHL + "*.whl"): |
93 | | - if file not in PLAYGROUND.keys(): |
94 | | - os.remove(file) |
95 | | - |
96 | 25 | if os.path.exists(OUTPUT_JS + "playground-config.js"): |
97 | 26 | os.remove(OUTPUT_JS + "playground-config.js") |
98 | 27 |
|
99 | | - # Clean up build directory |
100 | | - if os.path.exists("build"): |
101 | | - shutil.rmtree("build") |
102 | | - |
103 | | - if not os.path.exists("docs/wheels"): |
104 | | - os.mkdir("docs/wheels") |
105 | | - |
106 | | - # Build wheel |
107 | | - status = 0 |
108 | | - # status, package = build_package() |
109 | | - # if not status: |
110 | | - # for file, url in PLAYGROUND.items(): |
111 | | - # if os.path.exists(file): |
112 | | - # print("Skipping: {}".format(file)) |
113 | | - # continue |
114 | | - # status = download_wheel(url, file) |
115 | | - # if status: |
116 | | - # break |
117 | | - |
118 | | - if not status: |
119 | | - # Build up a list of wheels needed for playgrounds and notebooks |
120 | | - # playground = [ |
121 | | - # os.path.basename(file_path) for file_path in PLAYGROUND.keys() |
122 | | - # ] + [package] |
123 | | - |
124 | | - # Create the config that specifies which wheels need to be used |
125 | | - config = ( |
126 | | - CONFIG.format(str(PLAYGROUND_WHEELS), "\n".join(DEFAULT_COMMANDS)) |
127 | | - .replace("\r", "") |
128 | | - .encode("utf-8") |
| 28 | + # Scrape whl file |
| 29 | + response = requests.get("https://pypi.org/pypi/bigtree/json") |
| 30 | + PACKAGE_WHEEL = [ |
| 31 | + url["url"] for url in response.json()["urls"] if url["url"].endswith("whl") |
| 32 | + ] |
| 33 | + assert len(PACKAGE_WHEEL), "Cannot find package wheel" |
| 34 | + |
| 35 | + # Create the config that specifies which wheels need to be used |
| 36 | + config = ( |
| 37 | + CONFIG.format( |
| 38 | + str(PLAYGROUND_WHEELS + PACKAGE_WHEEL), "\n".join(DEFAULT_COMMANDS) |
129 | 39 | ) |
130 | | - with open(OUTPUT_JS + "playground-config.js", "wb") as f: |
131 | | - f.write(config) |
132 | | - |
133 | | - print("FAILED :(" if status else "SUCCESS :)") |
134 | | - sys.exit(status) |
| 40 | + .replace("\r", "") |
| 41 | + .encode("utf-8") |
| 42 | + ) |
| 43 | + with open(OUTPUT_JS + "playground-config.js", "wb") as f: |
| 44 | + f.write(config) |
0 commit comments