~pkgcrosswire/xiphos/main

« back to all changes in this revision

Viewing changes to waffles/hashing.py

  • Committer: Dmitrijs Ledkovs
  • Date: 2010-11-14 00:38:52 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: dmitrij.ledkov@ubuntu.com-20101114003852-sjt227lz4qqi85xj
New upstream release 3.1.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# http://akiscode.com/articles/sha-1directoryhash.shtml
 
2
# Copyright (c) 2009 Stephen Akiki
 
3
# MIT License (Means you can do whatever you want with this)
 
4
#  See http://www.opensource.org/licenses/mit-license.php
 
5
# Error Codes:
 
6
#   -1 -> Directory does not exist
 
7
#   -2 -> General error (see stack traceback)
 
8
 
 
9
def GetHashofDirs(dirs, verbose=0):
 
10
  import hashlib, os
 
11
  SHAhash = hashlib.sha1()
 
12
    
 
13
  try:
 
14
    for directory in dirs:
 
15
      if not os.path.exists (directory):
 
16
        pass
 
17
      for files in os.listdir(directory):
 
18
        if verbose == 1:
 
19
          print 'Hashing', files
 
20
        filepath = os.path.join(directory,files)
 
21
        try:
 
22
          f1 = open(filepath, 'rb')
 
23
        except:
 
24
          # You can't open the file for some reason
 
25
          f1.close()
 
26
          continue
 
27
 
 
28
        while 1:
 
29
          # Read file in as little chunks
 
30
          buf = f1.read(4096)
 
31
          if not buf : break
 
32
          SHAhash.update(hashlib.sha1(buf).hexdigest())
 
33
        f1.close()
 
34
 
 
35
  except:
 
36
    import traceback
 
37
    # Print the stack traceback
 
38
    traceback.print_exc()
 
39
    return -2
 
40
 
 
41
  return SHAhash.hexdigest()
 
42
#print GetHashofDirs('My Documents', 1)