def __parseDirectory(self, skipPaths, skipExtensions, skipNames, doHash, skipAttributes): if self.__contents != None: raise SyntaxError("Already Created") self.__contents= bXML.create("manifest") if skipExtensions: for item in skipExtensions: bXML.appendText(self.__contents.documentElement, "\n\t") bXML.appendElement(self.__contents.documentElement, "filter", {'extension': item}) if skipPaths: for item in skipPaths: bXML.appendText(self.__contents.documentElement, "\n\t") bXML.appendElement(self.__contents.documentElement, "filter", {'path': item}) if skipNames: for item in skipNames: bXML.appendText(self.__contents.documentElement, "\n\t") bXML.appendElement(self.__contents.documentElement, "filter", {'name': item}) for path, dirs, files in os.walk(self.__path): relativePath= getSubPathRelative(self.__path, path) if self.__skipped(relativePath, skipPaths, skipExtensions, skipNames): continue files.extend(dirs) for item in files: if self.__skipped(item, None, skipExtensions, skipNames): continue itemFullPath= os.path.join(path, item) itemRelativePath= os.path.join(relativePath, item) if self.__skipped(itemRelativePath, skipPaths, None, None): continue self.__addElement(itemRelativePath, doHash, skipAttributes) if self.__contents.documentElement.firstChild: bXML.appendText(self.__contents.documentElement, "\n")
def __addElement(self, relativePath, doHash, skipAttributes): try: fullPath= os.path.join(self.__path, relativePath) stats= os.lstat(fullPath) properties= {'path': relativePath} if stat.S_ISLNK(stats.st_mode): kind= "link" properties['target']= os.readlink(fullPath) elif stat.S_ISDIR(stats.st_mode): kind= "directory" elif stat.S_ISREG(stats.st_mode): kind= "file" properties['size']= str(stats.st_size) else: return None # unknown file type, skip it properties['modified']= formatDate(stats.st_mtime) mods= stat.S_IMODE(stats.st_mode) if mods & (stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH) == 0: properties['readonly']= "true" if mods & (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) != 0: properties['executable']= "true" bXML.appendText(self.__contents.documentElement, "\n\t") element= bXML.appendElement(self.__contents.documentElement, kind, properties) if kXattrAvailable: try: attrs= xattr.listxattr(fullPath) for attr in attrs: try: value= xattr.getxattr(fullPath, attr, True) bXML.appendText(element, "\n\t\t") tag= bXML.appendElement(element, "xattr", {'name': attr}) bXML.appendText(tag, escape(value)) except: # can't read this attribute exceptionType, exceptionValue, exceptionTraceback = sys.exc_info() traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback, limit=5, file=sys.stderr) pass except: # something went wrong exceptionType, exceptionValue, exceptionTraceback = sys.exc_info() traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback, limit=5, file=sys.stderr) pass if element.firstChild: bXML.appendText(element, "\n\t") except: # skip files we can't look at exceptionType, exceptionValue, exceptionTraceback = sys.exc_info() traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback, limit=5, file=sys.stderr) pass
def __parseDirectory(self, skipPaths, skipExtensions, skipNames, doHash, skipAttributes): if self.__contents != None: raise SyntaxError("Already Created") self.__contents = bXML.create("manifest") if skipExtensions: for item in skipExtensions: bXML.appendText(self.__contents.documentElement, "\n\t") bXML.appendElement(self.__contents.documentElement, "filter", {'extension': item}) if skipPaths: for item in skipPaths: bXML.appendText(self.__contents.documentElement, "\n\t") bXML.appendElement(self.__contents.documentElement, "filter", {'path': item}) if skipNames: for item in skipNames: bXML.appendText(self.__contents.documentElement, "\n\t") bXML.appendElement(self.__contents.documentElement, "filter", {'name': item}) for path, dirs, files in os.walk(self.__path): relativePath = getSubPathRelative(self.__path, path) if self.__skipped(relativePath, skipPaths, skipExtensions, skipNames): continue files.extend(dirs) for item in files: if self.__skipped(item, None, skipExtensions, skipNames): continue itemFullPath = os.path.join(path, item) itemRelativePath = os.path.join(relativePath, item) if self.__skipped(itemRelativePath, skipPaths, None, None): continue self.__addElement(itemRelativePath, doHash, skipAttributes) if self.__contents.documentElement.firstChild: bXML.appendText(self.__contents.documentElement, "\n")
def __addElement(self, relativePath, doHash, skipAttributes): try: fullPath = os.path.join(self.__path, relativePath) stats = os.lstat(fullPath) properties = {'path': relativePath} if stat.S_ISLNK(stats.st_mode): kind = "link" properties['target'] = os.readlink(fullPath) elif stat.S_ISDIR(stats.st_mode): kind = "directory" elif stat.S_ISREG(stats.st_mode): kind = "file" properties['size'] = str(stats.st_size) else: return None # unknown file type, skip it properties['modified'] = formatDate(stats.st_mtime) mods = stat.S_IMODE(stats.st_mode) if mods & (stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH) == 0: properties['readonly'] = "true" if mods & (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) != 0: properties['executable'] = "true" bXML.appendText(self.__contents.documentElement, "\n\t") element = bXML.appendElement(self.__contents.documentElement, kind, properties) if kXattrAvailable: try: attrs = xattr.listxattr(fullPath) for attr in attrs: try: value = xattr.getxattr(fullPath, attr, True) bXML.appendText(element, "\n\t\t") tag = bXML.appendElement(element, "xattr", {'name': attr}) bXML.appendText(tag, escape(value)) except: # can't read this attribute exceptionType, exceptionValue, exceptionTraceback = sys.exc_info( ) traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback, limit=5, file=sys.stderr) pass except: # something went wrong exceptionType, exceptionValue, exceptionTraceback = sys.exc_info( ) traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback, limit=5, file=sys.stderr) pass if element.firstChild: bXML.appendText(element, "\n\t") except: # skip files we can't look at exceptionType, exceptionValue, exceptionTraceback = sys.exc_info() traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback, limit=5, file=sys.stderr) pass