| # Gaudi Framework, version v21r7p1 |

Home |  | Generated: 15 Feb 2010

# ZipPythonDir.py

Go to the documentation of this file.

    00001 #!/usr/bin/env python
    00002
    00003 ## file ZipPythonDir.py
    00004 #  Script to generate a zip file that can replace a directory in the python path.
    00005
    00006 import os, sys, zipfile, logging, stat, time
    00007 from StringIO import StringIO
    00008
    00009 # Add to the path the entry needed to import the locker module.
    00010 import locker
    00011
    00012 ## Class for generic exception coming from the zipdir() function
    00013 class ZipdirError(RuntimeError):
    00014     pass
    00015
    00016 ## Collect the changes to be applied to the zip file.
    00017 #
    00018 #  @param directory: directory to be packed in the zip file
    00019 #  @param infolist: list of ZipInfo objects already contained in the zip archive
    00020 #
    00021 #  @return: tuple of (added, modified, untouched, removed) entries in the directory with respect to the zip file
    00022 #
    00023 def _zipChanges(directory, infolist):
    00024     # gets the dates of the files in the zip archive
    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     # gets the changes
    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": # extensions that can enter the zip file
    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"]: # extensions that can be ignored
    00061                 raise ZipdirError("Cannot add '%s' to the zip file, only '.py' are allowed." % os.path.join(arcdir, f))
    00062     # check for removed files
    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 ## Make a zip file out of a directory containing python modules
    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     # Open the file in read an update mode
    00081     if os.path.exists(filename):
    00082         zipFile = open(filename, "r+b")
    00083     else:
    00084         # If the file does not exist, we need to create it.
    00085         # "append mode" ensures that, in case of two processes trying to
    00086         # create the file, they do not truncate each other file
    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                     # Remove the .pyc file to always force a re-compilation
    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 ## Main function of the script.
    00123 #  Parse arguments and call zipdir() for each directory passed as argument
    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     # Initialize the logging module
    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     # zip all the directories passed as arguments
    00152     for d in args:
    00153         zipdir(d, opts.no_pyc)
    00154
    00155 if __name__ == '__main__':
    00156     main()