-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Refactor code logic to make API more flexible.
Signed-off-by: loonghao <[email protected]>
- Loading branch information
Showing
24 changed files
with
22,158 additions
and
378 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ exclude_lines = | |
ignore_errors = True | ||
omit = | ||
tests/* | ||
maya_umbrella/maya_funs.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,3 +27,6 @@ jobs: | |
- name: isort | ||
run: | | ||
nox -s isort_check | ||
- name: make-zip | ||
run: | | ||
nox -s make-zip -- --version 0.5.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,16 @@ | ||
import os | ||
import glob | ||
import maya.cmds as cmds | ||
from functools import wraps | ||
from maya_umbrella.maya_funs import open_maya_file | ||
|
||
ROOT = os.path.dirname(os.path.abspath(__file__)) | ||
|
||
|
||
def block_prompt(func): | ||
@wraps(func) | ||
def wrap(*args, **kwargs): | ||
# Grabs the initial value. | ||
prompt_val = cmds.file(prompt=True, q=True) | ||
|
||
try: | ||
cmds.file(prompt=False) | ||
return func(*args, **kwargs) | ||
|
||
finally: | ||
# Resets to the original value, this way you don't suddenly turn the prompt on, when someone wanted it off. | ||
cmds.file(prompt=prompt_val) | ||
|
||
return wrap | ||
|
||
|
||
def get_virus_files(): | ||
return glob.glob(os.path.join(ROOT, "tests", "virus", "*.ma")) | ||
|
||
|
||
@block_prompt | ||
def start(): | ||
for maya_file in get_virus_files(): | ||
cmds.file(new=True, force=True) | ||
try: | ||
cmds.file(maya_file, open=True, force=True, ignoreVersion=True, executeScriptNodes=False) | ||
except Exception: | ||
pass | ||
open_maya_file(maya_file) | ||
cmds.file(new=True, force=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
# Import local modules | ||
from maya_umbrella.core import MayaVirusDefender | ||
from maya_umbrella.cleaner import MayaVirusCleaner | ||
from maya_umbrella.collector import MayaVirusCollector | ||
from maya_umbrella.defender import MayaVirusDefender | ||
from maya_umbrella.defender import context_defender | ||
from maya_umbrella.scanner import MayaVirusScanner | ||
|
||
|
||
__all__ = ["MayaVirusDefender"] | ||
__all__ = ["MayaVirusDefender", "MayaVirusCleaner", "MayaVirusCollector", "MayaVirusScanner", "context_defender"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# Import built-in modules | ||
import glob | ||
import logging | ||
import os | ||
import re | ||
|
||
# Import local modules | ||
from maya_umbrella.constants import FILE_VIRUS_SIGNATURES | ||
from maya_umbrella.filesystem import remove_virus_file_by_signature | ||
from maya_umbrella.filesystem import safe_remove_file | ||
from maya_umbrella.filesystem import safe_rmtree | ||
from maya_umbrella.i18n import Translator | ||
from maya_umbrella.maya_funs import check_reference_node_exists | ||
from maya_umbrella.maya_funs import cmds | ||
|
||
|
||
class MayaVirusCleaner(object): | ||
"""A class to clean Maya virus files. | ||
Attributes: | ||
logger (Logger): Logger object for logging purposes. | ||
translator (Translator): Translator object for translation purposes. | ||
collector (MayaVirusCollector): MayaVirusCollector object for collecting issues. | ||
""" | ||
def __init__(self, collector, logger=None): | ||
"""Initialize the MayaVirusCleaner. | ||
Args: | ||
collector (MayaVirusCollector): MayaVirusCollector object for collecting issues. | ||
logger (Logger, optional): Logger object for logging purposes. Defaults to None, which creates a new logger. | ||
""" | ||
self.logger = logger or logging.getLogger(__name__) | ||
self.translator = Translator() | ||
self.collector = collector | ||
|
||
def callback_remove_rename_temp_files(self, *args, **kwargs): | ||
"""Remove temporary files in the local script path.""" | ||
self.logger.info("Removing temporary files in %s", self.collector.local_script_path) | ||
for temp_file in glob.glob(os.path.join(self.collector.local_script_path, "._*")): | ||
safe_remove_file(temp_file) | ||
|
||
def fix_script_jobs(self): | ||
"""Fix infected script jobs.""" | ||
for script_job in self.collector.infected_script_jobs: | ||
script_num = int(re.findall(r"^(\d+):", script_job)[0]) | ||
self.logger.info("Kill script job %s", script_job) | ||
cmds.scriptJob(kill=script_num, force=True) | ||
self.collector.remove_infected_script_job(script_job) | ||
|
||
def fix_malicious_files(self): | ||
"""Fix malicious files.""" | ||
for file_ in self.collector.malicious_files: | ||
if os.path.exists(file_): | ||
if os.path.isfile(file_): | ||
self.logger.info("Removing %s", file_) | ||
safe_remove_file(file_) | ||
self.collector.remove_malicious_file(file_) | ||
else: | ||
self.logger.info("Removing folder %s", file_) | ||
safe_rmtree(file_) | ||
self.collector.remove_malicious_file(file_) | ||
|
||
def fix_infected_nodes(self): | ||
"""Fix infected nodes.""" | ||
for node in self.collector.infected_nodes: | ||
self.logger.info(self.translator.translate("delete", name=node)) | ||
is_referenced = check_reference_node_exists(node) | ||
if is_referenced: | ||
try: | ||
cmds.setAttr("{node}.before".format(node=node), "", type="string") | ||
cmds.setAttr("{node}.after".format(node=node), "", type="string") | ||
cmds.setAttr("{node}.scriptType".format(node=node), 0) | ||
self.collector.remove_infected_node(node) | ||
except Exception as e: | ||
self.logger.debug(e) | ||
else: | ||
try: | ||
cmds.lockNode(node, lock=False) | ||
except ValueError: | ||
pass | ||
try: | ||
cmds.delete(node) | ||
except ValueError: | ||
pass | ||
self.collector.remove_infected_node(node) | ||
|
||
def setup_default_callbacks(self): | ||
"""Set up default callbacks.""" | ||
self.collector.add_maya_initialized_callback(self.callback_remove_rename_temp_files) | ||
self.collector.add_maya_exiting_callback(self.callback_remove_rename_temp_files) | ||
|
||
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)) | ||
remove_virus_file_by_signature(file_path, FILE_VIRUS_SIGNATURES) | ||
self.collector.remove_infected_file(file_path) | ||
|
||
def fix(self): | ||
"""Fix all issues related to the Maya virus.""" | ||
if self.collector.have_issues: | ||
maya_file = cmds.file(query=True, sceneName=True, shortName=True) or "empty/scene" | ||
self.logger.info("Starting Fixing all issues related to the Maya virus from %s.", maya_file) | ||
self.fix_malicious_files() | ||
self.fix_infected_files() | ||
self.fix_infected_nodes() | ||
self.fix_script_jobs() | ||
for func in self.collector.get_additionally_fix_funcs(): | ||
func() | ||
self.logger.info("Finished Fixing all issues related to the Maya virus from %s.", maya_file) |
Oops, something went wrong.