| # Gaudi Framework, version v22r0 |

Home |  | Generated: 9 Feb 2011

# ZipPythonDir Namespace Reference

## Classes
class | ZipdirError
| Class for generic exception coming from the zipdir() function. More...
## Functions
def | _zipChanges
| Collect the changes to be applied to the zip file.
def | zipdir
| Make a zip file out of a directory containing python modules.
def | main
| Main function of the script.

## Function Documentation

def ZipPythonDir::_zipChanges | ( |  | directory,
|  |  | infolist |
| ) |  |  | `[private]`

Collect the changes to be applied to the zip file.

Parameters:

  | directory,: | directory to be packed in the zip file
  | infolist,: | list of ZipInfo objects already contained in the zip archive

Returns:
  : tuple of (added, modified, untouched, removed) entries in the directory with respect to the zip file

Definition at line 23 of file ZipPythonDir.py.

    00024                                     :
    00025     # gets the dates of the files in the zip archive
    00026     infos = {}
    00027     for i in infolist:
    00028         fn = i.filename
    00029         if fn.endswith(".pyc"):
    00030             fn = fn[:-1]
    00031         infos[fn] = i.date_time
    00032
    00033     # gets the changes
    00034     added = []
    00035     modified = []
    00036     untouched = []
    00037     removed = []
    00038     all_files = set()
    00039
    00040     log = logging.getLogger("zipdir")
    00041     dirlen = len(directory) + 1
    00042     for root, dirs, files in os.walk(directory):
    00043         if "lib-dynload" in dirs:
    00044             # exclude the directory containing binary modules
    00045             dirs.remove("lib-dynload")
    00046         arcdir = root[dirlen:]
    00047         for f in files:
    00048             ext = os.path.splitext(f)[1]
    00049             if ext == ".py": # extensions that can enter the zip file
    00050                 filename = os.path.join(arcdir, f)
    00051                 all_files.add(filename)
    00052                 if filename not in infos:
    00053                     action = "A"
    00054                     added.append(filename)
    00055                 else:
    00056                     filetime = time.localtime(os.stat(os.path.join(directory,filename))[stat.ST_MTIME])[:6]
    00057                     if filetime > infos[filename]:
    00058                         action = "M"
    00059                         modified.append(filename)
    00060                     else:
    00061                         action = "U"
    00062                         untouched.append(filename)
    00063                 if action in ['U']:
    00064                     log.debug(" %s -> %s", action, filename)
    00065                 else:
    00066                     log.info(" %s -> %s", action, filename)
    00067             elif ext not in [".pyc", ".pyo", ".stamp", ".cmtref"]: # extensions that can be ignored
    00068                 raise ZipdirError("Cannot add '%s' to the zip file, only '.py' are allowed." % os.path.join(arcdir, f))
    00069     # check for removed files
    00070     for filename in infos:
    00071         if filename not in all_files:
    00072             removed.append(filename)
    00073             log.info(" %s -> %s", "R", filename)
    00074     return (added, modified, untouched, removed)

def ZipPythonDir::main | ( |  | argv = `None` | ) |

Main function of the script.

Parse arguments and call zipdir() for each directory passed as argument

Definition at line 130 of file ZipPythonDir.py.

    00131                      :
    00132     from optparse import OptionParser
    00133     parser = OptionParser(usage = "%prog [options] directory1 [directory2 ...]")
    00134     parser.add_option("--no-pyc", action = "store_true",
    00135                       help = "copy the .py files without pre-compiling them")
    00136     parser.add_option("--quiet", action = "store_true",
    00137                       help = "do not print info messages")
    00138     parser.add_option("--debug", action = "store_true",
    00139                       help = "print debug messages (has priority over --quiet)")
    00140
    00141     if argv is None:
    00142         argv = sys.argv
    00143     opts, args = parser.parse_args(argv[1:])
    00144
    00145     if not args:
    00146         parser.error("Specify at least one directory to zip")
    00147
    00148     # Initialize the logging module
    00149     level = logging.INFO
    00150     if opts.quiet:
    00151         level = logging.WARNING
    00152     if opts.debug:
    00153         level = logging.DEBUG
    00154     logging.basicConfig(level = level)
    00155
    00156     if "GAUDI_BUILD_LOCK" in os.environ:
    00157         _scopedLock = locker.LockFile(os.environ["GAUDI_BUILD_LOCK"], temporary =  True)
    00158     # zip all the directories passed as arguments
    00159     for d in args:
    00160         zipdir(d, opts.no_pyc)

def ZipPythonDir::zipdir | ( |  | directory,
|  |  | no_pyc = `False` |
| ) |  |  |

Make a zip file out of a directory containing python modules.

Definition at line 76 of file ZipPythonDir.py.

    00077                                      :
    00078     log = logging.getLogger("zipdir")
    00079     if not os.path.isdir(directory):
    00080         raise OSError(20, "Not a directory", directory)
    00081     msg = "Zipping directory '%s'"
    00082     if no_pyc:
    00083         msg += " (without pre-compilation)"
    00084     log.info(msg, directory)
    00085     filename = os.path.realpath(directory + ".zip")
    00086
    00087     # Open the file in read an update mode
    00088     if os.path.exists(filename):
    00089         zipFile = open(filename, "r+b")
    00090     else:
    00091         # If the file does not exist, we need to create it.
    00092         # "append mode" ensures that, in case of two processes trying to
    00093         # create the file, they do not truncate each other file
    00094         zipFile = open(filename, "ab")
    00095
    00096     locker.lock(zipFile)
    00097     try:
    00098         if zipfile.is_zipfile(filename):
    00099             infolist = zipfile.ZipFile(filename).infolist()
    00100         else:
    00101             infolist = []
    00102         (added, modified, untouched, removed) = _zipChanges(directory, infolist)
    00103         if added or modified or removed:
    00104             tempBuf = StringIO()
    00105             z = zipfile.PyZipFile(tempBuf, "w", zipfile.ZIP_DEFLATED)
    00106             for f in added + modified + untouched:
    00107                 src = os.path.join(directory, f)
    00108                 if no_pyc:
    00109                     log.debug("adding '%s'", f)
    00110                     z.write(src, f)
    00111                 else:
    00112                     # Remove the .pyc file to always force a re-compilation
    00113                     if os.path.exists(src + 'c'):
    00114                         log.debug("removing old .pyc for '%s'", f)
    00115                         os.remove(src + 'c')
    00116                     log.debug("adding '%s'", f)
    00117                     z.writepy(src, os.path.dirname(f))
    00118             z.close()
    00119             zipFile.seek(0)
    00120             zipFile.write(tempBuf.getvalue())
    00121             zipFile.truncate()
    00122             log.info("File '%s' closed", filename)
    00123         else:
    00124             log.info("Nothing to do on '%s'", filename)
    00125     finally:
    00126         locker.unlock(zipFile)
    00127         zipFile.close()

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines