14 from StringIO
import StringIO
37 if fn.endswith(
".pyc"):
39 infos[fn] = i.date_time
48 log = logging.getLogger(
"zipdir")
49 dirlen = len(directory) + 1
50 for root, dirs, files
in os.walk(directory):
51 if "lib-dynload" in dirs:
53 dirs.remove(
"lib-dynload")
54 arcdir = root[dirlen:]
56 ext = os.path.splitext(f)[1]
58 filename = os.path.join(arcdir, f)
59 all_files.add(filename)
60 if filename
not in infos:
62 added.append(filename)
64 filetime = time.localtime(
65 os.stat(os.path.join(directory,
66 filename))[stat.ST_MTIME])[:6]
67 if filetime > infos[filename]:
69 modified.append(filename)
72 untouched.append(filename) 74 log.debug(" %s -> %s", action, filename)
76 log.info(
" %s -> %s", action, filename)
78 elif (ext
not in [
".pyc",
".pyo",
".stamp",
".cmtref",
".confdb"]
79 and not f.startswith(
'.__afs')):
81 "Cannot add '%s' to the zip file, only '.py' are allowed." 82 % os.path.join(arcdir, f))
84 for filename
in infos:
85 if filename
not in all_files:
86 removed.append(filename)
87 log.info(
" %s -> %s",
"R", filename) 88 return (added, modified, untouched, removed)
93 Check that a file honors the declared encoding (default ASCII for Python 2 94 and UTF-8 for Python 3). 96 Raises a UnicodeDecodeError in case of decoding problems and LookupError if 97 the specified codec does not exists. 99 See http://www.python.org/dev/peps/pep-0263/ 101 from itertools
import islice
104 if sys.version_info[0] <= 2:
110 enc_exp = re.compile(
r"coding[:=]\s*([-\w.]+)")
111 for l
in islice(fileObj, 2):
112 m = enc_exp.search(l)
117 if hasattr(fileObj,
'name'):
118 logging.getLogger(
'checkEncoding').debug(
'checking encoding %s on %s',
121 logging.getLogger(
'checkEncoding').debug(
122 'checking encoding %s on file object', enc)
125 codecs.getreader(enc)(fileObj).
read()
130 filename = os.path.realpath(directory +
".zip")
131 log = logging.getLogger(
"zipdir")
132 if not os.path.isdir(directory):
133 log.warning(
'directory %s missing, creating empty .zip file',
135 open(filename,
"ab").close()
137 msg =
"Zipping directory '%s'" 139 msg +=
" (without pre-compilation)" 140 log.info(msg, directory)
143 if os.path.exists(filename):
144 zipFile = open(filename,
"r+b")
149 zipFile = open(filename,
"ab")
152 if zipfile.is_zipfile(filename):
153 infolist = zipfile.ZipFile(filename).infolist()
156 (added, modified, untouched, removed) =
_zipChanges(
158 if added
or modified
or removed:
160 z = zipfile.PyZipFile(tempBuf,
"w", zipfile.ZIP_DEFLATED)
161 for f
in added + modified + untouched:
162 src = os.path.join(directory, f)
165 log.debug(
"adding '%s'", f)
169 if os.path.exists(src +
'c'):
170 log.debug(
"removing old .pyc for '%s'", f)
172 log.debug(
"adding '%s'", f)
173 z.writepy(src, os.path.dirname(f))
176 zipFile.write(tempBuf.getvalue())
178 log.info(
"File '%s' closed", filename)
180 log.info(
"Nothing to do on '%s'", filename)
181 except UnicodeDecodeError, x:
182 log.error(
"Wrong encoding in file '%s':", src)
184 log.error(
"Probably you forgot the line '# -*- coding: utf-8 -*-'")
195 from optparse
import OptionParser
196 parser = OptionParser(usage=
"%prog [options] directory1 [directory2 ...]")
200 help=
"copy the .py files without pre-compiling them")
202 "--quiet", action=
"store_true", help=
"do not print info messages")
206 help=
"print debug messages (has priority over --quiet)")
210 opts, args = parser.parse_args(argv[1:])
213 parser.error(
"Specify at least one directory to zip")
218 level = logging.WARNING
220 level = logging.DEBUG
221 logging.basicConfig(level=level)
228 if __name__ ==
'__main__':
def checkEncoding(fileObj)
def read(f, regex='.*', skipevents=0)
def _zipChanges(directory, infolist)
def zipdir(directory, no_pyc=False)