-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathbittorrent.py
executable file
·286 lines (232 loc) · 9.72 KB
/
bittorrent.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
#!/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/>.
# Written by Matt Chisholm and Greg Hazel
from __future__ import division
app_name = "BitTorrent"
import os
import sys
try:
from BitTorrent.translation import _
except ImportError, e:
if os.name == 'posix':
# Ugly Idiot-proofing -- this should stop ALL bug reports from
# people unable to run BitTorrent after installation on Debian
# and RedHat based systems.
pythonversion = sys.version[:3]
py24 = os.path.exists('/usr/lib/python2.4/site-packages/BitTorrent/')
py23 = os.path.exists('/usr/lib/python2.3/site-packages/BitTorrent/')
if not py24 and not py23:
print "There is no BitTorrent package installed on this system."
elif py24 and py23:
print """
There is more than one BitTorrent package installed on this system,
at least one under Python 2.3 and at least one under Python 2.4."""
else:
print """
A BitTorrent package for the wrong version of Python is installed on this
system. The default version of Python on this system is %s. However, the
BitTorrent package is installed under Python %s.""" % (pythonversion, (py24 and '2.4' or '2.3'))
print """
To install BitTorrent correctly you must first:
* Remove *all* versions of BitTorrent currently installed.
Then, you have two options:
* Download and install the .deb or .rpm package for
BitTorrent & Python %s
* Download the source .tar.gz and follow the directions for
installing under Python %s
Visit http://www.bittorrent.com/ to download BitTorrent.
""" % (pythonversion, pythonversion)
sys.exit(1)
else:
raise
import time
import BTL.stackthreading as threading
import logging
debug=False
#debug=True
from BTL import atexit_threads
assert sys.version_info >= (2, 3), _("Install Python %s or greater") % '2.3'
from BitTorrent import BTFailure, inject_main_logfile
from BitTorrent import configfile
from BTL.defer import DeferredEvent, wrap_task
from BitTorrent.defaultargs import get_defaults
from BitTorrent.IPC import ipc_interface
from BitTorrent.prefs import Preferences
from BitTorrent.RawServer_twisted import RawServer
if os.name == 'nt':
from BitTorrent.platform import win_version_num
from BitTorrent import zurllib
defaults = get_defaults('bittorrent')
defaults.extend((('donated' , '', ''), # the version that the user last donated for
('notified', '', ''), # the version that the user was last notified of
))
defconfig = dict([(name, value) for (name, value, doc) in defaults])
del name, value, doc
inject_main_logfile()
global_logger = logging.getLogger('')
rawserver = None
if __name__ == '__main__':
psyco = None
try:
# 95, 98, and ME seem to have problems with psyco
# so only import it on NT and up
# and only if we're not using python 2.5, becuase it's broken
if (os.name == 'nt' and win_version_num >= (2, 4, 0) and
sys.version_info < (2, 5)):
import psyco_BROKEN
import traceback
psyco.cannotcompile(traceback.print_stack)
psyco.cannotcompile(traceback.format_stack)
psyco.cannotcompile(traceback.extract_stack)
#psyco.full(memory=10)
psyco.bind(RawServer.listen_forever)
from BTL import sparse_set
psyco.bind(sparse_set.SparseSet)
from BitTorrent import PiecePicker
psyco.bind(PiecePicker.PieceBuckets)
psyco.bind(PiecePicker.PiecePicker)
from BitTorrent import PieceSetBuckets
psyco.bind(PieceSetBuckets.PieceSetBuckets)
psyco.bind(PieceSetBuckets.SortedPieceBuckets)
psyco.profile(memorymax=30000) # that's 30MB for the whole process
#psyco.log()
# see below for more
except ImportError:
pass
zurllib.add_unsafe_thread()
try:
config, args = configfile.parse_configuration_and_args(defaults,
'bittorrent', sys.argv[1:], 0, None)
if debug:
config['upnp'] = False
config['one_connection_per_ip'] = False
except BTFailure, e:
print unicode(e.args[0])
sys.exit(1)
config = Preferences().initWithDict(config)
# bug set in DownloadInfoFrame
rawserver = RawServer(config)
zurllib.set_zurllib_rawserver(rawserver)
rawserver.install_sigint_handler()
ipc = ipc_interface(rawserver, config, "controlsocket")
# make sure we clean up the ipc when everything is done
atexit_threads.register_verbose(ipc.stop)
# this could be on the ipc object
ipc_master = True
try:
if not config['use_factory_defaults']:
ipc.create()
except BTFailure, e:
ipc_master = False
try:
ipc.send_command('no-op')
if config['publish']:
assert len(args) == 1
ipc.send_command('publish_torrent', args[0], config['publish'])
sys.exit(0)
elif args:
for arg in args:
ipc.send_command('start_torrent', arg)
sys.exit(0)
ipc.send_command('show_error', _("%s already running")%app_name)
except BTFailure:
global_logger.error((_("Failed to communicate with another %s process "
"but one seems to be running.") % app_name) +
(_(" Closing all %s windows may fix the problem.")
% app_name))
sys.exit(1)
from BitTorrent.MultiTorrent import MultiTorrent
from BTL.ThreadProxy import ThreadProxy
from BitTorrent.TorrentButler import DownloadTorrentButler, SeedTorrentButler
from BitTorrent.AutoUpdateButler import AutoUpdateButler
from BitTorrent.GUI_wx.DownloadManager import MainLoop
from BitTorrent.GUI_wx import gui_wrap
def gmtime():
return time.mktime(time.gmtime())
if __name__ == '__main__':
#import memleak_detection
#memleak_detection.begin_sampling('memleak_sample.log')
if psyco:
psyco.bind(MainLoop.run)
config['start_time'] = gmtime()
mainloop = MainLoop(config)
def init_core(mainloop):
core_doneflag = DeferredEvent()
class UILogger(logging.Handler):
def emit(self, record):
msg = "[%s] %s" % (record.name, self.format(record))
gui_wrap(mainloop.do_log, record.levelno, msg)
logging.getLogger('').addHandler(UILogger())
try:
multitorrent = MultiTorrent(config, rawserver, config['data_dir'],
listen_fail_ok=True,
init_torrents=False)
# Butlers
multitorrent.add_policy(DownloadTorrentButler(multitorrent))
multitorrent.add_policy(SeedTorrentButler(multitorrent))
auto_update_butler = AutoUpdateButler(multitorrent, rawserver,
test_new_version=config['new_version'],
test_current_version=config['current_version'])
multitorrent.add_auto_update_policy(auto_update_butler)
# attach to the UI
tpm = ThreadProxy(multitorrent,
gui_wrap,
wrap_task(rawserver.external_add_task))
mainloop.attach_multitorrent(tpm, core_doneflag)
ipc.start(mainloop.external_command)
#rawserver.associate_thread()
# register shutdown action
def shutdown():
df = multitorrent.shutdown()
stop_rawserver = lambda r : rawserver.stop()
df.addCallbacks(stop_rawserver, stop_rawserver)
rawserver.add_task(0, core_doneflag.addCallback,
lambda r: rawserver.external_add_task(0, shutdown))
rawserver.listen_forever()
except:
# oops, we failed.
# one message for the log w/ exception info
global_logger.exception("BitTorrent core initialization failed!")
# one message for the user w/o info
global_logger.critical("BitTorrent core initialization failed!")
core_doneflag.set()
rawserver.stop()
try:
gui_wrap(mainloop.ExitMainLoop)
except:
pass
try:
gui_wrap(mainloop.doneflag.set)
except:
pass
raise
threading.Thread(target=init_core, args=(mainloop,)).start()
mainloop.append_external_torrents(*args)
## # cause memleak stuff to be imported
## import code
## import sizer
##
## from sizer import annotate
## from sizer import formatting
## from sizer import operations
## from sizer import rules
## from sizer import scanner
## from sizer import set
## from sizer import sizes
## from sizer import wrapper
try:
mainloop.run()
except KeyboardInterrupt:
# the gui main loop is closed in MainLoop
pass