Skip to content

Commit e9c412b

Browse files
authored
Migrate translations upload (home-assistant#33926)
* Migrate translations upload * Fix token in download command * Minor cleanup
1 parent 2edfa82 commit e9c412b

8 files changed

+100
-154
lines changed

azure-pipelines-translation.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
export LOKALISE_TOKEN="$(lokaliseToken)"
3838
export AZURE_BRANCH="$(Build.SourceBranchName)"
3939
40-
./script/translations_upload
40+
python3 -m script.translations upload
4141
displayName: 'Upload Translation'
4242
4343
- job: 'Download'

script/translations/__main__.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
from pathlib import Path
44
import sys
55

6-
from . import download, error
6+
from . import download, error, upload
77

88

99
def get_arguments() -> argparse.Namespace:
1010
"""Get parsed passed in arguments."""
1111
parser = argparse.ArgumentParser(description="Home Assistant Scaffolder")
12-
parser.add_argument("action", type=str, choices=["download"])
12+
parser.add_argument("action", type=str, choices=["download", "upload"])
1313
parser.add_argument("--debug", action="store_true", help="Enable log output")
1414

1515
arguments = parser.parse_args()
@@ -27,6 +27,8 @@ def main():
2727

2828
if args.action == "download":
2929
download.run(args)
30+
elif args.action == "upload":
31+
upload.run(args)
3032

3133
return 0
3234

script/translations/const.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""Translation constants."""
2+
import pathlib
23

34
PROJECT_ID = "130246255a974bd3b5e8a1.51616605"
45
DOCKER_IMAGE = "b8329d20280263cad04f65b843e54b9e8e6909a348a678eac959550b5ef5c75f"
6+
INTEGRATIONS_DIR = pathlib.Path("homeassistant/components")

script/translations/download.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
LOCAL_DIR = pathlib.Path("build/translations-download").absolute()
1717

1818

19-
def run_download_docker(args):
19+
def run_download_docker():
2020
"""Run the Docker image to download the translations."""
21-
pipe_null = {} if args.debug else {"stdout": subprocess.DEVNULL}
22-
2321
print("Running Docker to download latest translations.")
2422
run = subprocess.run(
2523
[
@@ -31,18 +29,17 @@ def run_download_docker(args):
3129
f"lokalise/lokalise-cli@sha256:{DOCKER_IMAGE}",
3230
# Lokalise command
3331
"lokalise",
34-
"export",
35-
PROJECT_ID,
3632
"--token",
3733
get_lokalise_token(),
34+
"export",
35+
PROJECT_ID,
3836
"--export_empty",
3937
"skip",
4038
"--type",
4139
"json",
4240
"--unzip_to",
4341
"/opt/dest",
4442
],
45-
**pipe_null,
4643
)
4744
print()
4845

@@ -140,7 +137,7 @@ def run(args):
140137
"""Run the script."""
141138
LOCAL_DIR.mkdir(parents=True, exist_ok=True)
142139

143-
run_download_docker(args)
140+
run_download_docker()
144141

145142
paths = glob.iglob("build/translations-download/*.json")
146143
for path in paths:

script/translations/upload.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env python3
2+
"""Merge all translation sources into a single JSON file."""
3+
import json
4+
import os
5+
import pathlib
6+
import re
7+
import subprocess
8+
9+
from .const import DOCKER_IMAGE, INTEGRATIONS_DIR, PROJECT_ID
10+
from .error import ExitApp
11+
from .util import get_current_branch, get_lokalise_token
12+
13+
FILENAME_FORMAT = re.compile(r"strings\.(?P<suffix>\w+)\.json")
14+
LOCAL_FILE = pathlib.Path("build/translations-upload.json").absolute()
15+
CONTAINER_FILE = "/opt/src/build/translations-upload.json"
16+
LANG_ISO = "en"
17+
18+
19+
def run_upload_docker():
20+
"""Run the Docker image to upload the translations."""
21+
print("Running Docker to upload latest translations.")
22+
run = subprocess.run(
23+
[
24+
"docker",
25+
"run",
26+
"-v",
27+
f"{LOCAL_FILE}:{CONTAINER_FILE}",
28+
"--rm",
29+
f"lokalise/lokalise-cli@sha256:{DOCKER_IMAGE}",
30+
# Lokalise command
31+
"lokalise",
32+
"--token",
33+
get_lokalise_token(),
34+
"import",
35+
PROJECT_ID,
36+
"--file",
37+
CONTAINER_FILE,
38+
"--lang_iso",
39+
LANG_ISO,
40+
"--convert_placeholders",
41+
"0",
42+
"--replace",
43+
"1",
44+
],
45+
)
46+
print()
47+
48+
if run.returncode != 0:
49+
raise ExitApp("Failed to download translations")
50+
51+
52+
def run(args):
53+
"""Run the script."""
54+
if get_current_branch() != "dev" and os.environ.get("AZURE_BRANCH") != "dev":
55+
raise ExitApp(
56+
"Please only run the translations upload script from a clean checkout of dev."
57+
)
58+
59+
translations = {"component": {}}
60+
61+
for path in INTEGRATIONS_DIR.glob(f"*{os.sep}strings*.json"):
62+
component = path.parent.name
63+
match = FILENAME_FORMAT.search(path.name)
64+
platform = match.group("suffix") if match else None
65+
66+
parent = translations["component"].setdefault(component, {})
67+
68+
if platform:
69+
platforms = parent.setdefault("platform", {})
70+
parent = platforms.setdefault(platform, {})
71+
72+
parent.update(json.loads(path.read_text()))
73+
74+
LOCAL_FILE.parent.mkdir(parents=True, exist_ok=True)
75+
LOCAL_FILE.write_text(json.dumps(translations, indent=4, sort_keys=True))
76+
77+
# run_upload_docker()

script/translations/util.py

+12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Translation utils."""
22
import os
33
import pathlib
4+
import subprocess
45

56
from .error import ExitApp
67

@@ -20,3 +21,14 @@ def get_lokalise_token():
2021
)
2122

2223
return token_file.read_text().strip()
24+
25+
26+
def get_current_branch():
27+
"""Get current branch."""
28+
return (
29+
subprocess.run(
30+
["git", "rev-parse", "--abbrev-ref", "HEAD"], stdout=subprocess.PIPE
31+
)
32+
.stdout.decode()
33+
.strip()
34+
)

script/translations_upload

-45
This file was deleted.

script/translations_upload_merge.py

-99
This file was deleted.

0 commit comments

Comments
 (0)