-
Notifications
You must be signed in to change notification settings - Fork 46
/
purl2url.py
414 lines (309 loc) · 10.9 KB
/
purl2url.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# -*- coding: utf-8 -*-
#
# Copyright (c) the purl authors
# SPDX-License-Identifier: MIT
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Visit https://github.com/package-url/packageurl-python for support and
# download.
from packageurl import PackageURL
from packageurl.contrib.route import NoRouteAvailable
from packageurl.contrib.route import Router
def get_repo_download_url_by_package_type(
type, namespace, name, version, archive_extension="tar.gz"
):
"""
Return the download URL for a hosted git repository given a package type
or None.
"""
if archive_extension not in ("zip", "tar.gz"):
raise ValueError("Only zip and tar.gz extensions are supported")
download_url_by_type = {
"github": f"https://github.com/{namespace}/{name}/archive/refs/tags/{version}.{archive_extension}",
"bitbucket": f"https://bitbucket.org/{namespace}/{name}/get/{version}.{archive_extension}",
"gitlab": f"https://gitlab.com/{namespace}/{name}/-/archive/{version}/{name}-{version}.{archive_extension}",
}
return download_url_by_type.get(type)
repo_router = Router()
download_router = Router()
def _get_url_from_router(router, purl):
if purl:
try:
return router.process(purl)
except NoRouteAvailable:
return
def get_repo_url(purl):
"""
Return a repository URL inferred from the `purl` string.
"""
return _get_url_from_router(repo_router, purl)
def get_download_url(purl):
"""
Return a download URL inferred from the `purl` string.
"""
download_url = _get_url_from_router(download_router, purl)
if download_url:
return download_url
# Fallback on the `download_url` qualifier when available.
purl_data = PackageURL.from_string(purl)
return purl_data.qualifiers.get("download_url", None)
def get_inferred_urls(purl):
"""
Return all inferred URLs (repo, download) from the `purl` string.
"""
url_functions = (
get_repo_url,
get_download_url,
)
inferred_urls = []
for url_func in url_functions:
url = url_func(purl)
if url:
inferred_urls.append(url)
return inferred_urls
# Backward compatibility
purl2url = get_repo_url
get_url = get_repo_url
@repo_router.route("pkg:cargo/.*")
def build_cargo_repo_url(purl):
"""
Return a cargo repo URL from the `purl` string.
"""
purl_data = PackageURL.from_string(purl)
name = purl_data.name
version = purl_data.version
if name and version:
return f"https://crates.io/crates/{name}/{version}"
elif name:
return f"https://crates.io/crates/{name}"
@repo_router.route("pkg:bitbucket/.*")
def build_bitbucket_repo_url(purl):
"""
Return a bitbucket repo URL from the `purl` string.
"""
purl_data = PackageURL.from_string(purl)
namespace = purl_data.namespace
name = purl_data.name
if name and namespace:
return f"https://bitbucket.org/{namespace}/{name}"
@repo_router.route("pkg:github/.*")
def build_github_repo_url(purl):
"""
Return a github repo URL from the `purl` string.
"""
purl_data = PackageURL.from_string(purl)
namespace = purl_data.namespace
name = purl_data.name
version = purl_data.version
qualifiers = purl_data.qualifiers
if not (name and namespace):
return
repo_url = f"https://github.com/{namespace}/{name}"
if version:
version_prefix = qualifiers.get("version_prefix", "")
repo_url = f"{repo_url}/tree/{version_prefix}{version}"
return repo_url
@repo_router.route("pkg:gitlab/.*")
def build_gitlab_repo_url(purl):
"""
Return a gitlab repo URL from the `purl` string.
"""
purl_data = PackageURL.from_string(purl)
namespace = purl_data.namespace
name = purl_data.name
if name and namespace:
return f"https://gitlab.com/{namespace}/{name}"
@repo_router.route("pkg:(gem|rubygems)/.*")
def build_rubygems_repo_url(purl):
"""
Return a rubygems repo URL from the `purl` string.
"""
purl_data = PackageURL.from_string(purl)
name = purl_data.name
version = purl_data.version
if name and version:
return f"https://rubygems.org/gems/{name}/versions/{version}"
elif name:
return f"https://rubygems.org/gems/{name}"
@repo_router.route("pkg:cran/.*")
def build_cran_repo_url(purl):
"""
Return a cran repo URL from the `purl` string.
"""
purl_data = PackageURL.from_string(purl)
name = purl_data.name
version = purl_data.version
return f"https://cran.r-project.org/src/contrib/{name}_{version}.tar.gz"
@repo_router.route("pkg:npm/.*")
def build_npm_repo_url(purl):
"""
Return a npm repo URL from the `purl` string.
"""
purl_data = PackageURL.from_string(purl)
namespace = purl_data.namespace
name = purl_data.name
version = purl_data.version
repo_url = "https://www.npmjs.com/package/"
if namespace:
repo_url += f"{namespace}/"
repo_url += f"{name}"
if version:
repo_url += f"/v/{version}"
return repo_url
@repo_router.route("pkg:pypi/.*")
def build_pypi_repo_url(purl):
"""
Return a pypi repo URL from the `purl` string.
"""
purl_data = PackageURL.from_string(purl)
name = (purl_data.name or "").replace("_", "-")
version = purl_data.version
if name and version:
return f"https://pypi.org/project/{name}/{version}/"
elif name:
return f"https://pypi.org/project/{name}/"
@repo_router.route("pkg:composer/.*")
def build_composer_repo_url(purl):
"""
Return a composer repo URL from the `purl` string.
"""
purl_data = PackageURL.from_string(purl)
name = purl_data.name
version = purl_data.version
namespace = purl_data.namespace
if name and version:
return f"https://packagist.org/packages/{namespace}/{name}#{version}"
elif name:
return f"https://packagist.org/packages/{namespace}/{name}"
@repo_router.route("pkg:nuget/.*")
def build_nuget_repo_url(purl):
"""
Return a nuget repo URL from the `purl` string.
"""
purl_data = PackageURL.from_string(purl)
name = purl_data.name
version = purl_data.version
if name and version:
return f"https://www.nuget.org/packages/{name}/{version}"
elif name:
return f"https://www.nuget.org/packages/{name}"
@repo_router.route("pkg:hackage/.*")
def build_hackage_repo_url(purl):
"""
Return a hackage repo URL from the `purl` string.
"""
purl_data = PackageURL.from_string(purl)
name = purl_data.name
version = purl_data.version
if name and version:
return f"https://hackage.haskell.org/package/{name}-{version}"
elif name:
return f"https://hackage.haskell.org/package/{name}"
@repo_router.route("pkg:golang/.*")
def build_golang_repo_url(purl):
"""
Return a golang repo URL from the `purl` string.
"""
purl_data = PackageURL.from_string(purl)
namespace = purl_data.namespace
name = purl_data.name
version = purl_data.version
if name and version:
return f"https://pkg.go.dev/{namespace}/{name}@{version}"
elif name:
return f"https://pkg.go.dev/{namespace}/{name}"
# Download URLs:
@download_router.route("pkg:cargo/.*")
def build_cargo_download_url(purl):
"""
Return a cargo download URL from the `purl` string.
"""
purl_data = PackageURL.from_string(purl)
name = purl_data.name
version = purl_data.version
if name and version:
return f"https://crates.io/api/v1/crates/{name}/{version}/download"
@download_router.route("pkg:(gem|rubygems)/.*")
def build_rubygems_download_url(purl):
"""
Return a rubygems download URL from the `purl` string.
"""
purl_data = PackageURL.from_string(purl)
name = purl_data.name
version = purl_data.version
if name and version:
return f"https://rubygems.org/downloads/{name}-{version}.gem"
@download_router.route("pkg:npm/.*")
def build_npm_download_url(purl):
"""
Return a npm download URL from the `purl` string.
"""
purl_data = PackageURL.from_string(purl)
name = purl_data.name
version = purl_data.version
if name and version:
return f"http://registry.npmjs.org/{name}/-/{name}-{version}.tgz"
@download_router.route("pkg:hackage/.*")
def build_hackage_download_url(purl):
"""
Return a hackage download URL from the `purl` string.
"""
purl_data = PackageURL.from_string(purl)
name = purl_data.name
version = purl_data.version
if name and version:
return f"https://hackage.haskell.org/package/{name}-{version}/{name}-{version}.tar.gz"
@download_router.route("pkg:nuget/.*")
def build_nuget_download_url(purl):
"""
Return a nuget download URL from the `purl` string.
"""
purl_data = PackageURL.from_string(purl)
name = purl_data.name
version = purl_data.version
if name and version:
return f"https://www.nuget.org/api/v2/package/{name}/{version}"
@download_router.route("pkg:gitlab/.*", "pkg:bitbucket/.*", "pkg:github/.*")
def build_repo_download_url(purl):
"""
Return a gitlab download URL from the `purl` string.
"""
return get_repo_download_url(purl)
def get_repo_download_url(purl):
"""
Return ``download_url`` if present in ``purl`` qualifiers or
if ``namespace``, ``name`` and ``version`` are present in ``purl``
else return None.
"""
purl_data = PackageURL.from_string(purl)
namespace = purl_data.namespace
type = purl_data.type
name = purl_data.name
version = purl_data.version
qualifiers = purl_data.qualifiers
download_url = qualifiers.get("download_url")
if download_url:
return download_url
if not (namespace and name and version):
return
version_prefix = qualifiers.get("version_prefix", "")
version = f"{version_prefix}{version}"
return get_repo_download_url_by_package_type(
type=type, namespace=namespace, name=name, version=version
)