~ubuntu-branches/ubuntu/quantal/totem/quantal

« back to all changes in this revision

Viewing changes to src/plugins/opensubtitles/hash.py

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2012-05-29 22:51:06 UTC
  • mfrom: (1.11.14) (2.1.22 experimental) (2.2.28 sid)
  • Revision ID: package-import@ubuntu.com-20120529225106-g0wms0aylgh7x20m
Tags: 3.4.2-1ubuntu1
* Merge with Debian. Remaining changes:
* debian/control.in:
  - Drop build-depends on libepc-ui-dev and libgrilo-0.1-dev
  - Build-depend on libzeitgeist-dev
  - Suggest rather than recommend gstreamer components in universe
  - Add totem-plugins-extra for the components which have depends in universe
  - Add XB-Npp-Description and XB-Npp-Filename header to the 
    totem-mozilla package to improve ubufox/ubuntu plugin db integration 
  - Refer to Firefox in totem-mozilla description instead of Iceweasel
  - Don't have totem-mozilla recommend any particular browser
* debian/totem-common.install, debian/source_totem.py:
  - Install Ubuntu apport debugging hook
* debian/totem-plugins-extra.install:
  - Plugins split out of totem-plugins
* debian/totem-plugins.install:    
  - Simplify the install list and exclude the plugins split to -extra
* debian/totem-common.gsettings-override:
  - Enable the youtube and zeitgeist plugins by default
* debian/patches/01_fake_keypresses.patch:
  - dropped it's an old workaround and should not be required
* debian/patches/91_quicklist_entries.patch:
  - Add static quicklist
* debian/patches/correct_desktop_mimetypes.patch:
  - Don't list the mimetypes after the unity lists
* Dropped Ubuntu patches:
  - 03_default_options.patch: Moved to gsettings override instead
  - youtube-api-gdata9.patch: In new totem release
  - 04_port_to_gobject.patch: In new totem release
  - 90_fix_save_playlist.patch: In new totem release
  - git_commandfix_incorrect_free.patch: In new totem release
  - git_tracks_language.patch: In new totem release
  - git_remote_option_parsing.patch: In new totem release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import struct
2
2
import os
3
 
from gi.repository import Gio
 
3
from gi.repository import Gio # pylint: disable-msg=E0611
4
4
 
5
5
SIZE_ERROR = -1
6
6
SEEK_ERROR = -2
7
7
 
8
 
def hashFile(name):
9
 
       """ FIXME Need to handle exceptions !! """
10
 
 
11
 
 
12
 
       longlongformat = 'q'  # long long 
13
 
       bytesize = struct.calcsize(longlongformat) 
14
 
           
15
 
       fp = Gio.File.new_for_uri (name)
16
 
           
17
 
       file_info = fp.query_info ('standard::size', 0, None)
18
 
       filesize = file_info.get_attribute_uint64 ('standard::size')
19
 
       
20
 
       hash = filesize 
21
 
          
22
 
       if filesize < 65536 * 2: 
23
 
              return SIZE_ERROR, 0
24
 
 
25
 
       data = file(fp.get_path(), 'rb')
26
 
       
27
 
       for x in range(65536/bytesize):
28
 
               buffer = data.read(bytesize)
29
 
               (l_value,)= struct.unpack(longlongformat, buffer)  
30
 
               hash += l_value 
31
 
               hash = hash & 0xFFFFFFFFFFFFFFFF #to remain as 64bit number  
32
 
               
33
 
       data.seek (max (0, filesize - 65536), os.SEEK_SET)
34
 
 
35
 
       if data.tell() != max (0, filesize - 65536):
36
 
               return SEEK_ERROR, 0
37
 
 
38
 
       for x in range(65536/bytesize):
39
 
               buffer = data.read(bytesize)
40
 
               (l_value,)= struct.unpack(longlongformat, buffer)  
41
 
               hash += l_value 
42
 
               hash = hash & 0xFFFFFFFFFFFFFFFF 
43
 
        
44
 
       data.close() 
45
 
       returnedhash =  "%016x" % hash 
46
 
       return returnedhash, filesize 
 
8
def hash_file (name):
 
9
    """ FIXME Need to handle exceptions !! """
 
10
 
 
11
    longlongformat = 'q' # long long
 
12
    bytesize = struct.calcsize (longlongformat)
 
13
 
 
14
    file_to_hash = Gio.File.new_for_uri (name)
 
15
 
 
16
    file_info = file_to_hash.query_info ('standard::size', 0, None)
 
17
    filesize = file_info.get_attribute_uint64 ('standard::size')
 
18
 
 
19
    file_hash = filesize
 
20
 
 
21
    if filesize < 65536 * 2:
 
22
        return SIZE_ERROR, 0
 
23
 
 
24
    file_handle = file (file_to_hash.get_path (), "rb")
 
25
 
 
26
    for _ in range (65536 / bytesize):
 
27
        buf = file_handle.read (bytesize)
 
28
        (l_value,) = struct.unpack (longlongformat, buf)
 
29
        file_hash += l_value
 
30
        file_hash = file_hash & 0xFFFFFFFFFFFFFFFF #to remain as 64bit number
 
31
 
 
32
    file_handle.seek (max (0, filesize - 65536), os.SEEK_SET)
 
33
 
 
34
    if file_handle.tell() != max (0, filesize - 65536):
 
35
        return SEEK_ERROR, 0
 
36
 
 
37
    for _ in range (65536/bytesize):
 
38
        buf = file_handle.read (bytesize)
 
39
        (l_value,) = struct.unpack (longlongformat, buf)
 
40
        file_hash += l_value
 
41
        file_hash = file_hash & 0xFFFFFFFFFFFFFFFF
 
42
 
 
43
    file_handle.close ()
 
44
    returnedhash = "%016x" % file_hash
 
45
 
 
46
    print returnedhash
 
47
    print filesize
 
48
 
 
49
    return returnedhash, filesize
47
50