Gaudi Framework, version v22r0

Home   Generated: 9 Feb 2011

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 124 of file install.py.

00126                                                               :
00127     """
00128     Generate the list of copies. 
00129     """
00130     expansion = {}
00131     src_path,src_name = split(source)
00132     if destname:
00133         to_replace = source
00134         replacement = join(destination,destname)
00135     else:
00136         to_replace = src_path
00137         replacement = destination
00138     
00139     for dirname, dirs, files in walk(source):
00140         if to_replace:
00141             dest_path=dirname.replace(to_replace,replacement)
00142         else:
00143             dest_path=join(destination,dirname)
00144         # remove excluded dirs from the list
00145         dirs[:] = [ d for d in dirs if not filename_match(d,exclusions) ]
00146         # loop over files
00147         for f in files:
00148             if filename_match(f,exclusions): continue
00149             key = getRelativePath(dest_path, join(dirname,f))
00150             value = getRelativePath(logdir, join(dest_path,f))
00151             expansion[key] = value
00152     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.

00116                                                :
00117     """
00118     Check if the name is matched by any of the patterns in exclusions.
00119     """
00120     for x in patterns:
00121         if fnmatch(name,x):
00122             return True
00123     return default

def install::getCommonPath (   dirname,
  filename 
)

Definition at line 176 of file install.py.

00177                                     :
00178     # if the 2 components are on different drives (windows)
00179     if splitdrive(dirname)[0] != splitdrive(filename)[0]:
00180         return None
00181     dirl = dirname.split(sep)
00182     filel = filename.split(sep)
00183     commpth = []
00184     for d, f in itertools.izip(dirl, filel):
00185         if d == f :
00186             commpth.append(d)
00187         else :
00188             break
00189     commpth = sep.join(commpth)
00190     if not commpth:
00191         commpth = sep
00192     elif commpth[-1] != sep:
00193         commpth += sep
00194     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.

00196                                       :
00197     """ calculate the relative path of filename with regards to dirname """
00198     # Translate the filename to the realpath of the parent directory + basename
00199     filepath,basename = os.path.split(filename)
00200     filename = os.path.join(os.path.realpath(filepath),basename)
00201     # Get the absolute pathof the destination directory
00202     dirname = os.path.realpath(dirname)
00203     commonpath = getCommonPath(dirname, filename)
00204     # for windows if the 2 components are on different drives
00205     if not commonpath:
00206         return filename
00207     relname = filename[len(commonpath):]
00208     reldir = dirname[len(commonpath):]
00209     if reldir:
00210         relname = (os.path.pardir+os.path.sep)*len(reldir.split(os.path.sep)) \
00211              + relname
00212     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 246 of file install.py.

00248                                                                   :
00249     """
00250     Copy sources to destination keeping track of what has been done in logfile.
00251     The destination must be a directory and sources are copied into it.
00252     If exclusions is not empty, the files matching one of its elements are not
00253     copied.
00254     """
00255     for s in sources:
00256         src_path, src_name = split(s)
00257         if not exists(s):
00258             continue # silently ignore missing sources
00259         elif not isdir(s): # copy the file, without logging (?)
00260             if destname is None:
00261                 dest = join(destination,src_name)
00262             else:
00263                 dest = join(destination,destname)
00264             src = getRelativePath(destination,s)
00265             dest = getRelativePath(logdir,dest)
00266             old_dest = logfile.get_dest(src)
00267             update(src,dest,old_dest,syml,logdir)
00268             logfile.set_dest(src,dest) # update log
00269         else: # for directories
00270             # expand the content of the directory as a dictionary
00271             # mapping sources to destinations
00272             to_do = expand_source_dir(s,destination,exclusions,destname, logdir)
00273             src = getRelativePath(destination,s)
00274             last_done = logfile.get_dest(src)
00275             if last_done is None: last_done = {}
00276             for k in to_do:
00277                 try:
00278                     old_dest = last_done[k]
00279                     del last_done[k]
00280                 except KeyError:
00281                     old_dest = None  
00282                 update(k,to_do[k],old_dest,syml,logdir)
00283             # remove files that were copied but are not anymore in the list 
00284             for old_dest in last_done.values():
00285                 remove(old_dest,logdir)
00286             logfile.set_dest(src,to_do) # update log

def install::main (  ) 

Definition at line 27 of file install.py.

