-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcreate_index.py
90 lines (60 loc) · 2.55 KB
/
create_index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import os
import re
from pathlib import Path
import requests
HTML_PLUS = "%2B"
def normalize_pep_503(name):
return re.sub(r"[-_.]+", "-", name).lower()
def get_packages_dict(links_wheels):
packages = {}
for link_wheel in links_wheels:
file_name = link_wheel.split("/")[-1]
package_and_version = file_name.split(HTML_PLUS)[0]
package, _version = package_and_version.rsplit("-", 1)
try:
packages[package].append(link_wheel)
except KeyError:
packages[package] = [link_wheel]
return packages
def get_links(releases):
links = []
for release in releases.json():
release_links = (x["browser_download_url"] for x in release["assets"])
links.extend(release_links)
return links
def create_main_index(packages, output_file):
with open(output_file, "w") as f:
f.write('<!DOCTYPE html><html><head><meta name="pypi:repository-version" content="1.1"></head><body>\n')
for package in packages:
f.write(f'<a href="{normalize_pep_503(package)}/">{package}</a><br>\n')
f.write("</body></html>")
def create_package_index(links_wheels, output_file):
with open(output_file, "w") as f:
f.write('<!DOCTYPE html><html><head><meta name="pypi:repository-version" content="1.1"></head><body>\n')
file_names = set()
for link_wheel in links_wheels:
file_name = link_wheel.rsplit("/", 1)[1].replace(HTML_PLUS, "+")
if file_name not in file_names:
file_names.add(file_name)
f.write(f'<a href="{link_wheel}">{file_name}</a><br>\n')
f.write("</body></html>")
def create_pep_503_index(packages_dict, output_dir: Path):
packages = sorted(packages_dict.keys(), key=str.lower)
create_main_index(packages, output_dir / "index.html")
for package in packages:
package_dir = output_dir / normalize_pep_503(package)
package_dir.mkdir()
create_package_index(packages_dict[package], package_dir / "index.html")
def main():
github_token = os.environ["GITHUB_TOKEN"]
repository = os.environ["REPO_NAME"]
output_dir = Path("_site")
headers = {"Authorization": "token " + github_token}
releases = requests.get(f"https://api.github.com/repos/{repository}/releases", headers=headers)
links = get_links(releases)
links_wheels = [x for x in links if x.endswith(".whl")]
packages_dict = get_packages_dict(links_wheels)
output_dir.mkdir()
create_pep_503_index(packages_dict, output_dir)
if __name__ == "__main__":
main()