Skip to content

Commit 56e817b

Browse files
committed
Remove the dependencies on pip internal for wheel matching
Signed-off-by: Thomas Druez <tdruez@nexb.com>
1 parent c292e85 commit 56e817b

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

src/packageurl/contrib/url2purl.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@
3838
from urllib.parse import urlparse # Python 3
3939
from urllib.parse import unquote_plus
4040

41-
from pip._internal.wheel import Wheel
42-
from pip._internal.exceptions import InvalidWheelFilename
43-
4441
from packageurl import PackageURL
4542
from packageurl.contrib.route import Router
4643
from packageurl.contrib.route import NoRouteAvailable
@@ -187,25 +184,39 @@ def build_rubygems_purl(uri):
187184
return purl_from_pattern('rubygems', rubygems_pattern, uri)
188185

189186

190-
# https://pypi.python.org/packages/source/p/python-openid/python-openid-2.2.5.zip
187+
# https://pypi.python.org/packages/source/a/anyjson/anyjson-0.3.3.tar.gz
191188
pypi_pattern = (
192189
r"(?P<name>.+)-(?P<version>.+)"
193-
r"\.(zip|tar.gz|tar.bz2)$"
190+
r"\.(zip|tar.gz|tar.bz2|.tgz)$"
191+
)
192+
193+
# This pattern can be found in the following locations:
194+
# - wheel.wheelfile.WHEEL_INFO_RE
195+
# - distlib.wheel.FILENAME_RE
196+
# - setuptools.wheel.WHEEL_NAME
197+
# - pip._internal.wheel.Wheel.wheel_file_re
198+
wheel_file_re = re.compile(
199+
r"^(?P<namever>(?P<name>.+?)-(?P<version>.*?))"
200+
r"((-(?P<build>\d[^-]*?))?-(?P<pyver>.+?)-(?P<abi>.+?)-(?P<plat>.+?)"
201+
r"\.whl)$",
202+
re.VERBOSE
194203
)
195204

196205

197-
@purl_router.route('https?://pypi.python.org/packages/.*')
206+
@purl_router.route('https?://.+python.+org/packages/.*')
198207
def build_pypi_purl(uri):
199208
path = unquote_plus(urlparse(uri).path)
200209
last_segment = path.split('/')[-1]
201210

202-
# https://pypi.python.org/packages/py2.py3/w/wheel/wheel-0.29.0-py2.py3-none-any.whl
211+
# /wheel-0.29.0-py2.py3-none-any.whl
203212
if last_segment.endswith('.whl'):
204-
try:
205-
wheel = Wheel(last_segment)
206-
except InvalidWheelFilename:
207-
return
208-
return PackageURL('pypi', name=wheel.name, version=wheel.version)
213+
match = wheel_file_re.match(last_segment)
214+
if match:
215+
return PackageURL(
216+
'pypi',
217+
name=match.group('name'),
218+
version=match.group('version'),
219+
)
209220

210221
return purl_from_pattern('pypi', pypi_pattern, last_segment)
211222

tests/contrib/data/url2purl.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@
102102
"https://pypi.python.org/packages/py2.py3/w/wheel/bad-wheel-name-any.whl": null,
103103
"https://pypi.python.org/packages/py2.py3/w/wheel/wheel-0.29.0-py2.py3-none-any.whl": "pkg:pypi/wheel@0.29.0",
104104
"https://pypi.python.org/packages/py2.py3/w/wheel/wheel-0.29.0-py2.py3-none-any.whl#md5=d7db45db5c131af262b8ffccde46a88a": "pkg:pypi/wheel@0.29.0",
105+
"https://files.pythonhosted.org/packages/87/44/0fa8e9d0cccb8eb86fc1b5170208229dc6d6e9fd6e57ea1fe19cbeea68f5/aboutcode_toolkit-3.4.0rc1-py2.py3-none-any.whl": "pkg:pypi/aboutcode-toolkit@3.4.0rc1",
106+
"https://files.pythonhosted.org/packages/7f/cf/12d4611fc67babd4ae250c9e8249c5650ae1933395488e9e7e3562b4ff24/amqp-2.3.2-py2.py3-none-any.whl#sha256=eed41946890cd43e8dee44a316b85cf6fee5a1a34bb4a562b660a358eb529e1b": "pkg:pypi/amqp@2.3.2",
107+
"https://pypi.python.org/packages/34/c1/8806f99713ddb993c5366c362b2f908f18269f8d792aff1abfd700775a77/click-6.7-py2.py3-none-any.whl#md5=5e7a4e296b3212da2ff11017675d7a4d": "pkg:pypi/click@6.7",
105108
"https://pypi.python.org/packages/f6/ae/bbc6a204f33d9d57c798fb3857a072cd14b836792244eea4b446fdb674c6/pycryptodome-3.4.7-cp27-cp27m-win32.whl#md5=78b341de1cd686077745cd9e3a93d8d3": "pkg:pypi/pycryptodome@3.4.7",
106109
"https://pypi.python.org/packages/bd/e8/ea44ba5357a0b4fd16e5fb60c355fc8722eae31b93d7597eec50f7c35a52/pycryptodome-3.4.7-cp27-cp27m-win_amd64.whl#md5=f20bb847322baf7ae24700e5cbb15e07": "pkg:pypi/pycryptodome@3.4.7",
107110
"https://pypi.python.org/packages/1e/75/8005d086cac4cc41d3b320d338972c5e5c6a21f88472f21ac9d0e031d300/pyahocorasick-1.1.4.tar.bz2#md5=ad445b6648dc06e9040705ce1ccb4384": "pkg:pypi/pyahocorasick@1.1.4",

0 commit comments

Comments
 (0)