-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
328 lines (291 loc) · 11.4 KB
/
generate.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Create a static website out of output from the cdnjs API for directing people
to a reverse proxy of the cdnjs libraries.
"""
import argparse
import logging
import os
import re
import time
import urllib3.exceptions
import urllib.parse
import jinja2
import requests
def github_request(url, token, logger=None):
"""Make a request against the GitHub REST API."""
headers = {
"Accept": "application/vnd.github+json",
"Authorization": "Bearer {}".format(token),
"X-GitHub-Api-Version": "2022-11-28",
}
response = requests.get(url, headers=headers)
if response.status_code == 403:
reset_time = response.headers["x-ratelimit-reset"]
sleep_duration = (
float(reset_time)
- time.time()
+ 10
)
if logger:
logger.info(
"GitHub returned HTTP 403, "
"sleeping for %.1f s until @%s (plus ten seconds)",
sleep_duration,
reset_time,
)
time.sleep(sleep_duration)
return github_request(url, token, logger=logger)
return response.json(), response.status_code
def github_stars(user, repo, token, logger=None):
"""Get number of github stars a repo has"""
url = "https://api.github.com/repos/%s/%s" % (user, repo)
data, _ = github_request(url, token, logger=logger)
return data.get("stargazers_count", 0)
def github_trim_entry(entry):
"""Trim a GitHub tree entry to save memory."""
if "type" in entry and "path" in entry:
return {
"type": entry["type"],
"path": entry["path"],
# throw away mode, sha, size, url
}
else:
# might be an error, better keep all of it
return entry
def github_trim_tree(data):
"""Trim a GitHub tree to save memory."""
if "tree" in data:
return {
"tree": [
github_trim_entry(entry)
for entry in data["tree"]
],
# throw away sha, url, truncated
}
else:
# probably an error, better keep all of it
return data
def github_tree(url, token, recursive=False, logger=None):
"""Get a Git tree from the GitHub REST API."""
if not recursive:
data, _ = github_request(url, token, logger=logger)
return github_trim_tree(data)
recursive_url = url + "?recursive=true"
data, _ = github_request(recursive_url, token, logger=logger)
if not data.get("truncated", False):
return github_trim_tree(data)
if logger:
logger.info(
"GitHub returned truncated data for %s, "
"falling back to many non-recursive requests",
recursive_url,
)
# if GitHub returned a/1, a/2, b/1,
# then we can assume the a/ subtree is complete,
# and only do additional requests staring from b/
truncated_data = data
recursive_tree = []
current_prefix = None
current_subtree = []
complete_subtrees = set()
for entry in truncated_data.get("tree", []):
[prefix, *_] = entry["path"].split("/", 1)
if prefix != current_prefix:
recursive_tree += current_subtree
if current_prefix is not None:
complete_subtrees.add(current_prefix)
current_prefix = prefix
current_subtree = []
current_subtree.append(github_trim_entry(entry))
truncated_data = [] # free memory early
data, _ = github_request(url, token, logger=logger)
if "tree" not in data:
raise KeyError(f'{url} returned data without "tree": {data}')
for entry in data["tree"]:
if entry["path"] in complete_subtrees:
continue
recursive_tree.append(github_trim_entry(entry))
if entry["type"] == "tree":
subtree = github_tree(entry["url"], token, recursive=True, logger=logger)
for subentry in subtree["tree"]:
recursive_tree.append({
**subentry,
"path": f"{entry['path']}/{subentry['path']}",
})
return {
"tree": recursive_tree,
}
def main():
argparser = argparse.ArgumentParser()
argparser.add_argument(
"-v",
"--verbose",
action="count",
default=0,
dest="loglevel",
help="Increase logging verbosity",
)
argparser.add_argument(
"--token",
dest="github_token",
metavar="<github_token>",
type=str,
required=True,
help=(
"A filename with a GitHub personal access token"
"with public_repos permission"
),
)
argparser.add_argument("outputpath", help="Path to html output")
args = argparser.parse_args()
logging.basicConfig(
level=max(logging.DEBUG, logging.WARNING - (10 * args.loglevel)),
format="%(asctime)s %(name)-12s %(levelname)-8s: %(message)s",
datefmt="%Y-%m-%dT%H:%M:%SZ",
)
logging.captureWarnings(True)
logger = logging.getLogger("generate")
outputdir = args.outputpath
with open("template.html.j2") as index_temp:
html = jinja2.Template(index_temp.read(), autoescape=True)
with open("modal-template.html.j2") as modal_temp:
helper = jinja2.Template(modal_temp.read(), autoescape=True)
with open(args.github_token) as token_file:
github_token = token_file.read()
github_token = github_token.strip()
fields = "version,description,homepage,keywords,license,repository,author"
upstream_url = "https://api.cdnjs.com/libraries"
list_url = upstream_url + "?fields={}".format(fields)
with requests.get(list_url, stream=True) as resp:
json_resp = resp.json()
all_packages = json_resp["results"]
# get a github:cdnjs/cdnjs ajax/libs/ listing in preparation
# (using github_request() instead of github_tree()
# because here we need untrimmed trees with URLs)
github_cdnjs_url = "https://api.github.com/repos/cdnjs/cdnjs/git/trees/master"
github_cdnjs_tree, _ = github_request(github_cdnjs_url, github_token)
github_ajax_url = [entry["url"]
for entry
in github_cdnjs_tree["tree"]
if entry["path"] == "ajax"][0]
github_ajax_tree, _ = github_request(github_ajax_url, github_token)
github_libs_url = [entry["url"]
for entry
in github_ajax_tree["tree"]
if entry["path"] == "libs"][0]
github_libs_tree, _ = github_request(github_libs_url, github_token)
github_library_urls = {
entry["path"]: entry["url"]
for entry in github_libs_tree["tree"]
}
libraries = []
for package in all_packages:
name = package["name"]
logger.info("Processing %s...", name)
lib = {
"name": name,
"description": package.get("description", None),
"version": package.get("version", None),
"homepage": package.get("homepage", None),
"keywords": package.get("keywords", None),
"assets": {},
}
# get assets from CDNjs API as preferred source
# (note that since 2022 it only returns the latest version, see T342519)
assets_url = (
upstream_url
+ "/"
+ urllib.parse.quote(lib["name"])
+ "?fields=assets,versions"
)
num_versions = 1
with requests.get(assets_url) as resp:
try:
data = resp.json()
num_versions = len(data.get("versions", []))
for asset in data.get("assets", []):
lib["assets"][asset["version"]] = asset
except (KeyError, ValueError):
logger.exception("Failed to fetch assets using %s", assets_url)
if num_versions < 1000:
# get other versions from listing GitHub instead
# (we could get them from CDNjs but only with one request per version, way too slow)
try:
github_library_tree = github_tree(
github_library_urls[name],
github_token,
recursive=True,
logger=logger,
)
# we get a flat listing of ver1/file1, ver1/subdir/file2, ver2/file1 etc.
# group that into one entry per version and add it to the assets
current_asset = None
if "tree" not in github_library_tree:
raise KeyError(
f'{github_library_urls[name]} returned data '
f'without "tree": {github_library_tree}',
)
for entry in github_library_tree["tree"]:
if entry["path"] == "package.json":
continue
[version, *_] = entry["path"].split("/", 1)
if current_asset is None:
current_asset = {"version": version, "files": []}
elif version != current_asset["version"]:
lib["assets"].setdefault(current_asset["version"], current_asset)
current_asset = {"version": version, "files": []}
if entry["type"] == "blob":
current_asset["files"].append(entry["path"][len(version)+1:])
if current_asset is not None:
lib["assets"].setdefault(current_asset["version"], current_asset)
except (
KeyError,
ValueError,
requests.exceptions.RequestException,
urllib3.exceptions.HTTPError,
):
logger.exception("Failed to fetch additional versions for %s from GitHub", name)
else:
lib["too_many_assets"] = num_versions
logger.info(
"Too many versions for %s (%d), skipping fetching of older versions",
name,
num_versions,
)
if lib["keywords"] is None: # Found an example of this.
lib["keywords"] = []
if package["repository"] and "github.com/" in package["repository"].get(
"url", ""
):
url = package["repository"]["url"]
else:
url = None
if url is not None:
parts = re.sub(r"^\w+://", "", url).split("/")
user_name, repo_name = parts[-2], parts[-1]
if repo_name.endswith(".git"):
repo_name = repo_name[:-4]
logger.debug("Fetching starcount for %s/%s", user_name, repo_name)
lib["stars"] = github_stars(user_name, repo_name, github_token, logger=logger)
libraries.append(lib)
with open(
os.path.join(outputdir, "mod" + name + ".html"),
"w",
encoding="utf8",
) as modal_file:
modal_file.write(helper.render({"lib": lib}))
# throw away non-latest versions to save memory, not needed in index.html
try:
lib["assets"] = {lib["version"]: lib["assets"][lib["version"]]}
except KeyError:
# happened for 'xls' library (KeyError: '1.0.0')
pass
libraries.sort(key=lambda lib: lib.get("stars", 0), reverse=True)
with open(
os.path.join(outputdir, "index.html"), "w", encoding="utf8"
) as index_file:
index_file.write(html.render({"libraries": libraries}))
if __name__ == "__main__":
main()