00028           :
00029     from optparse import OptionParser
00030     parser = OptionParser()
00031     parser.add_option("-x","--exclude",action="append",
00032                       metavar="PATTERN", default = [],
00033                       dest="exclusions", help="which files/directories to avoid to install")
00034     parser.add_option("-l","--log",action="store",
00035                       dest="logfile", default="install.log",
00036                       help="where to store the informations about installed files [default: %default]")
00037     parser.add_option("-d","--destname",action="store",
00038                       dest="destname", default=None,
00039                       help="name to use when installing the source into the destination directory [default: source name]")
00040     parser.add_option("-u","--uninstall",action="store_true",
00041                       dest="uninstall", default=False,
00042                       help="do uninstall")
00043     parser.add_option("-s","--symlink",action="store_true",
00044                       dest="symlink", default=False,
00045                       help="create symlinks instead of copy")
00046     #parser.add_option("-p","--permission",action="store",
00047     #                  metavar="PERM",
00048     #                  dest="permission",
00049     #                  help="modify the permission of the destination file (see 'man chown'). Unix only.")
00050     (opts,args) = parser.parse_args()
00051     
00052     # options consistency check
00053     if opts.uninstall:
00054         if opts.exclusions:
00055             parser.error("Exclusion list does not make sense for uninstall")
00056         opts.destination = args
00057         try:
00058             log = load(open(opts.logfile,"rb"))
00059         except:
00060             log = LogFile() 
00061         uninstall(log,opts.destination,realpath(dirname(opts.logfile)))
00062         if log:
00063             dump(log,open(opts.logfile,"wb"))
00064         else:
00065             from os import remove
00066             try:
00067                 remove(opts.logfile)
00068             except OSError, x:
00069                 if x.errno != 2 : raise
00070     else : # install mode
00071         if len(args) < 2:
00072             parser.error("Specify at least one source and (only) one destination")
00073         opts.destination = args[-1]
00074         opts.sources = args[:-1]
00075         try:
00076             log = load(open(opts.logfile,"rb"))
00077         except:
00078             log = LogFile() 
00079         if opts.symlink :
00080             if len(opts.sources) != 1:
00081                 parser.error("no more that 2 args with --symlink")
00082             opts.destination, opts.destname = split(opts.destination) 
00083         install(opts.sources,opts.destination,
00084                 log,opts.exclusions,opts.destname, 
00085                 opts.symlink, realpath(dirname(opts.logfile)))
00086         dump(log,open(opts.logfile,"wb"))

def install::remove (   file,
  logdir 
)

Definition at line 153 of file install.py.

00154                         :
00155     file = normpath(join(logdir, file))
00156     try:
00157         print "Remove '%s'"%file
00158         os.remove(file)
00159         # For python files, remove the compiled versions too 
00160         if splitext(file)[-1] == ".py":
00161             for c in ['c', 'o']:
00162                 if exists(file + c):
00163                     print "Remove '%s'" % (file+c)
00164                     os.remove(file+c)
00165         file_path = split(file)[0]
00166         while file_path and (len(listdir(file_path)) == 0):
00167             print "Remove empty dir '%s'"%file_path
00168             rmdir(file_path)
00169             file_path = split(file_path)[0]
00170     except OSError, x: # ignore file-not-found errors
00171         if x.errno in [2, 13] :
00172             print "Previous removal ignored"
00173         else: 
00174             raise
00175         

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.

00288                                                                :
00289     """
00290     Remove copied files using logfile to know what to remove.
00291     If destinations is not empty, only the files/directories specified are
00292     removed.
00293     """
00294     for s in logfile.get_sources():
00295         dest = logfile.get_dest(s)
00296         if type(dest) is str:
00297             if filename_match(dest,destinations,default=True):
00298                 remove(dest, logdir)
00299                 logfile.remove(s)
00300         else:
00301             for subs in dest.keys():
00302                 subdest = dest[subs]
00303                 if filename_match(subdest,destinations,default=True):
00304                     remove(subdest,logdir)
00305                     del dest[subs]
00306             if not dest:
00307                 logfile.remove(s)
            

def install::update (   src,
  dest,
  old_dest = None,
  syml = False,
  logdir = realpath(".") 
)

Definition at line 213 of file install.py.

00214                                                                           :
00215     realdest = normpath(join(logdir, dest))
00216     dest_path = split(realdest)[0]
00217     realsrc = normpath(join(dest_path,src))
00218     # The modification time is compared only with the precision of the second
00219     # to avoid a bug in Python 2.5 + Win32 (Fixed in Python 2.5.1).
00220     # See:
00221     #   http://bugs.python.org/issue1671965
00222     #   http://bugs.python.org/issue1565150
00223     if (not exists(realdest)) or (int(getmtime(realsrc)) > int(getmtime(realdest))):
00224         if not isdir(dest_path):
00225             print "Create dir '%s'"%(dest_path)
00226             makedirs(dest_path)
00227         # the destination file is missing or older than the source
00228         if syml and sys.platform != "win32" :
00229             if exists(realdest):
00230                 remove(realdest,logdir)
00231             print "Create Link to '%s' in '%s'"%(src,dest_path)
00232             os.symlink(src,realdest)
00233         else:
00234             print "Copy '%s' -> '%s'"%(src, realdest)
00235             if exists(realdest):
00236                 # If the destination path exists it is better to remove it before
00237                 # doing the copy (shutil.copystat fails if the destination file
00238                 # is not owned by the current user).
00239                 os.remove(realdest)
00240             shutil.copy2(realsrc, realdest) # do the copy (cp -p src dest)
00241             
00242     #if old_dest != dest: # the file was installed somewhere else
00243     #    # remove the old destination
00244     #    if old_dest is not None:
00245     #        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.

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

Generated at Wed Feb 9 16:33:45 2011 for Gaudi Framework, version v22r0 by Doxygen version 1.6.2 written by Dimitri van Heesch, © 1997-2004