1
1
import os
2
+ import re
2
3
from itertools import permutations
3
4
4
5
EXTS = [".tar.gz" , ".zip" , ".tar" , ".tar.bz2" , ".tar.xz" , ".tgz" ]
6
+ PYPI_URLS = ["https://pypi.io" , "https://files.pythonhosted.org" ]
5
7
6
8
7
9
def _ext_munger (url ):
@@ -48,23 +50,42 @@ def _v_munger(url):
48
50
49
51
50
52
def _pypi_domain_munger (url ):
51
- for old_d , new_d in permutations (
52
- ["https://pypi.io" , "https://files.pythonhosted.org" ],
53
- 2 ,
54
- ):
53
+ for old_d , new_d in permutations (PYPI_URLS , 2 ):
55
54
yield url .replace (old_d , new_d , 1 )
56
55
57
56
58
57
def _pypi_name_munger (url ):
59
58
bn = os .path .basename (url )
60
- if (
61
- url .startswith ("https://pypi.io" )
62
- or url .startswith ("https://files.pythonhosted.org" )
63
- ) and ("{{ version }}" in bn and "{{ name" not in bn ):
64
- yield os .path .join (os .path .dirname (url ), "{{ name }}-{{ version }}.tar.gz" )
65
-
59
+ dn = os .path .dirname (url )
60
+ dist_bn = os .path .basename (os .path .dirname (url ))
61
+ is_sdist = url .endswith (".tar.gz" )
62
+ is_pypi = any (url .startswith (pypi ) for pypi in PYPI_URLS )
63
+ has_version = re .search (r"\{\{\s*version" , bn )
64
+ has_name = re .search (r"\{\{\s*name" , bn )
65
+
66
+ # try the original URL first, as a fallback (probably can't be removed?)
66
67
yield url
67
68
69
+ if is_pypi and has_version and not has_name :
70
+ yield os .path .join (dn , "{{ name }}-{{ version }}.tar.gz" )
71
+
72
+ if not (is_sdist and is_pypi and has_version ):
73
+ return
74
+
75
+ # try static PEP625 name with PEP345 distribution name (_ not -)
76
+ patterns = [
77
+ # fully normalized
78
+ r"[\.\-]+" ,
79
+ # older, partial normalization
80
+ r"[\-]+" ,
81
+ ]
82
+
83
+ for pattern in patterns :
84
+ for dist_bn_case in {dist_bn , dist_bn .lower ()}:
85
+ yield os .path .join (
86
+ dn , "%s-{{ version }}.tar.gz" % re .sub (pattern , "_" , dist_bn_case )
87
+ )
88
+
68
89
69
90
def _pypi_munger (url ):
70
91
names = [
@@ -141,7 +162,9 @@ def gen_transformed_urls(url):
141
162
url : str
142
163
The URL to transform.
143
164
"""
144
- yield from _gen_new_urls (
165
+ yielded = set ()
166
+
167
+ for new_url in _gen_new_urls (
145
168
url ,
146
169
[
147
170
_ext_munger ,
@@ -154,4 +177,7 @@ def gen_transformed_urls(url):
154
177
_pypi_name_munger ,
155
178
_github_munger ,
156
179
],
157
- )
180
+ ):
181
+ if new_url not in yielded :
182
+ yield new_url
183
+ yielded .add (new_url )
0 commit comments