-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathupdate.py
executable file
·215 lines (182 loc) · 5.83 KB
/
update.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
#!/usr/bin/env python
import argparse
import os
import sys
import time
from datetime import datetime
from statistics import median
from subprocess import (
call,
check_call,
check_output,
CalledProcessError,
DEVNULL,
)
import nbformat
import requests
from nbconvert.preprocessors import ExecutePreprocessor
def fetch_count(username, token, samples=3, tries=10):
"""Queries the GitHub API to get the current ipynb count.
Takes the median of multiple samples to account for wildly different results that appear from
time to time and truncates to an int.
Parameters
----------
username: str
GitHub API username
token: str
GitHub API token
samples: int
Number of samples to take from the GitHub API
tries: int
Maximum number of times to try to fetch before failing
Returns
-------
int
"""
counts = []
for i in range(tries):
resp = requests.get(
"https://api.github.com/search/code?q=nbformat+in:file+extension:ipynb",
headers={"Accept": "application/vnd.github.v3+json"},
auth=(username, token),
)
if resp.ok:
count = resp.json()["total_count"]
counts.append(count)
print(f"Fetched sample #{i}: {count}")
else:
print(resp.text)
if len(counts) == samples:
break
# Linear backoff in minutes
time.sleep((i + 1) * 60)
else:
raise RuntimeError(f"Could not fetch {samples} samples in {tries} tries")
return int(median(counts))
def store_count(date, count, filename="ipynb_counts.csv"):
"""Reads the CSV containing the historical `year-month-day,count` pairs
and upserts the count for the current date.
Parameters
----------
date: str
Date in year-month-day format
count: int
Count of ipynb files
filename: str
CSV filename
"""
# Read the historical counts if the file containing them exists
if os.path.isfile(filename):
with open(filename) as fh:
lines = fh.readlines()
counts = dict(line.strip().split(",") for line in lines[1:])
else:
counts = {}
# Upsert the count for the given date
counts[date] = count
# Write out the CSV sorted by date
with open(filename, "w") as fh:
fh.write("date,hits\n")
for date in sorted(counts):
fh.write(f"{date},{counts[date]}\n")
def execute_notebook(src="estimate.src.ipynb", dest="estimate.ipynb"):
"""Executes the analysis notebook and writes out a copy with all of the
resulting tables and plots.
Parameters
----------
src: str, optional
Source notebook to execute
dest: str, optional
Output notebook
"""
with open(src) as fp:
nb = nbformat.read(fp, 4)
exp = ExecutePreprocessor(timeout=300)
updated_nb, _ = exp.preprocess(nb, {})
with open(dest, "w") as fp:
nbformat.write(updated_nb, fp)
def configure_ci_git(token, repo="parente/nbestimate"):
"""Configures CI to push to GitHub.
Parameters
----------
token: str
GitHub API token
repo: str, optional
GitHub org/repo
"""
call(["git", "remote", "rm", "origin"])
check_call(
["git", "remote", "add", "origin", f"https://{token}@github.com/{repo}.git"],
stdout=DEVNULL,
stderr=DEVNULL,
)
call(["git", "config", "--global", "user.name", "GitHub Actions"])
call(
["git", "config", "--global", "user.email", "actions@users.noreply.github.com"]
)
def git_commit_and_push(date):
"""Commits all changed files in the local sandbox and pushes them to origin-pushback.
Parameters
----------
date: str
Date in year-month-day format
"""
print(check_output(["git", "checkout", "master"], encoding="utf-8"))
print(
check_output(
["git", "commit", "-a", "-m", "Update for {}".format(date)],
encoding="utf-8",
)
)
print(check_output(["git", "push", "origin", "master"], encoding="utf-8"))
def main(argv):
"""Uses the GitHub API to estimate the current count of public ipynb files on GitHub,
stores that count in a CSV file associated with today's date (localtime), executes
a notebook to analyze the growth, and commits the CSV and executed notebook back
to GitHub.
Parameters
----------
argv: list
Command line arguments
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"--assert-more-than",
type=int,
default=0,
help="Abort further actions if the count is less than the given value",
)
parser.add_argument(
"--skip-fetch",
action="store_true",
help="Skip fetching the current count from GitHub",
)
parser.add_argument(
"--skip-execute",
action="store_true",
help="Skip executing the notebook analysis",
)
parser.add_argument(
"--skip-push",
action="store_true",
help="Skip committing and pushing the result to GitHub",
)
args = parser.parse_args(argv)
date = datetime.now().strftime("%Y-%m-%d")
if not args.skip_fetch:
print(f"Fetching count for {date}")
count = fetch_count("parente", os.environ["GITHUB_TOKEN"])
assert count >= args.assert_more_than, f"{count} < {args.assert_more_than}"
print(f"Storing count {count} for {date}")
store_count(date, count)
if not args.skip_execute:
print("Executing notebook")
execute_notebook()
if not args.skip_push:
if os.getenv("CI"):
print("Configuring CI for commit to GitHub")
configure_ci_git(os.environ["GITHUB_TOKEN"])
print("Conmitting and pushing update")
git_commit_and_push(date)
if __name__ == "__main__":
main(sys.argv[1:])