forked from jorio/gitfourchette
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_resources.py
More file actions
executable file
·444 lines (345 loc) · 14.6 KB
/
update_resources.py
File metadata and controls
executable file
·444 lines (345 loc) · 14.6 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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#! /usr/bin/env python3
# -----------------------------------------------------------------------------
# Copyright (C) 2026 Iliyas Jorio.
# This file is part of GitFourchette, distributed under the GNU GPL v3.
# For full terms, see the included LICENSE file.
# -----------------------------------------------------------------------------
import argparse
import datetime
import difflib
import html
import json
import os
import re
import subprocess
import sys
import textwrap
from contextlib import suppress
from pathlib import Path
import pygit2
REPO_ROOTDIR = os.path.dirname(os.path.realpath(sys.argv[0]))
REPO_ROOTDIR = os.path.relpath(REPO_ROOTDIR)
SRC_DIR = os.path.join(REPO_ROOTDIR, "gitfourchette")
ASSETS_DIR = os.path.join(SRC_DIR, "assets")
LANG_DIR = os.path.join(ASSETS_DIR, "lang")
LANG_TEMPLATE = os.path.join(LANG_DIR, "gitfourchette.pot")
FORCE = False
def makeParser():
parser = argparse.ArgumentParser(description="Update GitFourchette assets")
parser.add_argument("-f", "--force", action="store_true",
help="skip mtime and equality checks before regenerating an asset")
parser.add_argument("-V", "--version", action="store_true",
help="show tool versions and exit")
loc_group = parser.add_argument_group("Localization options")
loc_group.add_argument("--pot", action="store_true",
help="sync .pot template with new strings from python code")
loc_group.add_argument("--po", action="store_true",
help="sync translatable .po files with .pot template")
loc_group.add_argument("--mo", action="store_true",
help="compile .po files to .mo so you can try them in GitFourchette")
loc_group.add_argument("-l", "--lang", action="store_true",
help="sync all .pot/.po/.mo files (run all localization steps above)")
loc_group.add_argument("--clean-po", action="store_true",
help="remove obsolete strings from .po files")
ui_group = parser.add_argument_group("UI Designer options")
ui_group.add_argument("-u", "--ui", action="store_true",
help="update ui_*.py files from .ui files (and svg status icons)")
ui_group.add_argument("--uic", default="pyuic6",
help="path to Python-compatible uic tool ('pyuic6' by default; AVOID 'pyside6-uic' because its output doesn't work with PyQt6)")
ui_group.add_argument("--no-uic-cleanup", action="store_true",
help="don't postprocess uic output")
pkg_group = parser.add_argument_group("Packaging options")
pkg_group.add_argument("--freeze", default="", metavar="QT_API",
help="write frozen constants to appconsts.py and exit")
credits_group = parser.add_argument_group("Credits options")
credits_group.add_argument("--contributors", action="store_true",
help="format code contributor list")
credits_group.add_argument("--weblate-credits", default="", metavar="JSON_REPORT",
help="format translator credits from Weblate JSON report (https://hosted.weblate.org/projects/gitfourchette/gitfourchette/#reports)")
return parser
def call(*args, **kwargs) -> subprocess.CompletedProcess:
cmdstr = ""
for token in args:
cmdstr += " "
if " " in token:
cmdstr += F"\"{token}\""
else:
cmdstr += token
print(F">{cmdstr}")
capture_output = kwargs.pop("capture_output", True)
check = kwargs.pop("check", True)
try:
return subprocess.run(args, encoding='utf-8', capture_output=capture_output, check=check, **kwargs)
except subprocess.CalledProcessError as e:
print(F"Aborting setup because: {e}")
sys.exit(1)
def writeIfDifferent(path: Path, text: str, ignoreChangedLines=None):
ignoreChangedLines = ignoreChangedLines or []
needRewrite = True
if not FORCE and path.is_file():
ignoreList = []
for icl in ignoreChangedLines:
ignoreList.append("+ " + icl)
ignoreList.append("- " + icl)
# See if the differences can be ignored (e.g. Qt User Interface Compiler version comment)
oldText = path.read_text(encoding="utf-8")
needRewrite = False
if oldText != text:
t1 = oldText.splitlines(keepends=True)
t2 = text.splitlines(keepends=True)
for dl in difflib.ndiff(t1, t2):
if (dl.rstrip() not in ["+", "-"] # pure whitespace change
and not dl.startswith(tuple(ignoreList))
and dl.startswith(("+ ", "- "))):
needRewrite = True
break
if needRewrite:
path.write_text(text, encoding="utf-8")
print("Wrote", path)
else:
path.touch()
def patchSection(path: Path, contents: str):
def ensureNewline(s: str):
return s + ("" if s.endswith("\n") else "\n")
text = path.read_text(encoding="utf-8")
contents = ensureNewline(contents)
lines = contents.splitlines(keepends=True)
assert len(lines) >= 2
beginMarker = lines[0]
endMarker = lines[-1]
assert beginMarker
assert endMarker
beginPos = text.index(beginMarker)
endPos = text.index(endMarker.rstrip())
newText = (text[: beginPos] + contents + text[endPos + len(endMarker) :])
writeIfDifferent(path, newText)
return newText
def writeStatusIcon(fill='#ff00ff', char='X', round=2):
svg = "\n".join([
"<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'>",
f"<rect rx='{round}' ry='{round}' x='0.5' y='0.5' width='15' height='15' stroke='white' stroke-width='1' fill='{fill}'/>",
f"<text x='8' y='12' font-weight='bold' font-size='11' font-family='sans-serif' text-anchor='middle' fill='white'>{char}</text>",
"</svg>",
])
svgPath = Path(ASSETS_DIR) / f"icons/status_{char.lower()}.svg"
writeIfDifferent(svgPath, svg)
def compileUi(uic: str, uiPath: Path, pyPath: Path, force=False, cleanupOutput=True):
if not force:
with suppress(FileNotFoundError):
if pyPath.stat().st_mtime > uiPath.stat().st_mtime:
return
result = call(uic, uiPath.name, cwd=uiPath.parent)
text = result.stdout
nukePatterns = []
ignoreDiffs = []
myImport = "from gitfourchette.localization import *\nfrom gitfourchette.qt import *"
if not cleanupOutput:
pass
elif "from PyQt" in text:
text = re.sub(r"^from PyQt[56] import .+$", myImport, text, count=1, flags=re.M)
text = re.sub(r"_translate(?=\(\")", "_p", text, flags=re.M)
nukePatterns = [
r"(?<!\w)Qt(Core|Gui|Widgets)\.",
r"^\s+_translate = QCoreApplication\.translate\n",
]
ignoreDiffs = ["# Created by: PyQt6 UI code generator"]
text = text.strip() + "\n"
elif "from PySide" in text:
text = re.sub(r"^from PySide6.* import \([^\)]+\)$", myImport, text, count=1, flags=re.M)
text = re.sub(r"QCoreApplication\.translate\((.+), None\)", r"_p(\1)", text, flags=re.M)
nukePatterns = [
r"^#if QT_CONFIG\(.+\n",
r"^#endif // QT_CONFIG\(.+\n",
r"^from PySide6.* import \([^\)]+\)$\n",
r"^ {4}# (setupUi|retranslateUi)$\n",
]
ignoreDiffs = ["## Created by: Qt User Interface Compiler version"]
text = text.strip() + "\n"
else:
print("Unknown uic output")
for pattern in nukePatterns:
text = re.sub(pattern, "", text, flags=re.M)
writeIfDifferent(pyPath, text, ignoreDiffs)
def compileUiFiles(uic, force, cleanupOutput):
for uiPath in Path(SRC_DIR).glob("**/*.ui"):
pyPath = uiPath.parent / f"ui_{uiPath.stem}.py"
compileUi(uic, uiPath, pyPath, force=force, cleanupOutput=cleanupOutput)
for pyPath in Path(SRC_DIR).glob("**/ui_*.py"):
uiPath = pyPath.parent / (pyPath.stem.removeprefix("ui_") + ".ui")
if not uiPath.exists():
print("[!] Removing obsolete compiled ui file because there's no matching designer file:", pyPath)
pyPath.unlink()
def generateIcons():
# Generate status icons.
# 'U' (unmerged) has custom colors/text, so don't generate it automatically.
# 'C' (copied) doesn't appear in GitFourchette.
writeStatusIcon('#0EDF00', 'A') # add
writeStatusIcon('#FE635F', 'D') # delete
writeStatusIcon('#F7C342', 'M') # modify
writeStatusIcon('#D18DE1', 'R') # rename
writeStatusIcon('#85144B', 'T') # typechange
writeStatusIcon('#ff00ff', 'X') # unknown
def updatePotTemplate():
""" Update .pot files from strings contained in the source code """
# Gather all .py files
pyFiles = [str(f.relative_to(SRC_DIR)) for f in Path(SRC_DIR).glob("**/*.py")]
pyFiles.sort()
call(
"xgettext",
"--output=" + LANG_TEMPLATE,
"--sort-by-file",
"--no-wrap",
"--language=Python",
"--from-code=UTF-8",
"--keyword=_n:1,2",
"--keyword=_p:1c,2",
"--keyword=_np:1c,2,3",
"--directory=" + SRC_DIR,
"--package-name=GitFourchette",
"--msgid-bugs-address=https://github.com/jorio/gitfourchette/issues",
*pyFiles,
capture_output=False,
)
nukePatterns = [
r"^# SOME DESCRIPTIVE TITLE\.\n",
r"^# Copyright .C. YEAR THE PACKAGE'S COPYRIGHT HOLDER\n",
r"^# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR\.\n",
r'^"POT-Creation-Date: .+"\n',
r'^"PO-Revision-Date: .+"\n',
r'^"Language-Team: .+"\n',
]
text = Path(LANG_TEMPLATE).read_text(encoding="utf-8")
for pattern in nukePatterns:
text = re.sub(pattern, "", text, flags=re.M)
Path(LANG_TEMPLATE).write_text(text, "utf-8")
def updatePoFiles():
""" Update .po files from strings contained in the .pot template """
for poPath in Path(LANG_DIR).glob("*.po"):
call(
"msgmerge",
"--update",
"--sort-by-file",
"--no-wrap",
str(poPath),
LANG_TEMPLATE,
capture_output=False)
def cleanUpPoFiles():
""" Remove obsolete strings from .po files """
for poPath in Path(LANG_DIR).glob("*.po"):
call(
"msgattrib",
"--sort-by-file",
"--no-wrap",
"--no-obsolete",
"-o", str(poPath),
str(poPath),
capture_output=False)
def compileMoFiles():
""" Generate .mo files from .po files """
wipLanguages = []
for poPath in Path(LANG_DIR).glob("*.po"):
moPath: Path = poPath.with_suffix(".mo")
call("msgfmt", "-o", str(moPath), str(poPath), capture_output=False)
moSizeKB = moPath.stat().st_size // 1024
# Remove empty po/mo files
if moSizeKB == 0:
print(f"*** Removing empty translation '{poPath.name}'")
poPath.unlink()
moPath.unlink()
continue
# Small .mo files are considered stubs and won't be included in builds
if moSizeKB < 5:
print(f"*** Removing '{moPath.name}' -- looks like a stub ({moSizeKB} KB)")
moPath.unlink()
continue
# .mo files below 100 KB are considered incomplete
if moSizeKB < 100:
wipLanguages += [moPath.stem]
Path(LANG_DIR, "wip.txt").write_text("\n".join(wipLanguages) + "\n")
def formatTranslatorCredits(jsonReportPath: str):
blob = Path(jsonReportPath).read_bytes()
table = json.loads(blob)
renameLanguages = {
"Chinese (Simplified Han script)": "S. Chinese",
}
def formatPerson(person):
full = person["full_name"]
user = person["username"]
if full.casefold() == user.casefold():
return html.escape(full)
else:
return f"{full} ({user})"
tableRows = []
for entry in table:
for language, people in entry.items():
if language == "French": # I manage that one
continue
languageName = renameLanguages.get(language, language)
peopleList = "\n\t<br>".join(formatPerson(p) for p in people if p["username"] != "jorio")
totalContribs = sum(p["change_count"] for p in people)
row = ("<tr>\n"
f"\t<td align=right>{languageName}: </td>\n"
f"\t<td>{peopleList}</td>\n"
"</tr>\n")
tableRows.append((totalContribs, row))
# Sort languages by total amount of contributions
tableRows.sort(reverse=True)
allRows = ''.join(tr for _, tr in tableRows)
markup = f"<table>\n{allRows}</table>"
Path(LANG_DIR, "credits.html").write_text(markup)
def formatContributors():
process = call("git", "shortlog", "--summary", ":!gitfourchette/assets/lang")
contributors = [
line.split("\t", 1)[1]
for line in process.stdout.splitlines(keepends=True)
]
Path(ASSETS_DIR, "contributors.txt").write_text("".join(contributors))
def writeFreezeFile(qtApi: str):
repo = pygit2.Repository(SRC_DIR)
headCommit = repo.head.target
buildDate = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M")
freezeText = textwrap.dedent(f"""\
# BEGIN_FREEZE_CONSTS
####################################
# Do not commit these changes!
####################################
APP_FREEZE_COMMIT = "{headCommit}"
APP_FREEZE_DATE = "{buildDate}"
APP_FREEZE_QT = "{qtApi.lower()}"
# END_FREEZE_CONSTS""")
patchSection(Path(SRC_DIR) / 'appconsts.py', freezeText)
if __name__ == '__main__':
args = makeParser().parse_args()
FORCE = args.force
if args.lang:
args.pot = True
args.po = True
args.mo = True
if args.version:
toolVersions = ""
toolVersions += args.uic + " " + call(args.uic, "--version").stdout
toolVersions += call("msgmerge", "--version").stdout.splitlines()[0]
print(toolVersions)
sys.exit(0)
if args.freeze:
writeFreezeFile(args.freeze)
sys.exit(0)
if not (args.ui or args.pot or args.po or args.mo or args.clean_po or args.weblate_credits or args.contributors):
makeParser().print_usage()
sys.exit(1)
# Generate .py files from .ui files
if args.ui:
generateIcons()
compileUiFiles(args.uic, args.force, not args.no_uic_cleanup)
if args.pot:
updatePotTemplate()
if args.po:
updatePoFiles()
if args.mo:
compileMoFiles()
if args.clean_po:
cleanUpPoFiles()
if args.weblate_credits:
formatTranslatorCredits(args.weblate_credits)
if args.contributors:
formatContributors()