Skip to content

Commit

Permalink
Eliminate redundant archive check
Browse files Browse the repository at this point in the history
  • Loading branch information
ikretz committed Jul 17, 2024
1 parent edc1113 commit 3fc3d3f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
21 changes: 8 additions & 13 deletions guarddog/scanners/pypi_package_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
8 changes: 3 additions & 5 deletions guarddog/scanners/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 3fc3d3f

Please sign in to comment.