00001
00002
00003
00004
00005
00006 import os, sys, zipfile, logging, stat, time
00007 from StringIO import StringIO
00008
00009
00010 import locker
00011
00012
00013 class ZipdirError(RuntimeError):
00014 pass
00015
00016
00017
00018
00019
00020
00021
00022
00023 def _zipChanges(directory, infolist):
00024
00025 infos = {}
00026 for i in infolist:
00027 fn = i.filename
00028 if fn.endswith(".pyc"):
00029 fn = fn[:-1]
00030 infos[fn] = i.date_time
00031
00032
00033 added = []
00034 modified = []
00035 untouched = []
00036 removed = []
00037 all_files = set()
00038
00039 log = logging.getLogger("zipdir")
00040 dirlen = len(directory) + 1
00041 for root, _dirs, files in os.walk(directory):
00042 arcdir = root[dirlen:]
00043 for f in files:
00044 ext = os.path.splitext(f)[1]
00045 if ext == ".py":
00046 filename = os.path.join(arcdir, f)
00047 all_files.add(filename)
00048 if filename not in infos:
00049 action = "A"
00050 added.append(filename)
00051 else:
00052 filetime = time.localtime(os.stat(os.path.join(directory,filename))[stat.ST_MTIME])[:6]
00053 if filetime > infos[filename]:
00054 action = "M"
00055 modified.append(filename)
00056 else:
00057 action = "U"
00058 untouched.append(filename)
00059 log.info(" %s -> %s", action, filename)
00060 elif ext not in [".pyc", ".pyo", ".stamp", ".cmtref"]:
00061 raise ZipdirError("Cannot add '%s' to the zip file, only '.py' are allowed." % os.path.join(arcdir, f))
00062
00063 for filename in infos:
00064 if filename not in all_files:
00065 removed.append(filename)
00066 log.info(" %s -> %s", "R", filename)
00067 return (added, modified, untouched, removed)
00068
00069
00070 def zipdir(directory, no_pyc = False):
00071 log = logging.getLogger("zipdir")
00072 if not os.path.isdir(directory):
00073 raise OSError(20, "Not a directory", directory)
00074 msg = "Zipping directory '%s'"
00075 if no_pyc:
00076 msg += " (without pre-compilation)"
00077 log.info(msg, directory)
00078 filename = os.path.realpath(directory + ".zip")
00079
00080
00081 if os.path.exists(filename):
00082 zipFile = open(filename, "r+b")
00083 else:
00084
00085
00086
00087 zipFile = open(filename, "ab")
00088
00089 locker.lock(zipFile)
00090 try:
00091 if zipfile.is_zipfile(filename):
00092 infolist = zipfile.ZipFile(filename).infolist()
00093 else:
00094 infolist = []
00095 (added, modified, untouched, removed) = _zipChanges(directory, infolist)
00096 if added or modified or removed:
00097 tempBuf = StringIO()
00098 z = zipfile.PyZipFile(tempBuf, "w", zipfile.ZIP_DEFLATED)
00099 for f in added + modified + untouched:
00100 src = os.path.join(directory, f)
00101 if no_pyc:
00102 log.debug("adding '%s'", f)
00103 z.write(src, f)
00104 else:
00105
00106 if os.path.exists(src + 'c'):
00107 log.debug("removing old .pyc for '%s'", f)
00108 os.remove(src + 'c')
00109 log.debug("adding '%s'", f)
00110 z.writepy(src, os.path.dirname(f))
00111 z.close()
00112 zipFile.seek(0)
00113 zipFile.write(tempBuf.getvalue())
00114 zipFile.truncate()
00115 log.info("File '%s' closed", filename)
00116 else:
00117 log.info("Nothing to do on '%s'", filename)
00118 finally:
00119 locker.unlock(zipFile)
00120 zipFile.close()
00121
00122
00123
00124 def main(argv = None):
00125 from optparse import OptionParser
00126 parser = OptionParser(usage = "%prog [options] directory1 [directory2 ...]")
00127 parser.add_option("--no-pyc", action = "store_true",
00128 help = "copy the .py files without pre-compiling them")
00129 parser.add_option("--quiet", action = "store_true",
00130 help = "do not print info messages")
00131 parser.add_option("--debug", action = "store_true",
00132 help = "print debug messages (has priority over --quiet)")
00133
00134 if argv is None:
00135 argv = sys.argv
00136 opts, args = parser.parse_args(argv[1:])
00137
00138 if not args:
00139 parser.error("Specify at least one directory to zip")
00140
00141
00142 level = logging.INFO
00143 if opts.quiet:
00144 level = logging.WARNING
00145 if opts.debug:
00146 level = logging.DEBUG
00147 logging.basicConfig(level = level)
00148
00149 if "GAUDI_BUILD_LOCK" in os.environ:
00150 _scopedLock = locker.LockFile(os.environ["GAUDI_BUILD_LOCK"], temporary = True)
00151
00152 for d in args:
00153 zipdir(d, opts.no_pyc)
00154
00155 if __name__ == '__main__':
00156 main()