-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmissing_locales.py
116 lines (95 loc) · 3.07 KB
/
missing_locales.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
"""
Retrieves a list of locales missing in Pontoon for a single project by comparing with GitHub repo.
Output as CSV file with column Missing Locales.
"""
import argparse
import json
import sys
from urllib.parse import quote as urlquote
from urllib.request import urlopen
def retrieve_pontoon_locales(project):
query = f'{{project(slug:"{project}"){{name,localizations{{locale{{code}}}}}}}}'
url = f"https://pontoon.mozilla.org/graphql?query={urlquote(query)}"
try:
response = urlopen(url)
json_data = json.load(response)
if "errors" in json_data:
sys.exit(f"Project {project} not found in Pontoon.")
locale_list = []
for locale in json_data["data"]["project"]["localizations"]:
locale_list.append(locale["locale"]["code"])
locale_list.sort()
return locale_list
except Exception as e:
sys.exit(e)
def retrieve_github_locales(owner, repo, path):
query = f"/repos/{owner}/{repo}/contents/{path}"
url = f"https://api.github.com{urlquote(query)}"
ignored_folders = ["templates", "configs"]
try:
response = urlopen(url)
json_data = json.load(response)
# Ignore files, hidden folder, non-locale folders via ignore list
locale_list = [
e["name"]
for e in json_data
if e["type"] == "dir"
and not e["name"].startswith(".")
and e["name"] not in ignored_folders
]
locale_list.sort()
return locale_list
except Exception as e:
sys.exit(e)
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--pontoon",
required=True,
dest="pontoon_project",
help="Pontoon project name",
)
parser.add_argument(
"--repo",
required=True,
dest="github_repo",
help="GitHub repository name",
)
parser.add_argument(
"--owner",
required=False,
default="mozilla-l10n",
dest="github_owner",
help="GitHub repository owner name",
)
parser.add_argument(
"--path",
required=False,
default="",
dest="github_path",
help="GitHub path that contains locale folders",
)
parser.add_argument(
"--csv",
required=False,
action="store_true",
default=False,
dest="csv_output",
help="Store data as output.csv",
)
args = parser.parse_args()
pontoon_locales = retrieve_pontoon_locales(args.pontoon_project)
github_locales = retrieve_github_locales(
args.github_owner, args.github_repo, args.github_path
)
output = ["Missing Locales"]
missing_locales = list(set(github_locales) - set(pontoon_locales))
missing_locales.sort()
print(f"Missing locales in Pontoon: {', '.join(missing_locales)}")
if args.csv_output:
with open("output.csv", "w") as f:
output.extend(missing_locales)
f.write("\n".join(output))
print("Missing locales saved to output.csv")
if __name__ == "__main__":
main()