1'''! Utilities for manipulating files
and paths.
'''
9# Generates relative path of path2 relative to path1.
19 root = commonprefix([path1, path2])
22 raise 'Error in relative_path - common directory cannot be / '
24 path2 = path2[len(root)+1:]
25 path1 = dirname(path1)
26 while not ( root == path1 ):
27 path2 = pardir + sep + path2
28 path1 = dirname(path1)
44 print(
'Creating directory ' + dir)
61 print(
'Creating directory ' + dir)
74 if os.path.exists(path):
75 if os.path.isfile(path):
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')
92 new_dir = dirname(new_path)
94 if not exists(new_dir):
95 print(
'Creating directory ' + new_dir)
97 return os.rename(old_path, new_path)
113 if self.
path and scan:
114 self.
mtime = getmtime(path)
115 self.
size = getsize(path)
128 return '%-40s size= %10d mtime= %10d' \
133 def xml(self,indent=''):
135 return indent +
'<File path ="' + self.
path +
'"' \
136 +
' size ="' + str(self.
size) +
'"' \
137 +
' mtime="' + str(self.
mtime) +
'" />\n'
139 return indent +
'<File path ="' + self.
path +
'" />\n'
155 file =
open(filename,
'w')
156 file.write(self.
xml())
167 if self.
path != other.path :
return 0
168 if self.
size != other.size :
return 0
197 for name
in os.listdir(self.
pathpath):
201 path = self.
pathpath + os.sep + name
208 for name
in os.listdir(self.
pathpath):
212 path = self.
pathpath + os.sep + name
233 r = glob( self.
pathpath +
'/' + pattern)
235 for x
in self.
dirs.keys():
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]) )
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]) )
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')
283 file =
open(filename,
'w')
284 file.write( self.
xmlxml() )
291 for x
in self.
files.keys() :
293 for x
in self.
dirs.keys() :
303 for x
in self.
files.keys():
304 if x
in other.files.keys():
305 if self.
files[x] != other.files[x]:
309 for x
in other.files.keys():
310 if not x
in self.
files.keys():
return 0
312 for x
in self.
dirs.keys():
313 if x
in other.dirs.keys():
314 if self.
dirs[x] != other.dirs[x]:
318 for x
in other.dirs.keys():
319 if not x
in self.
dirs.keys():
return 0
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')
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' )
352 for x
in self.
dirs.keys():
353 if x
in other.dirs.keys():
354 r.append( self.
dirs[x].
diff(other.dirs[x]) )
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' )
Class that represents a directory.
def write(self, filename)
Write XML representation to file.
def __ne__(self, other)
Test for inequality of two Directory objects.
def clear(self)
Clear all data for this Directory.
def __str__(self)
String representation of a directory.
def ls(self)
Print constituent file and subdirectories.
def filenames(self, pattern=' *', recursive=1)
Find files in directory that match a pattern.
def diff(self, other)
Return string reporting difference between Directory objects.
def xml(self, indent='')
XML string representation of a directory.
def __repr__(self)
String representation of a directory.
def __eq__(self, other)
Test for equality of two Directory objects.
def __init__(self, path=None, scan=1)
Constructor.
Class that contains metadata for a file.
def open(self, mode)
Open this file in specified mode.
def __ne__(self, other)
Test for inequality of files.
def __repr__(self)
String representation of file data.
def __eq__(self, other)
Test for equality of files.
def write(self, filename)
Write XML representation to a file.
def __init__(self, path=None, scan=1)
Constructor.
def __str__(self)
String representation of file data.
def mv(old_path, new_path)
Rename a file or directory, creating any necessary parent directories.
def chdirs(dir)
Change current working directory and create dir if necessary.
def relative_path(path1, path2)
Generates relative path of path2 relative to path1.
def rm(path)
Remove a file if possible (if the path exists and it is a file).
def open_w(path)
Open file with specified path for writing, return file object.