From 3b599002708814208af41b6e9b70071b3b44bac0 Mon Sep 17 00:00:00 2001 From: Mehrin Kiani Date: Mon, 12 Feb 2024 14:14:46 -0500 Subject: [PATCH] Update file paths relative to input path (#104) --- modelscan/cli.py | 2 +- modelscan/modelscan.py | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/modelscan/cli.py b/modelscan/cli.py index db93869..1005b8d 100644 --- a/modelscan/cli.py +++ b/modelscan/cli.py @@ -129,7 +129,7 @@ def scan( if not pathlibPath.exists(): raise FileNotFoundError(f"Path {path} does not exist") else: - modelscan.scan(pathlibPath) + modelscan.scan(path) else: raise click.UsageError("Command line must include a path") diff --git a/modelscan/modelscan.py b/modelscan/modelscan.py index 2fec531..fe43206 100644 --- a/modelscan/modelscan.py +++ b/modelscan/modelscan.py @@ -68,8 +68,8 @@ def scan( self._skipped = [] self._scanned = [] self._input_path = str(path) - - self._scan_path(Path(path)) + pathlibPath = Path().cwd() if path == "." else Path(path).absolute() + self._scan_path(Path(pathlibPath)) return self._generate_results() def _scan_path( @@ -138,6 +138,10 @@ def _scan_zip( def _generate_results(self) -> Dict[str, Any]: report: Dict[str, Any] = {} + absolute_path = Path(self._input_path).resolve() + if Path(self._input_path).is_file(): + absolute_path = Path(absolute_path).parent + issues_by_severity = self._issues.group_by_severity() total_issue_count = len(self._issues.all_issues) @@ -151,22 +155,30 @@ def _generate_results(self) -> Dict[str, Any]: report["summary"]["total_issues_by_severity"][severity.name] = 0 report["summary"]["total_issues"] = total_issue_count - report["summary"]["input_path"] = self._input_path + report["summary"]["input_path"] = str(self._input_path) + report["summary"]["absolute_path"] = str(absolute_path) report["summary"]["modelscan_version"] = __version__ report["summary"]["timestamp"] = datetime.now().isoformat() report["summary"]["skipped"] = {"total_skipped": len(self._skipped)} report["summary"]["skipped"]["skipped_files"] = [ - str(file_name) for file_name in self._skipped + str(Path(file_name).relative_to(Path(absolute_path))) + for file_name in self._skipped ] report["summary"]["scanned"] = {"total_scanned": len(self._scanned)} report["summary"]["scanned"]["scanned_files"] = [ - str(file_name) for file_name in self._scanned + str(Path(file_name).relative_to(Path(absolute_path))) + for file_name in self._scanned ] report["issues"] = [ issue.details.output_json() for issue in self._issues.all_issues ] + for issue in report["issues"]: + issue["source"] = str( + Path(issue["source"]).relative_to(Path(absolute_path)) + ) + report["errors"] = [str(error) for index, error in enumerate(self._errors)] return report