Skip to content

Commit

Permalink
fix: remove regex
Browse files Browse the repository at this point in the history
  • Loading branch information
mastersans committed Jun 21, 2024
1 parent d88d20d commit 367d2e0
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions cve_bin_tool/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def validate_location(location: str) -> bool:

def decode_purl(purl: str) -> ProductInfo | None:
"""
Validate and decode a Package URL (purl) in the format: pkg:type/namespace/product@version.
Decode a Package URL (purl) in the format: pkg:type/namespace/product@version.
Args:
- purl (str): Package URL (purl) string.
Expand All @@ -346,30 +346,26 @@ def decode_purl(purl: str) -> ProductInfo | None:
- ProductInfo | None: An instance of ProductInfo containing the vendor, product,
version, location, and purl, or None if the purl is invalid.
"""
# Define the regex pattern for the expected format
pattern = re.compile(r"^pkg:[^/]+/([^/]+)/([^@]+)@(.+)$")
location = "location/to/product"

try:
purl_obj = PackageURL.from_string(purl)
match = pattern.match(purl)
if match:
vendor = purl_obj.namespace
product = purl_obj.name
version = purl_obj.version
if vendor and product and version:
product_info = ProductInfo(
vendor=vendor,
product=product,
version=version,
location=location,
purl=purl,
)
return product_info
else:
raise ValueError("Invalid purl components")
vendor = purl_obj.namespace
product = purl_obj.name
version = purl_obj.version
if vendor and product and version:
product_info = ProductInfo(
vendor=vendor,
product=product,
version=version,
location=location,
purl=purl,
)
return product_info
else:
raise ValueError("Invalid purl format")
raise ValueError(
f"Invalid purl: expected format pkg:type/namespace/product@version, got {purl}"
)

except Exception as e:
LOGGER.error(f"Error decoding purl: {e}")
Expand Down

0 comments on commit 367d2e0

Please sign in to comment.