-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathreview.py
More file actions
236 lines (185 loc) · 9.21 KB
/
Copy pathreview.py
File metadata and controls
236 lines (185 loc) · 9.21 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
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
#!/usr/bin/env python
#
# See https://github.com/whatwg/meta/blob/main/MAINTAINERS.md#review-drafts for some notes on how to
# run this.
import argparse, datetime, json, os, subprocess, requests, glob, re
def print_header(string):
print()
print(f"\x1b[1m{string}\x1b[0m")
print()
def fetch_json(url):
return json.loads(requests.get(url).text)
def is_third_monday(d):
return d.weekday() == 0 and 15 <= d.day <= 21
def find_third_monday(d, days=1, limit=10):
i = 0
while not is_third_monday(d):
d = d + datetime.timedelta(days=days)
i += 1
if i > limit:
return None
return i * days
def days_from_third_monday(d):
if d.day < 15:
return find_third_monday(d)
elif d.day > 21:
return find_third_monday(d, -1)
else:
forward = find_third_monday(d)
if not forward:
return find_third_monday(d, -1)
return forward
def find_shortnames(workstreams, month=None):
shortnames = []
for workstream in workstreams:
for standard in workstream["standards"]:
if month and month not in standard["review_draft_schedule"]:
continue
shortnames.append(href_to_shortname(standard["href"]))
return shortnames
def href_to_shortname(href):
return href[len("https://"):href.index(".")]
def replace_rd_pointer(shortname, contents, path_month):
if shortname != "html":
return re.sub(
"Text Macro: LATESTRD [0-9]+-[0-9]+",
f"Text Macro: LATESTRD {path_month}",
contents
)
return re.sub(
"<a href=\"/review-drafts/[0-9]+-[0-9]+/\">",
f"<a href=\"/review-drafts/{path_month}/\">",
contents
)
def add_date_to_rd(shortname, contents, today):
if shortname != "html":
metadata_date = today.strftime("%Y-%m-%d")
return contents.replace(
"Group: WHATWG",
f"Group: WHATWG\nStatus: RD\nDate: {metadata_date}"
)
title_date = today.strftime("%B %Y")
contents = contents.replace(
"<title w-nodev>HTML Standard</title>",
f"<title w-nodev>HTML Standard Review Draft {title_date}</title>"
)
# This intentionally removes the <span class="pubdate"> since otherwise Wattsi would put in the build date.
pubdate = today.strftime("%d %B %Y")
contents = contents.replace(
'<p w-nohtml w-nosnap id="living-standard">Review Draft — Published <span class="pubdate">[DATE: 01 Jan 1901]</span></p>',
f'<p w-nohtml w-nosnap id="living-standard">Review Draft — Published {pubdate}</p>'
)
return contents
def create_pr(shortname, today):
nice_month = today.strftime("%B %Y")
path_month = today.strftime("%Y-%m")
# This is straight from MAINTAINERS.md and needs to be kept in sync with that.
pr_body = f"""The [{nice_month} Review Draft](https://{shortname}.spec.whatwg.org/review-drafts/{path_month}/) for this Workstream will be published shortly after merging this pull request.
Under the [WHATWG IPR Policy](https://whatwg.org/ipr-policy), Participants may, within 45 days after publication of a Review Draft, exclude certain Essential Patent Claims from the Review Draft Licensing Obligations. See the [IPR Policy](https://whatwg.org/ipr-policy) for details."""
subprocess.run(["gh", "pr", "create", "--title", f"Review Draft Publication: {nice_month}", "--body", pr_body])
def maybe_create_branch(shortname, today):
subprocess.run(["git", "checkout", "main"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True)
subprocess.run(["git", "pull"], stdout=subprocess.DEVNULL, check=True)
commits = subprocess.run(["git", "log", "--format=%s", "--max-count=40"], capture_output=True, check=True).stdout
for subject in commits.split(b"\n"):
if subject.startswith(b"Meta:"):
continue
elif subject.startswith(b"Review Draft Publication:"):
print_header(f"{shortname} had no non-Meta commits since the last publication")
return False
else:
print_header(f"Processing {shortname}")
break
nice_month = today.strftime("%B %Y")
path_month = today.strftime("%Y-%m")
subprocess.run(["git", "branch", "-D", f"review-draft-{path_month}"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
subprocess.run(["git", "checkout", "-B", f"review-draft-{path_month}"], check=True)
input_file = "source" if shortname == "html" else glob.glob("*.bs")[0]
with open(input_file, "r", encoding="utf-8", newline="\n") as file:
contents = file.read()
contents = replace_rd_pointer(shortname, contents, path_month)
with open(input_file, "w", encoding="utf-8", newline="\n") as file:
file.write(contents)
print("\nUpdated Living Standard to Point to the new Review Draft.")
print("Please verify that only one lined changed:")
subprocess.run(["git", "--no-pager", "diff"])
os.makedirs("review-drafts", exist_ok=True)
review_draft_contents = add_date_to_rd(shortname, contents, today)
file_extension = "wattsi" if shortname == "html" else "bs"
review_draft_file = f"review-drafts/{path_month}.{file_extension}"
with open(review_draft_file, "w", encoding="utf-8", newline="\n") as file:
file.write(review_draft_contents)
print(f"\nCreated Review Draft at {review_draft_file}")
print(f"Please verify that only two lines changed relative to {input_file}:")
subprocess.run(["git", "--no-pager", "diff", "--no-index", input_file, review_draft_file])
print()
subprocess.run(["git", "add", input_file], stdout=subprocess.DEVNULL, check=True)
subprocess.run(["git", "add", "review-drafts/*"], stdout=subprocess.DEVNULL, check=True)
subprocess.run(["git", "commit", "-m", f"Review Draft Publication: {nice_month}"], stdout=subprocess.DEVNULL, check=True)
return True
def regenerate_rd(shortname, today):
print_header(f"Regenerating Review Draft for {shortname}")
path_month = today.strftime("%Y-%m")
input_file = "source" if shortname == "html" else glob.glob("*.bs")[0]
with open(input_file, "r", encoding="utf-8", newline="\n") as file:
contents = file.read()
review_draft_contents = add_date_to_rd(shortname, contents, today)
file_extension = "wattsi" if shortname == "html" else "bs"
review_draft_file = f"review-drafts/{path_month}.{file_extension}"
with open(review_draft_file, "w", encoding="utf-8", newline="\n") as file:
file.write(review_draft_contents)
subprocess.run(["git", "add", input_file], stdout=subprocess.DEVNULL, check=True)
subprocess.run(["git", "add", review_draft_file], stdout=subprocess.DEVNULL, check=True)
subprocess.run(["git", "commit", "--amend", "--no-edit"], stdout=subprocess.DEVNULL, check=True)
print(f"\nRegenerated Review Draft at {review_draft_file}")
def main():
parser = argparse.ArgumentParser()
parser.add_argument("shortnames", nargs="*", help="Optional spec shortnames to create. If omitted, will use this month's per db.json.")
parser.add_argument("-f", "--force", action="store_true", help="bypass date checks")
parser.add_argument("-p", "--pr", action="store_true", help="create pull requests in addition to branches")
parser.add_argument("--regenerate", action="store_true", help="regenerate the review draft without creating a new branch")
args = parser.parse_args()
if os.path.basename(os.getcwd()) != "meta":
print("This script must be run from the meta directory.")
exit(1)
meta_directory = os.getcwd()
today = datetime.datetime.today()
ideal_publication_diff = days_from_third_monday(today)
if ideal_publication_diff == 0:
print("Right on, today is the day!")
if not args.force:
if ideal_publication_diff == None:
print("Publication is at least ten days away. Better wait. Use --force to ignore.")
exit(1)
elif ideal_publication_diff > 3:
print("It's still more than 3 days before publication. Use --force to ignore.")
exit(1)
elif ideal_publication_diff < -3:
print("It's 3 days after publication, hopefully you already published. Use --force to ignore.")
exit(1)
shortnames = args.shortnames
if not shortnames:
db = fetch_json("https://github.com/whatwg/sg/raw/main/db.json")
shortnames = find_shortnames(db["workstreams"], today.month)
if len(shortnames) == 0:
print("Looks like there's nothing to be published this month.")
exit(1)
for shortname in shortnames:
directory = os.path.join(os.path.dirname(meta_directory), shortname)
if not os.path.isdir(directory):
print(f"Directory for standard not found: {directory}.")
exit(1)
for shortname in shortnames:
directory = os.path.join(os.path.dirname(meta_directory), shortname)
try:
os.chdir(directory)
if args.regenerate:
regenerate_rd(shortname, today)
else:
branch_created = maybe_create_branch(shortname, today)
if (branch_created and args.pr):
create_pr(shortname, today)
finally:
os.chdir(meta_directory)
if __name__ == "__main__":
main()