|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Utilities to download and extract translations from the GitHub repository. |
| 4 | +""" |
| 5 | + |
| 6 | +import io |
| 7 | +import os |
| 8 | +import shutil |
| 9 | +from subprocess import ( |
| 10 | + PIPE, |
| 11 | + Popen, |
| 12 | +) |
| 13 | +import sys |
| 14 | +import tarfile |
| 15 | + |
| 16 | +import requests |
| 17 | +import yaml |
| 18 | + |
| 19 | + |
| 20 | +def get_config(config_fname: str) -> dict: |
| 21 | + """ |
| 22 | + Load the config yaml file and return it as a dictionary. |
| 23 | + """ |
| 24 | + with open(config_fname, encoding="utf-8") as f: |
| 25 | + context = yaml.safe_load(f) |
| 26 | + return context |
| 27 | + |
| 28 | + |
| 29 | +def download_and_extract_translations(url: str, dir_name: str) -> None: |
| 30 | + """ |
| 31 | + Download the translations from the GitHub repository. |
| 32 | + """ |
| 33 | + shutil.rmtree(dir_name, ignore_errors=True) |
| 34 | + response = requests.get(url) |
| 35 | + if response.status_code == 200: |
| 36 | + doc = io.BytesIO(response.content) |
| 37 | + with tarfile.open(None, "r:gz", doc) as tar: |
| 38 | + tar.extractall(dir_name) |
| 39 | + else: |
| 40 | + raise Exception(f"Failed to download translations: {response.status_code}") |
| 41 | + |
| 42 | + |
| 43 | +def get_languages(source_path: str) -> list[str]: |
| 44 | + """ |
| 45 | + Get the list of languages available in the translations directory. |
| 46 | + """ |
| 47 | + en_path = f"{source_path}/en/" |
| 48 | + if os.path.exists(en_path): |
| 49 | + shutil.rmtree(en_path) |
| 50 | + |
| 51 | + paths = os.listdir(source_path) |
| 52 | + return [path for path in paths if os.path.isdir(f"{source_path}/{path}")] |
| 53 | + |
| 54 | + |
| 55 | +def remove_translations(source_path: str, languages: list[str]) -> None: |
| 56 | + """ |
| 57 | + Remove the translations from the source path. |
| 58 | + """ |
| 59 | + for language in languages: |
| 60 | + shutil.rmtree(os.path.join(source_path, language), ignore_errors=True) |
| 61 | + |
| 62 | + |
| 63 | +def copy_translations(source_path: str, target_path: str, languages: list[str]) -> None: |
| 64 | + """ |
| 65 | + Copy the translations to the appropriate directory. |
| 66 | + """ |
| 67 | + for lang in languages: |
| 68 | + dest = f"{target_path}/{lang}/" |
| 69 | + shutil.rmtree(dest, ignore_errors=True) |
| 70 | + cmds = [ |
| 71 | + "rsync", |
| 72 | + "-av", |
| 73 | + "--delete", |
| 74 | + f"{source_path}/{lang}/", |
| 75 | + dest, |
| 76 | + ] |
| 77 | + p = Popen(cmds, stdout=PIPE, stderr=PIPE) |
| 78 | + stdout, stderr = p.communicate() |
| 79 | + sys.stderr.write(f"\nCopying: {lang}...\n\n") |
| 80 | + sys.stderr.write(stdout.decode()) |
| 81 | + sys.stderr.write(stderr.decode()) |
| 82 | + |
| 83 | + |
| 84 | +def process_translations( |
| 85 | + config_fname: str, source_path: str, process_translations: bool |
| 86 | +) -> tuple[list[str], list[str]]: |
| 87 | + """ |
| 88 | + Process the translations by downloading and extracting them from |
| 89 | + the GitHub repository. |
| 90 | + """ |
| 91 | + base_folder = os.path.dirname(__file__) |
| 92 | + config = get_config(os.path.join(source_path, config_fname)) |
| 93 | + translations_path = os.path.join(base_folder, f"{config['translations']['folder']}") |
| 94 | + translations_source_path = os.path.join( |
| 95 | + translations_path, config["translations"]["source_path"] |
| 96 | + ) |
| 97 | + default_language = config["translations"]["default_language"] |
| 98 | + sys.stderr.write("\nDownloading and extracting translations...\n\n") |
| 99 | + download_and_extract_translations(config["translations"]["url"], translations_path) |
| 100 | + languages = [default_language] |
| 101 | + translated_languages = get_languages(translations_source_path) |
| 102 | + remove_translations(source_path, translated_languages) |
| 103 | + if process_translations: |
| 104 | + languages = [default_language] + translated_languages |
| 105 | + |
| 106 | + sys.stderr.write("\nCopying translations...\n") |
| 107 | + copy_translations(translations_source_path, source_path, translated_languages) |
| 108 | + |
| 109 | + return translated_languages, languages |
0 commit comments