14 from StringIO
import StringIO
35 if fn.endswith(
".pyc"):
37 infos[fn] = i.date_time
46 log = logging.getLogger(
"zipdir")
47 dirlen = len(directory) + 1
48 for root, dirs, files
in os.walk(directory):
49 if "lib-dynload" in dirs:
51 dirs.remove(
"lib-dynload")
52 arcdir = root[dirlen:]
54 ext = os.path.splitext(f)[1]
56 filename = os.path.join(arcdir, f)
57 all_files.add(filename)
58 if filename
not in infos:
60 added.append(filename)
62 filetime = time.localtime(os.stat(os.path.join(directory,filename))[stat.ST_MTIME])[:6]
63 if filetime > infos[filename]:
65 modified.append(filename)
68 untouched.append(filename)
70 log.debug(" %s -> %s", action, filename)
72 log.info(
" %s -> %s", action, filename)
74 elif ext
not in [
".pyc",
".pyo",
".stamp",
".cmtref"]
and not f.startswith(
'.__afs'):
75 raise ZipdirError(
"Cannot add '%s' to the zip file, only '.py' are allowed." % os.path.join(arcdir, f))
77 for filename
in infos:
78 if filename
not in all_files:
79 removed.append(filename)
80 log.info(
" %s -> %s",
"R", filename)
81 return (added, modified, untouched, removed)
85 Check that a file honors the declared encoding (default ASCII for Python 2
86 and UTF-8 for Python 3).
88 Raises a UnicodeDecodeError in case of problems.
90 See http://www.python.org/dev/peps/pep-0263/
93 if sys.version_info[0] <= 2:
99 enc_exp = re.compile(
r"coding[:=]\s*([-\w.]+)")
103 m = enc_exp.search(f.readline())
109 logging.getLogger(
'checkEncoding').debug(
'checking encoding %s on %s', enc, path)
111 codecs.open(path, encoding=enc).read()
116 log = logging.getLogger(
"zipdir")
117 if not os.path.isdir(directory):
118 raise OSError(20,
"Not a directory", directory)
119 msg =
"Zipping directory '%s'"
121 msg +=
" (without pre-compilation)"
122 log.info(msg, directory)
123 filename = os.path.realpath(directory +
".zip")
126 if os.path.exists(filename):
127 zipFile = open(filename,
"r+b")
132 zipFile = open(filename,
"ab")
136 if zipfile.is_zipfile(filename):
137 infolist = zipfile.ZipFile(filename).infolist()
140 (added, modified, untouched, removed) =
_zipChanges(directory, infolist)
141 if added
or modified
or removed:
143 z = zipfile.PyZipFile(tempBuf,
"w", zipfile.ZIP_DEFLATED)
144 for f
in added + modified + untouched:
145 src = os.path.join(directory, f)
148 log.debug(
"adding '%s'", f)
152 if os.path.exists(src +
'c'):
153 log.debug(
"removing old .pyc for '%s'", f)
155 log.debug(
"adding '%s'", f)
156 z.writepy(src, os.path.dirname(f))
159 zipFile.write(tempBuf.getvalue())
161 log.info(
"File '%s' closed", filename)
163 log.info(
"Nothing to do on '%s'", filename)
164 except UnicodeDecodeError, x:
165 log.error(
"Wrong encoding in file '%s':", src)
167 log.error(
"Probably you forgot the line '# -*- coding: utf-8 -*-'")
176 from optparse
import OptionParser
177 parser = OptionParser(usage =
"%prog [options] directory1 [directory2 ...]")
178 parser.add_option(
"--no-pyc", action =
"store_true",
179 help =
"copy the .py files without pre-compiling them")
180 parser.add_option(
"--quiet", action =
"store_true",
181 help =
"do not print info messages")
182 parser.add_option(
"--debug", action =
"store_true",
183 help =
"print debug messages (has priority over --quiet)")
187 opts, args = parser.parse_args(argv[1:])
190 parser.error(
"Specify at least one directory to zip")
195 level = logging.WARNING
197 level = logging.DEBUG
198 logging.basicConfig(level = level)
200 if "GAUDI_BUILD_LOCK" in os.environ:
201 _scopedLock =
locker.LockFile(os.environ[
"GAUDI_BUILD_LOCK"], temporary =
True)
206 if __name__ ==
'__main__':