-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathfiles.py
More file actions
76 lines (61 loc) · 2.58 KB
/
files.py
File metadata and controls
76 lines (61 loc) · 2.58 KB
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
"""
A script that takes the stats Cloudflare provides from our local DB and generates the repo READMEs.
Also handles committing the all the monthly stats changes to Git.
"""
import calendar
import sqlite3
import subprocess
import utils
def readme():
# Connect to the DB and get all the unique months/years we have data for
conn = sqlite3.connect("data.db")
conn.row_factory = sqlite3.Row
c = conn.cursor()
c.execute("SELECT DISTINCT year, month FROM totals ORDER BY year DESC, month DESC")
rows = c.fetchall()
# Group by year
by_year = {}
for item in rows:
if item["year"] not in by_year:
by_year[item["year"]] = []
by_year[item["year"]].append(item["month"])
# Manually inject months with missing data
by_year[2021] += [10]
by_year[2019] += [2]
# Generate the stats lists for each year
stats_by_year = {}
years = sorted(by_year.keys(), reverse=True)
for year in years:
stats_list = ["* [{0} {1}]({1}/cdnjs_{0}_{1}.md)".format(calendar.month_name[month], year) for month in sorted(by_year[year], reverse=True)]
stats_by_year[year] = "\n".join(stats_list)
# Generate the root README
with open("./README.md", "w+") as f:
f.write(utils.get_template(
"root_readme.md",
{
"LATEST_STATS": "**📈 [{0} {1}]({1}/cdnjs_{0}_{1}.md)**".format(calendar.month_name[rows[0]["month"]], rows[0]["year"]),
"PREVIOUS_STATS": "\n".join(["### [{0}]({0})\n\n{1}\n".format(year, stats_by_year[year]) for year in years]).strip()
}
))
# Generate the year READMEs
for year in years:
with open(f"./{year}/README.md", "w+") as f:
f.write(utils.get_template(
"year_readme.md",
{
"YEAR": year,
"STATS": stats_by_year[year].replace("{}/".format(year), "")
}
))
def commit(month, year):
# Stage + commit all the Markdown files
subprocess.run(["git", "add", "README.md", f"{year}/README.md", f"{year}/cdnjs_{calendar.month_name[month]}_{year}.md"])
subprocess.run(["git", "commit", "-m", f"Stats: {calendar.month_name[month]} {year}"])
# Stage + commit the DB file
subprocess.run(["git", "add", "data.db"])
subprocess.run(["git", "commit", "-m", f"Data: {calendar.month_name[month]} {year}"])
# Stage + commit the graphs
subprocess.run(["git", "add", "*.png"])
subprocess.run(["git", "commit", "-m", f"Graphs: {calendar.month_name[month]} {year}"])
if __name__ == "__main__":
readme()