From be3b0adc711d2985ff8ad70d9d61b1b55884bce3 Mon Sep 17 00:00:00 2001 From: loonghao Date: Sun, 19 May 2024 15:25:53 +0800 Subject: [PATCH 1/2] refactor: ensure have permission to delete the file Signed-off-by: loonghao --- maya_umbrella/cleaner.py | 7 +++++++ maya_umbrella/locales/en_US.json | 3 ++- maya_umbrella/locales/zh_CN.json | 3 ++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/maya_umbrella/cleaner.py b/maya_umbrella/cleaner.py index 5f340ea..f2c1ce2 100644 --- a/maya_umbrella/cleaner.py +++ b/maya_umbrella/cleaner.py @@ -22,6 +22,7 @@ class MayaVirusCleaner(object): translator (Translator): Translator object for translation purposes. collector (MayaVirusCollector): MayaVirusCollector object for collecting issues. """ + def __init__(self, collector, logger=None): """Initialize the MayaVirusCleaner. @@ -50,6 +51,9 @@ def fix_script_jobs(self): def fix_malicious_files(self): """Fix malicious files.""" for file_ in self.collector.malicious_files: + if not os.access(file_, os.W_OK): + self.logger.debug(self.translator.translate("file_not_writable", name=file_)) + continue if os.path.exists(file_): if os.path.isfile(file_): self.logger.debug(self.translator.translate("remove_file", name=file_)) @@ -94,6 +98,9 @@ def fix_infected_files(self): """Fix infected files.""" for file_path in self.collector.infected_files: self.logger.info(self.translator.translate("fix_infected_files", name=file_path)) + if not os.access(file_path, os.W_OK): + self.logger.debug(self.translator.translate("file_not_writable", name=file_path)) + continue remove_virus_file_by_signature(file_path, FILE_VIRUS_SIGNATURES) self.collector.remove_infected_file(file_path) diff --git a/maya_umbrella/locales/en_US.json b/maya_umbrella/locales/en_US.json index 078ecdd..b885c15 100644 --- a/maya_umbrella/locales/en_US.json +++ b/maya_umbrella/locales/en_US.json @@ -15,5 +15,6 @@ "delete": "Deleting: $name", "remove_file": "Deleting file:$name", "remove_path": "Deleting path:$name", - "fix_script_job": "Kill script job: %s" + "fix_script_job": "Kill script job: %s", + "file_not_writable": "File not writable: $name" } diff --git a/maya_umbrella/locales/zh_CN.json b/maya_umbrella/locales/zh_CN.json index 4c7be15..3107a17 100644 --- a/maya_umbrella/locales/zh_CN.json +++ b/maya_umbrella/locales/zh_CN.json @@ -15,5 +15,6 @@ "delete": "删除感染文件:$name", "remove_file": "删除文件:$name", "remove_path": "删除文件夹:$name", - "fix_script_job": "删除被感染的节点:$name" + "fix_script_job": "删除被感染的节点:$name", + "file_not_writable": "文件不可写:$name" } From faebe89bc8b563655c81a0d3aea79363f307d5e8 Mon Sep 17 00:00:00 2001 From: loonghao Date: Mon, 20 May 2024 23:48:51 +0800 Subject: [PATCH 2/2] feat: Add new setup to auto translate locales. Signed-off-by: loonghao --- maya_umbrella/locales/en_US.json | 36 ++++++++++++++++---------------- maya_umbrella/locales/zh_CN.json | 4 ++-- nox_actions/release.py | 5 +++++ noxfile.py | 1 + scripts/translate_i18n.py | 29 +++++++++++++++++++++++++ 5 files changed, 55 insertions(+), 20 deletions(-) create mode 100644 scripts/translate_i18n.py diff --git a/maya_umbrella/locales/en_US.json b/maya_umbrella/locales/en_US.json index b885c15..ec6d433 100644 --- a/maya_umbrella/locales/en_US.json +++ b/maya_umbrella/locales/en_US.json @@ -1,20 +1,20 @@ { - "start_fix_issues": "Start fixing all problems related to Maya virus $name", - "finish_fix_issues": "Done.", - "init_message": "Successfully loaded maya_umbrella under protection.", - "init_standalone_message": "-----------------------Loading maya_umbrella successfully----------------------", - "report_issue": "$name: Infected by Malware!", - "infected_nodes": "Infected nodes: $name: ", - "bad_files": "Bad files: $name", - "infected_script_jobs": "Infected script jobs: $name", - "infected_files": "Infected files: $name", - "infected_reference_files": "Infected reference files: $name", - "fix_infected_files": "Clean infected files: $name", - "fix_infected_nodes": "Delete infected nodes:$name", - "fix_infected_reference_nodes": "trying fix infected nodes from reference:$name", - "delete": "Deleting: $name", - "remove_file": "Deleting file:$name", - "remove_path": "Deleting path:$name", - "fix_script_job": "Kill script job: %s", - "file_not_writable": "File not writable: $name" + "start_fix_issues": "Start fixing all problems related to Maya virus $name", + "finish_fix_issues": "Repair completed.", + "init_message": "Successfully loaded maya_umbrella protection.", + "init_standalone_message": "----------------------- successfully loaded maya_umbrella-----------------------", + "report_issue": "$name: Maliciously infected!", + "infected_nodes": "Infected node: $name:", + "bad_files": "Files to be cleaned up: $name", + "infected_script_jobs": "Infected Script jobs: $name", + "infected_files": "Infected file: $name", + "infected_reference_files": "Infected reference file: $name", + "fix_infected_files": "Clean up infected files: $name", + "fix_infected_nodes": "Delete infected node: $name", + "fix_infected_reference_nodes": "Attempt to repair the infected reference node: $name", + "delete": "Delete infected files: $name", + "remove_file": "Delete file: $name", + "remove_path": "Delete folder: $name", + "fix_script_job": "Delete infected node: $name", + "file_not_writable": "The file is not writable: $name" } diff --git a/maya_umbrella/locales/zh_CN.json b/maya_umbrella/locales/zh_CN.json index 3107a17..99d2ca2 100644 --- a/maya_umbrella/locales/zh_CN.json +++ b/maya_umbrella/locales/zh_CN.json @@ -6,8 +6,8 @@ "report_issue": "$name:被恶意感染!", "infected_nodes": "被感染节点:$name: ", "bad_files": "需要被清理的文件:$name", - "infected_script_jobs": "被感染的script jobs:$name", - "infected_files": "被感染的脚本:$name", + "infected_script_jobs": "被感染的Script jobs:$name", + "infected_files": "被感染的文件:$name", "infected_reference_files": "被感染的参考文件:$name", "fix_infected_files": "清理被感染的文件:$name", "fix_infected_nodes": "删除被感染的节点:$name", diff --git a/nox_actions/release.py b/nox_actions/release.py index 1d70046..4681418 100644 --- a/nox_actions/release.py +++ b/nox_actions/release.py @@ -102,3 +102,8 @@ def pinned_requirements(path: Path) -> Iterator[Tuple[str, str]]: # synchronize the contents session.run("vendoring", "sync", ".") + + +def translate(session: nox.Session) -> None: + session.install("deepl", "maya-umbrella") + session.run("python", os.path.join(THIS_ROOT, "scripts", "translate_i18n.py"), THIS_ROOT) diff --git a/noxfile.py b/noxfile.py index bfb1eb2..0d25a34 100644 --- a/noxfile.py +++ b/noxfile.py @@ -26,3 +26,4 @@ nox.session(release.make_install_zip, name="make-zip") nox.session(codetest.pytest, name="pytest") nox.session(release.vendoring, name="vendoring") +nox.session(release.translate, name="t") diff --git a/scripts/translate_i18n.py b/scripts/translate_i18n.py new file mode 100644 index 0000000..61d558d --- /dev/null +++ b/scripts/translate_i18n.py @@ -0,0 +1,29 @@ +# Import built-in modules +import json +import os +import sys + +# Import third-party modules +import deepl + +# Import local modules +from maya_umbrella.filesystem import read_json + + +def translate_locales(this_root): + auth_key = os.getenv("DEEPL_API_KEY") # Replace with your key + translator = deepl.Translator(auth_key) + locales_root = os.path.join(this_root, "maya_umbrella", "locales") + zh_cn = os.path.join(locales_root, "zh_CN.json") + dest_data = {} + src_data = read_json(zh_cn) + for key, value in src_data.items(): + result = translator.translate_text(value, target_lang="EN-US") + print(result.text) + dest_data[key] = result.text + with open(os.path.join(locales_root, "en_US.json"), "w") as f: + json.dump(dest_data, f, indent=4) + + +if __name__ == "__main__": + translate_locales(sys.argv[-1])