1
2
3
4
5
6
7 """
8 Helper functions for operations on files.
9 """
10
11 import os.path, stat
12
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
18 "expandfilename() and additionally: strip leading and trailing whitespaces and expand symbolic links"
19 return os.path.realpath(expandfilename(path.strip()))
20
22 "returns the previous cnt-th directory"
23 for i in range(cnt):
24 path = os.path.dirname(path)
25 return path
26
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
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
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
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
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
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
101 if not p or p == '.': continue
102 p = os.path.realpath(os.path.abspath(p))
103
104 idx = fn.find(p)
105 if idx != -1:
106
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
125 os.mknod('file1')
126 recursive_copy('file1',destdir)
127 assert os.path.isfile(destdir+'/file1')
128
129
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
136
137 os.mknod('src/file3')
138 recursive_copy('src/file3',destdir)
139 assert os.path.isfile(destdir+'/src/file3')
140
141
142 os.mknod('file4')
143 recursive_copy(os.getcwd()+'/file4',destdir)
144 assert os.path.isfile(destdir+'/file4')
145
146
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
153
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
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