From 425c97626a4e92c20ac47b1a5792e70fb441b1b4 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 9 Apr 2020 14:13:20 -0700 Subject: [PATCH] Consolidate translation script (#33911) * Consolidate translation script * Remove commented code --- azure-pipelines-translation.yml | 3 +- script/translations/__init__.py | 1 + script/translations/__main__.py | 40 ++++++++++++++ script/translations/const.py | 4 ++ .../download.py} | 53 ++++++++++++++++--- script/translations/error.py | 10 ++++ script/translations/util.py | 22 ++++++++ script/translations_download | 39 -------------- 8 files changed, 123 insertions(+), 49 deletions(-) create mode 100644 script/translations/__init__.py create mode 100644 script/translations/__main__.py create mode 100644 script/translations/const.py rename script/{translations_download_split.py => translations/download.py} (73%) create mode 100644 script/translations/error.py create mode 100644 script/translations/util.py delete mode 100755 script/translations_download diff --git a/azure-pipelines-translation.yml b/azure-pipelines-translation.yml index 0791cbcaab15..e448f45565bb 100644 --- a/azure-pipelines-translation.yml +++ b/azure-pipelines-translation.yml @@ -54,9 +54,8 @@ jobs: - template: templates/azp-step-git-init.yaml@azure - script: | export LOKALISE_TOKEN="$(lokaliseToken)" - export AZURE_BRANCH="$(Build.SourceBranchName)" - ./script/translations_download + python3 -m script.translations download displayName: 'Download Translation' - script: | git checkout dev diff --git a/script/translations/__init__.py b/script/translations/__init__.py new file mode 100644 index 000000000000..49c4b9b4b00c --- /dev/null +++ b/script/translations/__init__.py @@ -0,0 +1 @@ +"""Translation helper scripts.""" diff --git a/script/translations/__main__.py b/script/translations/__main__.py new file mode 100644 index 000000000000..f2f8b0a38911 --- /dev/null +++ b/script/translations/__main__.py @@ -0,0 +1,40 @@ +"""Validate manifests.""" +import argparse +from pathlib import Path +import sys + +from . import download, error + + +def get_arguments() -> argparse.Namespace: + """Get parsed passed in arguments.""" + parser = argparse.ArgumentParser(description="Home Assistant Scaffolder") + parser.add_argument("action", type=str, choices=["download"]) + parser.add_argument("--debug", action="store_true", help="Enable log output") + + arguments = parser.parse_args() + + return arguments + + +def main(): + """Run a translation script.""" + if not Path("requirements_all.txt").is_file(): + print("Run from project root") + return 1 + + args = get_arguments() + + if args.action == "download": + download.run(args) + + return 0 + + +if __name__ == "__main__": + try: + sys.exit(main()) + except error.ExitApp as err: + print() + print(f"Fatal Error: {err.reason}") + sys.exit(err.exit_code) diff --git a/script/translations/const.py b/script/translations/const.py new file mode 100644 index 000000000000..39daf02c204b --- /dev/null +++ b/script/translations/const.py @@ -0,0 +1,4 @@ +"""Translation constants.""" + +PROJECT_ID = "130246255a974bd3b5e8a1.51616605" +DOCKER_IMAGE = "b8329d20280263cad04f65b843e54b9e8e6909a348a678eac959550b5ef5c75f" diff --git a/script/translations_download_split.py b/script/translations/download.py similarity index 73% rename from script/translations_download_split.py rename to script/translations/download.py index 86944ec9f90e..1bb8d80f5522 100755 --- a/script/translations_download_split.py +++ b/script/translations/download.py @@ -3,10 +3,51 @@ import glob import json import os +import pathlib import re +import subprocess from typing import Dict, List, Union +from .const import DOCKER_IMAGE, PROJECT_ID +from .error import ExitApp +from .util import get_lokalise_token + FILENAME_FORMAT = re.compile(r"strings\.(?P\w+)\.json") +LOCAL_DIR = pathlib.Path("build/translations-download").absolute() + + +def run_download_docker(args): + """Run the Docker image to download the translations.""" + pipe_null = {} if args.debug else {"stdout": subprocess.DEVNULL} + + print("Running Docker to download latest translations.") + run = subprocess.run( + [ + "docker", + "run", + "-v", + f"{LOCAL_DIR}:/opt/dest/locale", + "--rm", + f"lokalise/lokalise-cli@sha256:{DOCKER_IMAGE}", + # Lokalise command + "lokalise", + "export", + PROJECT_ID, + "--token", + get_lokalise_token(), + "--export_empty", + "skip", + "--type", + "json", + "--unzip_to", + "/opt/dest", + ], + **pipe_null, + ) + print() + + if run.returncode != 0: + raise ExitApp("Failed to download translations") def load_json(filename: str) -> Union[List, Dict]: @@ -95,18 +136,14 @@ def save_language_translations(lang, translations): save_json(path, platform_translations) -def main(): +def run(args): """Run the script.""" - if not os.path.isfile("requirements_all.txt"): - print("Run this from HA root dir") - return + LOCAL_DIR.mkdir(parents=True, exist_ok=True) + + run_download_docker(args) paths = glob.iglob("build/translations-download/*.json") for path in paths: lang = get_language(path) translations = load_json(path) save_language_translations(lang, translations) - - -if __name__ == "__main__": - main() diff --git a/script/translations/error.py b/script/translations/error.py new file mode 100644 index 000000000000..bc8f21c23b5b --- /dev/null +++ b/script/translations/error.py @@ -0,0 +1,10 @@ +"""Errors for translations.""" + + +class ExitApp(Exception): + """Exception to indicate app should exit.""" + + def __init__(self, reason, exit_code=1): + """Initialize the exit app exception.""" + self.reason = reason + self.exit_code = exit_code diff --git a/script/translations/util.py b/script/translations/util.py new file mode 100644 index 000000000000..806a84457a34 --- /dev/null +++ b/script/translations/util.py @@ -0,0 +1,22 @@ +"""Translation utils.""" +import os +import pathlib + +from .error import ExitApp + + +def get_lokalise_token(): + """Get lokalise token.""" + token = os.environ.get("LOKALISE_TOKEN") + + if token is not None: + return token + + token_file = pathlib.Path(".lokalise_token") + + if not token_file.is_file(): + raise ExitApp( + "Lokalise token not found in env LOKALISE_TOKEN or file .lokalise_token" + ) + + return token_file.read_text().strip() diff --git a/script/translations_download b/script/translations_download deleted file mode 100755 index 2fa16604af1f..000000000000 --- a/script/translations_download +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/env bash - -# Safe bash settings -# -e Exit on command fail -# -u Exit on unset variable -# -o pipefail Exit if piped command has error code -set -eu -o pipefail - -cd "$(dirname "$0")/.." - -if [ -z "${LOKALISE_TOKEN-}" ] && [ ! -f .lokalise_token ] ; then - echo "Lokalise API token is required to download the latest set of" \ - "translations. Please create an account by using the following link:" \ - "https://lokalise.co/signup/130246255a974bd3b5e8a1.51616605/all/" \ - "Place your token in a new file \".lokalise_token\" in the repo" \ - "root directory." - exit 1 -fi - -# Load token from file if not already in the environment -[ -z "${LOKALISE_TOKEN-}" ] && LOKALISE_TOKEN="$(<.lokalise_token)" - -PROJECT_ID="130246255a974bd3b5e8a1.51616605" -LOCAL_DIR="$(pwd)/build/translations-download" -FILE_FORMAT=json - -mkdir -p ${LOCAL_DIR} - -docker run \ - -v ${LOCAL_DIR}:/opt/dest/locale \ - --rm \ - lokalise/lokalise-cli@sha256:b8329d20280263cad04f65b843e54b9e8e6909a348a678eac959550b5ef5c75f lokalise \ - --token ${LOKALISE_TOKEN} \ - export ${PROJECT_ID} \ - --export_empty skip \ - --type json \ - --unzip_to /opt/dest - -script/translations_download_split.py