forked from di/pyreadiness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
134 lines (113 loc) · 3.41 KB
/
main.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
import os
import collections
import datetime
import json
import urllib.request
from pathlib import Path
from google.cloud import bigquery
import jinja2
PYPI_URL = "https://pypi.org/pypi/{name}/json"
Status = collections.namedtuple(
"Status", ("dying", "eol", "dev", "alpha", "beta", "rc"), defaults=(False,) * 6
)
MAJORS = {
# version: (past_eol, alpha)
"2.3": Status(eol=True),
"2.4": Status(eol=True),
"2.5": Status(eol=True),
"2.6": Status(eol=True),
"2.7": Status(eol=True),
"3.0": Status(eol=True),
"3.1": Status(eol=True),
"3.2": Status(eol=True),
"3.3": Status(eol=True),
"3.4": Status(eol=True),
"3.5": Status(eol=True),
"3.6": Status(eol=True),
"3.7": Status(),
"3.8": Status(),
"3.9": Status(),
"3.10": Status(),
"3.11": Status(),
"3.12": Status(alpha=True),
}
QUERY = """
SELECT
file.project,
COUNT(*) AS total_downloads
FROM
`bigquery-public-data.pypi.file_downloads`
WHERE
DATE(timestamp) BETWEEN DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY)
AND DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY)
AND details.python LIKE '{major}.%'
GROUP BY
file.project
ORDER BY
total_downloads DESC
LIMIT
360
"""
bq_client = bigquery.Client()
templateLoader = jinja2.FileSystemLoader(searchpath="./templates/")
templateEnv = jinja2.Environment(loader=templateLoader)
major_template = templateEnv.get_template("major.html")
index_template = templateEnv.get_template("index.html")
def project_json(name):
print(f"Fetching '{name}'")
response = urllib.request.urlopen(PYPI_URL.format(name=name))
return json.loads(response.read())
def supports(major, classifiers, status):
return (f"Programming Language :: Python :: {major}" in classifiers) != (
status.eol or status.dying
)
def fetch_top_projects():
print("Fetching top projects")
projects = {
major: [
row["project"]
for row in bq_client.query(QUERY.format(major=major)).result()
]
for major in ["2", "3"]
}
print(projects)
print("Fetching top projects complete")
return projects
def fetch_classifiers(names):
print("Fetching classifiers")
classifiers = {
name: set(project_json(name)["info"]["classifiers"]) for name in names
}
print(classifiers)
print("Fetching classifiers complete")
return classifiers
def write_local_file(filename, contents, content_type="text/html"):
print(f"Writing file '{filename}' locally")
path = Path("docs") / filename
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w") as f:
f.write(contents)
if __name__ == "__main__":
updated = datetime.datetime.now()
projects = fetch_top_projects()
classifiers = fetch_classifiers(set().union(*projects.values()))
for major, status in MAJORS.items():
results = [
(name, supports(major, classifiers[name], status))
for name in projects[major[0]]
]
print(major, status, results)
do_support = sum(result[1] for result in results)
write_local_file(
f"{major}/index.html",
major_template.render(
results=results,
major=major,
status=status,
updated=updated,
do_support=do_support,
),
)
write_local_file(
"index.html", index_template.render(updated=updated, majors=MAJORS)
)