def make_md5(data): """Make a md5 hash based on``data``. Specifically, this knows about ``Hunk`` objects, and makes sure the actual content is hashed. This is very conservative, and raises an exception if there are data types that it does not explicitly support. This is because we had in the past some debugging headaches with the cache not working for this very reason. MD5 is faster than sha, and we don't care so much about collisions. We care enough however not to use hash(). """ def walk(obj): if isinstance(obj, (tuple, list)): for item in obj: for d in walk(item): yield d elif isinstance(obj, dict): for k in sorted(obj.keys()): for d in walk(k): yield d for d in walk(obj[k]): yield d elif isinstance(obj, BaseHunk): yield obj.data() elif isinstance(obj, Filter): yield str(hash(obj)) elif isinstance(obj, (int, basestring)): yield str(obj) else: raise ValueError('Cannot MD5 type %s' % type(obj)) md5 = md5_constructor() for d in walk(data): md5.update(d) return md5.hexdigest()
def make_md5(*data): """Make a md5 hash based on``data``. Specifically, this knows about ``Hunk`` objects, and makes sure the actual content is hashed. This is very conservative, and raises an exception if there are data types that it does not explicitly support. This is because we had in the past some debugging headaches with the cache not working for this very reason. MD5 is faster than sha, and we don't care so much about collisions. We care enough however not to use hash(). """ def walk(obj): if isinstance(obj, (tuple, list, frozenset)): for item in obj: for d in walk(item): yield d elif isinstance(obj, (dict)): for k in sorted(obj.keys()): for d in walk(k): yield d for d in walk(obj[k]): yield d elif isinstance(obj, BaseHunk): yield obj.data().encode('utf-8') elif isinstance(obj, int): yield str(obj).encode('utf-8') elif isinstance(obj, six.text_type): yield obj.encode('utf-8') elif isinstance(obj, six.binary_type): yield obj elif hasattr(obj, "id"): for i in walk(obj.id()): yield i elif obj is None: yield "None".encode('utf-8') elif isinstance(obj, types.FunctionType): yield str(hash(obj)).encode('utf-8') else: raise ValueError('Cannot MD5 type %s' % type(obj)) md5 = md5_constructor() for d in walk(data): md5.update(d) return md5.hexdigest()