Gaudi Framework, version v23r5

Home   Generated: Wed Nov 28 2012
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Classes | Functions
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.

23 
24 def _zipChanges(directory, infolist):
25  # gets the dates of the files in the zip archive
26  infos = {}
27  for i in infolist:
28  fn = i.filename
29  if fn.endswith(".pyc"):
30  fn = fn[:-1]
31  infos[fn] = i.date_time
32 
33  # gets the changes
34  added = []
35  modified = []
36  untouched = []
37  removed = []
38  all_files = set()
39 
40  log = logging.getLogger("zipdir")
41  dirlen = len(directory) + 1
42  for root, dirs, files in os.walk(directory):
43  if "lib-dynload" in dirs:
44  # exclude the directory containing binary modules
45  dirs.remove("lib-dynload")
46  arcdir = root[dirlen:]
47  for f in files:
48  ext = os.path.splitext(f)[1]
49  if ext == ".py": # extensions that can enter the zip file
50  filename = os.path.join(arcdir, f)
51  all_files.add(filename)
52  if filename not in infos:
53  action = "A"
54  added.append(filename)
55  else:
56  filetime = time.localtime(os.stat(os.path.join(directory,filename))[stat.ST_MTIME])[:6]
57  if filetime > infos[filename]:
58  action = "M"
59  modified.append(filename)
60  else:
61  action = "U"
62  untouched.append(filename)
63  if action in ['U']:
64  log.debug(" %s -> %s", action, filename)
65  else:
66  log.info(" %s -> %s", action, filename)
67  elif ext not in [".pyc", ".pyo", ".stamp", ".cmtref"]: # extensions that can be ignored
68  raise ZipdirError("Cannot add '%s' to the zip file, only '.py' are allowed." % os.path.join(arcdir, f))
69  # check for removed files
70  for filename in infos:
71  if filename not in all_files:
72  removed.append(filename)
73  log.info(" %s -> %s", "R", filename)
74  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.

131 def main(argv = None):
132  from optparse import OptionParser
133  parser = OptionParser(usage = "%prog [options] directory1 [directory2 ...]")
134  parser.add_option("--no-pyc", action = "store_true",
135  help = "copy the .py files without pre-compiling them")
136  parser.add_option("--quiet", action = "store_true",
137  help = "do not print info messages")
138  parser.add_option("--debug", action = "store_true",
139  help = "print debug messages (has priority over --quiet)")
140 
141  if argv is None:
142  argv = sys.argv
143  opts, args = parser.parse_args(argv[1:])
144 
145  if not args:
146  parser.error("Specify at least one directory to zip")
147 
148  # Initialize the logging module
149  level = logging.INFO
150  if opts.quiet:
151  level = logging.WARNING
152  if opts.debug:
153  level = logging.DEBUG
154  logging.basicConfig(level = level)
155 
156  if "GAUDI_BUILD_LOCK" in os.environ:
157  _scopedLock = locker.LockFile(os.environ["GAUDI_BUILD_LOCK"], temporary = True)
158  # zip all the directories passed as arguments
159  for d in args:
160  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.

76 
77 def zipdir(directory, no_pyc = False):
78  log = logging.getLogger("zipdir")
79  if not os.path.isdir(directory):
80  raise OSError(20, "Not a directory", directory)
81  msg = "Zipping directory '%s'"
82  if no_pyc:
83  msg += " (without pre-compilation)"
84  log.info(msg, directory)
85  filename = os.path.realpath(directory + ".zip")
86 
87  # Open the file in read an update mode
88  if os.path.exists(filename):
89  zipFile = open(filename, "r+b")
90  else:
91  # If the file does not exist, we need to create it.
92  # "append mode" ensures that, in case of two processes trying to
93  # create the file, they do not truncate each other file
94  zipFile = open(filename, "ab")
95 
96  locker.lock(zipFile)
97  try:
98  if zipfile.is_zipfile(filename):
99  infolist = zipfile.ZipFile(filename).infolist()
100  else:
101  infolist = []
102  (added, modified, untouched, removed) = _zipChanges(directory, infolist)
103  if added or modified or removed:
104  tempBuf = StringIO()
105  z = zipfile.PyZipFile(tempBuf, "w", zipfile.ZIP_DEFLATED)
106  for f in added + modified + untouched:
107  src = os.path.join(directory, f)
108  if no_pyc:
109  log.debug("adding '%s'", f)
110  z.write(src, f)
111  else:
112  # Remove the .pyc file to always force a re-compilation
113  if os.path.exists(src + 'c'):
114  log.debug("removing old .pyc for '%s'", f)
115  os.remove(src + 'c')
116  log.debug("adding '%s'", f)
117  z.writepy(src, os.path.dirname(f))
118  z.close()
119  zipFile.seek(0)
120  zipFile.write(tempBuf.getvalue())
121  zipFile.truncate()
122  log.info("File '%s' closed", filename)
123  else:
124  log.info("Nothing to do on '%s'", filename)
125  finally:
126  locker.unlock(zipFile)
127  zipFile.close()

Generated at Wed Nov 28 2012 12:17:44 for Gaudi Framework, version v23r5 by Doxygen version 1.8.2 written by Dimitri van Heesch, © 1997-2004