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 | Variables
install Namespace Reference

Classes

class  LogFile
 

Functions

def main
 
def filename_match
 
def expand_source_dir
 
def remove
 
def getCommonPath
 
def getRelativePath
 
def update
 
def install
 
def uninstall
 

Variables

string _version "$Id: install.py,v 1.15 2008/10/28 17:24:39 marcocle Exp $"
 

Function Documentation

def install.expand_source_dir (   source,
  destination,
  exclusions = [],
  destname = None,
  logdir = realpath(".") 
)
Generate the list of copies. 

Definition at line 125 of file install.py.

126  destname = None, logdir = realpath(".")):
127  """
128  Generate the list of copies.
129  """
130  expansion = {}
131  src_path,src_name = split(source)
132  if destname:
133  to_replace = source
134  replacement = join(destination,destname)
135  else:
136  to_replace = src_path
137  replacement = destination
138 
139  for dirname, dirs, files in walk(source):
140  if to_replace:
141  dest_path=dirname.replace(to_replace,replacement)
142  else:
143  dest_path=join(destination,dirname)
144  # remove excluded dirs from the list
145  dirs[:] = [ d for d in dirs if not filename_match(d,exclusions) ]
146  # loop over files
147  for f in files:
148  if filename_match(f,exclusions): continue
149  key = getRelativePath(dest_path, join(dirname,f))
150  value = getRelativePath(logdir, join(dest_path,f))
151  expansion[key] = value
152  return expansion
def install.filename_match (   name,
  patterns,
  default = False 
)
Check if the name is matched by any of the patterns in exclusions.

Definition at line 115 of file install.py.

116 def filename_match(name,patterns,default=False):
117  """
118  Check if the name is matched by any of the patterns in exclusions.
119  """
120  for x in patterns:
121  if fnmatch(name,x):
122  return True
123  return default
def install.getCommonPath (   dirname,
  filename 
)

Definition at line 176 of file install.py.

177 def getCommonPath(dirname, filename):
178  # if the 2 components are on different drives (windows)
179  if splitdrive(dirname)[0] != splitdrive(filename)[0]:
180  return None
181  dirl = dirname.split(sep)
182  filel = filename.split(sep)
183  commpth = []
184  for d, f in itertools.izip(dirl, filel):
185  if d == f :
186  commpth.append(d)
187  else :
188  break
189  commpth = sep.join(commpth)
190  if not commpth:
191  commpth = sep
192  elif commpth[-1] != sep:
193  commpth += sep
194  return commpth
def install.getRelativePath (   dirname,
  filename 
)
calculate the relative path of filename with regards to dirname 

Definition at line 195 of file install.py.

196 def getRelativePath(dirname, filename):
197  """ calculate the relative path of filename with regards to dirname """
198  # Translate the filename to the realpath of the parent directory + basename
199  filepath,basename = os.path.split(filename)
200  filename = os.path.join(os.path.realpath(filepath),basename)
201  # Get the absolute pathof the destination directory
202  dirname = os.path.realpath(dirname)
203  commonpath = getCommonPath(dirname, filename)
204  # for windows if the 2 components are on different drives
205  if not commonpath:
206  return filename
207  relname = filename[len(commonpath):]
208  reldir = dirname[len(commonpath):]
209  if reldir:
210  relname = (os.path.pardir+os.path.sep)*len(reldir.split(os.path.sep)) \
211  + relname
212  return relname
def install.install (   sources,
  destination,
  logfile,
  exclusions = [],
  destname = None,
  syml = False,
  logdir = realpath(".") 
)
Copy sources to destination keeping track of what has been done in logfile.
The destination must be a directory and sources are copied into it.
If exclusions is not empty, the files matching one of its elements are not
copied.

Definition at line 247 of file install.py.

248  destname = None, syml = False, logdir = realpath(".")):
249  """
250  Copy sources to destination keeping track of what has been done in logfile.
251  The destination must be a directory and sources are copied into it.
252  If exclusions is not empty, the files matching one of its elements are not
253  copied.
254  """
255  for s in sources:
256  src_path, src_name = split(s)
257  if not exists(s):
258  continue # silently ignore missing sources
259  elif not isdir(s): # copy the file, without logging (?)
260  if destname is None:
261  dest = join(destination,src_name)
262  else:
263  dest = join(destination,destname)
264  src = getRelativePath(destination,s)
265  dest = getRelativePath(logdir,dest)
266  old_dest = logfile.get_dest(src)
267  update(src,dest,old_dest,syml,logdir)
268  logfile.set_dest(src,dest) # update log
269  else: # for directories
270  # expand the content of the directory as a dictionary
271  # mapping sources to destinations
272  to_do = expand_source_dir(s,destination,exclusions,destname, logdir)
273  src = getRelativePath(destination,s)
274  last_done = logfile.get_dest(src)
275  if last_done is None: last_done = {}
276  for k in to_do:
277  try:
278  old_dest = last_done[k]
279  del last_done[k]
280  except KeyError:
281  old_dest = None
282  update(k,to_do[k],old_dest,syml,logdir)
283  # remove files that were copied but are not anymore in the list
284  for old_dest in last_done.values():
285  remove(old_dest,logdir)
286  logfile.set_dest(src,to_do) # update log
def install.main ( )

Definition at line 27 of file install.py.

