Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: ensure have permission to delete the file #47

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions maya_umbrella/cleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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_))
Expand Down Expand Up @@ -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)

Expand Down
35 changes: 18 additions & 17 deletions maya_umbrella/locales/en_US.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
{
"start_fix_issues": "Start fixing all problems related to Maya virus $name",
"finish_fix_issues": "Done.",
"init_message": "Successfully loaded <hl>maya_umbrella</hl> 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"
"start_fix_issues": "Start fixing all problems related to Maya virus $name",
"finish_fix_issues": "Repair completed.",
"init_message": "Successfully loaded <hl>maya_umbrella</hl> 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"
}
7 changes: 4 additions & 3 deletions maya_umbrella/locales/zh_CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
"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",
"fix_infected_reference_nodes": "尝试修复被感染的参考节点:$name",
"delete": "删除感染文件:$name",
"remove_file": "删除文件:$name",
"remove_path": "删除文件夹:$name",
"fix_script_job": "删除被感染的节点:$name"
"fix_script_job": "删除被感染的节点:$name",
"file_not_writable": "文件不可写:$name"
}
5 changes: 5 additions & 0 deletions nox_actions/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
29 changes: 29 additions & 0 deletions scripts/translate_i18n.py
Original file line number Diff line number Diff line change
@@ -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])