1'''!   Utilities for manipulating files and paths. ''' 
   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
 
 
 
  192    def __init__(self, path = None, scan = 1):
 
  196        if self.
path and scan: 
 
  197            for name 
in os.listdir(self.
path):
 
  201                    path = self.
path + os.sep + name
 
  208        for name 
in os.listdir(self.
path):
 
  209            if self.
path == 
'.' :
 
  212                path = self.
path + os.sep + name
 
  233        r = glob( self.
path + 
'/' + pattern)
 
  235           for x 
in self.
dirs.keys():
 
 
  244        r.append( self.
path + 
'\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])  )
 
 
  256        r.append(self.
path + 
'\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]) )
 
 
  268        r.append( indent + 
'<Directory path="'+ self.
path + 
'" >\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.
xml() )
 
 
  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.
 
__repr__(self)
String representation of a directory.
 
xml(self, indent='')
XML string representation of a directory.
 
clear(self)
Clear all data for this Directory.
 
filenames(self, pattern=' *', recursive=1)
Find files in directory that match a pattern.
 
diff(self, other)
Return string reporting difference between Directory objects.
 
__str__(self)
String representation of a directory.
 
__eq__(self, other)
Test for equality of two Directory objects.
 
__ne__(self, other)
Test for inequality of two Directory objects.
 
ls(self)
Print constituent file and subdirectories.
 
write(self, filename)
Write XML representation to file.
 
Class that contains metadata for a file.
 
open(self, mode)
Open this file in specified mode.
 
write(self, filename)
Write XML representation to a file.
 
__repr__(self)
String representation of file data.
 
__ne__(self, other)
Test for inequality of files.
 
__eq__(self, other)
Test for equality of files.
 
__str__(self)
String representation of file data.
 
__init__(self, path=None, scan=1)
Constructor.
 
rm(path)
Remove a file if possible (if the path exists and it is a file).
 
mv(old_path, new_path)
Rename a file or directory, creating any necessary parent directories.
 
open_w(path)
Open file with specified path for writing, return file object.
 
chdirs(dir)
Change current working directory and create dir if necessary.
 
relative_path(path1, path2)
Generates relative path of path2 relative to path1.