forked from YunoHost/yunohost
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmissing_i18n_keys.py
267 lines (237 loc) · 9.54 KB
/
missing_i18n_keys.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
#!/usr/bin/env python3
#
# Copyright (c) 2024 YunoHost Contributors
#
# This file is part of YunoHost (see https://yunohost.org)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import glob
import json
import os
import re
import subprocess
import sys
import toml
import yaml
ROOT = os.path.dirname(__file__) + "/../"
LOCALE_FOLDER = ROOT + "/locales/"
REFERENCE_FILE = LOCALE_FOLDER + "en.json"
###############################################################################
# Find used keys in python code #
###############################################################################
def find_expected_string_keys():
# Try to find :
# m18n.n( "foo"
# YunohostError("foo"
# YunohostValidationError("foo"
# # i18n: foo
p1 = re.compile(r"m18n\.n\(\n*\s*[\"\'](\w+)[\"\']")
p2 = re.compile(r"YunohostError\(\n*\s*[\'\"](\w+)[\'\"]")
p3 = re.compile(
r"Yunohost(Validation|Authentication)Error\(\n*\s*[\'\"](\w+)[\'\"]"
)
p4 = re.compile(r"# i18n: [\'\"]?(\w+)[\'\"]?")
python_files = glob.glob(ROOT + "src/*.py")
python_files.extend(glob.glob(ROOT + "src/utils/*.py"))
python_files.extend(glob.glob(ROOT + "src/migrations/*.py"))
python_files.extend(glob.glob(ROOT + "src/migrations/*.py.disabled"))
python_files.extend(glob.glob(ROOT + "src/authenticators/*.py"))
python_files.extend(glob.glob(ROOT + "src/diagnosers/*.py"))
python_files.append(ROOT + "bin/yunohost")
for python_file in python_files:
content = open(python_file).read()
for m in p1.findall(content):
if m.endswith("_"):
continue
yield m
for m in p2.findall(content):
if m.endswith("_"):
continue
yield m
for _, m in p3.findall(content):
if m.endswith("_"):
continue
yield m
for m in p4.findall(content):
yield m
# For each diagnosis, try to find strings like "diagnosis_stuff_foo" (c.f. diagnosis summaries)
# Also we expect to have "diagnosis_description_<name>" for each diagnosis
p3 = re.compile(r"[\"\'](diagnosis_[a-z]+_\w+)[\"\']")
for python_file in glob.glob(ROOT + "src/diagnosers/*.py"):
if "__init__.py" in python_file:
continue
content = open(python_file).read()
for m in p3.findall(content):
if m.endswith("_"):
# Ignore some name fragments which are actually concatenated with other stuff..
continue
yield m
yield "diagnosis_description_" + os.path.basename(python_file)[:-3].split("-")[
-1
]
# For each migration, expect to find "migration_description_<name>"
for path in glob.glob(ROOT + "src/migrations/*.py"):
if "__init__" in path:
continue
yield "migration_description_" + os.path.basename(path)[:-3]
# For each default service, expect to find "service_description_<name>"
for service, info in yaml.safe_load(
open(ROOT + "conf/yunohost/services.yml")
).items():
if info is None:
continue
yield "service_description_" + service
# For all unit operations, expect to find "log_<name>"
# A unit operation is created either using the @is_unit_operation decorator
# or using OperationLogger(
cmd = f"grep -hr '@is_unit_operation([^f]' {ROOT}/src/ -A3 2>/dev/null | grep '^def' | sed -E 's@^def (\\w+)\\(.*@\\1@g'"
for funcname in (
subprocess.check_output(cmd, shell=True).decode("utf-8").strip().split("\n")
):
yield "log_" + funcname
p4 = re.compile(r"OperationLogger\(\n*\s*[\"\'](\w+)[\"\']")
for python_file in python_files:
content = open(python_file).read()
for m in ("log_" + match for match in p4.findall(content)):
yield m
# Keys for the actionmap ...
for category in yaml.safe_load(open(ROOT + "share/actionsmap.yml")).values():
if "actions" not in category.keys():
continue
for action in category["actions"].values():
if "arguments" not in action.keys():
continue
for argument in action["arguments"].values():
extra = argument.get("extra")
if not extra:
continue
if "password" in extra:
yield extra["password"]
if "ask" in extra:
yield extra["ask"]
if "comment" in extra:
yield extra["comment"]
if "pattern" in extra:
yield extra["pattern"][1]
if "help" in extra:
yield extra["help"]
# Hardcoded expected keys ...
yield "admin_password" # Not sure that's actually used nowadays...
for method in ["tar", "copy", "custom"]:
yield "backup_applying_method_%s" % method
yield "backup_method_%s_finished" % method
registrars = toml.load(open(ROOT + "share/registrar_list.toml"))
supported_registrars = ["ovh", "gandi", "godaddy"]
for registrar in supported_registrars:
for key in registrars[registrar].keys():
yield f"domain_config_{key}"
# Domain config panel
domain_config = toml.load(open(ROOT + "share/config_domain.toml"))
domain_settings_with_help_key = [
"portal_logo",
"portal_public_intro",
"portal_theme",
"portal_user_intro",
"search_engine",
"custom_css",
"dns",
"enable_public_apps_page",
]
domain_section_with_no_name = ["app", "cert_", "mail", "registrar"]
for panel_key, panel in domain_config.items():
if not isinstance(panel, dict):
continue
yield f"domain_config_{panel_key}_name"
for section_key, section in panel.items():
if not isinstance(section, dict):
continue
if section_key not in domain_section_with_no_name:
yield f"domain_config_{section_key}_name"
for key, values in section.items():
if not isinstance(values, dict):
continue
yield f"domain_config_{key}"
if key in domain_settings_with_help_key:
yield f"domain_config_{key}_help"
# Global settings
global_config = toml.load(open(ROOT + "share/config_global.toml"))
# Boring hard-coding because there's no simple other way idk
settings_without_help_key = [
"passwordless_sudo",
"smtp_relay_host",
"smtp_relay_password",
"smtp_relay_port",
"smtp_relay_user",
"ssowat_panel_overlay_enabled",
"root_password",
"root_access_explain",
"root_password_confirm",
"tls_passthrough_explain",
]
for panel_key, panel in global_config.items():
if not isinstance(panel, dict):
continue
yield f"global_settings_setting_{panel_key}_name"
for section_key, section in panel.items():
if not isinstance(section, dict):
continue
yield f"global_settings_setting_{section_key}_name"
for key, values in section.items():
if not isinstance(values, dict):
continue
yield f"global_settings_setting_{key}"
if key not in settings_without_help_key:
yield f"global_settings_setting_{key}_help"
###############################################################################
# Compare keys used and keys defined #
###############################################################################
if len(sys.argv) <= 1 or sys.argv[1] not in ["--check", "--fix"]:
print("Please specify --check or --fix")
sys.exit(1)
expected_string_keys = set(find_expected_string_keys())
keys_defined_for_en = json.loads(open(REFERENCE_FILE).read()).keys()
keys_defined = set(keys_defined_for_en)
unused_keys = keys_defined.difference(expected_string_keys)
unused_keys = sorted(unused_keys)
undefined_keys = expected_string_keys.difference(keys_defined)
undefined_keys = sorted(undefined_keys)
mode = sys.argv[1].strip("-")
if mode == "check":
# Unused keys are not too problematic, will be automatically
# removed by the other autoreformat script,
# but still informative to display them
if unused_keys:
print(
"Those i18n keys appears unused:\n" " - " + "\n - ".join(unused_keys)
)
if undefined_keys:
print(
"Those i18n keys should be defined in en.json:\n"
" - " + "\n - ".join(undefined_keys)
)
sys.exit(1)
elif mode == "fix":
j = json.loads(open(REFERENCE_FILE).read())
for key in undefined_keys:
j[key] = "FIXME"
for key in unused_keys:
del j[key]
json.dump(
j,
open(REFERENCE_FILE, "w"),
indent=4,
ensure_ascii=False,
sort_keys=True,
)