Skip to content

Commit c767fbf

Browse files
committed
Treat filenames from the Wheel file
Filenames in a Wheel are returned as bytestrings. When we check to find METADATA files, we need to juggle the fact that strings are unicode by default and that the filenames are bytestrings. Closes pypagh-235
1 parent 94b00eb commit c767fbf

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

tests/test_wheel.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,20 @@ def example_wheel(request):
2828

2929
def test_version_parsing(example_wheel):
3030
assert example_wheel.py_version == 'py2.py3'
31+
32+
33+
def test_find_metadata_files():
34+
names = [
35+
b'package/lib/__init__.py',
36+
b'package/lib/version.py',
37+
b'package/METADATA.txt',
38+
b'package/METADATA.json',
39+
b'package/METADATA',
40+
]
41+
expected = [
42+
['package', 'METADATA'],
43+
['package', 'METADATA.json'],
44+
['package', 'METADATA.txt'],
45+
]
46+
candidates = wheel.Wheel.find_candidate_metadata_files(names)
47+
assert expected == candidates

twine/wheel.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
re.VERBOSE)
3939

4040

41+
def try_decode(s):
42+
if isinstance(s, bytes):
43+
return s.decode('utf8')
44+
return s
45+
46+
4147
class Wheel(Distribution):
4248

4349
def __init__(self, filename, metadata_version=None):
@@ -51,6 +57,15 @@ def py_version(self):
5157
wheel_info = wheel_file_re.match(self.basefilename)
5258
return wheel_info.group("pyver")
5359

60+
@staticmethod
61+
def find_candidate_metadata_files(names):
62+
"""Filter files that may be METADATA files."""
63+
tuples = [
64+
x.split('/') for x in map(try_decode, names)
65+
if 'METADATA' in x
66+
]
67+
return [x[1] for x in sorted([(len(x), x) for x in tuples])]
68+
5469
def read(self):
5570
fqn = os.path.abspath(os.path.normpath(self.filename))
5671
if not os.path.exists(fqn):
@@ -66,9 +81,7 @@ def read_file(name):
6681
raise ValueError('Not a known archive format: %s' % fqn)
6782

6883
try:
69-
tuples = [x.split('/') for x in names if 'METADATA' in x]
70-
schwarz = sorted([(len(x), x) for x in tuples])
71-
for path in [x[1] for x in schwarz]:
84+
for path in self.find_candidate_metadata_files(names):
7285
candidate = '/'.join(path)
7386
data = read_file(candidate)
7487
if b'Metadata-Version' in data:

0 commit comments

Comments
 (0)