11from collections import defaultdict
22from copy import copy
3- from glob import glob
43from gzip import GzipFile
54import io
65import logging
76import os
8- import os .path as osp
97from pathlib import Path
108from posixpath import join as pjoin
119import tarfile
@@ -43,9 +41,9 @@ def __init__(self, patterns, basedir):
4341 self .files = set ()
4442
4543 for pattern in patterns :
46- for path in sorted ( glob ( osp . join ( basedir , pattern )) ):
47- rel = osp . relpath ( path , basedir )
48- if osp . isdir ( path ):
44+ for path in self . basedir . glob ( pattern ):
45+ rel = path . relative_to ( basedir )
46+ if rel . is_dir ( ):
4947 self .dirs .add (rel )
5048 else :
5149 self .files .add (rel )
@@ -54,14 +52,14 @@ def match_file(self, rel_path):
5452 if rel_path in self .files :
5553 return True
5654
57- return any (rel_path . startswith ( d + os . sep ) for d in self .dirs )
55+ return any (d in rel_path . parents for d in self .dirs )
5856
5957 def match_dir (self , rel_path ):
6058 if rel_path in self .dirs :
6159 return True
6260
6361 # Check if it's a subdirectory of any directory in the list
64- return any (rel_path . startswith ( d + os . sep ) for d in self .dirs )
62+ return any (d in rel_path . parents for d in self .dirs )
6563
6664
6765class SdistBuilder :
@@ -75,12 +73,12 @@ def __init__(self, module, metadata, cfgdir, reqs_by_extra, entrypoints,
7573 extra_files , include_patterns = (), exclude_patterns = ()):
7674 self .module = module
7775 self .metadata = metadata
78- self .cfgdir = cfgdir
76+ self .cfgdir = Path ( cfgdir )
7977 self .reqs_by_extra = reqs_by_extra
8078 self .entrypoints = entrypoints
81- self .extra_files = extra_files
82- self .includes = FilePatterns (include_patterns , str ( cfgdir ) )
83- self .excludes = FilePatterns (exclude_patterns , str ( cfgdir ) )
79+ self .extra_files = [ Path ( p ) for p in extra_files ]
80+ self .includes = FilePatterns (include_patterns , cfgdir )
81+ self .excludes = FilePatterns (exclude_patterns , cfgdir )
8482
8583 @classmethod
8684 def from_ini_path (cls , ini_path : Path ):
@@ -112,39 +110,30 @@ def select_files(self):
112110 This is overridden in flit itself to use information from a VCS to
113111 include tests, docs, etc. for a 'gold standard' sdist.
114112 """
115- cfgdir_s = str (self .cfgdir )
116113 return [
117- osp . relpath ( p , cfgdir_s ) for p in self .module .iter_files ()
114+ p . relative_to ( self . cfgdir ) for p in self .module .iter_files ()
118115 ] + self .extra_files
119116
120117 def apply_includes_excludes (self , files ):
121- cfgdir_s = str (self .cfgdir )
122- files = {f for f in files if not self .excludes .match_file (f )}
118+ files = {Path (f ) for f in files if not self .excludes .match_file (Path (f ))}
123119
124120 for f_rel in self .includes .files :
125121 if not self .excludes .match_file (f_rel ):
126122 files .add (f_rel )
127123
128124 for rel_d in self .includes .dirs :
129- for dirpath , dirs , dfiles in os .walk (osp .join (cfgdir_s , rel_d )):
130- for file in dfiles :
131- f_abs = osp .join (dirpath , file )
132- f_rel = osp .relpath (f_abs , cfgdir_s )
133- if not self .excludes .match_file (f_rel ):
134- files .add (f_rel )
135-
136- # Filter subdirectories before os.walk scans them
137- dirs [:] = [d for d in dirs if not self .excludes .match_dir (
138- osp .relpath (osp .join (dirpath , d ), cfgdir_s )
139- )]
125+ for abs_path in self .cfgdir .joinpath (rel_d ).glob ('**/*' ):
126+ path = abs_path .relative_to (self .cfgdir )
127+ if not self .excludes .match_file (path ):
128+ files .add (path )
140129
141130 crucial_files = set (
142- self .extra_files + [str ( self .module .file .relative_to (self .cfgdir ) )]
131+ self .extra_files + [self .module .file .relative_to (self .cfgdir )]
143132 )
144133 missing_crucial = crucial_files - files
145134 if missing_crucial :
146135 raise Exception ("Crucial files were excluded from the sdist: {}"
147- .format (", " .join (missing_crucial )))
136+ .format (", " .join (str ( m ) for m in missing_crucial )))
148137
149138 return sorted (files )
150139
@@ -157,26 +146,27 @@ def dir_name(self):
157146 return '{}-{}' .format (self .metadata .name , self .metadata .version )
158147
159148 def build (self , target_dir , gen_setup_py = True ):
160- os .makedirs (str (target_dir ), exist_ok = True )
161- target = target_dir / '{}-{}.tar.gz' .format (
149+ target_dir = Path (target_dir )
150+ target_dir .mkdir (exist_ok = True )
151+ target = target_dir .joinpath ('{}-{}.tar.gz' .format (
162152 self .metadata .name , self .metadata .version
163- )
153+ ))
164154 source_date_epoch = os .environ .get ('SOURCE_DATE_EPOCH' , '' )
165155 mtime = int (source_date_epoch ) if source_date_epoch else None
166- gz = GzipFile (str ( target ) , mode = 'wb' , mtime = mtime )
167- tf = tarfile .TarFile (str ( target ) , mode = 'w' , fileobj = gz ,
156+ gz = GzipFile (target , mode = 'wb' , mtime = mtime )
157+ tf = tarfile .TarFile (target , mode = 'w' , fileobj = gz ,
168158 format = tarfile .PAX_FORMAT )
169159
170160 try :
171161 files_to_add = self .apply_includes_excludes (self .select_files ())
172162
173163 for relpath in files_to_add :
174- path = str ( self .cfgdir / relpath )
175- ti = tf .gettarinfo (path , arcname = pjoin (self .dir_name , relpath ))
164+ path = self .cfgdir . joinpath ( relpath )
165+ ti = tf .gettarinfo (str ( path ) , arcname = pjoin (self .dir_name , relpath ))
176166 ti = clean_tarinfo (ti , mtime )
177167
178168 if ti .isreg ():
179- with open (path , 'rb' ) as f :
169+ with path . open ('rb' ) as f :
180170 tf .addfile (ti , f )
181171 else :
182172 tf .addfile (ti ) # Symlinks & ?
0 commit comments