]> git.ipfire.org Git - ipfire.org.git/blame - webapp/backend/tracker.py
Drop tracker backend
[ipfire.org.git] / webapp / backend / tracker.py
CommitLineData
43d991f6
MT
1#!/usr/bin/python
2
9068dba1 3from __future__ import division
43d991f6 4
43d991f6
MT
5##### This is borrowed from the bittorrent client libary #####
6
7def decode_int(x, f):
8 f += 1
9 newf = x.index('e', f)
10 n = int(x[f:newf])
11 if x[f] == '-':
12 if x[f + 1] == '0':
13 raise ValueError
14 elif x[f] == '0' and newf != f+1:
15 raise ValueError
16 return (n, newf+1)
17
18def decode_string(x, f):
19 colon = x.index(':', f)
20 n = int(x[f:colon])
21 if x[f] == '0' and colon != f+1:
22 raise ValueError
23 colon += 1
24 return (x[colon:colon+n], colon+n)
25
26def decode_list(x, f):
27 r, f = [], f+1
28 while x[f] != 'e':
29 v, f = decode_func[x[f]](x, f)
30 r.append(v)
31 return (r, f + 1)
32
33def decode_dict(x, f):
34 r, f = {}, f+1
35 while x[f] != 'e':
36 k, f = decode_string(x, f)
37 r[k], f = decode_func[x[f]](x, f)
38 return (r, f + 1)
39
40decode_func = {}
41decode_func['l'] = decode_list
42decode_func['d'] = decode_dict
43decode_func['i'] = decode_int
44decode_func['0'] = decode_string
45decode_func['1'] = decode_string
46decode_func['2'] = decode_string
47decode_func['3'] = decode_string
48decode_func['4'] = decode_string
49decode_func['5'] = decode_string
50decode_func['6'] = decode_string
51decode_func['7'] = decode_string
52decode_func['8'] = decode_string
53decode_func['9'] = decode_string
54
55def bdecode(x):
56 try:
57 r, l = decode_func[x[0]](x, 0)
58 except (IndexError, KeyError, ValueError):
59 raise Exception("not a valid bencoded string")
60 if l != len(x):
61 raise Exception("invalid bencoded value (data after valid prefix)")
62 return r
63
64from types import StringType, IntType, LongType, DictType, ListType, TupleType
65
66
67class Bencached(object):
68
69 __slots__ = ['bencoded']
70
71 def __init__(self, s):
72 self.bencoded = s
73
74def encode_bencached(x,r):
75 r.append(x.bencoded)
76
77def encode_int(x, r):
78 r.extend(('i', str(x), 'e'))
79
80def encode_bool(x, r):
81 if x:
82 encode_int(1, r)
83 else:
84 encode_int(0, r)
85
86def encode_string(x, r):
87 r.extend((str(len(x)), ':', x))
88
89def encode_list(x, r):
90 r.append('l')
91 for i in x:
92 encode_func[type(i)](i, r)
93 r.append('e')
94
95def encode_dict(x,r):
96 r.append('d')
97 ilist = x.items()
98 ilist.sort()
99 for k, v in ilist:
100 r.extend((str(len(k)), ':', k))
101 encode_func[type(v)](v, r)
102 r.append('e')
103
104encode_func = {}
105encode_func[Bencached] = encode_bencached
106encode_func[IntType] = encode_int
107encode_func[LongType] = encode_int
108encode_func[StringType] = encode_string
109encode_func[ListType] = encode_list
110encode_func[TupleType] = encode_list
111encode_func[DictType] = encode_dict
112
113try:
114 from types import BooleanType
115 encode_func[BooleanType] = encode_bool
116except ImportError:
117 pass
118
119def bencode(x):
120 r = []
121 encode_func[type(x)](x, r)
122 return ''.join(r)