forked from botaor/Python-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MakeZip.py
142 lines (99 loc) · 3.25 KB
/
MakeZip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/python
# -*- coding: latin-1 -*-
"Zip a folder with the possibility to exclude some files."
# This line must be at the beginning of the file
from __future__ import print_function
import os
import sys
import fnmatch
import argparse
import zipfile
def _IsIgnoreFolder( name, ignoreList ):
for n in ignoreList:
if n[-1] != '/':
continue
if fnmatch.fnmatch( name, n[:-1] ):
return True
return False
def _IsIgnoreFile( name, ignoreList ):
for n in ignoreList:
if n[-1] == '/':
continue
if fnmatch.fnmatch( name, n ):
return True
return False
def ReadIgnoreFromFile( fname ):
"Read an ignore list from a text file. The format is the same as .gitignore"
" fname - name of text file."
" returns the list"
if not fname:
return []
ignores = []
try:
f = open( fname, 'rU' )
except (IOError) as e:
return []
for line in f:
if line[0] == '#':
continue
if len( line[:-1].strip() ) == 0:
continue
ignores.append( line[:-1] )
f.close()
return ignores
def ReadIgnoreFromString( str ):
"Take a string of comma separated wildcards and create an ignore list."
" str - string with comma separated wildcards."
" returns the list"
if not str:
return []
tmp = str.split( ',' )
ignores = []
for s in tmp:
ignores.append( s.strip() )
return ignores
def zipdir( zipname, folder, ignore=[]):
"Create a zip file of a folder (including subfolders)."
" zipname - name of zip file to create."
" folder - folder to zip."
" ignore - list of wildcards to ignore."
zipf = zipfile.ZipFile( zipname, 'w', zipfile.ZIP_DEFLATED )
ignoreFolders = []
tot = 0
for root, dirs, files in os.walk(folder):
for dir in dirs:
if _IsIgnoreFolder( dir, ignore ):
ignoreFolders.append( os.path.join( root, dir ) )
if root in ignoreFolders:
continue
for file in files:
if _IsIgnoreFile( file, ignore ):
continue
zipf.write(os.path.join(root, file))
tot += 1
zipf.close()
return tot
def main():
"The main function called when the utility is run"
print( 'Zip folder V1.00 Rui Botão, Portugal' )
print()
parser = argparse.ArgumentParser(description="<Put application description here>" )
parser.add_argument( "zipfile", help="Name of zip file to create" )
parser.add_argument( "folder", help="Folder to zip" )
parser.add_argument( "-iF", "-ignoreFile", help="File with ignore wildcards (like .gitignore)", type=str )
parser.add_argument( "-iS", "-ignoreString", help="List of ignore wildcards (comma separated)", type=str )
args = parser.parse_args()
ignores = ReadIgnoreFromFile( args.iF )
ignores = ignores + ReadIgnoreFromString( args.iS )
print( "Zip filename:", args.zipfile )
print( "Zip folder:", args.folder )
print( "Ignores", ignores )
print( "Working..." )
tot = zipdir( args.zipfile, args.folder, ignores )
print( "Compressed", tot, "files." )
return 0
# This is the standard boilerplate that calls the main() function.
if __name__ == '__main__':
rc = main()
# This function will set the result value of this utility
sys.exit( rc )