r101 - in /debtorrent/branches/http-listen/DebTorrent: BT1/AptListener.py launchmanycore.py
camrdale-guest at users.alioth.debian.org
camrdale-guest at users.alioth.debian.org
Wed Jun 13 21:00:55 UTC 2007
Author: camrdale-guest
Date: Wed Jun 13 21:00:55 2007
New Revision: 101
URL: http://svn.debian.org/wsvn/debtorrent/?sc=1&rev=101
Log:
AptListener starts torrents when downloading Packages files.
btlaunchmany no longer scans a directory.
Modified:
debtorrent/branches/http-listen/DebTorrent/BT1/AptListener.py
debtorrent/branches/http-listen/DebTorrent/launchmanycore.py
Modified: debtorrent/branches/http-listen/DebTorrent/BT1/AptListener.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/branches/http-listen/DebTorrent/BT1/AptListener.py?rev=101&op=diff
==============================================================================
--- debtorrent/branches/http-listen/DebTorrent/BT1/AptListener.py (original)
+++ debtorrent/branches/http-listen/DebTorrent/BT1/AptListener.py Wed Jun 13 21:00:55 2007
@@ -27,6 +27,8 @@
from os import rename, getpid
from os.path import exists, isfile
from cStringIO import StringIO
+from gzip import GzipFile
+from bz2 import decompress
from traceback import print_exc
from time import time, gmtime, strftime, localtime
from DebTorrent.clock import clock
@@ -36,6 +38,8 @@
from binascii import b2a_hex, a2b_hex, a2b_base64
from string import lower
from time import sleep
+from makemetafile import uniconvertl, uniconvert
+from os.path import split
import sys, os
import signal
import re
@@ -570,6 +574,111 @@
self.enqueue_request(connection, d, f, pieces_needed)
+ def get_Packages(self, path):
+ """Download a Packages file and start a torrent.
+
+ @type path: C{list} of C{string}
+ @param path: the path of the file to download, starting with the mirror name
+ @rtype: (C{int}, C{string}, C{dictionary}, C{string})
+ @return: the HTTP status code, status message, headers, and Packages file
+
+ """
+
+ # Download the Packages file
+ r = self.get_file(path)
+
+ if not r[0] == 200:
+ return r
+
+ try:
+ # Decompress the data
+ if path[-1].endswith('.gz'):
+ compressed = StringIO(r[3])
+ f = GzipFile(fileobj = compressed)
+ data = f.read()
+ elif path[-1].endswith('.bz2'):
+ data = decompress(r[3])
+ else:
+ data = r[3]
+
+ name = "dt_" + '_'.join(path)
+
+ assert data[:8] == "Package:"
+ h = data.split('\n')
+ except:
+ if DEBUG:
+ print 'ERROR: Packages file could not be converted to a torrent'
+ return r
+
+ pieces = []
+ lengths = []
+ fs = []
+
+ p = [None, None, None]
+ for line in h:
+ line = line.rstrip()
+ if line == "":
+ if (p[0] and p[1] and p[2]):
+ fpath = []
+ while p[1]:
+ p[1],d = split(p[1])
+ fpath.insert(0,d)
+ fs.append({'length': p[0], 'path': fpath})
+ lengths.append(p[0])
+ pieces.append(p[2])
+ p = [None, None, None]
+ if line[:9] == "Filename:":
+ p[1] = line[10:]
+ if line[:5] == "Size:":
+ p[0] = long(line[6:])
+ if line[:5] == "SHA1:":
+ p[2] = a2b_hex(line[6:])
+
+ response = {'info': {'pieces': ''.join(pieces),
+ 'piece lengths': lengths, 'files': fs },
+ 'announce': 'http://dttracker.debian.net:6969/announce',
+ 'name': name }
+
+ if path.count('dists'):
+ mirror = 'http://' + '/'.join(path[:path.index('dists')]) + '/'
+ response['deb_mirrors'] = [mirror]
+
+ infohash = sha(bencode(response['info'])).digest()
+
+ if self.handler.has_torrent(infohash):
+ return r
+
+ a = {}
+ a['path'] = '/'.join(path)
+ a['file'] = name
+ a['type'] = path[-1]
+ i = response['info']
+ l = 0
+ nf = 0
+ if i.has_key('length'):
+ l = i.get('length',0)
+ nf = 1
+ elif i.has_key('files'):
+ for li in i['files']:
+ nf += 1
+ if li.has_key('length'):
+ l += li['length']
+ a['numfiles'] = nf
+ a['length'] = l
+ a['name'] = name
+ def setkey(k, d = response, a = a):
+ if d.has_key(k):
+ a[k] = d[k]
+ setkey('failure reason')
+ setkey('warning message')
+ setkey('announce-list')
+ a['metainfo'] = response
+
+ self.handler.add(infohash, a)
+
+ return r
+
+
def get(self, connection, path, headers):
"""Respond to a GET request.
@@ -655,8 +764,8 @@
if 'Packages.diff' in path:
return (404, 'Not Found', {'Server': VERSION, 'Content-Type': 'text/plain', 'Pragma': 'no-cache'}, alas)
-# if path[-1] in ('Packages', 'Packages.gz', 'Packages.bz2'):
-# return self.get_Packages(path)
+ if path[-1] in ('Packages', 'Packages.gz', 'Packages.bz2'):
+ return self.get_Packages(path)
if path[-1][-4:] == '.deb':
return self.get_package(connection, path)
Modified: debtorrent/branches/http-listen/DebTorrent/launchmanycore.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/branches/http-listen/DebTorrent/launchmanycore.py?rev=101&op=diff
==============================================================================
--- debtorrent/branches/http-listen/DebTorrent/launchmanycore.py (original)
+++ debtorrent/branches/http-listen/DebTorrent/launchmanycore.py Wed Jun 13 21:00:55 2007
@@ -385,7 +385,7 @@
self.handler = MultiHandler(self.rawserver, self.doneflag, config)
seed(createPeerID())
- self.rawserver.add_task(self.scan, 0)
+# self.rawserver.add_task(self.scan, 0)
self.rawserver.add_task(self.stats, 0)
self.handler.listen_forever()
@@ -510,6 +510,7 @@
"""
+ self.torrent_cache.setdefault(hash, data)
c = self.counter
self.counter += 1
x = ''
@@ -650,6 +651,18 @@
if self.torrent_cache.has_key(hash):
self.Output.message('DIED: "'+self.torrent_cache[hash]['path']+'"')
+ def has_torrent(self, hash):
+ """Determine whether there is a downloader for the torrent.
+
+ @type hash: C{string}
+ @param hash: the info hash of the torrent
+ @rtype: C{boolean}
+ @return: whether the torrent is in the cache of known torrents
+
+ """
+
+ return self.torrent_cache.has_key(hash)
+
def was_stopped(self, hash):
"""Remove the torrent from the hash check queue, even if it's already happening.
More information about the Debtorrent-commits
mailing list