Package Ganga :: Package Utility :: Module files
[hide private]
[frames] | no frames]

Source Code for Module Ganga.Utility.files

  1  ################################################################################ 
  2  # Ganga Project. http://cern.ch/ganga 
  3  # 
  4  # $Id: files.py,v 1.1 2008-07-17 16:41:00 moscicki Exp $ 
  5  ################################################################################ 
  6   
  7  """ 
  8  Helper functions for operations on files. 
  9  """ 
 10   
 11  import os.path, stat 
 12   
13 -def expandfilename(filename):
14 "expand a path or filename in a standard way so that it may contain ~ and ${VAR} strings" 15 return os.path.expandvars(os.path.expanduser(filename))
16
17 -def fullpath(path):
18 "expandfilename() and additionally: strip leading and trailing whitespaces and expand symbolic links" 19 return os.path.realpath(expandfilename(path.strip()))
20
21 -def previous_dir(path,cnt):
22 "returns the previous cnt-th directory" 23 for i in range(cnt): 24 path = os.path.dirname(path) 25 return path
26
27 -def chmod_executable(path):
28 "make a file executable by the current user (u+x)" 29 import stat 30 os.chmod(path,stat.S_IXUSR|os.stat(path).st_mode)
31
32 -def is_executable(path):
33 "check if the file is executable by the current user (u+x)" 34 import stat 35 return os.stat(path)[0] & stat.S_IXUSR
36
37 -def real_basename(x):
38 """ a 'better' basename (removes the trailing / like on Unix) """ 39 import os.path,os 40 x = x.rstrip(os.sep) 41 return os.path.basename(x)
42
43 -def multi_glob(pats,exclude=None):
44 """ glob using a list of patterns and removing duplicate files, exclude name in the list for which the callback exclude(name) return true 45 example: advanced_glob(['*.jpg','*.gif'],exclude=lambda n:len(n)>20) return a list of all JPG and GIF files which have names shorter then 20 characters 46 """ 47 import glob 48 49 unique = {} 50 if exclude is None: 51 def exclude(n): return 0 52 53 for p in pats: 54 for f in glob.glob(p): 55 unique[f] = 1 56 57 return [name for name in unique.keys() if not exclude(name)]
58 59
60 -def recursive_copy(src,dest):
61 """ copy src file (or a directory tree if src specifies a directory) to dest directory. dest must be a directory and must exist. 62 if src is a relative path, then the src directory structure is preserved in dest. 63 """ 64 import shutil, os.path 65 66 if not os.path.isdir(dest): 67 raise ValueError('resursive_copy: destination %s must specify a directory (which exists)'%dest) 68 69 if os.path.isdir(src): 70 destdir = dest 71 srcdir,srcbase = os.path.split(src.rstrip('/')) 72 if not srcdir=='' and not os.path.isabs(src): 73 destdir = os.path.join(destdir,srcdir) 74 if not os.path.isdir(destdir): 75 os.makedirs(destdir) 76 shutil.copytree(src,os.path.join(destdir,srcbase)) 77 else: 78 79 srcdir = os.path.dirname(src.rstrip('/')) 80 if srcdir=='' or os.path.isabs(src): 81 shutil.copy(src,dest) 82 else: 83 destdir = os.path.join(dest,srcdir) 84 if not os.path.isdir(destdir): 85 os.makedirs(destdir) 86 shutil.copy(src,destdir)
87
88 -def remove_prefix(fn,path_list):
89 """Remove the common prefix of fn and the first matching element in the path_list. 90 91 Example: for fn='/a/b/c' and path=['d','/a','/a/b'] return 'b/c' 92 93 If no matching path is found, then fn is returned unchanged. 94 95 This function converts each element of the path_list using realpath.abspath. 96 97 """ 98 import os.path 99 for p in path_list: 100 # normalize path 101 if not p or p == '.': continue 102 p = os.path.realpath(os.path.abspath(p)) 103 #print 'searching path component: %s'%p 104 idx = fn.find(p) 105 if idx != -1: 106 #print 'found "%s" atr index %d in "%s"'%(p,idx,fn) 107 return fn[len(p)+len(os.sep):] 108 109 return fn
110 111 if __name__ == "__main__": 112 print "Testing resursive copy..." 113 import os,shutil 114 from files import * 115 116 workdir = 'test_recursive_copy' 117 shutil.rmtree(workdir,True) 118 os.mkdir(workdir) 119 os.chdir(workdir) 120 121 destdir = 'dest' 122 os.mkdir(destdir) 123 124 # Copy file 125 os.mknod('file1') 126 recursive_copy('file1',destdir) 127 assert os.path.isfile(destdir+'/file1') 128 129 # Copy file in dir 130 os.mkdir('src') 131 os.mknod('src/file2') 132 recursive_copy('src/file2',destdir) 133 assert os.path.isfile(destdir+'/src/file2') 134 135 # Copy file in dir 136 # Check possibility to copy to existing dir 137 os.mknod('src/file3') 138 recursive_copy('src/file3',destdir) 139 assert os.path.isfile(destdir+'/src/file3') 140 141 # Copy file with abs path 142 os.mknod('file4') 143 recursive_copy(os.getcwd()+'/file4',destdir) 144 assert os.path.isfile(destdir+'/file4') 145 146 # Copy subdir 147 os.mkdir('src/subdir') 148 os.mknod('src/subdir/file5') 149 recursive_copy('src/subdir',destdir) 150 assert os.path.isfile(destdir+'/src/subdir/file5') 151 152 # Copy subdir 153 # Check "/" at the end 154 os.mkdir('src/subdir2') 155 os.mknod('src/subdir2/file6') 156 recursive_copy('src/subdir2/',destdir) 157 assert os.path.isfile(destdir+'/src/subdir2/file6') 158 159 # Copy subdir with abs path 160 os.mkdir('src/subdir3') 161 os.mknod('src/subdir3/file7') 162 recursive_copy(os.getcwd()+'/src/subdir3',destdir) 163 assert os.path.isfile(destdir+'/subdir3/file7') 164 165 print "Test OK" 166