def hash_directory(path, root=None, visited=None): """Hashes a directory to a 40 hex character string. """ h = sha1() if visited is None: visited = set() if os.path.realpath(path) in visited: raise ValueError("Can't hash directory structure: loop detected at " "%s" % path) visited.add(os.path.realpath(path)) if root is None: root = os.path.realpath(path) h.update(b'dir\n') for f in sorted(os.listdir(path)): pf = os.path.join(path, f) if os.path.islink(pf): link = relativize_link(pf, root) if link is not None: h.update('link %s %s\n' % (f, sha1(link).hexdigest())) continue if os.path.isdir(pf): if os.path.islink(pf): warnings.warn("%s is a symbolic link, recursing on target " "directory" % pf, UsageWarning) h.update('dir %s %s\n' % (f, hash_directory(pf, root, visited))) else: if os.path.islink(pf): warnings.warn("%s is a symbolic link, using target file " "instead" % pf, UsageWarning) with open(pf, 'rb') as fd: h.update('file %s %s\n' % (f, hash_file(fd))) return h.hexdigest()
def hash_directory(path, root=None, visited=None): """Hashes a directory to a 40 hex character string. """ h = sha1() if visited is None: visited = set() if os.path.realpath(path) in visited: raise ValueError("Can't hash directory structure: loop detected at " "%s" % path) visited.add(os.path.realpath(path)) if root is None: root = os.path.realpath(path) h.update(b'dir\n') for f in sorted(os.listdir(path)): pf = os.path.join(path, f) if os.path.islink(pf): link = relativize_link(pf, root) if link is not None: h.update('link %s %s\n' % (f, sha1(link).hexdigest())) continue if os.path.isdir(pf): if os.path.islink(pf): warnings.warn( "%s is a symbolic link, recursing on target " "directory" % pf, UsageWarning) h.update('dir %s %s\n' % (f, hash_directory(pf, root, visited))) else: if os.path.islink(pf): warnings.warn( "%s is a symbolic link, using target file " "instead" % pf, UsageWarning) with open(pf, 'rb') as fd: h.update('file %s %s\n' % (f, hash_file(fd))) return h.hexdigest()
def hash_file(f): """Hashes a file to a 40 hex character SHA1. """ h = sha1() h.update(b'file\n') for chunk in BufferedReader(f): h.update(chunk) return h.hexdigest()
def hash_metadata(metadata): """Hashes a dictionary of metadata. """ assert 'hash' in metadata metadata = normalize_metadata(metadata) h = sha1() for k, v in sorted(metadata.items(), key=lambda p: p[0]): h.update('%d:%s' % (len(k), k)) if v['type'] == 'int': h.update('i%de' % v['value']) else: # v['type'] == 'str': h.update('%d:%s' % (len(v['value']), v['value'])) return h.hexdigest()