Skip to content

Commit 3873a6e

Browse files
committed
Add initial purl map
Signed-off-by: Jono Yang <jyang@nexb.com>
1 parent 2aa3e98 commit 3873a6e

2 files changed

Lines changed: 29 additions & 30 deletions

File tree

src/purl_validator/__init__.py

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,30 @@
2626
import ducer
2727

2828

29-
PURLS = """
30-
pkg:maven/be.sweetmustard.vinegar/vinegar-pattern-matcher@0.1.1
31-
pkg:maven/be.sweetmustard.vinegar/vinegar-pattern-matcher@0.1.1?classifier=sources
32-
pkg:maven/be.sweetmustard.vinegar/vinegar-pattern-matcher@0.1.0
33-
pkg:maven/be.sweetmustard.vinegar/vinegar-pattern-matcher@0.1.0?classifier=sources
34-
""".split()
29+
PURL_MAP_LOCATION = Path(__file__).parent / "purls.map"
3530

3631

3732
def create_purl_map(purls):
38-
# Ensure elements of `purls` are strings:
39-
purl_strs = []
33+
# Ensure elements of `purls` are PackageURLs:
34+
purl_objs = []
4035
for purl in purls:
4136
if not isinstance(purl, (PackageURL, str)):
4237
raise ValueError(f"invalid purl in `purls`: {purl}")
43-
if isinstance(purl, PackageURL):
44-
purl_str = purl.to_string()
38+
if isinstance(purl, str):
39+
p = PackageURL.from_string(purl)
4540
else:
46-
purl_str = purl
47-
purl_strs.append(purl_str)
48-
49-
# purl strs must be sorted and converted to bytes before going into the Map
50-
prepared_purl_strs = [(bytes(purl_str, "utf-8"), 1) for purl_str in sorted(purl_strs)]
41+
p = purl
42+
purl_objs.append(p)
43+
44+
# purl strs must be unique, sorted, and converted to bytes before going into the Map
45+
purl_strs = set()
46+
for purl_obj in purl_objs:
47+
if purl_obj.namespace:
48+
purl_str = f"{purl_obj.type}/{purl_obj.namespace}/{purl_obj.name}"
49+
else:
50+
purl_str = f"{purl_obj.type}/{purl_obj.name}"
51+
purl_strs.add(purl_str)
52+
prepared_purl_strs = sorted((bytes(purl_str, "utf-8"), 1) for purl_str in purl_strs)
5153

5254
# create map
5355
temp_dir = fileutils.get_temp_dir()
@@ -58,18 +60,18 @@ def create_purl_map(purls):
5860

5961

6062
class PurlValidator:
61-
def __init__(self, purl_map_loc=None):
63+
def __init__(self, purls=None, purl_map_loc=PURL_MAP_LOCATION):
6264
self.created_maps = []
6365

64-
if purl_map_loc:
66+
if purls:
67+
# Create purl map from list of purls
68+
purl_map_loc = self.create_purl_map(purls=purls)
69+
elif purl_map_loc:
6570
if not isinstance(purl_map_loc, (Path, str)):
6671
raise ValueError("`purl_map_loc` must be a Path or path string")
6772
if not isinstance(purl_map_loc, Path):
6873
# Ensure purl_map_loc is a Path
6974
purl_map_loc = Path(purl_map_loc)
70-
else:
71-
# Create purl map from PURLS
72-
purl_map_loc = self.create_purl_map(purls=PURLS)
7375

7476
self.purl_map = self.load_purl_map(location=purl_map_loc)
7577

@@ -97,15 +99,12 @@ def validate_purl(self, purl):
9799
purl = PackageURL.from_string(purl)
98100

99101
# Convert purl to bytes
100-
purl_bytes = bytes(purl.to_string(), "utf-8")
101-
102-
return bool(self.purl_map.get(purl_bytes))
102+
if purl.namespace:
103+
purl_str = f"{purl.type}/{purl.namespace}/{purl.name}"
104+
else:
105+
purl_str = f"{purl.type}/{purl.name}"
103106

107+
purl_bytes = bytes(purl_str, "utf-8")
108+
in_purl_map = bool(self.purl_map.get(purl_bytes))
104109

105-
if __name__ == "__main__":
106-
purl_validator = PurlValidator()
107-
print(
108-
purl_validator.validate_purl(
109-
"pkg:maven/be.sweetmustard.vinegar/vinegar-pattern-matcher@0.1.1"
110-
)
111-
)
110+
return in_purl_map

src/purl_validator/purls.map

19.2 MB
Binary file not shown.

0 commit comments

Comments
 (0)