14 from StringIO
import StringIO
36 if fn.endswith(
".pyc"):
38 infos[fn] = i.date_time
47 log = logging.getLogger(
"zipdir")
48 dirlen = len(directory) + 1
49 for root, dirs, files
in os.walk(directory):
50 if "lib-dynload" in dirs:
52 dirs.remove(
"lib-dynload")
53 arcdir = root[dirlen:]
55 ext = os.path.splitext(f)[1]
57 filename = os.path.join(arcdir, f)
58 all_files.add(filename)
59 if filename
not in infos:
61 added.append(filename)
63 filetime = time.localtime(
64 os.stat(os.path.join(directory, filename))[stat.ST_MTIME])[:6]
65 if filetime > infos[filename]:
67 modified.append(filename)
70 untouched.append(filename) 72 log.debug(" %s -> %s", action, filename)
74 log.info(
" %s -> %s", action, filename)
76 elif (ext
not in [
".pyc",
".pyo",
".stamp",
".cmtref",
".confdb"]
77 and not f.startswith(
'.__afs')):
79 "Cannot add '%s' to the zip file, only '.py' are allowed." % os.path.join(arcdir, f))
81 for filename
in infos:
82 if filename
not in all_files:
83 removed.append(filename)
84 log.info(
" %s -> %s",
"R", filename) 85 return (added, modified, untouched, removed)
90 Check that a file honors the declared encoding (default ASCII for Python 2 91 and UTF-8 for Python 3). 93 Raises a UnicodeDecodeError in case of decoding problems and LookupError if 94 the specified codec does not exists. 96 See http://www.python.org/dev/peps/pep-0263/ 98 from itertools
import islice
101 if sys.version_info[0] <= 2:
107 enc_exp = re.compile(
r"coding[:=]\s*([-\w.]+)")
108 for l
in islice(fileObj, 2):
109 m = enc_exp.search(l)
114 if hasattr(fileObj,
'name'):
115 logging.getLogger(
'checkEncoding').debug(
'checking encoding %s on %s',
118 logging.getLogger(
'checkEncoding').debug(
'checking encoding %s on file object',
122 codecs.getreader(enc)(fileObj).
read()
127 filename = os.path.realpath(directory +
".zip")
128 log = logging.getLogger(
"zipdir")
129 if not os.path.isdir(directory):
130 log.warning(
'directory %s missing, creating empty .zip file', directory)
131 open(filename,
"ab").close()
133 msg =
"Zipping directory '%s'" 135 msg +=
" (without pre-compilation)" 136 log.info(msg, directory)
139 if os.path.exists(filename):
140 zipFile = open(filename,
"r+b")
145 zipFile = open(filename,
"ab")
148 if zipfile.is_zipfile(filename):
149 infolist = zipfile.ZipFile(filename).infolist()
152 (added, modified, untouched, removed) =
_zipChanges(directory, infolist)
153 if added
or modified
or removed:
155 z = zipfile.PyZipFile(tempBuf,
"w", zipfile.ZIP_DEFLATED)
156 for f
in added + modified + untouched:
157 src = os.path.join(directory, f)
160 log.debug(
"adding '%s'", f)
164 if os.path.exists(src +
'c'):
165 log.debug(
"removing old .pyc for '%s'", f)
167 log.debug(
"adding '%s'", f)
168 z.writepy(src, os.path.dirname(f))
171 zipFile.write(tempBuf.getvalue())
173 log.info(
"File '%s' closed", filename)
175 log.info(
"Nothing to do on '%s'", filename)
176 except UnicodeDecodeError, x:
177 log.error(
"Wrong encoding in file '%s':", src)
179 log.error(
"Probably you forgot the line '# -*- coding: utf-8 -*-'")
189 from optparse
import OptionParser
190 parser = OptionParser(usage=
"%prog [options] directory1 [directory2 ...]")
191 parser.add_option(
"--no-pyc", action=
"store_true",
192 help=
"copy the .py files without pre-compiling them")
193 parser.add_option(
"--quiet", action=
"store_true",
194 help=
"do not print info messages")
195 parser.add_option(
"--debug", action=
"store_true",
196 help=
"print debug messages (has priority over --quiet)")
200 opts, args = parser.parse_args(argv[1:])
203 parser.error(
"Specify at least one directory to zip")
208 level = logging.WARNING
210 level = logging.DEBUG
211 logging.basicConfig(level=level)
218 if __name__ ==
'__main__':
def checkEncoding(fileObj)
def read(f, regex='.*', skipevents=0)
def _zipChanges(directory, infolist)
def zipdir(directory, no_pyc=False)