24
24
import stat
25
25
import sys
26
26
import zipfile
27
+ from collections .abc import Iterable
27
28
from pathlib import Path
28
29
29
30
_ZIP_EPOCH = (1980 , 1 , 1 , 0 , 0 , 0 )
@@ -98,6 +99,30 @@ def normalize_pep440(version):
98
99
return str (packaging .version .Version (f"0+{ sanitized } " ))
99
100
100
101
102
+ def arcname_from (
103
+ name : str , distribution_prefix : str , strip_path_prefixes : Sequence [str ] = ()
104
+ ) -> str :
105
+ """Return the within-archive name for a given file path name.
106
+
107
+ Prefixes to strip are checked in order and only the first match will be used.
108
+
109
+ Args:
110
+ name: The file path eg 'mylib/a/b/c/file.py'
111
+ distribution_prefix: The
112
+ strip_path_prefixes: Remove these prefixes from names.
113
+ """
114
+ # Always use unix path separators.
115
+ normalized_arcname = name .replace (os .path .sep , "/" )
116
+ # Don't manipulate names filenames in the .distinfo or .data directories.
117
+ if distribution_prefix and normalized_arcname .startswith (distribution_prefix ):
118
+ return normalized_arcname
119
+ for prefix in strip_path_prefixes :
120
+ if normalized_arcname .startswith (prefix ):
121
+ return normalized_arcname [len (prefix ) :]
122
+
123
+ return normalized_arcname
124
+
125
+
101
126
class _WhlFile (zipfile .ZipFile ):
102
127
def __init__ (
103
128
self ,
@@ -126,18 +151,6 @@ def data_path(self, basename):
126
151
def add_file (self , package_filename , real_filename ):
127
152
"""Add given file to the distribution."""
128
153
129
- def arcname_from (name ):
130
- # Always use unix path separators.
131
- normalized_arcname = name .replace (os .path .sep , "/" )
132
- # Don't manipulate names filenames in the .distinfo or .data directories.
133
- if normalized_arcname .startswith (self ._distribution_prefix ):
134
- return normalized_arcname
135
- for prefix in self ._strip_path_prefixes :
136
- if normalized_arcname .startswith (prefix ):
137
- return normalized_arcname [len (prefix ) :]
138
-
139
- return normalized_arcname
140
-
141
154
if os .path .isdir (real_filename ):
142
155
directory_contents = os .listdir (real_filename )
143
156
for file_ in directory_contents :
@@ -147,7 +160,11 @@ def arcname_from(name):
147
160
)
148
161
return
149
162
150
- arcname = arcname_from (package_filename )
163
+ arcname = arcname_from (
164
+ package_filename ,
165
+ distribution_prefix = self ._distribution_prefix ,
166
+ strip_path_prefixes = self ._strip_path_prefixes ,
167
+ )
151
168
zinfo = self ._zipinfo (arcname )
152
169
153
170
# Write file to the zip archive while computing the hash and length
@@ -569,7 +586,9 @@ def get_new_requirement_line(reqs_text, extra):
569
586
else :
570
587
return f"Requires-Dist: { req .name } { req_extra_deps } { req .specifier } ; { req .marker } "
571
588
else :
572
- return f"Requires-Dist: { req .name } { req_extra_deps } { req .specifier } ; { extra } " .strip (" ;" )
589
+ return f"Requires-Dist: { req .name } { req_extra_deps } { req .specifier } ; { extra } " .strip (
590
+ " ;"
591
+ )
573
592
574
593
for meta_line in metadata .splitlines ():
575
594
if not meta_line .startswith ("Requires-Dist: " ):
0 commit comments