~ubuntu-branches/ubuntu/raring/bittornado/raring

« back to all changes in this revision

Viewing changes to .pc/32_use_hashlib_for_sha.patch/BitTornado/__init__.py

  • Committer: Bazaar Package Importer
  • Author(s): Ronny Cardona (Rcart)
  • Date: 2011-01-24 17:27:47 UTC
  • Revision ID: james.westby@ubuntu.com-20110124172747-j2en9t9kja4cgl06
Tags: 0.3.18-10ubuntu1
* debian/patches/32_use_hashlib_for_sha.patch:
  - Updated use of deprecated sha module to hashlib. (LP: #420387,
  Closes: #593653) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
product_name = 'BitTornado'
 
2
version_short = 'T-0.3.18'
 
3
 
 
4
version = version_short+' ('+product_name+')'
 
5
report_email = 'http://www.debian.org/Bugs/Reporting'
 
6
 
 
7
from types import StringType
 
8
from sha import sha
 
9
from time import time, clock
 
10
try:
 
11
    from os import getpid
 
12
except ImportError:
 
13
    def getpid():
 
14
        return 1
 
15
 
 
16
mapbase64 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-'
 
17
 
 
18
_idprefix = version_short[0]
 
19
for subver in version_short[2:].split('.'):
 
20
    try:
 
21
        subver = int(subver)
 
22
    except:
 
23
        subver = 0
 
24
    _idprefix += mapbase64[subver]
 
25
_idprefix += ('-' * (6-len(_idprefix)))
 
26
_idrandom = [None]
 
27
 
 
28
def resetPeerIDs():
 
29
    try:
 
30
        f = open('/dev/urandom','rb')
 
31
        x = f.read(20)
 
32
        f.close()
 
33
    except:
 
34
        x = ''
 
35
 
 
36
    l1 = 0
 
37
    t = clock()
 
38
    while t == clock():
 
39
        l1 += 1
 
40
    l2 = 0
 
41
    t = long(time()*100)
 
42
    while t == long(time()*100):
 
43
        l2 += 1
 
44
    l3 = 0
 
45
    if l2 < 1000:
 
46
        t = long(time()*10)
 
47
        while t == long(clock()*10):
 
48
            l3 += 1
 
49
    x += ( repr(time()) + '/' + str(time()) + '/'
 
50
           + str(l1) + '/' + str(l2) + '/' + str(l3) + '/'
 
51
           + str(getpid()) )
 
52
 
 
53
    s = ''
 
54
    for i in sha(x).digest()[-11:]:
 
55
        s += mapbase64[ord(i) & 0x3F]
 
56
    _idrandom[0] = s
 
57
        
 
58
resetPeerIDs()
 
59
 
 
60
def createPeerID(ins = '---'):
 
61
    assert type(ins) is StringType
 
62
    assert len(ins) == 3
 
63
    return _idprefix + ins + _idrandom[0]