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",
".confdb"]
75 and not f.startswith(
'.__afs')):
76 raise ZipdirError(
"Cannot add '%s' to the zip file, only '.py' are allowed." % os.path.join(arcdir, f))
78 for filename
in infos:
79 if filename
not in all_files:
80 removed.append(filename)
81 log.info(
" %s -> %s",
"R", filename)
82 return (added, modified, untouched, removed)
86 Check that a file honors the declared encoding (default ASCII for Python 2
87 and UTF-8 for Python 3).
89 Raises a UnicodeDecodeError in case of decoding problems and LookupError if
90 the specified codec does not exists.
92 See http://www.python.org/dev/peps/pep-0263/
94 from itertools
import islice
97 if sys.version_info[0] <= 2:
103 enc_exp = re.compile(
r"coding[:=]\s*([-\w.]+)")
104 for l
in islice(fileObj, 2):
105 m = enc_exp.search(l)
110 if hasattr(fileObj,
'name'):
111 logging.getLogger(
'checkEncoding').debug(
'checking encoding %s on %s',
114 logging.getLogger(
'checkEncoding').debug(
'checking encoding %s on file object',
118 codecs.getreader(enc)(fileObj).read()
123 log = logging.getLogger(
"zipdir")
124 if not os.path.isdir(directory):
125 raise OSError(20,
"Not a directory", directory)
126 msg =
"Zipping directory '%s'"
128 msg +=
" (without pre-compilation)"
129 log.info(msg, directory)
130 filename = os.path.realpath(directory +
".zip")
133 if os.path.exists(filename):
134 zipFile = open(filename,
"r+b")
139 zipFile = open(filename,
"ab")
143 if zipfile.is_zipfile(filename):
144 infolist = zipfile.ZipFile(filename).infolist()
147 (added, modified, untouched, removed) =
_zipChanges(directory, infolist)
148 if added
or modified
or removed:
150 z = zipfile.PyZipFile(tempBuf,
"w", zipfile.ZIP_DEFLATED)
151 for f
in added + modified + untouched:
152 src = os.path.join(directory, f)
155 log.debug(
"adding '%s'", f)
159 if os.path.exists(src +
'c'):
160 log.debug(
"removing old .pyc for '%s'", f)
162 log.debug(
"adding '%s'", f)
163 z.writepy(src, os.path.dirname(f))
166 zipFile.write(tempBuf.getvalue())
168 log.info(
"File '%s' closed", filename)
170 log.info(
"Nothing to do on '%s'", filename)
171 except UnicodeDecodeError, x:
172 log.error(
"Wrong encoding in file '%s':", src)
174 log.error(
"Probably you forgot the line '# -*- coding: utf-8 -*-'")
183 from optparse
import OptionParser
184 parser = OptionParser(usage =
"%prog [options] directory1 [directory2 ...]")
185 parser.add_option(
"--no-pyc", action =
"store_true",
186 help =
"copy the .py files without pre-compiling them")
187 parser.add_option(
"--quiet", action =
"store_true",
188 help =
"do not print info messages")
189 parser.add_option(
"--debug", action =
"store_true",
190 help =
"print debug messages (has priority over --quiet)")
194 opts, args = parser.parse_args(argv[1:])
197 parser.error(
"Specify at least one directory to zip")
202 level = logging.WARNING
204 level = logging.DEBUG
205 logging.basicConfig(level = level)
207 if "GAUDI_BUILD_LOCK" in os.environ:
208 _scopedLock =
locker.LockFile(os.environ[
"GAUDI_BUILD_LOCK"], temporary =
True)
213 if __name__ ==
'__main__':
def _zipChanges
Collect the changes to be applied to the zip file.
def main
Main function of the script.
def zipdir
Make a zip file out of a directory containing python modules.
Class for generic exception coming from the zipdir() function.