2424import stat
2525import sys
2626import zipfile
27+ from collections .abc import Iterable
2728from pathlib import Path
2829
2930_ZIP_EPOCH = (1980 , 1 , 1 , 0 , 0 , 0 )
@@ -98,6 +99,30 @@ def normalize_pep440(version):
9899 return str (packaging .version .Version (f"0+{ sanitized } " ))
99100
100101
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+
101126class _WhlFile (zipfile .ZipFile ):
102127 def __init__ (
103128 self ,
@@ -126,18 +151,6 @@ def data_path(self, basename):
126151 def add_file (self , package_filename , real_filename ):
127152 """Add given file to the distribution."""
128153
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-
141154 if os .path .isdir (real_filename ):
142155 directory_contents = os .listdir (real_filename )
143156 for file_ in directory_contents :
@@ -147,7 +160,11 @@ def arcname_from(name):
147160 )
148161 return
149162
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+ )
151168 zinfo = self ._zipinfo (arcname )
152169
153170 # Write file to the zip archive while computing the hash and length
@@ -569,7 +586,9 @@ def get_new_requirement_line(reqs_text, extra):
569586 else :
570587 return f"Requires-Dist: { req .name } { req_extra_deps } { req .specifier } ; { req .marker } "
571588 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+ )
573592
574593 for meta_line in metadata .splitlines ():
575594 if not meta_line .startswith ("Requires-Dist: " ):
0 commit comments