forked from pyvec/docs.pyvec.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
190 lines (166 loc) · 5.77 KB
/
Copy pathcli.py
File metadata and controls
190 lines (166 loc) · 5.77 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
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
import subprocess
import sys
from operator import itemgetter
from pathlib import Path
from datetime import date
import click
import requests
from jinja2 import Template
from pyvec_docs.boards import load_boards
from pyvec_docs.grants import get_resolution_date, get_lock_date
from pyvec_docs.grants import get_votes, remove_comments, to_date
@click.group()
def main() -> None:
pass
@main.command()
def test() -> None:
"""Run the tests"""
try:
subprocess.run(["pytest"], check=True)
except subprocess.CalledProcessError:
sys.exit(1)
@main.command()
def build() -> None:
"""Build the documentation into the build directory"""
try:
subprocess.run(["sphinx-build", "-nWaE", "docs", "build"], check=True)
except subprocess.CalledProcessError:
sys.exit(1)
@main.command()
def watch() -> None:
"""Serve documentation on a local webserver, rebuild on changes"""
try:
subprocess.run(["sphinx-autobuild", "-nWaE", "docs", "build"], check=True)
except subprocess.CalledProcessError:
sys.exit(1)
@main.command()
@click.option(
"--template",
"template_path",
type=click.Path(exists=True, path_type=Path),
default="docs/operations/grants.rst.jinja",
)
@click.option(
"--output",
"output_path",
type=click.Path(path_type=Path),
default="docs/operations/grants.rst",
)
@click.option(
"--github-url", type=str, default="https://api.github.com/repos/pyvec/money/issues"
)
@click.option("--github-token", type=str, envvar="GITHUB_TOKEN")
def gen_grants(
template_path: Path,
output_path: Path,
github_url: str,
github_token: str,
) -> None:
"""Generate grants.rst (needs authorization token)"""
github_headers = {
"Accept": "application/vnd.github.squirrel-girl-preview",
"Authorization": f"token {github_token}",
}
res = requests.get(
github_url,
headers=github_headers,
params={"per_page": 100, "state": "closed"},
)
res.raise_for_status()
issues = res.json()
boards = load_boards()
grants = []
with click.progressbar(issues, label="Processing issues") as progress:
for issue in progress:
labels = [label["name"] for label in issue["labels"]]
if "approved" not in labels and "rejected" not in labels:
# skip unlabeled, e.g. https://github.com/pyvec/money/issues/1
continue
# Figure out what to report as the date of the vote.
# For issues newer than 2024-09-05:
# - use the date of adding the "approved" or "rejected" label
# For issues older than that, use older method (this way the
# already-generated dates don't change):
# - for locked issues, use the date the issue was locked
# - for unlocked issues, use date when the issue was closed
is_new = to_date(issue["created_at"]) >= date(2024, 9, 5)
if is_new or issue["locked"]:
res = requests.get(
issue["events_url"],
headers=github_headers,
params={"per_page": 100},
)
res.raise_for_status()
if res.headers.get("link"):
raise NotImplementedError(
f"The number of events of issue #{issue['number']} "
"is paginated and the code isn't yet designed "
"to handle this!"
)
if is_new:
voted_at = get_resolution_date(res.json())
else:
voted_at = get_lock_date(res.json())
if not voted_at:
continue
else:
voted_at = to_date(issue["closed_at"])
res = requests.get(issue["reactions"]["url"], headers=github_headers)
res.raise_for_status()
votes = list(get_votes(res.json(), voted_at, boards))
grants.append(
{
"title": issue["title"],
"description": remove_comments(issue["body"]),
"url": issue["html_url"],
"user": {
"username": issue["user"]["login"],
"url": issue["user"]["html_url"],
},
"is_approved": "approved" in labels,
"filed_at": to_date(issue["created_at"]),
"voted_at": voted_at,
"votes": votes,
}
)
grants = sorted(grants, key=itemgetter("voted_at"), reverse=True)
tpl = Template(template_path.read_text())
output_path.write_text(tpl.render(grants=grants))
@main.command()
@click.option(
"--template",
"template_path",
type=click.Path(exists=True, path_type=Path),
default="docs/operations/boards.rst.jinja",
)
@click.option(
"--output",
"output_path",
type=click.Path(path_type=Path),
default="docs/operations/boards.rst",
)
def gen_boards(
template_path: Path,
output_path: Path,
) -> None:
"""Generate boards.rst"""
boards = load_boards()
tpl = Template(template_path.read_text())
output_path.write_text(tpl.render(boards=boards))
@main.command()
@click.option(
"--source-url",
type=str,
default="https://cdn.jsdelivr.net/npm/@twemoji/api@latest/dist/twemoji.min.js",
)
@click.option(
"--output",
"output_path",
type=click.Path(path_type=Path),
default="docs/_static/twemoji.min.js",
)
def gen_twemoji(source_url: str, output_path: Path) -> None:
"""Download the latest version of twemoji.min.js from CDN of https://github.com/jdecked/twemoji/"""
res = requests.get(source_url)
res.raise_for_status()
output_path.write_bytes(res.content)