Skip to content

Commit

Permalink
fix: encode PURL qualifiers as a normalized string
Browse files Browse the repository at this point in the history
This PR sets ``encode=True`` to encode qualifiers as a normalized
string because SQLite doesn't support ``dict`` type.

It also adds exception handling for deserializing a PURL string while
initializing a Component instance.

Signed-off-by: behnazh-w <[email protected]>
  • Loading branch information
behnazh-w committed Sep 10, 2023
1 parent 40c2faf commit 2c0a3b4
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/macaron/database/table_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from macaron.database.database_manager import ORMBase
from macaron.database.rfc3339_datetime import RFC3339DateTime
from macaron.errors import CUEExpectationError, CUERuntimeError
from macaron.errors import CUEExpectationError, CUERuntimeError, InvalidPURLError
from macaron.slsa_analyzer.provenance.expectations.cue import cue_validator
from macaron.slsa_analyzer.provenance.expectations.expectation import Expectation
from macaron.slsa_analyzer.slsa_req import ReqName
Expand Down Expand Up @@ -172,9 +172,21 @@ def __init__(self, purl: str, analysis: Analysis, repository: "Repository | None
The corresponding analysis.
repository: Repository | None
The corresponding repository.
Raises
------
InvalidPURLError
If the PURL provided from the user is invalid.
"""
purl_parts = PackageURL.from_string(purl)
purl_kwargs = purl_parts.to_dict()
try:
purl_parts = PackageURL.from_string(purl)
except ValueError as error:
raise InvalidPURLError(f"The package url {purl} is not valid.") from error

# We set ``encode=True`` to encode qualifiers as a normalized string because SQLite doesn't support ``dict`` type.
# TODO: Explore the ``dbm`` or ``shelve`` packages to support dict type, which are part of the Python standard library.
purl_kwargs = purl_parts.to_dict(encode=True)

super().__init__(purl=purl, analysis=analysis, repository=repository, **purl_kwargs)

@property
Expand Down

0 comments on commit 2c0a3b4

Please sign in to comment.