sandbox1
inpos 2016-03-04 22:00:13 +03:00
parent 430c467172
commit a17e9da7c4
2 changed files with 50 additions and 24 deletions

View File

@ -203,7 +203,7 @@ class Engine:
self.logpipe = None self.logpipe = None
# self.process = None # self.process = None
self.started = False self.started = False
self.pyrrent2http_loop = None
@staticmethod @staticmethod
def _validate_save_path(path): def _validate_save_path(path):
@ -329,7 +329,7 @@ class Engine:
self.pyrrent2http.startServices() self.pyrrent2http.startServices()
self.pyrrent2http.addTorrent() self.pyrrent2http.addTorrent()
self.pyrrent2http.startHTTP() self.pyrrent2http.startHTTP()
self.pyrrent2http_loop = threading.Thread(target = self.pyrrent2http.loop, args = (False,)) self.pyrrent2http_loop = threading.Thread(target = self.pyrrent2http.loop)
self.pyrrent2http_loop.start() self.pyrrent2http_loop.start()

View File

@ -16,11 +16,15 @@ except Exception, e:
except Exception as e: except Exception as e:
strerror = e.args strerror = e.args
print(strerror) print(strerror)
if STANDALONE:
sys.exit(1) sys.exit(1)
else:
raise
import libtorrent as lt
from random import SystemRandom from random import SystemRandom
import time import time
import urlparse, urllib import urlparse, urllib
import platform
import BaseHTTPServer import BaseHTTPServer
import SocketServer import SocketServer
import threading import threading
@ -320,7 +324,7 @@ class TorrentFS(object):
self.openedFiles.append(file_) self.openedFiles.append(file_)
def setPriority(self, index, priority): def setPriority(self, index, priority):
if self.priorities[index] != priority: if self.priorities[index] != priority:
logging.info('Setting %s priority to %d', self.info.file_at(index).path, priority) logging.info('Setting %s priority to %d' % (self.info.file_at(index).path, priority))
self.priorities[index] = priority self.priorities[index] = priority
self.handle.file_priority(index, priority) self.handle.file_priority(index, priority)
def findOpenedFile(self, file): def findOpenedFile(self, file):
@ -637,12 +641,18 @@ class Pyrrent2http(object):
self.config[k] = config_.__dict__[k] self.config[k] = config_.__dict__[k]
if self.config.uri == '': if self.config.uri == '':
parser.print_usage() parser.print_usage()
if STANDALONE:
sys.exit(1) sys.exit(1)
else:
raise "Invalid argument"
if self.config.uri.startswith('magnet:'): if self.config.uri.startswith('magnet:'):
self.magnet = True self.magnet = True
if self.config.resumeFile != '' and not self.config.keepFiles: if self.config.resumeFile != '' and not self.config.keepFiles:
logging.error('Usage of option --resume-file is allowed only along with --keep-files') logging.error('Usage of option --resume-file is allowed only along with --keep-files')
if STANDALONE:
sys.exit(1) sys.exit(1)
else:
raise
def buildTorrentParams(self, uri): def buildTorrentParams(self, uri):
fileUri = urlparse.urlparse(uri) fileUri = urlparse.urlparse(uri)
torrentParams = {} torrentParams = {}
@ -650,33 +660,39 @@ class Pyrrent2http(object):
torrentParams['url'] = uri torrentParams['url'] = uri
elif fileUri.scheme == 'file': elif fileUri.scheme == 'file':
uriPath = fileUri.path uriPath = fileUri.path
if uriPath != '' and platform.system().lower() == 'windows' and (os.path.sep == uriPath[0] or uriPath[0] == '/'): if uriPath != '' and sys.platform.startswith('win') and (os.path.sep == uriPath[0] or uriPath[0] == '/'):
uriPath = uriPath[1:] uriPath = uriPath[1:]
try: try:
absPath = os.path.abspath(uriPath) absPath = os.path.abspath(uriPath)
logging.info('Opening local file: %s', absPath) logging.info('Opening local file: %s' % (absPath,))
with open(absPath, 'rb') as f: with open(absPath, 'rb') as f:
torrent_info = lt.torrent_info(lt.bdecode(f.read())) torrent_info = lt.torrent_info(lt.bdecode(f.read()))
except Exception as e: except Exception as e:
strerror = e.args strerror = e.args
logging.error(strerror) logging.error(strerror)
if STANDALONE:
sys.exit(1) sys.exit(1)
else:
raise
torrentParams['ti'] = torrent_info torrentParams['ti'] = torrent_info
else: else:
logging.info('Will fetch: %s', uri) logging.info('Will fetch: %s' % (uri,))
try: try:
torrent_raw = urllib.urlopen(uri).read() torrent_raw = urllib.urlopen(uri).read()
torrent_info = lt.torrent_info(torrent_raw, len(torrent_raw)) torrent_info = lt.torrent_info(torrent_raw, len(torrent_raw))
except Exception as e: except Exception as e:
strerror = e.args strerror = e.args
logging.error(strerror) logging.error(strerror)
if STANDALONE:
sys.exit(1) sys.exit(1)
else:
raise
torrentParams['ti'] = torrent_info torrentParams['ti'] = torrent_info
logging.info('Setting save path: %s', self.config.downloadPath) logging.info('Setting save path: %s' % (self.config.downloadPath,))
torrentParams['save_path'] = self.config.downloadPath torrentParams['save_path'] = self.config.downloadPath
if os.path.exists(self.config.resumeFile): if os.path.exists(self.config.resumeFile):
logging.info('Loading resume file: %s', self.config.resumeFile) logging.info('Loading resume file: %s' % (self.config.resumeFile,))
try: try:
with open(self.config.resumeFile, 'rb') as f: with open(self.config.resumeFile, 'rb') as f:
torrentParams['resume_data'] = lt.bencode(f.read()) torrentParams['resume_data'] = lt.bencode(f.read())
@ -711,7 +727,7 @@ class Pyrrent2http(object):
info = self.torrentHandle.torrent_file() info = self.torrentHandle.torrent_file()
except: except:
info = self.torrentHandle.get_torrent_info() info = self.torrentHandle.get_torrent_info()
logging.info('Downloading torrent: %s', info.name()) logging.info('Downloading torrent: %s' % (info.name(),))
self.TorrentFS = TorrentFS(self, self.torrentHandle, self.config.fileIndex) self.TorrentFS = TorrentFS(self, self.torrentHandle, self.config.fileIndex)
def startHTTP(self): def startHTTP(self):
@ -724,7 +740,7 @@ class Pyrrent2http(object):
#self.main_alive.set() #self.main_alive.set()
logging.info('Starting HTTP Server...') logging.info('Starting HTTP Server...')
handler = HttpHandlerFactory() handler = HttpHandlerFactory()
logging.info('Listening HTTP on %s...\n', self.config.bindAddress) logging.info('Listening HTTP on %s...\n' % (self.config.bindAddress,))
host, strport = self.config.bindAddress.split(':') host, strport = self.config.bindAddress.split(':')
if len(strport) > 0: if len(strport) > 0:
srv_port = int(strport) srv_port = int(strport)
@ -798,7 +814,10 @@ class Pyrrent2http(object):
except IOError as e: except IOError as e:
strerror = e.args strerror = e.args
logging.error(strerror) logging.error(strerror)
if STANDALONE:
sys.exit(1) sys.exit(1)
else:
raise
settings = self.session.get_settings() settings = self.session.get_settings()
if self.config.userAgent != '': if self.config.userAgent != '':
@ -827,9 +846,12 @@ class Pyrrent2http(object):
except ValueError as e: except ValueError as e:
strerror = e.args strerror = e.args
logging.error(strerror) logging.error(strerror)
if STANDALONE:
sys.exit(1) sys.exit(1)
else:
raise
self.session.add_dht_router(host, port) self.session.add_dht_router(host, port)
logging.info('Added DHT router: %s:%d', host, port) logging.info('Added DHT router: %s:%d' % (host, port))
logging.info('Setting encryption settings') logging.info('Setting encryption settings')
try: try:
encryptionSettings = lt.pe_settings() encryptionSettings = lt.pe_settings()
@ -943,10 +965,10 @@ class Pyrrent2http(object):
alert = self.session.pop_alert() alert = self.session.pop_alert()
if isinstance(alert, alertClass): if isinstance(alert, alertClass):
return alert return alert
def loop(self, standalone = True): def loop(self):
def sigterm_handler(_signo, _stack_frame): def sigterm_handler(_signo, _stack_frame):
self.forceShutdown = True self.forceShutdown = True
if standalone: if STANDALONE:
import signal import signal
signal.signal(signal.SIGTERM, sigterm_handler) signal.signal(signal.SIGTERM, sigterm_handler)
self.statsTicker = Ticker(30) self.statsTicker = Ticker(30)
@ -1056,11 +1078,15 @@ class Pyrrent2http(object):
logging.info('Aborting the session') logging.info('Aborting the session')
del self.session del self.session
logging.info('Bye bye') logging.info('Bye bye')
if STANDALONE:
sys.exit(0) sys.exit(0)
else:
return
STANDALONE = False
if __name__ == '__main__': if __name__ == '__main__':
STANDALONE = True
try: try:
import logging import logging
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG) logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)