~osclone-developers/flubtitles/alpha

« back to all changes in this revision

Viewing changes to scripts/subtitle-couch/client/hashfile.py

  • Committer: swick at 2flub
  • Date: 2011-02-22 15:12:51 UTC
  • Revision ID: swick@2flub.org-20110222151251-nsjsh0yrn1nolqa2
Moving to REST

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
import struct, os
3
 
 
4
 
def hashFile(name): 
5
 
      try: 
6
 
                 
7
 
                longlongformat = 'q'  # long long 
8
 
                bytesize = struct.calcsize(longlongformat) 
9
 
                    
10
 
                f = open(name, "rb") 
11
 
                    
12
 
                filesize = os.path.getsize(name) 
13
 
                hash = filesize 
14
 
                    
15
 
                if filesize < 65536 * 2: 
16
 
                       return "SizeError" 
17
 
                 
18
 
                for x in range(65536/bytesize): 
19
 
                        buffer = f.read(bytesize) 
20
 
                        (l_value,)= struct.unpack(longlongformat, buffer)  
21
 
                        hash += l_value 
22
 
                        hash = hash & 0xFFFFFFFFFFFFFFFF #to remain as 64bit number  
23
 
                         
24
 
    
25
 
                f.seek(max(0,filesize-65536),0) 
26
 
                for x in range(65536/bytesize): 
27
 
                        buffer = f.read(bytesize) 
28
 
                        (l_value,)= struct.unpack(longlongformat, buffer)  
29
 
                        hash += l_value 
30
 
                        hash = hash & 0xFFFFFFFFFFFFFFFF 
31
 
                 
32
 
                f.close() 
33
 
                returnedhash =  "%016x" % hash 
34
 
                return returnedhash 
35
 
    
36
 
      except(IOError): 
37
 
                return "IOError"
38