PSCF v1.1
file.py
1'''! Utilities for manipulating files and paths. '''
2
3import os
4from os.path import *
5from string import *
6from glob import glob
7
8##
9# Generates relative path of path2 relative to path1.
10#
11# The paths path1 and path2 must be either absolute paths or relative
12# paths defined relative to the same directory.
13#
14# \param path1 reference path
15# \param path2 path of interest
16# \return Path for file2 relative to directory containing path1
17#
18def relative_path(path1,path2):
19 root = commonprefix([path1, path2])
20 root = dirname(root)
21 if (root == '/'):
22 raise 'Error in relative_path - common directory cannot be / '
23 if root :
24 path2 = path2[len(root)+1:]
25 path1 = dirname(path1)
26 while not ( root == path1 ):
27 path2 = pardir + sep + path2
28 path1 = dirname(path1)
29 return path2
30
31
32
42def chdirs(dir):
43 if not exists(dir):
44 print('Creating directory ' + dir)
45 os.makedirs(dir)
46 os.chdir(dir)
47
48
49
57def open_w(path):
58 dir = dirname(path)
59 if dir:
60 if not exists(dir):
61 print('Creating directory ' + dir)
62 os.makedirs(dir)
63 return open(path,'w')
64
65
73def rm(path):
74 if os.path.exists(path):
75 if os.path.isfile(path):
76 os.remove(path)
77
78
88def mv(old_path, new_path):
89 if (not isfile(old_path)) and (not isdir(old_path) ):
90 print('Path ' + old_path + ' is not a file or directory')
91 return
92 new_dir = dirname(new_path)
93 if new_dir:
94 if not exists(new_dir):
95 print('Creating directory ' + new_dir)
96 os.makedirs(new_dir)
97 return os.rename(old_path, new_path)
98
99
100
103class File:
104
105
110 def __init__(self, path=None, scan=1):
111 self.path = path
112 self.scan = scan
113 if self.path and scan:
114 self.mtime = getmtime(path)
115 self.size = getsize(path)
116
117
120 def __str__(self):
121 return self.path
122
123
126 def __repr__(self):
127 if scan:
128 return '%-40s size= %10d mtime= %10d' \
129 % (self.path, self.size, self.mtime)
130 else:
131 return self.path
132
133 def xml(self,indent=''):
134 if scan:
135 return indent + '<File path ="' + self.path + '"' \
136 + ' size ="' + str(self.size) + '"' \
137 + ' mtime="' + str(self.mtime) + '" />\n'
138 else:
139 return indent + '<File path ="' + self.path + '" />\n'
140
141
146 def open(self, mode):
147 return open(self.path, mode)
148
149
154 def write(self, filename):
155 file = open(filename, 'w')
156 file.write(self.xml())
157 file.close()
158
159
166 def __eq__(self,other):
167 if self.path != other.path : return 0
168 if self.size != other.size : return 0
169 # if self.mtime != other.mtime : return 0
170 return 1
171
172
179 def __ne__(self,other):
180 if self == other :
181 return 0
182 return 1
183
184
185
191
192 def __init__(self, path = None, scan = 1):
193 self.pathpath = path
194 self.files = {}
195 self.dirs = {}
196 if self.pathpath and scan:
197 for name in os.listdir(self.pathpath):
198 if self.pathpath == '.':
199 path = name
200 else:
201 path = self.pathpath + os.sep + name
202 if isfile(path):
203 self.files[path] = File(path)
204 if isdir(path):
205 self.dirs[path] = Directory(path)
206
207 def scan(self):
208 for name in os.listdir(self.pathpath):
209 if self.pathpath == '.' :
210 path = name
211 else:
212 path = self.pathpath + os.sep + name
213 if isfile(path):
214 self.files[path] = File(path)
215 if isdir(path):
216 self.dirs[path] = Directory(path)
217
218
221 def clear(self):
222 self.files = {}
223 self.dirs = {}
224
225
231 def filenames(self, pattern = '*', recursive = 1):
232 #r = []
233 r = glob( self.pathpath + '/' + pattern)
234 if recursive:
235 for x in self.dirs.keys():
236 r.extend(self.dirs[x].filenames(pattern))
237 return r
238
239
242 def __repr__(self):
243 r = []
244 r.append( self.pathpath + '\n' )
245 for x in self.files.keys() :
246 r.append( str(self.files[x]) + '\n' )
247 for x in self.dirs.keys():
248 r.append( repr(self.dirs[x]) )
249 return join(r,'')
250
251
254 def __str__(self):
255 r = []
256 r.append(self.pathpath + '\n' )
257 for x in self.files.keys() :
258 r.append( str(self.files[x]) + '\n' )
259 for x in self.dirs.keys() :
260 r.append( str(self.dirs[x]) )
261 return join(r,'')
262
263
266 def xml(self,indent=''):
267 r = []
268 r.append( indent + '<Directory path="'+ self.pathpath + '" >\n' )
269 n_indent = indent + ' '
270 for x in self.files.keys():
271 r.append( self.files[x].xml(n_indent) )
272 for x in self.dirs.keys() :
273 r.append( self.dirs[x].xml(n_indent) )
274 r.append( indent + '</Directory>'+'\n')
275 return join(r,'')
276
277
282 def write(self,filename):
283 file = open(filename,'w')
284 file.write( self.xmlxml() )
285 file.close()
286
287
290 def ls(self):
291 for x in self.files.keys() :
292 print(x)
293 for x in self.dirs.keys() :
294 print(x + os.sep)
295
296
301 def __eq__(self,other) :
302 # Check files
303 for x in self.files.keys():
304 if x in other.files.keys():
305 if self.files[x] != other.files[x]:
306 return 0
307 else:
308 return 0
309 for x in other.files.keys():
310 if not x in self.files.keys(): return 0
311 # Check directories
312 for x in self.dirs.keys():
313 if x in other.dirs.keys():
314 if self.dirs[x] != other.dirs[x]:
315 return 0
316 else:
317 return 0
318 for x in other.dirs.keys():
319 if not x in self.dirs.keys(): return 0
320 # Return true if all tests passed
321 return 1
322
323
328 def __ne__(self,other) :
329 if self == other :
330 return 0
331 return 1
332
333
338 def diff(self,other) :
339 r = []
340 # Check files
341 for x in self.files.keys():
342 if x in other.files.keys():
343 if self.files[x] != other.files[x]:
344 r.append( '> ' + self.files[x].xml() )
345 r.append( '< ' + other.files[x].xml() + '\n')
346 else:
347 r.append('> ' + self.files[x].xml() + '\n' )
348 for x in other.files.keys():
349 if not x in self.files.keys():
350 r.append('< ' + other.files[x].xml() + '\n' )
351 # Check directories
352 for x in self.dirs.keys():
353 if x in other.dirs.keys():
354 r.append( self.dirs[x].diff(other.dirs[x]) )
355 else:
356 r.append('> ' + self.dirs[x].path + '\n' )
357 for x in other.dirs.keys():
358 if not x in self.dirs.keys():
359 r.append('< ' + other.dirs[x].path + '\n' )
360 # Return true if all tests passed
361 return join(r,'')
362
Class that represents a directory.
Definition: file.py:190
def write(self, filename)
Write XML representation to file.
Definition: file.py:282
def __ne__(self, other)
Test for inequality of two Directory objects.
Definition: file.py:328
def clear(self)
Clear all data for this Directory.
Definition: file.py:221
def __str__(self)
String representation of a directory.
Definition: file.py:254
def ls(self)
Print constituent file and subdirectories.
Definition: file.py:290
def filenames(self, pattern=' *', recursive=1)
Find files in directory that match a pattern.
Definition: file.py:231
def diff(self, other)
Return string reporting difference between Directory objects.
Definition: file.py:338
def xml(self, indent='')
XML string representation of a directory.
Definition: file.py:266
def __repr__(self)
String representation of a directory.
Definition: file.py:242
def __eq__(self, other)
Test for equality of two Directory objects.
Definition: file.py:301
def __init__(self, path=None, scan=1)
Constructor.
Definition: file.py:192
Class that contains metadata for a file.
Definition: file.py:103
def open(self, mode)
Open this file in specified mode.
Definition: file.py:146
def xml(self, indent='')
Definition: file.py:133
def __ne__(self, other)
Test for inequality of files.
Definition: file.py:179
def __repr__(self)
String representation of file data.
Definition: file.py:126
def __eq__(self, other)
Test for equality of files.
Definition: file.py:166
def write(self, filename)
Write XML representation to a file.
Definition: file.py:154
def __init__(self, path=None, scan=1)
Constructor.
Definition: file.py:110
def __str__(self)
String representation of file data.
Definition: file.py:120
def mv(old_path, new_path)
Rename a file or directory, creating any necessary parent directories.
Definition: file.py:88
def chdirs(dir)
Change current working directory and create dir if necessary.
Definition: file.py:42
def relative_path(path1, path2)
Generates relative path of path2 relative to path1.
Definition: file.py:18
def rm(path)
Remove a file if possible (if the path exists and it is a file).
Definition: file.py:73
def open_w(path)
Open file with specified path for writing, return file object.
Definition: file.py:57