1515import argparse
1616import base64
1717import collections
18- import csv
1918import hashlib
20- import io
2119import os
2220import os .path
2321import sys
@@ -35,14 +33,15 @@ def commonpath(path1, path2):
3533
3634class WheelMaker (object ):
3735 def __init__ (self , name , version , build_tag , python_tag , abi , platform ,
38- outfile = None ):
36+ outfile = None , strip_path_prefixes = None ):
3937 self ._name = name
4038 self ._version = version
4139 self ._build_tag = build_tag
4240 self ._python_tag = python_tag
4341 self ._abi = abi
4442 self ._platform = platform
4543 self ._outfile = outfile
44+ self ._strip_path_prefixes = strip_path_prefixes if strip_path_prefixes is not None else []
4645
4746 self ._zipfile = None
4847 self ._record = []
@@ -93,8 +92,17 @@ def add_string(self, filename, contents):
9392
9493 def add_file (self , package_filename , real_filename ):
9594 """Add given file to the distribution."""
96- # Always use unix path separators.
97- arcname = package_filename .replace (os .path .sep , '/' )
95+ def arcname_from (name ):
96+ # Always use unix path separators.
97+ normalized_arcname = name .replace (os .path .sep , '/' )
98+ for prefix in self ._strip_path_prefixes :
99+ if normalized_arcname .startswith (prefix ):
100+ return normalized_arcname [len (prefix ):]
101+
102+ return normalized_arcname
103+
104+ arcname = arcname_from (package_filename )
105+
98106 self ._zipfile .write (real_filename , arcname = arcname )
99107 # Find the hash and length
100108 hash = hashlib .sha256 ()
@@ -208,6 +216,15 @@ def main():
208216 output_group .add_argument ('--out' , type = str , default = None ,
209217 help = "Override name of ouptut file" )
210218
219+ output_group .add_argument ('--strip_path_prefix' ,
220+ type = str ,
221+ action = "append" ,
222+ default = [],
223+ help = "Path prefix to be stripped from input package files' path. "
224+ "Can be supplied multiple times. "
225+ "Evaluated in order."
226+ )
227+
211228 wheel_group = parser .add_argument_group ("Wheel metadata" )
212229 wheel_group .add_argument (
213230 '--header' , action = 'append' ,
@@ -248,13 +265,17 @@ def main():
248265 # Sort the files for reproducible order in the archive.
249266 all_files = sorted (all_files .items ())
250267
268+ strip_prefixes = [p for p in arguments .strip_path_prefix ]
269+
251270 with WheelMaker (name = arguments .name ,
252271 version = arguments .version ,
253272 build_tag = arguments .build_tag ,
254273 python_tag = arguments .python_tag ,
255274 abi = arguments .abi ,
256275 platform = arguments .platform ,
257- outfile = arguments .out ) as maker :
276+ outfile = arguments .out ,
277+ strip_path_prefixes = strip_prefixes
278+ ) as maker :
258279 for package_filename , real_filename in all_files :
259280 maker .add_file (package_filename , real_filename )
260281 maker .add_wheelfile ()
0 commit comments