2121from __future__ import with_statement
2222
2323import sys
24+ import re
25+ import fnmatch
2426import os .path
2527import zipfile
2628import contextlib
2729import shutil
2830
2931
30- def zip_folder (folder , zip_path , ignored_extenstions = []):
32+ def zip_folder (folder , zip_path , ignored_patterns = []):
3133 '''
34+
3235 note: creates a folder inside the zip with the same name of the original
3336 folder, in contrast to other implementation which put all of the files on
3437 the root level of the zip.
3538
3639 Doesn't put empty folders in the zip file.
40+
41+ tododoc ignored_patterns, they're fnmatch style
3742 '''
3843 assert os .path .isdir (folder )
3944 source_folder = os .path .realpath (folder )
4045
46+ ignored_re_patterns = [re .compile (fnmatch .translate (ignored_pattern )) for
47+ ignored_pattern in ignored_patterns ]
48+
4149 zip_name = os .path .splitext (os .path .split (zip_path )[1 ])[0 ]
4250 source_folder_name = os .path .split (source_folder )[1 ]
43-
44- ### Ensuring ignored extensions start with '.': ###########################
45- # #
46- for ignored_extenstion in ignored_extenstions :
47- if not ignored_extenstion .startswith ('.' ):
48- ignored_extenstions [
49- ignored_extenstions .index (ignored_extenstion )
50- ] = \
51- ('.' + ignored_extenstion )
52- # #
53- ### Finished ensuring ignored extensions start with '.'. ##################
5451
5552 with contextlib .closing (
5653 zipfile .ZipFile (zip_path , 'w' , zipfile .ZIP_DEFLATED )
@@ -60,9 +57,9 @@ def zip_folder(folder, zip_path, ignored_extenstions=[]):
6057
6158 for file_path in files :
6259
63- extension = os . path . splitext ( file_path )[ 1 ]
64- if extension in ignored_extenstions :
65- continue
60+ for ignored_re_pattern in ignored_re_patterns :
61+ if ignored_re_pattern . match ( file_path ) :
62+ continue
6663
6764 absolute_file_path = os .path .join (root , file_path )
6865
@@ -114,7 +111,7 @@ def make_zip():
114111
115112 sys .stdout .write ('Zipping... ' )
116113 zip_folder (package_path , zip_destination_path ,
117- ignored_extenstions = ['.pyc' , '.pyo' ])
114+ ignored_patterns = ['* .pyc' , '* .pyo' , '*__pycache__* ' ])
118115
119116 sys .stdout .write ('Done.\n ' )
120117 # #
0 commit comments