From 5ba6232cfcdc37fc3335f703a59d50da826340e0 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Thu, 28 Mar 2024 05:48:08 +0100 Subject: [PATCH] refactor(translate_apps): create a base.py file to avoid duplicating code --- .../apps_translations_to_apps.py | 79 +---------------- tools/translate_apps/base.py | 85 +++++++++++++++++++ .../push_or_update_apps_on_repository.py | 79 +---------------- 3 files changed, 88 insertions(+), 155 deletions(-) create mode 100644 tools/translate_apps/base.py diff --git a/tools/translate_apps/apps_translations_to_apps.py b/tools/translate_apps/apps_translations_to_apps.py index 0f83f4e4..b4ec6948 100644 --- a/tools/translate_apps/apps_translations_to_apps.py +++ b/tools/translate_apps/apps_translations_to_apps.py @@ -1,88 +1,11 @@ -import os import time import json -import tempfile -import subprocess -from typing import Union from pathlib import Path import tomlkit -github_webhook_secret = open("github_webhook_secret", "r").read().strip() - -login = open("login").read().strip() -token = open("token").read().strip() - -weblate_token = open("weblate_token").read().strip() - -my_env = os.environ.copy() -my_env["GIT_TERMINAL_PROMPT"] = "0" -my_env["GIT_AUTHOR_NAME"] = "yunohost-bot" -my_env["GIT_AUTHOR_EMAIL"] = "yunohost@yunohost.org" -my_env["GIT_COMMITTER_NAME"] = "yunohost-bot" -my_env["GIT_COMMITTER_EMAIL"] = "yunohost@yunohost.org" -my_env["GITHUB_USER"] = login -my_env["GITHUB_TOKEN"] = token - - -class Repository: - def __init__(self, url, branch): - self.url = url - self.branch = branch - - def __enter__(self): - self.temporary_directory = tempfile.TemporaryDirectory() - self.path = Path(self.temporary_directory.name) - self.run_command( - [ - "git", - "clone", - self.url, - "--single-branch", - "--branch", - self.branch, - self.path, - ] - ) - - return self - - def run_command( - self, command: Union[str, list], capture_output=False - ) -> Union[str, int]: - if isinstance(command, str): - kwargs = {"args": f"cd {self.path} && {command}", "shell": True, "env": my_env} - - elif isinstance(command, list): - kwargs = {"args": command, "cwd": self.path, "env": my_env} - - if capture_output: - return subprocess.check_output(**kwargs).decode() - else: - print(f"\033[1;31m>>\033[0m \033[0;34m{command}\033[0m") - return subprocess.check_call(**kwargs) - - def file_exists(self, file_name: str) -> bool: - return (self.path / file_name).exists() - - def read_file(self, file_name: str) -> str: - return open((self.path / file_name).resolve(), "r").read() - - def write_file(self, file_name: str, content: str) -> None: - open((self.path / file_name).resolve(), "w").write(content) - - def remove_file(self, file_name: str) -> None: - os.remove(self.path / file_name) - - def append_to_file(self, file_name: str, content: str) -> None: - open((self.path / file_name).resolve(), "a").write(content) - - def __repr__(self): - return f'<__main__.Repository "{self.url.split("@")[1]}" path="{self.path}">' - - def __exit__(self, *args, **kwargs): - pass +from base import Repository, login, token def extract_strings_to_translate_from_apps(apps, translations_repository): diff --git a/tools/translate_apps/base.py b/tools/translate_apps/base.py new file mode 100644 index 00000000..b0f5b957 --- /dev/null +++ b/tools/translate_apps/base.py @@ -0,0 +1,85 @@ +import os +import tempfile +import subprocess + +from typing import Union +from pathlib import Path + +github_webhook_secret = open("github_webhook_secret", "r").read().strip() + +login = open("login").read().strip() +token = open("token").read().strip() + +weblate_token = open("weblate_token").read().strip() + +my_env = os.environ.copy() +my_env["GIT_TERMINAL_PROMPT"] = "0" +my_env["GIT_AUTHOR_NAME"] = "yunohost-bot" +my_env["GIT_AUTHOR_EMAIL"] = "yunohost@yunohost.org" +my_env["GIT_COMMITTER_NAME"] = "yunohost-bot" +my_env["GIT_COMMITTER_EMAIL"] = "yunohost@yunohost.org" +my_env["GITHUB_USER"] = login +my_env["GITHUB_TOKEN"] = token + + +class Repository: + def __init__(self, url, branch): + self.url = url + self.branch = branch + + def __enter__(self): + self.temporary_directory = tempfile.TemporaryDirectory() + self.path = Path(self.temporary_directory.name) + self.run_command( + [ + "git", + "clone", + self.url, + "--single-branch", + "--branch", + self.branch, + self.path, + ] + ) + + return self + + def run_command( + self, command: Union[str, list], capture_output=False + ) -> Union[str, int, subprocess.CompletedProcess]: + if isinstance(command, str): + kwargs = { + "args": f"cd {self.path} && {command}", + "shell": True, + "env": my_env, + } + + elif isinstance(command, list): + kwargs = {"args": command, "cwd": self.path, "env": my_env} + + if capture_output: + return subprocess.check_output(**kwargs).decode() + else: + print(f"\033[1;31m>>\033[0m \033[0;34m{command}\033[0m") + return subprocess.check_call(**kwargs) + + def file_exists(self, file_name: str) -> bool: + return (self.path / file_name).exists() + + def read_file(self, file_name: str) -> str: + return open((self.path / file_name).resolve(), "r").read() + + def write_file(self, file_name: str, content: str) -> None: + open((self.path / file_name).resolve(), "w").write(content) + + def remove_file(self, file_name: str) -> None: + os.remove(self.path / file_name) + + def append_to_file(self, file_name: str, content: str) -> None: + open((self.path / file_name).resolve(), "a").write(content) + + def __repr__(self): + return f'<__main__.Repository "{self.url.split("@")[1]}" path="{self.path}">' + + def __exit__(self, *args, **kwargs): + pass diff --git a/tools/translate_apps/push_or_update_apps_on_repository.py b/tools/translate_apps/push_or_update_apps_on_repository.py index 83f0d4ad..e8119ae3 100644 --- a/tools/translate_apps/push_or_update_apps_on_repository.py +++ b/tools/translate_apps/push_or_update_apps_on_repository.py @@ -1,29 +1,13 @@ -import os import time import json -import tempfile -import subprocess -from collections import defaultdict from pathlib import Path -from typing import Union +from collections import defaultdict import wlc import tomlkit -github_webhook_secret = open("github_webhook_secret", "r").read().strip() - -login = open("login").read().strip() -token = open("token").read().strip() - -weblate_token = open("weblate_token").read().strip() - -my_env = os.environ.copy() -my_env["GIT_TERMINAL_PROMPT"] = "0" -my_env["GIT_AUTHOR_NAME"] = "yunohost-bot" -my_env["GIT_AUTHOR_EMAIL"] = "yunohost@yunohost.org" -my_env["GIT_COMMITTER_NAME"] = "yunohost-bot" -my_env["GIT_COMMITTER_EMAIL"] = "yunohost@yunohost.org" +from base import Repository, login, token, weblate_token def get_weblate_component(weblate, component_path): @@ -35,65 +19,6 @@ def get_weblate_component(weblate, component_path): return True -class Repository: - def __init__(self, url, branch): - self.url = url - self.branch = branch - - def __enter__(self): - self.temporary_directory = tempfile.TemporaryDirectory() - self.path = Path(self.temporary_directory.name) - self.run_command( - [ - "git", - "clone", - self.url, - "--single-branch", - "--branch", - self.branch, - self.path, - ] - ) - - return self - - def run_command( - self, command: Union[str, list], capture_output=False - ) -> Union[str, int]: - if isinstance(command, str): - kwargs = {"args": f"cd {self.path} && {command}", "shell": True} - - elif isinstance(command, list): - kwargs = {"args": command, "cwd": self.path} - - if capture_output: - return subprocess.check_output(**kwargs).decode() - else: - print(f"\033[1;31m>>\033[0m \033[0;34m{command}\033[0m") - return subprocess.check_call(**kwargs) - - def file_exists(self, file_name: str) -> bool: - return (self.path / file_name).exists() - - def read_file(self, file_name: str) -> str: - return open((self.path / file_name).resolve(), "r").read() - - def write_file(self, file_name: str, content: str) -> None: - open((self.path / file_name).resolve(), "w").write(content) - - def remove_file(self, file_name: str) -> None: - os.remove(self.path / file_name) - - def append_to_file(self, file_name: str, content: str) -> None: - open((self.path / file_name).resolve(), "a").write(content) - - def __repr__(self): - return f'<__main__.Repository "{self.url.split("@")[1]}" path="{self.path}">' - - def __exit__(self, *args, **kwargs): - pass - - def extract_strings_to_translate_from_apps(apps, translations_repository): weblate = wlc.Weblate(key=weblate_token, url="https://translate.yunohost.org/api/")