"""pathinfo.py: Object for manipulating information about pathnames and their related i-nodes. Written by John W. Shipman (john@nmt.edu), New Mexico Tech Computer Center, Socorro, NM 87801, in March 1997. Freely available under the terms of the GNU General Public License. This class defines methods for the ten standard components of the os.stat() value, giving information about file status. NOTE: The file's information is recorded as of the time the PathInfo object is instantiated. It does not automatically track changes in file size or other attributes. If you want new information, get a new object. Exported methods: ================================================================ PathInfo(pathName) [ if pathname is a valid, existing path name to which this program has directory access -> return a PathInfo representing that path's state else -> raise OSError ] .__str__() [ return a string of the form "m t p" where: m := "drwxrwxrwx" part of `ls -l' t := time as "yyyy-mm-dd hh:mm:ss" p := pathname used to create self ] .path [ self's pathname as a string (read-only, please!) ] .size() [ return self's size as an integer ] .stat [ the file status tuple for self ] .mode [ the mode bits from the file status tuple for self ] .modEpoch [ the file's modification timestamp as an epoch time ] .modTime() [ return self's modification timestamp as a string of the form "YYYY-MM-DD hh:mm:ss" ] .ownerCanRead() [ if owner can read self -> return a nonzero integer else -> return 0 ] .ownerCanWrite() [ if owner can write self -> return a nonzero integer else -> return 0 ] .ownerCanExec() [ if owner can execute self -> return a nonzero integer else -> return 0 ] .groupCanRead() [ if group can read self -> return a nonzero integer else -> return 0 ] .groupCanWrite() [ if group can write self -> return a nonzero integer else -> return 0 ] .groupCanExec() [ if group can execute self -> return a nonzero integer else -> return 0 ] .worldCanRead() [ if world can read self -> return a nonzero integer else -> return 0 ] .worldCanWrite() [ if world can write self -> return a nonzero integer else -> return 0 ] .worldCanExec() [ if world can execute self -> return a nonzero integer else -> return 0 ] .isDir() [ if self is a directory -> return a nonzero integer else -> return 0 ] .isLink() [ if self is a symbolic link -> return a nonzero integer else -> return 0 ] .isFile() [ if self is an ordinary disk file -> return a nonzero integer else -> return 0 ] """ import os # Operating system functions import time # Functions for handling time import stat # Functions for interpreting file status tuples # - - - - - c l a s s P a t h I n f o - - - - - class PathInfo: """Object to represent a pathname and the information from the associated inode. """ # - - - P a t h I n f o . _ _ i n i t _ _ - - - def __init__ ( self, pathName ): """Constructor for a PathInfo object """ #-- 1 -- self.path = pathName #-- 2 -- # [ if pathName is an existing path name -> # self.stat := that path's file status tuple # self.mode := that path's mode bits # self.modEpoch := that path's modification epoch time ] self.stat = os.lstat(pathName) self.mode = self.stat[stat.ST_MODE] self.modEpoch = self.stat[stat.ST_MTIME] # - - - P a t h I n f o . _ _ s t r _ _ - - - def __str__ ( self ): baseName = (os.path.split(self.path))[1] return ( "%s %s %8d %s" % ( self.__modeFlags(), self.modTime(), self.size(), baseName ) ) # - - - P a t h I n f o . m o d e F l a g s - - - def __modeFlags ( self ): return self.__dirFlag() + self.__permFlags() # - - - P a t h I n f o . _ d i r F l a g - - - def __dirFlag ( self ): """ [ if self is a directory -> return "d" else if self is a symbolic link -> return "l" else if self is a regular file -> return "-" else -> return "?" ] """ if stat.S_ISLNK(self.mode): return "l" elif stat.S_ISDIR(self.mode): return "d" elif stat.S_ISREG(self.mode): return "-" else: return "?" # - - - P a t h I n f o . _ _ p e r m F l a g s - - - def __permFlags ( self ): """Return the `rwxrwxrwx' part of ls output """ return ( self.__rwx ( self.mode & stat.S_IRUSR, self.mode & stat.S_IWUSR, self.mode & stat.S_IXUSR ) + self.__rwx ( self.mode & stat.S_IRGRP, self.mode & stat.S_IWGRP, self.mode & stat.S_IXGRP ) + self.__rwx ( self.mode & stat.S_IROTH, self.mode & stat.S_IWOTH, self.mode & stat.S_IXOTH ) ) # - - - P a t h I n f o . _ _ r w x - - - def __rwx ( self, r, w, x ): """ [ return a string of the form "abc" where: a := "r" if r is nonzero, else "-" b := "w" if w is nonzero, else "-" c := "x" if x is nonzero, else "-" ] """ return ( self.__dasher ( r, "r" ) + self.__dasher ( w, "w" ) + self.__dasher ( x, "x" ) ) # - - - P a t h I n f o . _ _ d a s h e r - - - def __dasher ( self, b, c ): """ [ if b is true -> return c else -> return "-" ] """ if b: return c else: return "-" # - - - P a t h I n f o . m o d T i m e - - - def modTime ( self ): """ [ return self's modification time as "yyyy-dd-mm hh:mm:ss" ] """ return time.strftime ( "%Y-%m-%d %H:%M:%S", time.localtime ( self.modEpoch ) ) # - - - P a t h I n f o . s i z e - - - def size ( self ): return self.stat[stat.ST_SIZE] # - - - P a t h I n f o . { o w n e r } C a n { R e a d } - - - # { g r o u p } { W r i t e } # { w o r l d } { E x e c } def ownerCanRead ( self ): return self.mode & stat.S_IRUSR def ownerCanWrite ( self ): return self.mode & stat.S_IWUSR def ownerCanExec ( self ): return self.mode & stat.S_IXUSR def groupCanRead ( self ): return self.mode & stat.S_IRGRP def groupCanWrite ( self ): return self.mode & stat.S_IWGRP def groupCanExec ( self ): return self.mode & stat.S_IXGRP def worldCanRead ( self ): return self.mode & stat.S_IROTH def worldCanWrite ( self ): return self.mode & stat.S_IWOTH def worldCanExec ( self ): return self.mode & stat.S_IXOTH # - - - P a t h I n f o . i s { D i r } - - - # { L i n k } # { F i l e } def isDir ( self ): return stat.S_ISDIR ( self.mode ) def isLink ( self ): return stat.S_ISLNK ( self.mode ) def isFile ( self ): return stat.S_ISREG ( self.mode )