-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathpo-refresh
More file actions
executable file
·122 lines (101 loc) · 3.82 KB
/
Copy pathpo-refresh
File metadata and controls
executable file
·122 lines (101 loc) · 3.82 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
#!/usr/bin/env python3
# Copyright (C) 2017-2026 Red Hat, Inc.
# SPDX-License-Identifier: GPL-3.0-or-later
import argparse
import json
import os
import subprocess
from pathlib import Path
from typing import Literal
from lib import git, testmap
from lib.constants import BASE_DIR
from lib.github import NOT_TESTED_DIRECT, GitHub
MANIFEST_PATH = Path("pkg/shell/manifest.json")
LANGUAGE_MAP_PATH = Path("po/language_map.txt")
def modify_shell_manifest(op: Literal['+', '-'], lang: str) -> None:
"""Add or remove a language from the shell's locale list, if it exists."""
try:
data = json.loads(MANIFEST_PATH.read_text())
language_map = LANGUAGE_MAP_PATH.read_text()
except FileNotFoundError:
return
locales = dict(data["locales"])
for line in language_map.splitlines():
map_lang, display_name, locale_code = line.strip().split(":")
if map_lang == lang:
if op == '+':
locales[locale_code] = display_name
else:
locales.pop(locale_code, None)
break
else:
raise KeyError(f"Language {lang!r} not found in {LANGUAGE_MAP_PATH}")
MANIFEST_PATH.write_text(
json.dumps(
{
**data,
"locales": {k: locales[k] for k in sorted(locales)},
},
ensure_ascii=False,
indent=4,
)
+ "\n"
)
git.add(MANIFEST_PATH)
def main() -> None:
parser = argparse.ArgumentParser(description="Update translations from Fedora Weblate")
parser.add_argument("--dry-run", "-n", action="store_true", help="Dry run to validate this task if supported")
args = parser.parse_args()
github = GitHub()
lang_changes = 0
# Remove languages that fall under the coverage threshold
coverage_limit = float(os.getenv("COVERAGE_THRESHOLD", "0.8"))
for po in Path("po").glob("*.po"):
stats = subprocess.check_output(
["msgfmt", "--statistics", str(po)],
cwd=BASE_DIR, text=True, stderr=subprocess.STDOUT,
)
# "2310 translated messages, 4 fuzzy translations, 4 untranslated messages."
counts = [int(part.split()[0]) for part in stats.split(", ")]
n_translated_messages = counts[0]
n_messages_total = sum(counts)
if n_translated_messages / n_messages_total < coverage_limit:
lang = po.stem
po.unlink() # to avoid "remove modified file?" from git
git.rm(po)
if not git.changes_staged():
continue # file was never tracked
modify_shell_manifest('-', lang)
git.commit(f"po: Drop '{lang}' language")
lang_changes += 1
# Add languages that got over 80% translated
for po in git.untracked("po/"):
if po.suffix == ".po":
git.add(po)
modify_shell_manifest('+', po.stem)
git.commit(f"po: Add '{po.stem}' language")
lang_changes += 1
# Add anything else (ie: updates of existing languages)
git.add("po/")
if git.changes_staged():
git.commit("po: Update from Fedora Weblate")
lang_changes += 1
if not lang_changes or args.dry_run:
return
# Create a pull request from these changes
branch = git.push(github, "po-refresh")
pr_number = github.create_pull_request(
branch,
"Update translations from Fedora Weblate",
labels=['bot', 'no-test', 'release-blocker'],
)
git.amend_and_forcepush(github, branch, closes=pr_number)
head = git.get_current_head()
# Trigger the tests for po-refresh
for trigger in testmap.tests_for_po_refresh(github.repo):
github.post(
f"statuses/{head}",
{"context": trigger, "state": "pending", "description": NOT_TESTED_DIRECT},
)
if __name__ == '__main__':
main()