-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathbittorrent-curses.py
executable file
·632 lines (568 loc) · 25.7 KB
/
bittorrent-curses.py
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
#!/usr/bin/env python
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Original version written by Henry 'Pi' James, modified by (at least)
# John Hoffman and Uoti Urpala and David Harrison
from __future__ import division
app_name = "BitTorrent"
from BitTorrent.translation import _
SPEW_SCROLL_RATE = 1
import sys
import os
import logging
from logging import ERROR, WARNING
from time import time, strftime, sleep
import traceback
from BitTorrent import platform
from BTL.platform import encode_for_filesystem, decode_from_filesystem
import BTL.stackthreading as threading
from BTL.defer import DeferredEvent
from BitTorrent.MultiTorrent import MultiTorrent, TorrentAlreadyRunning
from BitTorrent.Torrent import Feedback
from BitTorrent.defaultargs import get_defaults
from BitTorrent.parseargs import printHelp
from BitTorrent.prefs import Preferences
from BTL.obsoletepythonsupport import import_curses
from BitTorrent import configfile
from BitTorrent import BTFailure, UserFailure
from BitTorrent import version
from BitTorrent import GetTorrent
from BitTorrent.RawServer_twisted import RawServer
from twisted.internet import task
from BTL.ConvertedMetainfo import ConvertedMetainfo
from BitTorrent import inject_main_logfile
debug = False
#debug = True
inject_main_logfile()
from BitTorrent import console
from BitTorrent import stderr_console # must import after inject_main_logfile
# because import is really a copy.
# If imported earlier, stderr_console
# doesn't reflect the changes made in
# inject_main_logfile!!
# BAAAHHHH!!
def wrap_log(context_string, logger):
"""Useful when passing a logger to a deferred's errback. The context
specifies what was being done when the exception was raised."""
return lambda e, *args, **kwargs : logger.error(context_string, exc_info=e)
try:
curses = import_curses()
import curses.panel
from curses.wrapper import wrapper as curses_wrapper
from signal import signal, SIGWINCH
except:
print _("Textmode UI initialization failed, cannot proceed.")
print
print _("This download interface requires the standard Python module "
"\"curses\", which is unfortunately not available for the native "
"Windows port of Python. It is however available for the Cygwin "
"port of Python, running on all Win32 systems (www.cygwin.com).")
print
print _('You may still use "bittorrent-console" to download.')
sys.exit(1)
def fmttime(n):
if n == 0:
return _("download 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)
return _("finishing in %d:%02d:%02d") % (h, m, s)
def fmtsize(n):
s = str(n)
size = s[-3:]
while len(s) > 3:
s = s[:-3]
size = '%s,%s' % (s[-3:], size)
if n > 999:
unit = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']
i = 1
while i + 1 < len(unit) and (n >> 10) >= 999:
i += 1
n >>= 10
n /= (1 << 10)
size = '%s (%.0f %s)' % (size, n, unit[i])
return size
class CursesDisplayer(object):
def __init__(self, scrwin, errlist, doneflag, reread_config, ulrate):
self.scrwin = scrwin
self.errlist = errlist
self.doneflag = doneflag
signal(SIGWINCH, self.winch_handler)
self.changeflag = DeferredEvent()
self.done = False
self.reread_config = reread_config
self.ulrate = ulrate
self.activity = ''
self.status = ''
self.progress = ''
self.downRate = '---'
self.upRate = '---'
self.shareRating = ''
self.seedStatus = ''
self.peerStatus = ''
self.errors = []
self.file = ''
self.downloadTo = ''
self.fileSize = ''
self.numpieces = 0
self.spew_scroll_time = 0
self.spew_scroll_pos = 0
self._remake_window()
curses.use_default_colors()
def set_torrent_values(self, name, path, size, numpieces):
self.file = name
self.downloadTo = path
self.fileSize = fmtsize(size)
self.numpieces = numpieces
self._remake_window()
def winch_handler(self, signum, stackframe):
self.changeflag.set()
curses.endwin()
self.scrwin.refresh()
self.scrwin = curses.newwin(0, 0, 0, 0)
self._remake_window()
def _remake_window(self):
self.scrh, self.scrw = self.scrwin.getmaxyx()
self.scrpan = curses.panel.new_panel(self.scrwin)
self.labelh, self.labelw, self.labely, self.labelx = 11, 9, 1, 2
self.labelwin = curses.newwin(self.labelh, self.labelw,
self.labely, self.labelx)
self.labelpan = curses.panel.new_panel(self.labelwin)
self.fieldh, self.fieldw, self.fieldy, self.fieldx = (
self.labelh, self.scrw-2 - self.labelw-3,
1, self.labelw+3)
self.fieldwin = curses.newwin(self.fieldh, self.fieldw,
self.fieldy, self.fieldx)
self.fieldwin.nodelay(1)
self.fieldpan = curses.panel.new_panel(self.fieldwin)
self.spewh, self.speww, self.spewy, self.spewx = (
self.scrh - self.labelh - 2, self.scrw - 3, 1 + self.labelh, 2)
self.spewwin = curses.newwin(self.spewh, self.speww,
self.spewy, self.spewx)
self.spewpan = curses.panel.new_panel(self.spewwin)
try:
self.scrwin.border(ord('|'),ord('|'),ord('-'),ord('-'),ord(' '),ord(' '),ord(' '),ord(' '))
except:
pass
self.labelwin.addstr(0, 0, _("file:"))
self.labelwin.addstr(1, 0, _("size:"))
self.labelwin.addstr(2, 0, _("dest:"))
self.labelwin.addstr(3, 0, _("progress:"))
self.labelwin.addstr(4, 0, _("status:"))
self.labelwin.addstr(5, 0, _("dl speed:"))
self.labelwin.addstr(6, 0, _("ul speed:"))
self.labelwin.addstr(7, 0, _("sharing:"))
self.labelwin.addstr(8, 0, _("seeds:"))
self.labelwin.addstr(9, 0, _("peers:"))
curses.panel.update_panels()
curses.doupdate()
self.changeflag.clear()
def finished(self):
self.done = True
self.downRate = '---'
self.display({'activity':_("download succeeded"), 'fractionDone':1})
def error(self, errormsg):
newerrmsg = strftime('[%H:%M:%S] ') + errormsg
self.errors.append(newerrmsg.split('\n')[0])
self.errlist.append(newerrmsg)
self.display({})
def display(self, statistics):
fractionDone = statistics.get('fractionDone')
activity = statistics.get('activity')
timeEst = statistics.get('timeEst')
downRate = statistics.get('downRate')
upRate = statistics.get('upRate')
spew = statistics.get('spew')
inchar = self.fieldwin.getch()
if inchar == 12: # ^L
self._remake_window()
elif inchar in (ord('q'),ord('Q')):
self.doneflag.set()
elif inchar in (ord('r'),ord('R')):
self.reread_config()
elif inchar in (ord('u'),ord('U')):
curses.echo()
self.fieldwin.nodelay(0)
s = self.fieldwin.getstr(6,10)
curses.noecho()
self.fieldwin.nodelay(1)
r = None
try:
r = int(s)
except ValueError:
pass
if r is not None:
self.ulrate(r)
if timeEst is not None:
self.activity = fmttime(timeEst)
elif activity is not None:
self.activity = activity
if self.changeflag.isSet():
return
if fractionDone is not None:
blocknum = int(self.fieldw * fractionDone)
self.progress = blocknum * '#' + (self.fieldw - blocknum) * '_'
self.status = '%s (%.1f%%)' % (self.activity, fractionDone * 100)
if downRate is not None:
self.downRate = '%.1f KB/s' % (downRate / (1 << 10))
if upRate is not None:
self.upRate = '%.1f KB/s' % (upRate / (1 << 10))
downTotal = statistics.get('downTotal')
if downTotal is not None:
upTotal = statistics['upTotal']
if downTotal <= upTotal / 100:
self.shareRating = _("oo (%.1f MB up / %.1f MB down)") % (
upTotal / (1<<20), downTotal / (1<<20))
else:
self.shareRating = _("%.3f (%.1f MB up / %.1f MB down)") % (
upTotal / downTotal, upTotal / (1<<20), downTotal / (1<<20))
#numCopies = statistics['numCopies']
#nextCopies = ', '.join(["%d:%.1f%%" % (a,int(b*1000)/10) for a,b in
# zip(xrange(numCopies+1, 1000), statistics['numCopyList'])])
if not self.done:
self.seedStatus = _("%d seen now") % statistics['numSeeds']
# self.seedStatus = _("%d seen now, plus %d distributed copies"
# "(%s)") % (statistics['numSeeds' ],
# statistics['numCopies'],
# nextCopies)
else:
self.seedStatus = ""
# self.seedStatus = _("%d distributed copies (next: %s)") % (
# statistics['numCopies'], nextCopies)
self.peerStatus = _("%d seen now") % statistics['numPeers']
self.fieldwin.erase()
self.fieldwin.addnstr(0, 0, self.file, self.fieldw, curses.A_BOLD)
self.fieldwin.addnstr(1, 0, self.fileSize, self.fieldw)
self.fieldwin.addnstr(2, 0, self.downloadTo, self.fieldw)
if self.progress:
self.fieldwin.addnstr(3, 0, self.progress, self.fieldw, curses.A_BOLD)
self.fieldwin.addnstr(4, 0, self.status, self.fieldw)
self.fieldwin.addnstr(5, 0, self.downRate, self.fieldw)
self.fieldwin.addnstr(6, 0, self.upRate, self.fieldw)
self.fieldwin.addnstr(7, 0, self.shareRating, self.fieldw)
self.fieldwin.addnstr(8, 0, self.seedStatus, self.fieldw)
self.fieldwin.addnstr(9, 0, self.peerStatus, self.fieldw)
self.spewwin.erase()
if not spew:
errsize = self.spewh
if self.errors:
self.spewwin.addnstr(0, 0, _("log:"), self.speww,
curses.A_BOLD)
errsize = len(self.errors)
displaysize = min(errsize, self.spewh-1)
displaytop = errsize - displaysize
errlst = [e[0:self.speww-self.labelw-2] for e in self.errors]
for i in range(displaysize):
self.spewwin.addnstr(i, self.labelw,
errlst[displaytop + i],
#self.speww-self.labelw-1,
self.speww-self.labelw-2,
curses.A_BOLD)
else:
if self.errors:
self.spewwin.addnstr(0, 0, _("log:"), self.speww,
curses.A_BOLD)
err = self.errors[-1][0:self.speww-self.labelw-2]
self.spewwin.addnstr(0, self.labelw, err,
self.speww-self.labelw-1, curses.A_BOLD)
self.spewwin.addnstr(2, 0, _(" # IP Upload Download Completed Speed"), self.speww, curses.A_BOLD)
if self.spew_scroll_time + SPEW_SCROLL_RATE < time():
self.spew_scroll_time = time()
if len(spew) > self.spewh-5 or self.spew_scroll_pos > 0:
self.spew_scroll_pos += 1
if self.spew_scroll_pos > len(spew):
self.spew_scroll_pos = 0
for i in range(len(spew)):
spew[i]['lineno'] = i+1
spew.append({'lineno': None})
spew = spew[self.spew_scroll_pos:] + spew[:self.spew_scroll_pos]
for i in range(min(self.spewh - 5, len(spew))):
if not spew[i]['lineno']:
continue
self.spewwin.addnstr(i+3, 0, '%3d' % spew[i]['lineno'], 3)
self.spewwin.addnstr(i+3, 4, spew[i]['ip'], 15)
ul = spew[i]['upload']
if ul[1] > 100:
self.spewwin.addnstr(i+3, 20, '%6.0f KB/s' % (
ul[1] / 1000), 11)
self.spewwin.addnstr(i+3, 32, '-----', 5)
if ul[2]:
self.spewwin.addnstr(i+3, 33, 'I', 1)
if ul[3]:
self.spewwin.addnstr(i+3, 35, 'C', 1)
dl = spew[i]['download']
if dl[1] > 100:
self.spewwin.addnstr(i+3, 38, '%6.0f KB/s' % (
dl[1] / 1000), 11)
self.spewwin.addnstr(i+3, 50, '-------', 7)
if dl[2]:
self.spewwin.addnstr(i+3, 51, 'I', 1)
if dl[3]:
self.spewwin.addnstr(i+3, 53, 'C', 1)
if dl[4]:
self.spewwin.addnstr(i+3, 55, 'S', 1)
self.spewwin.addnstr(i+3, 58,
'%5.1f%%' % (int(spew[i]['completed']*1000)/10), 6)
if spew[i]['speed'] is not None:
self.spewwin.addnstr(i+3, 64,
'%5.0f KB/s' % (spew[i]['speed']/1000), 10)
"""
self.spewwin.addnstr(self.spewh-1, 0,
_("downloading %d pieces, have %d fragments, "
"%d of %d pieces completed") %
(statistics['storage_active'], statistics['storage_dirty'],
statistics['storage_numcomplete'], self.numpieces),
self.speww-1)
"""
curses.panel.update_panels()
curses.doupdate()
class CursesTorrentApp(object):
class LogHandler(logging.Handler):
def __init__(self, app, level=logging.NOTSET):
logging.Handler.__init__(self,level)
self.app = app
def emit(self, record):
if len(record.getMessage()) > 0:
self.app.display_error(record.getMessage() )
if record.exc_info is not None:
self.app.display_error(
"Traceback (most recent call last):" )
tb = record.exc_info[2]
stack = traceback.extract_tb(tb)
l = traceback.format_list(stack)
for s in l:
self.app.display_error( " %s" % s )
self.app.display_error( " %s: %s" %
( str(record.exc_info[0]),str(record.exc_info[1])))
class LogFilter(logging.Filter):
def filter( self, record):
if record.name == "NatTraversal":
return 0
return 1 # allow.
def __init__(self, metainfo, config, errlist):
assert isinstance(metainfo, ConvertedMetainfo )
self.metainfo = metainfo
self.config = Preferences().initWithDict(config)
self.errlist = errlist
self.torrent = None
self.multitorrent = None
self.d = None # displayer (i.e., curses UI)
self.logger = logging.getLogger("bittorrent-curses")
log_handler = CursesTorrentApp.LogHandler(self)
log_handler.setLevel(WARNING)
#log_handler.setLevel(0)
logger = logging.getLogger()
logger.addHandler(log_handler)
# disable stdout and stderr error reporting.
global stderr_console
logging.getLogger().removeHandler(console)
if stderr_console is not None:
logging.getLogger().removeHandler(stderr_console)
# We log everything. If we want to omit certain errors then
# either raise the level or install a logging.Filter:
log_handler.addFilter(CursesTorrentApp.LogFilter())
logging.getLogger().setLevel(WARNING)
#logging.getLogger().setLevel(0)
def start_torrent(self,metainfo,save_incomplete_as,save_as):
"""Tells the MultiTorrent to begin downloading."""
try:
self.d.display({'activity':_("initializing"),
'fractionDone':0})
multitorrent = self.multitorrent
df = multitorrent.create_torrent(metainfo, save_incomplete_as,
save_as)
df.addErrback( wrap_log('Failed to start torrent', self.logger))
def create_finished(*args, **argv):
self.torrent = multitorrent.get_torrent(metainfo.infohash)
if self.torrent.is_initialized():
multitorrent.start_torrent(metainfo.infohash)
else:
self.d.display({'activity':_("already being downloaded"),
'fractionDone':0})
self.core_doneflag.set() # e.g., if already downloading...
df.addCallback( create_finished )
except KeyboardInterrupt:
raise
except UserFailure, e:
self.logger.error( "Failed to create torrent: " + unicode(e.args[0]) )
except Exception, e:
self.logger.error( "Failed to create torrent", exc_info = e )
def run(self, scrwin):
self.core_doneflag = DeferredEvent()
rawserver = RawServer(self.config)
# set up shut-down procedure before we begin doing things that
# can throw exceptions.
def shutdown():
if self.d:
self.d.display({'activity':_("shutting down"),
'fractionDone':0})
if self.multitorrent:
df = self.multitorrent.shutdown()
set_flag = lambda *a : rawserver.stop()
df.addCallbacks(set_flag, set_flag)
else:
rawserver.stop()
# It is safe to addCallback here, because there is only one thread,
# but even if the code were multi-threaded, core_doneflag has not
# been passed to anyone. There is no chance of a race condition
# between the DeferredEvent'scallback and addCallback.
self.core_doneflag.addCallback(
lambda r: rawserver.external_add_task(0, shutdown))
def reread():
if self.multitorrent is not None:
rawserver.external_add_task(0,self.reread_config)
def ulrate(value):
if self.multitorrent is not None:
self.multitorrent.set_option('max_upload_rate', value)
if self.torrent != None:
self.torrent.set_option('max_upload_rate', value)
self.d = CursesDisplayer(scrwin, self.errlist, self.core_doneflag,
reread, ulrate)
rawserver.install_sigint_handler(self.core_doneflag)
# semantics for --save_in vs --save_as:
# save_in specifies the directory in which torrent is written.
# If the torrent is a batch torrent then the files in the batch
# go in save_in/metainfo.name_fs/.
# save_as specifies the filename for the torrent in the case of
# a non-batch torrent, and specifies the directory name
# in the case of a batch torrent. Thus the files in a batch
# torrent go in save_as/.
metainfo = self.metainfo
torrent_name = metainfo.name_fs # if batch then this contains
# directory name.
if config['save_as']:
if config['save_in']:
raise BTFailure(_("You cannot specify both --save_as and "
"--save_in."))
saveas,bad = encode_for_filesystem(config['save_as'])
if bad:
raise BTFailure(_("Invalid path encoding."))
savein = os.path.dirname(os.path.abspath(saveas))
elif config['save_in']:
savein,bad = encode_for_filesystem(config['save_in'])
if bad:
raise BTFailure(_("Invalid path encoding."))
saveas = os.path.join(savein,torrent_name)
else:
saveas = torrent_name
if config['save_incomplete_in']:
save_incomplete_in,bad = \
encode_for_filesystem(config['save_incomplete_in'])
if bad:
raise BTFailure(_("Invalid path encoding."))
save_incomplete_as = os.path.join(save_incomplete_in,torrent_name)
else:
save_incomplete_as = os.path.join(savein,torrent_name)
data_dir,bad = encode_for_filesystem(config['data_dir'])
if bad:
raise BTFailure(_("Invalid path encoding."))
try:
self.multitorrent = \
MultiTorrent(self.config, rawserver, data_dir,
is_single_torrent = True,
resume_from_torrent_config = False)
self.d.set_torrent_values(metainfo.name, os.path.abspath(saveas),
metainfo.total_bytes, len(metainfo.hashes))
self.start_torrent(self.metainfo, save_incomplete_as, saveas)
self.get_status()
except UserFailure, e:
self.logger.error( unicode(e.args[0]) )
rawserver.add_task(0,core_doneflag.set())
except Exception, e:
self.logger.error( "", exc_info = e )
rawserver.add_task(0,core_doneflag.set())
# always make sure events get processed even if only for
# shutting down.
rawserver.listen_forever()
def reread_config(self):
try:
newvalues = configfile.get_config(self.config, 'bittorrent-curses')
except Exception, e:
self.d.error(_("Error reading config: ") + unicode(e.args[0]))
return
self.config.update(newvalues)
# The set_option call can potentially trigger something that kills
# the torrent (when writing this the only possibility is a change in
# max_files_open causing an IOError while closing files), and so
# the self.failed() callback can run during this loop.
for option, value in newvalues.iteritems():
self.multitorrent.set_option(option, value)
if self.torrent is not None:
for option, value in newvalues.iteritems():
self.torrent.set_option(option, value)
def get_status(self):
self.multitorrent.rawserver.add_task(self.config['display_interval'],
self.get_status)
if self.torrent is not None:
status = self.torrent.get_status(self.config['spew'])
self.d.display(status)
def display_error(self, text):
"""Called by the logger via LogHandler to display error messages in the
curses window."""
self.d.error(text)
if __name__ == '__main__':
uiname = 'bittorrent-curses'
defaults = get_defaults(uiname)
metainfo = None
if len(sys.argv) <= 1:
printHelp(uiname, defaults)
sys.exit(1)
try:
# Modifying default values from get_defaults is annoying...
# Implementing specific default values for each uiname in
# defaultargs.py is even more annoying. --Dave
data_dir = [[name, value,doc] for (name, value, doc) in defaults
if name == "data_dir"][0]
defaults = [(name, value,doc) for (name, value, doc) in defaults
if not name == "data_dir"]
ddir = os.path.join( platform.get_dot_dir(), "curses" )
data_dir[1] = decode_from_filesystem(ddir)
defaults.append( tuple(data_dir) )
config, args = configfile.parse_configuration_and_args(defaults,
uiname, sys.argv[1:], 0, 1)
torrentfile = None
if len(args):
torrentfile = args[0]
if torrentfile is not None:
try:
metainfo = GetTorrent.get(torrentfile)
except GetTorrent.GetTorrentException, e:
raise UserFailure(_("Error reading .torrent file: ") + '\n' + \
unicode(e.args[0]))
else:
raise UserFailure(_("you must specify a .torrent file"))
except BTFailure, e:
print unicode(e.args[0])
sys.exit(1)
errlist = []
app = CursesTorrentApp(metainfo, config, errlist)
curses_wrapper(app.run)
if errlist:
print _("These errors occurred during execution:")
for error in errlist:
print error
sys.stdout.flush()
# if after a reasonable amount of time there are still
# non-daemon threads hanging around then print them.
nondaemons = [d for d in threading.enumerate() if not d.isDaemon()]
if len(nondaemons) > 1:
sleep(4)
nondaemons = [d for d in threading.enumerate() if not d.isDaemon()]
if len(nondaemons) > 1:
print "non-daemon threads not shutting down:"
for th in nondaemons:
print " ", th