-
Notifications
You must be signed in to change notification settings - Fork 204
/
cc0_update.py
executable file
·411 lines (374 loc) · 12.4 KB
/
cc0_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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#!/usr/bin/env python3
# vim: set fileencoding=utf-8:
"""Normalize file and add/update the language list at the bottom of all CC0
legalcode files.
"""
# Copyright 2016, 2017 Creative Commons
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Standard library
import argparse
import difflib
import glob
import os.path
import re
import sys
import traceback
# Local/library specific
import lang_tag_to
FAQ_TRANSLATION_LINK = "/faq/#officialtranslations"
FOOTER_COMMENTS = [
"<!-- Language Footer Start - DO NOT DELETE -->",
"<!-- Language Footer End - - DO NOT DELETE -->",
]
class ToolError(Exception):
def __init__(self, message, code=None):
self.code = code if code else 1
message = "({}) {}".format(self.code, message)
super(ToolError, self).__init__(message)
def diff_changes(filename, old, new):
"""Display changes as a colorized unified diff.
"""
diff = list(
difflib.unified_diff(
old.split("\n"),
new.split("\n"),
fromfile=f"{filename}: current",
tofile=f"{filename}: proposed",
n=3,
)
)
if not diff:
return
# Color diff output
rst = "\033[0m"
for i, line in enumerate(diff):
if line.startswith("---"):
diff[i] = f"\033[91m{line.rstrip()}{rst}"
elif line.startswith("+++"):
diff[i] = f"\033[92m{line.rstrip()}{rst}"
elif line.startswith("@"):
diff[i] = f"\033[36m{line.rstrip()}{rst}"
elif line.startswith("-"):
diff[i] = f"\033[31m{line}{rst}"
elif line.startswith("+"):
diff[i] = f"\033[32m{line}{rst}"
else:
diff[i] = f"\033[90m{line}{rst}"
print("\n".join(diff))
def update_lang_footer(args, filename, content, lang_tags):
"""Replace the contents of the language footer (everything between the
FOOTER_COMMENTS) with a list of links based on the legalcode files
currently present.
"""
current_language = lang_tags_from_filenames(filename)[0]
footer = ""
for lang_tag in lang_tags:
if lang_tag == current_language:
continue
# Determine to option value for the language. English breaks the
# pattern so handle it differently.
if lang_tag == "en":
index = "legalcode"
else:
index = f"legalcode.{lang_tag}"
link = (
f'<a href="/publicdomain/zero/1.0/{index}">'
f"{lang_tag_to.LABEL[lang_tag]}</a>,\n"
)
footer = f"{footer}{link}"
footer = footer.rstrip(",\n")
# Add the language footer block to the content
start, end = FOOTER_COMMENTS
target = re.search(f"{start}.*{end}", content, re.DOTALL).group()
if current_language in ["ja", "zh-Hans", "zh-Hant"]:
# Use ideographic full stop ("。")
period = "\u3002"
else:
# Use ASCII period
period = "."
replacement = f"{start}\n{footer}{period}\n{end}"
if target == replacement:
print(
f"{filename}: Skipping unneeded insertion of language footer"
" links"
)
else:
print(f"{filename}: Inserting language footer links")
if args.debug:
new_content = content.replace(target, replacement, 1)
diff_changes(filename, content, new_content)
return new_content
else:
return content.replace(target, replacement, 1)
def has_footer_comments(content):
"""Determine if the FOOTER_COMMENTS are already present.
"""
for comment in FOOTER_COMMENTS:
if content.find(comment) == -1:
return False
return True
def insert_missing_lang_footer_comments(args, filename, content):
"""Insert the FOOTER_COMMENTS in the appropriate locations, if they are not
present.
"""
if has_footer_comments(content):
print(
f"{filename}: Skipping unneeded language footer comments"
"insertion"
)
return content
print(f"{filename}: inserting language footer HTML comments")
re_pattern = re.compile(
f"""
(?P<prefix>
<blockquote>\\s*<a\\ id="languages">[^<]*</a>[^<]+
)
(?P<target>
# Maches all language link anchors
# Period or Ideographic Full Stop (\u3002)
.+</a>[.\u3002]
)
(?P<suffix>
.*"{FAQ_TRANSLATION_LINK}"
)
""",
re.DOTALL | re.MULTILINE | re.VERBOSE,
)
matches = re_pattern.search(content)
if matches is None:
print(
f"{filename}: ERROR: language block not matched. Aborting"
" processing"
)
return None
target = matches.group("target")
replacement = (
f"\n{FOOTER_COMMENTS[0]}\n"
f"{target.strip()}\n"
f"{FOOTER_COMMENTS[1]}\n"
)
if args.debug:
new_content = content.replace(target, replacement, 1)
diff_changes(filename, content, new_content)
return new_content
else:
return content.replace(target, replacement, 1)
def has_correct_faq_officialtranslations(content):
"""Determine if the link to the translation FAQ is correct.
"""
if content.find(f'"{FAQ_TRANSLATION_LINK}"') == -1:
return False
return True
def normalize_faq_translation_link(args, filename, content):
"""Replace various incorrect translation FAQ links with the correct link
(FAQ_TRANSLATION_LINK).
"""
if has_correct_faq_officialtranslations(content):
print(
f"{filename}: Skipping unneeded translation FAQ link"
" normalization"
)
return content
print(f"{filename}: normalizing translation FAQ link")
re_pattern = re.compile(
r"""
(?P<prefix>
href=['"]
)
(?P<target>
# Matches various translation FAQ URLs
[^'"]*/[Ff][Aa][Qq]/?[#][^'"]*
)
(?P<suffix>
['"]
)
""",
re.DOTALL | re.MULTILINE | re.VERBOSE,
)
matches = re_pattern.search(content)
if matches is None:
print(
f"{filename}: ERROR: translation link not matched. Aborting"
" processing"
)
return
target = matches.group("target")
replacement = FAQ_TRANSLATION_LINK
if args.debug:
new_content = content.replace(target, replacement, 1)
diff_changes(filename, content, new_content)
return new_content
else:
return content.replace(target, replacement, 1)
def has_correct_languages_anchor(content):
"""Determine if language anchor uses id
"""
if content.find('id="languages"') == -1:
return False
return True
def normalize_languages_anchor(args, filename, content):
"""Replace name with id in languages anchor (HTML5 compatibility)
"""
if has_correct_languages_anchor(content):
print(
f"{filename}: Skipping unneeded language anchor normalization"
)
return content
print(f"{filename}: normalizing language anchor id")
re_pattern = re.compile("name=['\"]languages['\"]", re.IGNORECASE)
matches = re_pattern.search(content)
if matches is None:
print(
f"{filename}: ERROR: languages anchor not matched. Aborting"
" processing"
)
return
target = matches.group()
replacement = 'id="languages"'
if args.debug:
new_content = content.replace(target, replacement, 1)
diff_changes(filename, content, new_content)
return new_content
else:
return content.replace(target, replacement, 1)
def normalize_line_endings(args, filename, content):
"""Normalize line endings to unix LF (\\n)
"""
re_pattern = re.compile("\r(?!\n)")
matches = re_pattern.findall(content)
message = ""
if matches:
message = f" {len(matches)} mac newlines (CR)"
re_pattern = re.compile("\r\n")
matches = re_pattern.findall(content)
if matches:
if message:
message = f"{message} and"
message = f"{message} {len(matches)} windows newlines (CRLF)"
if message:
print(f"{filename}: Converting{message} to unix newlines (LF)")
return "\n".join(content.split("\r\n"))
else:
print(f"{filename}: Skipping unneeded newline conversion")
return content
def process_file_contents(args, file_list, lang_tags):
"""Process each of the CC0 legalcode files and update them, as necessary.
"""
for filename in file_list:
with open(filename, "r", encoding="utf-8", newline="") as file_in:
content = file_in.read()
new_content = content
new_content = normalize_line_endings(args, filename, new_content)
new_content = normalize_languages_anchor(args, filename, new_content)
if new_content is None:
sys.exit(1)
new_content = normalize_faq_translation_link(
args, filename, new_content
)
if new_content is None:
sys.exit(1)
new_content = insert_missing_lang_footer_comments(
args, filename, new_content
)
if new_content is None:
sys.exit(1)
new_content = update_lang_footer(
args, filename, new_content, lang_tags
)
if new_content is None:
sys.exit(1)
if content == new_content:
print(
f"{filename}: Skipping writing back to file (no changes)"
)
elif args.debug:
print(f"{filename}: DEBUG: Skipping writing changes to file")
else:
print(f"{filename}: Writing changes to file")
with open(filename, "w", encoding="utf-8") as file_out:
file_out.write(new_content)
print()
def lang_tags_from_filenames(file_list):
"""Extract RFC 5646 language tags from filename(s)
"""
if isinstance(file_list, str):
lang_tags = [file_list.split(".")[1][2:]]
else:
lang_tags = list(
set([filename.split(".")[1][2:] for filename in file_list])
)
try:
lang_tags[lang_tags.index("")] = "en"
except ValueError:
pass
lang_tags.sort()
return lang_tags
def setup():
"""Instantiate and configure argparse and logging.
Return argsparse namespace.
"""
default_glob = ["zero_1.0*.html"]
ap = argparse.ArgumentParser(description=__doc__)
ap.add_argument(
"-d",
"--debug",
action="store_true",
help="Debug mode: list changes without modification",
)
ap.add_argument(
"globs",
nargs="*",
default=default_glob,
help=(
"Filename or shell glob of the file(s) that will be updated"
f' (default: "{default_glob[0]}")'
),
metavar="FILENAME",
)
args = ap.parse_args()
return args
def main():
args = setup()
file_list = sorted(
list(
set(
[
filename
for fileglob in args.globs
for filename in glob.glob(fileglob)
if os.path.isfile(filename)
if not os.path.islink(filename)
]
)
)
)
lang_tags = lang_tags_from_filenames(file_list)
process_file_contents(args, file_list, lang_tags)
if __name__ == "__main__":
try:
main()
except SystemExit as e:
sys.exit(e.code)
except KeyboardInterrupt:
print("INFO (130) Halted via KeyboardInterrupt.", file=sys.stderr)
sys.exit(130)
except ToolError:
error_type, error_value, error_traceback = sys.exc_info()
print("CRITICAL {}".format(error_value), file=sys.stderr)
sys.exit(error_value.code)
except: # noqa: ignore flake8: E722 do not use bare 'except'
print("ERROR (1) Unhandled exception:", file=sys.stderr)
print(traceback.print_exc(), file=sys.stderr)
sys.exit(1)