summaryrefslogtreecommitdiffstats
path: root/askbot/utils/hasher.py
blob: 5a73213d5288354f81ac05b545f83bfddb5f2d5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""hasher function that will calculate sha1 hash
directory contents
"""
import hashlib, os
import logging

def get_hash_of_dirs(dirs):
    """Hasher function for a directory and its files"""
    sha_hash = hashlib.sha1()
    for directory in dirs:
        if not os.path.exists (directory):
            return -1
          
        try:
            for root, dirs, files in os.walk(directory):
                for names in files:
                    filepath = os.path.join(root, names)
                    try:
                        file_obj = open(filepath, 'rb')
                    except Exception, error:
                        # You can't open the file for some reason
                        logging.critical(
                            'cannot open file %s: %s',
                            filepath,
                            error
                        )
                        file_obj.close()
                        continue

                    while 1:
                        # Read file in as little chunks
                        buf = file_obj.read(4096)
                        if not buf : break
                        sha_hash.update(hashlib.sha1(buf).hexdigest())
                    file_obj.close()

        except Exception:
            import traceback
            # Print the stack traceback
            traceback.print_exc()
            return -2

    return sha_hash.hexdigest()