usr bin env python Written by Bram Cohen see LICENSE txt for license i

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#!/usr/bin/env python
# Written by Bram Cohen
# see LICENSE.txt for license information
from BitTornado import PSYCO
if PSYCO.psyco:
try:
import psyco
assert psyco.__version__ >= 0x010100f0
psyco.full()
except:
pass
from BitTornado.download_bt1 import BT1Download, defaults, parse_params, get_usage, get_response
from BitTornado.RawServer import RawServer, UPnP_ERROR
from random import seed
from socket import error as socketerror
from BitTornado.bencode import bencode
from BitTornado.natpunch import UPnP_test
from threading import Event
from os.path import abspath
from os import getpid, remove
from sys import argv, stdout
import sys
from sha import sha
from time import strftime
from BitTornado.clock import clock
from BitTornado import createPeerID, version
assert sys.version >= '2', "Install Python 2.0 or greater"
try:
True
except:
True = 1
False = 0
PROFILER = False
if __debug__: LOGFILE=open(argv[3]+"."+str(getpid()),"w")
def traceMsg(msg):
try:
if __debug__:
LOGFILE.write(msg + "\n")
LOGFILE.flush()
except:
return
def hours(n):
if n == 0:
return 'complete!'
try:
n = int(n)
assert n >= 0 and n < 5184000 # 60 days
except:
return '<unknown>'
m, s = divmod(n, 60)
h, m = divmod(m, 60)
d, h = divmod(h, 24)
if d > 0:
return '%dd %02d:%02d:%02d' % (d, h, m, s)
else:
return '%02d:%02d:%02d' % (h, m, s)
class HeadlessDisplayer:
def __init__(self):
self.done = False
self.file = ''
self.percentDone = ''
self.timeEst = 'Connecting to peers'
self.downloadTo = ''
self.downRate = ''
self.upRate = ''
self.shareRating = ''
self.percentShare = ''
self.upTotal = 0
self.downTotal = 0
self.seedStatus = ''
self.peerStatus = ''
self.seeds = ''
self.peers = ''
self.errors = []
self.last_update_time = -1
self.statFile = 'percent.txt'
self.autoShutdown = 'False'
self.user = 'unknown'
self.size = 0
self.shareKill = '100'
self.distcopy = ''
self.stoppedAt = ''
def finished(self):
if __debug__: traceMsg('finished - begin')
self.done = True
self.percentDone = '100'
self.timeEst = 'Zagruzka zavershena!'
self.downRate = ''
self.display()
if self.autoShutdown == 'True':
self.upRate = ''
if self.stoppedAt == '':
self.writeStatus()
if __debug__: traceMsg('finished - end - raised ki')
raise KeyboardInterrupt
if __debug__: traceMsg('finished - end')
def failed(self, errormsg = None):
if __debug__: traceMsg('failed - begin')
self.done = True
self.percentDone = '0'
self.timeEst = 'Zagruzka nevozmojna!'
self.downRate = ''
if errormsg:
self.errors.append(errormsg)
self.display()
if self.autoShutdown == 'True':
self.upRate = ''
if self.stoppedAt == '':
self.writeStatus()
if __debug__:traceMsg('failed - end - raised ki')
raise KeyboardInterrupt
if __debug__: traceMsg('failed - end')
def error(self, errormsg):
self.errors.append(errormsg)
self.display()
def display(self, dpflag = Event(), fractionDone = None, timeEst = None,
downRate = None, upRate = None, activity = None,
statistics = None, **kws):
if __debug__: traceMsg('display - begin')
if self.last_update_time + 0.1 > clock() and fractionDone not in (0.0, 1.0) and activity is not None:
return
self.last_update_time = clock()
if fractionDone is not None:
self.percentDone = str(float(int(fractionDone * 1000)) / 10)
if timeEst is not None:
self.timeEst = hours(timeEst)
if activity is not None and not self.done:
self.timeEst = activity
if downRate is not None:
self.downRate = '%.1f kB/s' % (float(downRate) / (1 << 10))
if upRate is not None:
self.upRate = '%.1f kB/s' % (float(upRate) / (1 << 10))
if statistics is not None:
if (statistics.shareRating < 0) or (statistics.shareRating > 100):
self.shareRating = 'oo (%.1f MB up / %.1f MB down)' % (float(statistics.upTotal) / (1<<20), float(statistics.downTotal) / (1<<20))
self.downTotal = statistics.downTotal
self.upTotal = statistics.upTotal
else:
self.shareRating = '%.3f (%.1f MB up / %.1f MB down)' % (statistics.shareRating, float(statistics.upTotal) / (1<<20), float(statistics.downTotal) / (1<<20))
self.downTotal = statistics.downTotal
self.upTotal = statistics.upTotal
if not self.done:
self.seedStatus = '%d seen now, plus %.3f distributed copies' % (statistics.numSeeds,0.001*int(1000*statistics.numCopies))
self.seeds = (str(statistics.numSeeds))
else:
self.seedStatus = '%d seen recently, plus %.3f distributed copies' % (statistics.numOldSeeds,0.001*int(1000*statistics.numCopies))
self.seeds = (str(statistics.numOldSeeds))
self.peers = '%d' % (statistics.numPeers)
self.distcopy = '%.3f' % (0.001*int(1000*statistics.numCopies))
self.peerStatus = '%d seen now, %.1f%% done at %.1f kB/s' % (statistics.numPeers,statistics.percentDone,float(statistics.torrentRate) / (1 << 10))
dpflag.set()
if __debug__: traceMsg('display - prior to self.write')
if self.stoppedAt == '':
self.writeStatus()
if __debug__: traceMsg('display - end')
def chooseFile(self, default, size, saveas, dir):
if __debug__: traceMsg('chooseFile - begin')
self.file = '%s (%.1f MB)' % (default, float(size) / (1 << 20))
self.size = size
if saveas != '':
default = saveas
self.downloadTo = abspath(default)
if __debug__: traceMsg('chooseFile - end')
return default
def writeStatus(self):
if __debug__: traceMsg('writeStatus - begin')
downRate = self.downTotal
die = False
try:
f=open(self.statFile,'r')
running = f.read(1)
f.close
except:
running = 0
self.timeEst = 'Failed To Open StatFile'
if __debug__: traceMsg('writeStatus - Failed to Open StatFile')
if __debug__: traceMsg('writeStatus - running :' + str(running))
if __debug__: traceMsg('writeStatus - stoppedAt :' + self.stoppedAt)
if running == '0':
if self.stoppedAt == '':
if self.percentDone == '100':
self.stoppedAt = '100'
else:
self.stoppedAt = str((float(self.percentDone)+100)*-1)
self.timeEst = 'Torrent Ostanovlen'
die = True
self.upRate = ''
self.downRate = ''
self.percentDone = self.stoppedAt
else:
if downRate == 0 and self.upTotal > 0:
downRate = self.size
if self.done:
self.percentDone = '100'
downRate = self.size
if self.autoShutdown == 'True':
running = '0'
if self.upTotal > 0:
self.percentShare = '%.1f' % ((float(self.upTotal)/float(downRate))*100)
else:
self.percentShare = '0.0'
if self.done and self.percentShare is not '' and self.autoShutdown == 'False':
if (float(self.percentShare) >= float(self.shareKill)) and (self.shareKill != '0'):
die = True
running = '0'
self.upRate = ''
elif (not self.done) and (self.timeEst == 'complete!') and (self.percentDone == '100.0'):
if (float(self.percentShare) >= float(self.shareKill)) and (self.shareKill != '0'):
die = True
running = '0'
self.upRate = ''
#self.finished()
lcount = 0
while 1:
lcount += 1
try:
f=open(self.statFile,'w')
f.write(running + '\n')
f.write(self.percentDone + '\n')
f.write(self.timeEst + '\n')
f.write(self.downRate + '\n')
f.write(self.upRate + '\n')
f.write(self.user + '\n')
f.write(self.seeds + '+' + self.distcopy + '\n')
f.write(self.peers + '\n')
f.write(self.percentShare + '\n')
f.write(self.shareKill + '\n')
f.write(str(self.upTotal) + '\n')
f.write(str(self.downTotal) + '\n')
f.write(str(self.size))
try:
errs = []
errs = self.scrub_errs()
for errmsg in errs:
f.write('\n' + errmsg)
except:
if __debug__: traceMsg('writeStatus - Failed during writing Errors')
pass
f.flush()
f.close()
break
except:
if __debug__: traceMsg('writeStatus - Failed to Open StatFile for Writing')
if lcount > 30:
break
pass
if die:
if __debug__: traceMsg('writeStatus - dieing - raised ki')
raise KeyboardInterrupt
def newpath(self, path):
self.downloadTo = path
def scrub_errs(self):
new_errors = []
try:
if self.errors:
last_errMsg = ''
errCount = 0
for err in self.errors:
try:
if last_errMsg == '':
last_errMsg = err
elif last_errMsg == err:
errCount += 1
elif last_errMsg != err:
if errCount > 0:
new_errors.append(last_errMsg + ' (x' + str(errCount+1) + ')')
else:
new_errors.append(last_errMsg)
errCount = 0
last_errMsg = err
except:
if __debug__: traceMsg('scrub_errs - Failed scrub')
pass
try:
if len(new_errors) > 0:
if last_errMsg != new_errors[len(new_errors)-1]:
if errCount > 0:
new_errors.append(last_errMsg + ' (x' + str(errCount+1) + ')')
else:
new_errors.append(last_errMsg)
else:
if errCount > 0:
new_errors.append(last_errMsg + ' (x' + str(errCount+1) + ')')
else:
new_errors.append(last_errMsg)
except:
if __debug__: traceMsg('scrub_errs - Failed during scrub last Msg ')
pass
if len(self.errors) > 100:
while len(self.errors) > 100 :
del self.errors[0:99]
self.errors = new_errors
except:
if __debug__: traceMsg('scrub_errs - Failed during scrub Errors')
pass
return new_errors
def run(autoDie,shareKill,statusFile,userName,params):
if __debug__: traceMsg('run - begin')
try:
f=open(statusFile+".pid",'w')
f.write(str(getpid()).strip() + "\n")
f.flush()
f.close()
except:
if __debug__: traceMsg('run - Failed to Create PID file')
pass
try:
h = HeadlessDisplayer()
h.statFile = statusFile
h.autoShutdown = autoDie
h.shareKill = shareKill
h.user = userName
while 1:
try:
config = parse_params(params)
except ValueError, e:
print 'error: ' + str(e) + '\nrun with no args for parameter explanations'
break
if not config:
print get_usage()
break
myid = createPeerID()
seed(myid)
doneflag = Event()
def disp_exception(text):
print text
rawserver = RawServer(doneflag, config['timeout_check_interval'],
config['timeout'], ipv6_enable = config['ipv6_enabled'],
failfunc = h.failed, errorfunc = disp_exception)
upnp_type = UPnP_test(config['upnp_nat_access'])
while True:
try:
listen_port = rawserver.find_and_bind(config['minport'], config['maxport'],
config['bind'], ipv6_socket_style = config['ipv6_binds_v4'],
upnp = upnp_type, randomizer = config['random_port'])
break
except socketerror, e:
if upnp_type and e == UPnP_ERROR:
print 'WARNING: COULD NOT FORWARD VIA UPnP'
upnp_type = 0
continue
print "error: Couldn't listen - " + str(e)
h.failed()
return
response = get_response(config['responsefile'], config['url'], h.error)
if not response:
break
infohash = sha(bencode(response['info'])).digest()
dow = BT1Download(h.display, h.finished, h.error, disp_exception, doneflag,
config, response, infohash, myid, rawserver, listen_port)
if not dow.saveAs(h.chooseFile, h.newpath):
break
if not dow.initFiles(old_style = True):
break
if not dow.startEngine():
dow.shutdown()
break
dow.startRerequester()
dow.autoStats()
if not dow.am_I_finished():
h.display(activity = 'Podklyushenie k piram')
rawserver.listen_forever(dow.getPortHandler())
h.display(activity = 'shutting down')
dow.shutdown()
break
try:
rawserver.shutdown()
except:
pass
if not h.done:
h.failed()
finally:
if __debug__: traceMsg('run - removing PID file :'+statusFile+".pid")
remove(statusFile+".pid")
if __debug__: traceMsg('run - end')
if __name__ == '__main__':
if argv[1:] == ['--version']:
print version
sys.exit(0)
if PROFILER:
import profile, pstats
p = profile.Profile()
p.runcall(run, argv[1],argv[2],argv[3],argv[4],argv[5:])
log = open('profile_data.'+strftime('%y%m%d%H%M%S')+'.txt','a')
normalstdout = sys.stdout
sys.stdout = log
# pstats.Stats(p).strip_dirs().sort_stats('cumulative').print_stats()
pstats.Stats(p).strip_dirs().sort_stats('time').print_stats()
sys.stdout = normalstdout
else:
run(argv[1],argv[2],argv[3],argv[4],argv[5:])