27 
28 def main():
29  from optparse import OptionParser
30  parser = OptionParser()
31  parser.add_option("-x","--exclude",action="append",
32  metavar="PATTERN", default = [],
33  dest="exclusions", help="which files/directories to avoid to install")
34  parser.add_option("-l","--log",action="store",
35  dest="logfile", default="install.log",
36  help="where to store the informations about installed files [default: %default]")
37  parser.add_option("-d","--destname",action="store",
38  dest="destname", default=None,
39  help="name to use when installing the source into the destination directory [default: source name]")
40  parser.add_option("-u","--uninstall",action="store_true",
41  dest="uninstall", default=False,
42  help="do uninstall")
43  parser.add_option("-s","--symlink",action="store_true",
44  dest="symlink", default=False,
45  help="create symlinks instead of copy")
46  #parser.add_option("-p","--permission",action="store",
47  # metavar="PERM",
48  # dest="permission",
49  # help="modify the permission of the destination file (see 'man chown'). Unix only.")
50  (opts,args) = parser.parse_args()
51 
52  # options consistency check
53  if opts.uninstall:
54  if opts.exclusions:
55  parser.error("Exclusion list does not make sense for uninstall")
56  opts.destination = args
57  try:
58  log = load(open(opts.logfile,"rb"))
59  except:
60  log = LogFile()
61  uninstall(log,opts.destination,realpath(dirname(opts.logfile)))
62  if log:
63  dump(log,open(opts.logfile,"wb"))
64  else:
65  from os import remove
66  try:
67  remove(opts.logfile)
68  except OSError, x:
69  if x.errno != 2 : raise
70  else : # install mode
71  if len(args) < 2:
72  parser.error("Specify at least one source and (only) one destination")
73  opts.destination = args[-1]
74  opts.sources = args[:-1]
75  try:
76  log = load(open(opts.logfile,"rb"))
77  except:
78  log = LogFile()
79  if opts.symlink :
80  if len(opts.sources) != 1:
81  parser.error("no more that 2 args with --symlink")
82  opts.destination, opts.destname = split(opts.destination)
83  install(opts.sources,opts.destination,
84  log,opts.exclusions,opts.destname,
85  opts.symlink, realpath(dirname(opts.logfile)))
86  dump(log,open(opts.logfile,"wb"))
def install.remove (   file,
  logdir 
)

Definition at line 153 of file install.py.

154 def remove(file, logdir):
155  file = normpath(join(logdir, file))
156  try:
157  print "Remove '%s'"%file
158  os.remove(file)
159  # For python files, remove the compiled versions too
160  if splitext(file)[-1] == ".py":
161  for c in ['c', 'o']:
162  if exists(file + c):
163  print "Remove '%s'" % (file+c)
164  os.remove(file+c)
165  file_path = split(file)[0]
166  while file_path and (len(listdir(file_path)) == 0):
167  print "Remove empty dir '%s'"%file_path
168  rmdir(file_path)
169  file_path = split(file_path)[0]
170  except OSError, x: # ignore file-not-found errors
171  if x.errno in [2, 13] :
172  print "Previous removal ignored"
173  else:
174  raise
175 
def install.uninstall (   logfile,
  destinations = [],
  logdir = realpath(".") 
)
Remove copied files using logfile to know what to remove.
If destinations is not empty, only the files/directories specified are
removed.

Definition at line 287 of file install.py.

288 def uninstall(logfile, destinations = [], logdir=realpath(".")):
289  """
290  Remove copied files using logfile to know what to remove.
291  If destinations is not empty, only the files/directories specified are
292  removed.
293  """
294  for s in logfile.get_sources():
295  dest = logfile.get_dest(s)
296  if type(dest) is str:
297  if filename_match(dest,destinations,default=True):
298  remove(dest, logdir)
299  logfile.remove(s)
300  else:
301  for subs in dest.keys():
302  subdest = dest[subs]
303  if filename_match(subdest,destinations,default=True):
304  remove(subdest,logdir)
305  del dest[subs]
306  if not dest:
307  logfile.remove(s)
def install.update (   src,
  dest,
  old_dest = None,
  syml = False,
  logdir = realpath(".") 
)

Definition at line 213 of file install.py.

214 def update(src,dest,old_dest = None, syml = False, logdir = realpath(".")):
215  realdest = normpath(join(logdir, dest))
216  dest_path = split(realdest)[0]
217  realsrc = normpath(join(dest_path,src))
218  # The modification time is compared only with the precision of the second
219  # to avoid a bug in Python 2.5 + Win32 (Fixed in Python 2.5.1).
220  # See:
221  # http://bugs.python.org/issue1671965
222  # http://bugs.python.org/issue1565150
223  if (not exists(realdest)) or (int(getmtime(realsrc)) > int(getmtime(realdest))):
224  if not isdir(dest_path):
225  print "Create dir '%s'"%(dest_path)
226  makedirs(dest_path)
227  # the destination file is missing or older than the source
228  if syml and sys.platform != "win32" :
229  if exists(realdest):
230  remove(realdest,logdir)
231  print "Create Link to '%s' in '%s'"%(src,dest_path)
232  os.symlink(src,realdest)
233  else:
234  print "Copy '%s' -> '%s'"%(src, realdest)
235  if exists(realdest):
236  # If the destination path exists it is better to remove it before
237  # doing the copy (shutil.copystat fails if the destination file
238  # is not owned by the current user).
239  os.remove(realdest)
240  shutil.copy2(realsrc, realdest) # do the copy (cp -p src dest)
241 
242  #if old_dest != dest: # the file was installed somewhere else
243  # # remove the old destination
244  # if old_dest is not None:
245  # remove(old_dest,logdir)

Variable Documentation

string install._version "$Id: install.py,v 1.15 2008/10/28 17:24:39 marcocle Exp $"

Definition at line 17 of file install.py.


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