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 decoding problems and LookupError if
89 the specified codec does not exists.
91 See http://www.python.org/dev/peps/pep-0263/
93 from itertools
import islice
96 if sys.version_info[0] <= 2:
102 enc_exp = re.compile(
r"coding[:=]\s*([-\w.]+)")
103 for l
in islice(fileObj, 2):
104 m = enc_exp.search(l)
109 if hasattr(fileObj,
'name'):
110 logging.getLogger(
'checkEncoding').debug(
'checking encoding %s on %s',
113 logging.getLogger(
'checkEncoding').debug(
'checking encoding %s on file object',
117 codecs.getreader(enc)(fileObj).read()
122 log = logging.getLogger(
"zipdir")
123 if not os.path.isdir(directory):
124 raise OSError(20,
"Not a directory", directory)
125 msg =
"Zipping directory '%s'"
127 msg +=
" (without pre-compilation)"
128 log.info(msg, directory)
129 filename = os.path.realpath(directory +
".zip")
132 if os.path.exists(filename):
133 zipFile = open(filename,
"r+b")
138 zipFile = open(filename,
"ab")
142 if zipfile.is_zipfile(filename):
143 infolist = zipfile.ZipFile(filename).infolist()
146 (added, modified, untouched, removed) =
_zipChanges(directory, infolist)
147 if added
or modified
or removed:
149 z = zipfile.PyZipFile(tempBuf,
"w", zipfile.ZIP_DEFLATED)
150 for f
in added + modified + untouched:
151 src = os.path.join(directory, f)
154 log.debug(
"adding '%s'", f)
158 if os.path.exists(src +
'c'):
159 log.debug(
"removing old .pyc for '%s'", f)
161 log.debug(
"adding '%s'", f)
162 z.writepy(src, os.path.dirname(f))
165 zipFile.write(tempBuf.getvalue())
167 log.info(
"File '%s' closed", filename)
169 log.info(
"Nothing to do on '%s'", filename)
170 except UnicodeDecodeError, x:
171 log.error(
"Wrong encoding in file '%s':", src)
173 log.error(
"Probably you forgot the line '# -*- coding: utf-8 -*-'")
182 from optparse
import OptionParser
183 parser = OptionParser(usage =
"%prog [options] directory1 [directory2 ...]")
184 parser.add_option(
"--no-pyc", action =
"store_true",
185 help =
"copy the .py files without pre-compiling them")
186 parser.add_option(
"--quiet", action =
"store_true",
187 help =
"do not print info messages")
188 parser.add_option(
"--debug", action =
"store_true",
189 help =
"print debug messages (has priority over --quiet)")
193 opts, args = parser.parse_args(argv[1:])
196 parser.error(
"Specify at least one directory to zip")
201 level = logging.WARNING
203 level = logging.DEBUG
204 logging.basicConfig(level = level)
206 if "GAUDI_BUILD_LOCK" in os.environ:
207 _scopedLock =
locker.LockFile(os.environ[
"GAUDI_BUILD_LOCK"], temporary =
True)
212 if __name__ ==
'__main__':