Skip to content

Commit 73aebcf

Browse files
committed
Add PURL_TYPES and enforce validation of PackageURL types
Closes: #181
1 parent abe3806 commit 73aebcf

2 files changed

Lines changed: 109 additions & 2 deletions

File tree

CHANGELOG.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Changelog
22
=========
33

4+
0.16.1 (unreleased)
5+
-------------------
6+
7+
- Add ``PURL_TYPES`` constant to the ``packageurl`` module.
8+
Enforce validation of PackageURL types.
9+
https://github.com/package-url/packageurl-python/issues/181
10+
411
0.16.0 (2024-10-22)
512
-------------------
613

src/packageurl/__init__.py

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,99 @@
5252
"""
5353

5454

55+
PURL_TYPES: set[str] = {
56+
"alpine",
57+
"alpm",
58+
"android",
59+
"apache",
60+
"apk",
61+
"bitbucket",
62+
"bitnami",
63+
"bower",
64+
"buildroot",
65+
"cargo",
66+
"carthage",
67+
"chef",
68+
"chocolatey",
69+
"clojars",
70+
"cocoapods",
71+
"composer",
72+
"conan",
73+
"conda",
74+
"coreos",
75+
"cpan",
76+
"cran",
77+
"crystal",
78+
"ctan",
79+
"deb",
80+
"docker",
81+
"drupal",
82+
"dtype",
83+
"dub",
84+
"ebuild",
85+
"eclipse",
86+
"elm",
87+
"gem",
88+
"generic",
89+
"gitea",
90+
"github",
91+
"gitlab",
92+
"golang",
93+
"gradle",
94+
"guix",
95+
"hackage",
96+
"haxe",
97+
"helm",
98+
"hex",
99+
"huggingface",
100+
"julia",
101+
"luarocks",
102+
"maven",
103+
"melpa",
104+
"meteor",
105+
"mlflow",
106+
"nim",
107+
"nix",
108+
"npm",
109+
"nuget",
110+
"oci",
111+
"opam",
112+
"openwrt",
113+
"osgi",
114+
"p2",
115+
"pear",
116+
"pecl",
117+
"perl6",
118+
"platformio",
119+
"pub",
120+
"puppet",
121+
"pypi",
122+
"qpkg",
123+
"rpm",
124+
"rubygems",
125+
"sourceforge",
126+
"sublime",
127+
"swid",
128+
"terraform",
129+
"vagrant",
130+
"vim",
131+
"wordpress",
132+
"yocto",
133+
}
134+
"""List of recognized pURL types.
135+
136+
.. warning::
137+
138+
There is no official list of ``pkg:<type>/...`` prefixes defined in the pURL
139+
specification.
140+
141+
The only source we found lying around in the pURL literature is this `list of
142+
diverse aliases, examples and libraries
143+
<https://github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst>`_. This
144+
list is based on this document.
145+
"""
146+
147+
55148
def quote(s: AnyStr) -> str:
56149
"""
57150
Return a percent-encoded unicode string, except for colon :, given an `s`
@@ -102,8 +195,15 @@ def normalize_type(type: AnyStr | None, encode: bool | None = True) -> str | Non
102195

103196
type_str = type if isinstance(type, str) else type.decode("utf-8")
104197
quoter = get_quoter(encode)
105-
type_str = quoter(type_str)
106-
return type_str.strip().lower() or None
198+
type_str = quoter(type_str).strip().lower()
199+
if not type_str:
200+
return None
201+
if type_str not in PURL_TYPES:
202+
raise ValueError(
203+
f"Invalid purl type: {type_str!r}. "
204+
f"Must be one of: {', '.join(sorted(PURL_TYPES))}."
205+
)
206+
return type_str
107207

108208

109209
def normalize_namespace(

0 commit comments

Comments
 (0)