Skip to content

Commit

Permalink
refactor: moved repetitive code from parsers to a generic function
Browse files Browse the repository at this point in the history
Signed-off-by: Meet Soni <meetsoni3017@gmail.com>
  • Loading branch information
inosmeet committed Jul 30, 2024
1 parent 7d7be28 commit 8c3b1ff
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 63 deletions.
9 changes: 9 additions & 0 deletions cve_bin_tool/parsers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ def generate_purl(self, product, vendor="", version="", qualifier={}, subpath=No
)
return purl

def get_vendor(self, purl, product, version):
"""Returns the finalised vendor after utilising various mechanisms."""
vendor, result = self.find_vendor_from_purl(purl, version)

if not result:
vendor = self.find_vendor(product, version)

return self.mismatch(purl, vendor)

def find_vendor_from_purl(self, purl, ver) -> tuple[list[ScanInfo], bool]:
"""
Finds the vendor information for a given PackageURL (purl) and version from the database.
Expand Down
6 changes: 1 addition & 5 deletions cve_bin_tool/parsers/dart.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ def run_checker(self, filename):
product = package_name
version = package_detail.get("version").replace('"', "")
purl = self.generate_purl(product)
vendor, result = self.find_vendor_from_purl(purl, version)
if not result:
vendor = self.find_vendor(product, version)

vendor = self.mismatch(purl, vendor)
vendor = self.get_vendor(purl, product, version)
if vendor:
yield from vendor
self.logger.debug(f"Done scanning file: {self.filename}")
6 changes: 1 addition & 5 deletions cve_bin_tool/parsers/go.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,7 @@ def run_checker(self, filename):
product = line.split(" ")[0].split("/")[-1]
version = line.split(" ")[1][1:].split("-")[0].split("+")[0]
purl = self.generate_purl(product)
vendors, result = self.find_vendor_from_purl(purl, version)

if not result:
vendors = self.find_vendor(product, version)
vendors = self.mismatch(purl, vendors)
vendors = self.get_vendor(purl, product, version)
if vendors is not None:
yield from vendors
self.logger.debug(f"Done scanning file: {self.filename}")
12 changes: 2 additions & 10 deletions cve_bin_tool/parsers/javascript.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ def run_checker(self, filename):
product = data["name"]
version = data["version"]
purl = self.generate_purl(product)
vendor, result = self.find_vendor_from_purl(purl, version)

if not result:
vendor = self.find_vendor(product, version)
vendor = self.mismatch(purl, vendor)
vendor = self.get_vendor(purl, product, version)
else:
vendor = None
if vendor is not None:
Expand Down Expand Up @@ -102,11 +98,7 @@ def run_checker(self, filename):

for product, version in product_version_mapping:
purl = self.generate_purl(product, "")
vendor, result = self.find_vendor_from_purl(purl, version)

if not result:
vendor = self.find_vendor(product, version)
vendor = self.mismatch(purl, vendor)
vendor = self.get_vendor(purl, product, version)
if vendor is not None:
yield from vendor
self.logger.debug(f"Done scanning file: {self.filename}")
6 changes: 1 addition & 5 deletions cve_bin_tool/parsers/perl.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,7 @@ def run_checker(self, filename):
product = dependency[0]
version = dependency[1]
purl = self.generate_purl(product)
vendor, result = self.find_vendor_from_purl(purl, version)

if not result:
vendor = self.find_vendor(product, version)
vendor = self.mismatch(purl, vendor)
vendor = self.get_vendor(purl, product, version)
if vendor is not None:
yield from vendor
self.logger.debug(f"Done scanning file: {self.filename}")
6 changes: 1 addition & 5 deletions cve_bin_tool/parsers/php.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,7 @@ def run_checker(self, filename):
if "dev" in version:
continue
purl = self.generate_purl(product)
vendor, result = self.find_vendor_from_purl(purl, version)

if not result:
vendor = self.find_vendor(product, version)
vendor = self.mismatch(purl, vendor)
vendor = self.get_vendor(purl, product, version)
if vendor is not None:
yield from vendor
self.logger.debug(f"Done scanning file: {self.filename}")
13 changes: 2 additions & 11 deletions cve_bin_tool/parsers/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,8 @@ def run_checker(self, filename):
product = line["metadata"]["name"]
version = line["metadata"]["version"]
purl = self.generate_purl(product)
vendor, result = self.find_vendor_from_purl(purl, version)
vendor = self.get_vendor(purl, product, version)

if not result:
vendor = self.find_vendor(product, version)

vendor = self.mismatch(purl, vendor)
if vendor is not None:
yield from vendor
self.logger.debug(f"Done scanning file: {self.filename}")
Expand Down Expand Up @@ -157,12 +153,7 @@ def run_checker(self, filename):
product = search(compile(r"^Name: (.+)$", MULTILINE), lines).group(1)
version = search(compile(r"^Version: (.+)$", MULTILINE), lines).group(1)
purl = self.generate_purl(product)
vendor, result = self.find_vendor_from_purl(purl, version)

if not result:
vendor = self.find_vendor(product, version)

vendor = self.mismatch(purl, vendor)
vendor = self.get_vendor(purl, product, version)
if vendor is not None:
yield from vendor

Expand Down
7 changes: 1 addition & 6 deletions cve_bin_tool/parsers/r.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,7 @@ def run_checker(self, filename):
product = content["Packages"][package]["Package"]
version = content["Packages"][package]["Version"]
purl = self.generate_purl(product)
vendor, result = self.find_vendor_from_purl(purl, version)

if not result:
vendor = self.find_vendor(product, version)

vendor = self.mismatch(purl, vendor)
vendor = self.get_vendor(purl, product, version)
if vendor is not None:
yield from vendor
self.logger.debug(f"Done scanning file: {self.filename}")
6 changes: 1 addition & 5 deletions cve_bin_tool/parsers/ruby.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,7 @@ def run_checker(self, filename):
product = line.strip().split()[0]
version = line.strip().split("(")[1][:-1]
purl = self.generate_purl(product)
vendors, result = self.find_vendor_from_purl(purl, version)

if not result:
vendors = self.find_vendor(product, version)
vendors = self.mismatch(purl, vendors)
vendors = self.get_vendor(purl, product, version)
if vendors is not None:
yield from vendors
self.logger.debug(f"Done scanning file: {self.filename}")
7 changes: 1 addition & 6 deletions cve_bin_tool/parsers/rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,7 @@ def run_checker(self, filename):
continue

purl = self.generate_purl(product)
vendors, result = self.find_vendor_from_purl(purl, version)

if not result:
vendors = self.find_vendor(product, version)

vendors = self.mismatch(purl, vendors)
vendors = self.get_vendor(purl, product, version)
if vendors is not None:
yield from vendors
product = ""
Expand Down
6 changes: 1 addition & 5 deletions cve_bin_tool/parsers/swift.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,7 @@ def run_checker(self, filename):
self.logger.debug(domain)

purl = self.generate_purl(product)
vendors, result = self.find_vendor_from_purl(purl, version)

if not result:
vendors = self.find_vendor(product, version)
vendors = self.mismatch(purl, vendors)
vendors = self.get_vendor(purl, product, version)
if vendors is not None:
yield from vendors
self.logger.debug(f"Done scanning file: {self.filename}")

0 comments on commit 8c3b1ff

Please sign in to comment.