def checksum(sumtype, file, CHUNK=2**16, datasize=None): """takes filename, hand back Checksum of it sumtype = md5 or sha/sha1/sha256/sha512 (note sha == sha1) filename = /path/to/file CHUNK=65536 by default""" # chunking brazenly lifted from Ryan Tomayko if isinstance(file, basestring): try: with open(file, 'rb', CHUNK) as fo: return checksum(sumtype, fo, CHUNK, datasize) except (IOError, OSError): raise MiscError('Error opening file for checksum: %s' % file) try: # assumes file is a file-like-object data = Checksums([sumtype]) while data.read(file, CHUNK): if datasize is not None and data.length > datasize: break # This screws up the length, but that shouldn't matter. We only care # if this checksum == what we expect. if datasize is not None and datasize != data.length: return '!%u!%s' % (datasize, data.hexdigest(sumtype)) return data.hexdigest(sumtype) except (IOError, OSError) as e: raise MiscError('Error reading file for checksum: %s' % file)
def __init__(self, checksums=None, ignore_missing=False, ignore_none=False): if checksums is None: checksums = _default_checksums self._sumalgos = [] self._sumtypes = [] self._len = 0 done = set() for sumtype in checksums: if sumtype == 'sha': sumtype = 'sha1' if sumtype in done: continue if sumtype in _available_checksums: sumalgo = hashlib.new(sumtype) elif ignore_missing: continue else: raise MiscError('Error Checksumming, bad checksum type %s' % sumtype) done.add(sumtype) self._sumtypes.append(sumtype) self._sumalgos.append(sumalgo) if not done and not ignore_none: raise MiscError('Error Checksumming, no valid checksum type')
def getFileList(path, ext, filelist): """Return all files in path matching ext, store them in filelist, recurse dirs return list object""" extlen = len(ext) try: dir_list = os.listdir(path) except OSError as e: raise MiscError(('Error accessing directory %s, %s') % (path, e)) for d in dir_list: if os.path.isdir(path + '/' + d): filelist = getFileList(path + '/' + d, ext, filelist) else: if not ext or d[-extlen:].lower() == '%s' % (ext): newpath = os.path.normpath(path + '/' + d) filelist.append(newpath) return filelist