All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 filename_match
Definition: install.py:115
def getRelativePath
Definition: install.py:195
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 filename_match
Definition: install.py:115
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 getCommonPath
Definition: install.py:176
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 getRelativePath
Definition: install.py:195
def getCommonPath
Definition: install.py:176
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 250 of file install.py.

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

291 def uninstall(logfile, destinations = [], logdir=realpath(".")):
292  """
293  Remove copied files using logfile to know what to remove.
294  If destinations is not empty, only the files/directories specified are
295  removed.
296  """
297  for s in logfile.get_sources():
298  dest = logfile.get_dest(s)
299  if type(dest) is str:
300  if filename_match(dest,destinations,default=True):
301  remove(dest, logdir)
302  logfile.remove(s)
303  else:
304  for subs in dest.keys():
305  subdest = dest[subs]
306  if filename_match(subdest,destinations,default=True):
307  remove(subdest,logdir)
308  del dest[subs]
309  if not dest:
310  logfile.remove(s)
def filename_match
Definition: install.py:115
string type
Definition: gaudirun.py:126
def remove
Definition: install.py:153
def uninstall
Definition: install.py:290
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  if sys.platform != "darwin":
241  shutil.copy2(realsrc, realdest) # do the copy (cp -p src dest)
242  else:
243  shutil.copy(realsrc, realdest) # do the copy (cp src dest)
244 
245  #if old_dest != dest: # the file was installed somewhere else
246  # # remove the old destination
247  # if old_dest is not None:
248  # remove(old_dest,logdir)
def remove
Definition: install.py:153
def update
Definition: install.py:213

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.