From 3fc3d3fcffadb8c37fbdff80174ac010c2129bbd Mon Sep 17 00:00:00 2001 From: Ian Kretz <44385082+ikretz@users.noreply.github.com> Date: Wed, 17 Jul 2024 12:10:29 +0200 Subject: [PATCH] Eliminate redundant archive check --- guarddog/scanners/pypi_package_scanner.py | 21 ++++++++------------- guarddog/scanners/scanner.py | 8 +++----- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/guarddog/scanners/pypi_package_scanner.py b/guarddog/scanners/pypi_package_scanner.py index 09a8f0cb..e6d7015b 100644 --- a/guarddog/scanners/pypi_package_scanner.py +++ b/guarddog/scanners/pypi_package_scanner.py @@ -4,7 +4,7 @@ from guarddog.analyzer.analyzer import Analyzer from guarddog.ecosystems import ECOSYSTEM from guarddog.scanners.scanner import PackageScanner -from guarddog.utils.archives import is_tar_archive, is_zip_archive +from guarddog.utils.archives import is_supported_archive from guarddog.utils.package_info import get_package_info @@ -43,25 +43,20 @@ def download_package(self, package_name, directory, version=None) -> str: raise Exception(f"Version {version} for package {package_name} doesn't exist.") files = releases[version] - url = None - file_extension = None + url, file_extension = None, None for file in files: - # Store url to compressed package and appropriate file extension - if is_tar_archive(file["filename"]): + if is_supported_archive(file["filename"]): url = file["url"] - file_extension = ".tar.gz" + _, file_extension = os.path.splitext(file["filename"]) + break - if is_zip_archive(file["filename"]): - url = file["url"] - file_extension = ".zip" - - if not (url or file_extension): + if not (url and file_extension): raise Exception(f"Compressed file for {package_name} does not exist on PyPI.") # Path to compressed package zippath = os.path.join(directory, package_name + file_extension) - unzippedpath = zippath.removesuffix(file_extension) - + unzippedpath = os.path.join(directory, package_name) self.download_compressed(url, zippath, unzippedpath) + return unzippedpath diff --git a/guarddog/scanners/scanner.py b/guarddog/scanners/scanner.py index 389ff288..f23cdafe 100644 --- a/guarddog/scanners/scanner.py +++ b/guarddog/scanners/scanner.py @@ -11,7 +11,7 @@ import requests from guarddog.analyzer.analyzer import Analyzer -from guarddog.utils.archives import is_supported_archive, safe_extract +from guarddog.utils.archives import safe_extract from guarddog.utils.config import PARALLELISM log = logging.getLogger("guarddog") @@ -248,14 +248,12 @@ def scan_local( results = None if os.path.isdir(path): results = self.analyzer.analyze_sourcecode(path, rules=rules) - elif (os.path.isfile(path) and is_supported_archive(path)): + elif os.path.isfile(path): with tempfile.TemporaryDirectory() as tempdir: safe_extract(path, tempdir) results = self.analyzer.analyze_sourcecode(tempdir, rules=rules) else: - raise Exception( - f"Path {path} is not a directory nor an archive supported by GuardDog." - ) + raise Exception(f"Local scan target {path} is neither a directory nor a file.") callback(results)