@@ -135,6 +135,11 @@ def create_archive(source, target=None, interpreter=None, main=None,
135
135
# the target is being created in the source directory - we
136
136
# don't want the target being added to itself
137
137
files_to_add = sorted (source .rglob ('*' ))
138
+ if filter :
139
+ files_to_add = {f : f2 for f in files_to_add
140
+ if filter (f2 := f .relative_to (source ))}
141
+ else :
142
+ files_to_add = {f : f .relative_to (source ) for f in files_to_add }
138
143
139
144
# The target cannot be in the list of files to add. If it were, we'd
140
145
# end up overwriting the source file and writing the archive into
@@ -151,22 +156,17 @@ def create_archive(source, target=None, interpreter=None, main=None,
151
156
# equal to any of the entries in files_to_add, so there's no need
152
157
# to add a special check for that.
153
158
if target in files_to_add :
154
- if filter :
155
- arcname = target .relative_to (source )
156
- not_filtered = filter (arcname )
157
- if not filter or not_filtered :
158
- raise ZipAppError (f"The target archive { target } overwrites "
159
- "one of the source files." )
159
+ raise ZipAppError (
160
+ f"The target archive { target } overwrites one of the source files." )
161
+
160
162
161
163
with _maybe_open (target , 'wb' ) as fd :
162
164
_write_file_prefix (fd , interpreter )
163
165
compression = (zipfile .ZIP_DEFLATED if compressed else
164
166
zipfile .ZIP_STORED )
165
167
with zipfile .ZipFile (fd , 'w' , compression = compression ) as z :
166
- for child in files_to_add :
167
- arcname = child .relative_to (source )
168
- if filter is None or filter (arcname ):
169
- z .write (child , arcname .as_posix ())
168
+ for child , arcname in files_to_add .items ():
169
+ z .write (child , arcname .as_posix ())
170
170
if main_py :
171
171
z .writestr ('__main__.py' , main_py .encode ('utf-8' ))
172
172
0 commit comments