From 28129d97c6b0b5d1b12372992cd9979d37590e85 Mon Sep 17 00:00:00 2001 From: Jennifer Cwagenberg Date: Sat, 6 Apr 2024 08:00:35 -0500 Subject: [PATCH] style(logging): :zap: use lazy concetenation for logging --- .gitignore | 6 +++++- modelscan/cli.py | 11 +++++----- modelscan/issues.py | 2 +- modelscan/modelscan.py | 28 +++++++++++++++---------- notebooks/utils/pickle_codeinjection.py | 3 +-- 5 files changed, 30 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 7e48834..1de7eb8 100644 --- a/.gitignore +++ b/.gitignore @@ -128,4 +128,8 @@ cython_debug/ .DS_Store .vscode/ -.idea/ \ No newline at end of file +.idea/ + +# Notebook Model Downloads +notebooks/PyTorchModels/ +pytorch-model-scan-results.json \ No newline at end of file diff --git a/modelscan/cli.py b/modelscan/cli.py index eafc69b..a10fcb9 100644 --- a/modelscan/cli.py +++ b/modelscan/cli.py @@ -112,7 +112,7 @@ def scan( settings = DEFAULT_SETTINGS if settings_file_path and settings_file_path.is_file(): - with open(settings_file_path) as sf: + with open(settings_file_path, encoding="utf-8") as sf: settings = parse(sf.read()).unwrap() click.echo(f"Detected settings file. Using {settings_file_path}. \n") else: @@ -174,16 +174,17 @@ def create_settings(force: bool, location: Optional[str]) -> None: settings_path = location try: - open(settings_path) + open(settings_path, encoding="utf-8") if force: - with open(settings_path, "w") as settings_file: + with open(settings_path, mode="w", encoding="utf-8") as settings_file: settings_file.write(SettingsUtils.get_default_settings_as_toml()) else: logger.warning( - f"{settings_path} file already exists. Please use `--force` flag if you intend to overwrite it." + "%s file already exists. Please use `--force` flag if you intend to overwrite it.", + settings_path, ) except FileNotFoundError: - with open(settings_path, "w") as settings_file: + with open(settings_path, mode="w", encoding="utf-8") as settings_file: settings_file.write(SettingsUtils.get_default_settings_as_toml()) diff --git a/modelscan/issues.py b/modelscan/issues.py index 2e8da87..130318d 100644 --- a/modelscan/issues.py +++ b/modelscan/issues.py @@ -85,7 +85,7 @@ def print(self) -> None: if self.code == IssueCode.UNSAFE_OPERATOR: issue_description = "Unsafe operator" else: - logger.error(f"No issue description for issue code ${self.code}") + logger.error("No issue description for issue code %s", self.code) print(f"\n{issue_description} found:") print(f" - Severity: {self.severity.name}") diff --git a/modelscan/modelscan.py b/modelscan/modelscan.py index ac2ddb3..a41368d 100644 --- a/modelscan/modelscan.py +++ b/modelscan/modelscan.py @@ -70,7 +70,7 @@ def _load_scanners(self) -> None: self._scanners_to_run.append(scanner_class) except Exception as e: - logger.error(f"Error importing scanner {scanner_path}") + logger.error("Error importing scanner %s", scanner_path) self._init_errors.append( ModelScanError( scanner_path, @@ -81,7 +81,7 @@ def _load_scanners(self) -> None: def _iterate_models(self, model_path: Path) -> Generator[Model, None, None]: if not model_path.exists(): - logger.error(f"Path {model_path} does not exist") + logger.error("Path %s does not exist", model_path) self._errors.append( ModelScanError( "ModelScan", @@ -93,7 +93,7 @@ def _iterate_models(self, model_path: Path) -> Generator[Model, None, None]: files = [model_path] if model_path.is_dir(): - logger.debug(f"Path {str(model_path)} is a directory") + logger.debug("Path %s is a directory", str(model_path)) files = [f for f in model_path.rglob("*") if Path.is_file(f)] for file in files: @@ -127,15 +127,15 @@ def _iterate_models(self, model_path: Path) -> Generator[Model, None, None]: yield Model(file_name, file_io) except zipfile.BadZipFile as e: logger.debug( - f"Skipping zip file {str(model.get_source())}, due to error", - e, + "Skipping zip file %s, due to error", + str(model.get_source()), exc_info=True, ) self._skipped.append( ModelScanSkipped( "ModelScan", SkipCategories.BAD_ZIP, - f"Skipping zip file due to error: {e}", + "Skipping zip file due to error: %s" % e, str(model.get_source()), ) ) @@ -189,13 +189,17 @@ def _scan_source( scan_results = scanner.scan(model) except Exception as e: logger.error( - f"Error encountered from scanner {scanner.full_name()} with path {str(model.get_source())}: {e}" + "Error encountered from scanner %s with path %s: %s", + scanner.full_name(), + str(model.get_source()), + e, ) self._errors.append( ModelScanError( scanner.full_name(), ErrorCategories.MODEL_SCAN, - f"Error encountered from scanner {scanner.full_name()}: {e}", + "Error encountered from scanner %s: %s" + % (scanner.full_name(), e), str(model.get_source()), ) ) @@ -204,7 +208,9 @@ def _scan_source( if scan_results is not None: scanned = True logger.info( - f"Scanning {model.get_source()} using {scanner.full_name()} model scan" + "Scanning %s using %s model scan", + model.get_source(), + scanner.full_name(), ) if scan_results.errors: self._errors.extend(scan_results.errors) @@ -343,12 +349,12 @@ def generate_report(self) -> Optional[str]: scan_report = report_class.generate(scan=self, settings=report_settings) except Exception as e: - logger.error(f"Error generating report using {reporting_module}: {e}") + logger.error("Error generating report using %s: %s", reporting_module, e) self._errors.append( ModelScanError( "ModelScan", ErrorCategories.MODEL_SCAN, - f"Error generating report using {reporting_module}: {e}", + "Error generating report using %s: %s" % (reporting_module, e), ) ) diff --git a/notebooks/utils/pickle_codeinjection.py b/notebooks/utils/pickle_codeinjection.py index c4b7e94..28b2931 100644 --- a/notebooks/utils/pickle_codeinjection.py +++ b/notebooks/utils/pickle_codeinjection.py @@ -2,8 +2,7 @@ import struct import os from typing import overload - -from git import Union +from typing import Union class PickleInject: