r181 - in /debtorrent/trunk: ./ DebTorrent/ DebTorrent/BT1/

camrdale-guest at users.alioth.debian.org camrdale-guest at users.alioth.debian.org
Tue Jul 17 06:42:13 UTC 2007


Author: camrdale-guest
Date: Tue Jul 17 06:42:12 2007
New Revision: 181

URL: http://svn.debian.org/wsvn/debtorrent/?sc=1&rev=181
Log:
Use logging module for almost all output logging (tracker as well).
Tracker behaves more like the client, using a cache directory for logs/state.

Modified:
    debtorrent/trunk/DebTorrent/BT1/AptListener.py
    debtorrent/trunk/DebTorrent/BT1/Connecter.py
    debtorrent/trunk/DebTorrent/BT1/Encrypter.py
    debtorrent/trunk/DebTorrent/BT1/FileSelector.py
    debtorrent/trunk/DebTorrent/BT1/HTTPDownloader.py
    debtorrent/trunk/DebTorrent/BT1/NatCheck.py
    debtorrent/trunk/DebTorrent/BT1/Rerequester.py
    debtorrent/trunk/DebTorrent/BT1/Storage.py
    debtorrent/trunk/DebTorrent/BT1/StorageWrapper.py
    debtorrent/trunk/DebTorrent/BT1/StreamCheck.py
    debtorrent/trunk/DebTorrent/BT1/T2T.py
    debtorrent/trunk/DebTorrent/BT1/makemetafile.py
    debtorrent/trunk/DebTorrent/BT1/track.py
    debtorrent/trunk/DebTorrent/ConfigDir.py
    debtorrent/trunk/DebTorrent/HTTPCache.py
    debtorrent/trunk/DebTorrent/HTTPHandler.py
    debtorrent/trunk/DebTorrent/RateLimiter.py
    debtorrent/trunk/DebTorrent/RawServer.py
    debtorrent/trunk/DebTorrent/SocketHandler.py
    debtorrent/trunk/DebTorrent/bencode.py
    debtorrent/trunk/DebTorrent/download_bt1.py
    debtorrent/trunk/DebTorrent/inifile.py
    debtorrent/trunk/DebTorrent/iprangeparse.py
    debtorrent/trunk/DebTorrent/launchmanycore.py
    debtorrent/trunk/DebTorrent/parsedir.py
    debtorrent/trunk/DebTorrent/piecebuffer.py
    debtorrent/trunk/DebTorrent/subnetparse.py
    debtorrent/trunk/DebTorrent/torrentlistparse.py
    debtorrent/trunk/debtorrent-client.conf
    debtorrent/trunk/debtorrent-client.py
    debtorrent/trunk/test.py

Modified: debtorrent/trunk/DebTorrent/BT1/AptListener.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/BT1/AptListener.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/BT1/AptListener.py (original)
+++ debtorrent/trunk/DebTorrent/BT1/AptListener.py Tue Jul 17 06:42:12 2007
@@ -22,17 +22,14 @@
 from cStringIO import StringIO
 from gzip import GzipFile
 from bz2 import decompress
-from traceback import print_exc
 from time import time, gmtime, strftime
 from DebTorrent.clock import clock
 from sha import sha
 from binascii import b2a_hex
 from makemetafile import getpieces, getsubpieces, uniconvert, convert_all
 from DebTorrent.HTTPCache import HTTPCache
-import os
+import os, logging
 from DebTorrent.__init__ import version, product_name,version_short
-
-DEBUG = True
 
 VERSION = product_name+'/'+version_short
 
@@ -127,7 +124,7 @@
                 self.favicon = h.read()
                 h.close()
             except:
-                print "**warning** specified favicon file -- %s -- does not exist." % favicon
+                logging.warning('specified favicon file does not exist.')
         self.rawserver = rawserver
         self.times = {}
         self.state = {}
@@ -171,8 +168,7 @@
         
         assert not self.request_queue.has_key(connection)
         
-        if DEBUG:
-            print 'queueing request as file', file_num, 'needs pieces:', pieces_needed
+        logging.info('queueing request as file '+str(file_num)+' needs pieces: '+str(pieces_needed))
 
         self.request_queue[connection] = (downloader, file_num, pieces_needed, clock())
         
@@ -192,14 +188,12 @@
             # Remove the downloaded pieces from the list of needed ones
             for piece in list(v[2]):
                 if v[0].storagewrapper.do_I_have(piece):
-                    if DEBUG:
-                        print 'queued request for file', v[1], 'got piece', piece
+                    logging.debug('queued request for file '+str(v[1])+' got piece '+str(piece))
                     v[2].remove(piece)
                     
             # If no more pieces are needed, return the answer and remove the request
             if not v[2]:
-                if DEBUG:
-                    print 'queued request for file', v[1], 'is complete'
+                logging.info('queued request for file '+str(v[1])+' is complete')
                 del self.request_queue[c]
                 self.answer_package(c, v[0], v[1])
 
@@ -306,7 +300,7 @@
                 '</html>\n')
             return (200, 'OK', {'Server': VERSION, 'Content-Type': 'text/html; charset=iso-8859-1'}, s.getvalue())
         except:
-            print_exc()
+            logging.exception('Error returning info_page')
             return (500, 'Internal Server Error', {'Server': VERSION, 'Content-Type': 'text/html; charset=iso-8859-1'}, 'Server Error')
 
 
@@ -381,8 +375,7 @@
         connections = self.cache_waiting.pop('/'.join(path), None)
         
         if connections is None:
-            if DEBUG:
-                print 'no connection exists to return the cached file on'
+            logging.warning('no connection exists to return the cached file on')
             return
 
         # If it's a torrent file, start it
@@ -475,8 +468,7 @@
             return
 
         # Something strange has happened, requeue it
-        if DEBUG:
-            print 'request for', f, 'still needs pieces:', pieces_needed
+        logging.warning('requeuing request for file '+str(f)+' as it still needs pieces: '+str(pieces_needed))
         self.enqueue_request(connection, d, f, pieces_needed)
         
     
@@ -504,8 +496,7 @@
             assert data[:8] == "Package:"
             h = data.split('\n')
         except:
-            if DEBUG:
-                print 'ERROR: Packages file could not be converted to a torrent'
+            logging.warning('Packages file could not be converted to a torrent')
             return 
 
         sub_pieces = getsubpieces('_'.join(path))
@@ -698,7 +689,7 @@
                 self.allowed_IPs.read_fieldlist(f)
                 self.allowed_ip_mtime = os.path.getmtime(f)
             except (IOError, OSError):
-                print '**warning** unable to read allowed_IP list'
+                logging.warning('unable to read allowed_IP list')
                 
         f = self.config['banned_ips']
         if f and self.banned_ip_mtime != os.path.getmtime(f):
@@ -707,7 +698,7 @@
                 self.banned_IPs.read_rangelist(f)
                 self.banned_ip_mtime = os.path.getmtime(f)
             except (IOError, OSError):
-                print '**warning** unable to read banned_IP list'
+                logging.warning('unable to read banned_IP list')
                 
 
 def size_format(s):

Modified: debtorrent/trunk/DebTorrent/BT1/Connecter.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/BT1/Connecter.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/BT1/Connecter.py (original)
+++ debtorrent/trunk/DebTorrent/BT1/Connecter.py Tue Jul 17 06:42:12 2007
@@ -6,10 +6,6 @@
 
 """For maintaining connections to peers.
 
- at type DEBUG1: C{boolean}
- at var DEBUG1: whether to print debugging information for the L{Connection}
- at type DEBUG2: C{boolean}
- at var DEBUG2: whether to print debugging information for the L{Connecter}
 @type CHOKE: C{char}
 @var CHOKE: the code for choke messages
 @type UNCHOKE: C{char}
@@ -34,15 +30,13 @@
 from DebTorrent.bitfield import Bitfield
 from DebTorrent.clock import clock
 from binascii import b2a_hex
+import logging
 
 try:
     True
 except:
     True = 1
     False = 0
-
-DEBUG1 = True
-DEBUG2 = True
 
 def toint(s):
     """Convert four-byte big endian representation to a long.
@@ -128,6 +122,7 @@
         self.next_upload = None
         self.outqueue = []
         self.partial_message = None
+        self.upload = None
         self.download = None
         self.send_choke_queued = False
         self.just_unchoked = None
@@ -164,8 +159,7 @@
 
     def close(self):
         """Close the connection."""
-        if DEBUG1:
-            print (self.get_ip(),'connection closed')
+        logging.debug(self.get_ip()+': connection closed')
         self.connection.close()
 
     def is_locally_initiated(self):
@@ -209,8 +203,7 @@
         """Send the L{UNCHOKE} message."""
         if self.send_choke_queued:
             self.send_choke_queued = False
-            if DEBUG1:
-                print (self.get_ip(),'CHOKE SUPPRESSED')
+            logging.info(self.get_ip()+': CHOKE SUPPRESSED')
         else:
             self._send_message(UNCHOKE)
             if ( self.partial_message or self.just_unchoked is None
@@ -233,8 +226,7 @@
         
         self._send_message(REQUEST + tobinary(index) + 
             tobinary(begin) + tobinary(length))
-        if DEBUG1:
-            print (self.get_ip(),'sent request',index,begin,begin+length)
+        logging.debug(self.get_ip()+': sent request '+str(index)+', '+str(begin)+', '+str(begin+length))
 
     def send_cancel(self, index, begin, length):
         """Send the L{CANCEL} message.
@@ -252,8 +244,7 @@
         
         self._send_message(CANCEL + tobinary(index) + 
             tobinary(begin) + tobinary(length))
-        if DEBUG1:
-            print (self.get_ip(),'sent cancel',index,begin,begin+length)
+        logging.debug(self.get_ip()+': sent cancel '+str(index)+', '+str(begin)+', '+str(begin+length))
 
     def send_bitfield(self, bitfield):
         """Send the L{BITFIELD} message.
@@ -287,11 +278,10 @@
         
         """
         
-        if DEBUG2:
-            if s:
-                print (self.get_ip(),'SENDING MESSAGE',ord(s[0]),len(s))
-            else:
-                print (self.get_ip(),'SENDING MESSAGE','keepalive',0)
+        if s:
+            logging.debug(self.get_ip()+': SENDING MESSAGE '+str(ord(s[0]))+' ('+str(len(s))+')')
+        else:
+            logging.debug(self.get_ip()+': SENDING MESSAGE keepalive (0)')
         s = tobinary(len(s))+s
         if self.partial_message:
             self.outqueue.append(s)
@@ -318,8 +308,7 @@
             self.partial_message = ''.join((
                             tobinary(len(piece) + 9), PIECE,
                             tobinary(index), tobinary(begin), piece.tostring() ))
-            if DEBUG1:
-                print (self.get_ip(),'sending chunk',index,begin,begin+len(piece))
+            logging.debug(self.get_ip()+': sending chunk '+str(index)+', '+str(begin)+', '+str(begin+len(piece)))
 
         if bytes < len(self.partial_message):
             self.connection.send_message_raw(self.partial_message[:bytes])
@@ -489,8 +478,7 @@
         
         self.ccount += 1
         c = Connection(connection, self, self.ccount)
-        if DEBUG2:
-            print (c.get_ip(),'connection made')
+        logging.debug(c.get_ip()+': connection made')
         self.connections[connection] = c
         c.upload = self.make_upload(c, self.ratelimiter, self.totalup)
         c.download = self.downloader.make_download(c)
@@ -506,8 +494,7 @@
         """
         
         c = self.connections[connection]
-        if DEBUG2:
-            print (c.get_ip(),'connection closed')
+        logging.debug(c.get_ip()+': connection closed')
         del self.connections[connection]
         if c.download:
             c.download.disconnected()
@@ -549,18 +536,15 @@
         
         c = self.connections[connection]
         t = message[0]
-        if DEBUG2:
-            print (c.get_ip(),'message received',ord(t))
+        logging.debug(c.get_ip()+': message received '+str(ord(t)))
         if t == BITFIELD and c.got_anything:
-            if DEBUG2:
-                print (c.get_ip(),'misplaced bitfield')
+            logging.info(c.get_ip()+': misplaced bitfield, closing connection')
             connection.close()
             return
         c.got_anything = True
         if (t in [CHOKE, UNCHOKE, INTERESTED, NOT_INTERESTED] and 
                 len(message) != 1):
-            if DEBUG2:
-                print (c.get_ip(),'bad message length')
+            logging.debug(c.get_ip()+': bad message length, closing connection')
             connection.close()
             return
         if t == CHOKE:
@@ -574,14 +558,12 @@
             c.upload.got_not_interested()
         elif t == HAVE:
             if len(message) != 5:
-                if DEBUG2:
-                    print (c.get_ip(),'bad message length')
+                logging.debug(c.get_ip()+': bad message length, closing connection')
                 connection.close()
                 return
             i = toint(message[1:])
             if i >= self.numpieces:
-                if DEBUG2:
-                    print (c.get_ip(),'bad piece number')
+                logging.debug(c.get_ip()+': bad piece number, closing connection')
                 connection.close()
                 return
             if c.download.got_have(i):
@@ -590,55 +572,47 @@
             try:
                 b = Bitfield(self.numpieces, message[1:])
             except ValueError:
-                if DEBUG2:
-                    print (c.get_ip(),'bad bitfield')
+                logging.debug(c.get_ip()+': bad bitfield, closing connection')
                 connection.close()
                 return
             if c.download.got_have_bitfield(b):
                 c.upload.got_not_interested()
         elif t == REQUEST:
             if len(message) != 13:
-                if DEBUG2:
-                    print (c.get_ip(),'bad message length')
+                logging.debug(c.get_ip()+': bad message length, closing connection')
                 connection.close()
                 return
             i = toint(message[1:5])
             if i >= self.numpieces:
-                if DEBUG2:
-                    print (c.get_ip(),'bad piece number')
+                logging.debug(c.get_ip()+': bad piece number, closing connection')
                 connection.close()
                 return
             c.got_request(i, toint(message[5:9]), 
                 toint(message[9:]))
         elif t == CANCEL:
             if len(message) != 13:
-                if DEBUG2:
-                    print (c.get_ip(),'bad message length')
+                logging.debug(c.get_ip()+': bad message length, closing connection')
                 connection.close()
                 return
             i = toint(message[1:5])
             if i >= self.numpieces:
-                if DEBUG2:
-                    print (c.get_ip(),'bad piece number')
+                logging.debug(c.get_ip()+': bad piece number, closing connection')
                 connection.close()
                 return
             c.upload.got_cancel(i, toint(message[5:9]), 
                 toint(message[9:]))
         elif t == PIECE:
             if len(message) <= 9:
-                if DEBUG2:
-                    print (c.get_ip(),'bad message length')
+                logging.debug(c.get_ip()+': bad message length, closing connection')
                 connection.close()
                 return
             i = toint(message[1:5])
             if i >= self.numpieces:
-                if DEBUG2:
-                    print (c.get_ip(),'bad piece number')
+                logging.debug(c.get_ip()+': bad piece number, closing connection')
                 connection.close()
                 return
             if c.download.got_piece(i, toint(message[5:9]), message[9:]):
                 self.got_piece(i)
         else:
-            if DEBUG2:
-                print (c.get_ip(),'unknown message type')
+            logging.debug(c.get_ip()+': unknown message type, closing connection')
             connection.close()

Modified: debtorrent/trunk/DebTorrent/BT1/Encrypter.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/BT1/Encrypter.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/BT1/Encrypter.py (original)
+++ debtorrent/trunk/DebTorrent/BT1/Encrypter.py Tue Jul 17 06:42:12 2007
@@ -8,9 +8,9 @@
 from binascii import b2a_hex
 from socket import error as socketerror
 from urllib import quote
-from traceback import print_exc
 from DebTorrent.BTcrypto import Crypto
 from DebTorrent.__init__ import protocol_name
+import logging
 
 try:
     True
@@ -587,8 +587,7 @@
             if self.config['security'] and ip != 'unknown' and ip == dns[0]:
                 return True
         try:
-            if DEBUG:
-                print 'initiating connection to:', dns, id, encrypted
+            logging.debug('initiating connection to: '+str(dns)+', '+str(id)+', '+str(encrypted))
             c = self.raw_server.start_connection(dns)
             con = Connection(self, c, id, encrypted = encrypted)
             self.connections[c] = con

Modified: debtorrent/trunk/DebTorrent/BT1/FileSelector.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/BT1/FileSelector.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/BT1/FileSelector.py (original)
+++ debtorrent/trunk/DebTorrent/BT1/FileSelector.py Tue Jul 17 06:42:12 2007
@@ -5,7 +5,7 @@
 # $Id$
 
 from random import shuffle
-from traceback import print_exc
+import logging
 try:
     True
 except:
@@ -63,7 +63,7 @@
                 assert v >= -1
                 assert v <= 2
         except:
-#           print_exc()            
+            logging.warning('Initializing the priority failed', exc_info = True)
             return False
         try:
             for f in xrange(self.numfiles):

Modified: debtorrent/trunk/DebTorrent/BT1/HTTPDownloader.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/BT1/HTTPDownloader.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/BT1/HTTPDownloader.py (original)
+++ debtorrent/trunk/DebTorrent/BT1/HTTPDownloader.py Tue Jul 17 06:42:12 2007
@@ -19,15 +19,13 @@
 from httplib import HTTPConnection, BadStatusLine
 from urllib import quote
 from threading import Thread
-from traceback import print_exc
+import logging
 from DebTorrent.__init__ import product_name,version_short
 try:
     True
 except:
     True = 1
     False = 0
-
-DEBUG = True
 
 VERSION = product_name+'/'+version_short
 
@@ -209,8 +207,7 @@
             self.endflag = True
             self.resched()
         else:
-            if DEBUG:
-                print 'HTTPDownloader: downloading piece', self.index
+            logging.debug('HTTPDownloader: downloading piece '+str(self.index))
             (start, end, length, file) = self.downloader.storage.storage.get_file_range(self.index)
             filename = self.downloader.filenamefunc()
             if len(filename) > 0 and file.startswith(filename):
@@ -240,9 +237,7 @@
         self.error = None
         self.received_data = None
         try:
-            if DEBUG:
-                print 'HTTPDownloader: sending request'
-                print 'GET', self.url, self.headers
+            logging.debug('HTTPDownloader: sending request: GET '+self.url+' '+str(self.headers))
             self.connection.request('GET',self.url, None, self.headers)
             
             # Check for closed persistent connection due to server timeout
@@ -255,15 +250,11 @@
                 self.connection.request('GET',self.url, None, self.headers)
                 r = self.connection.getresponse()
                 
-            if DEBUG:
-                print 'HTTPDownloader: got response'
-                print r.status, r.reason, r.getheaders()
+            logging.debug('HTTPDownloader: got response: '+str(r.status)+', '+r.reason+', '+str(r.getheaders()))
             self.connection_status = r.status
             self.received_data = r.read()
         except Exception, e:
-            if DEBUG:
-                print 'error accessing http seed: '+str(e)
-                print_exc()
+            logging.exception('error accessing http seed: '+str(e))
             self.error = 'error accessing http seed: '+str(e)
             try:
                 self.connection.close()
@@ -487,8 +478,7 @@
         
         """
         
-        if DEBUG:
-            print 'Starting a deb_mirror downloader for:', url
+        logging.info('Starting a deb_mirror downloader for: '+url)
         self.downloads.append(SingleDownload(self, url))
         return self.downloads[-1]
 
@@ -512,8 +502,7 @@
         
         """
         
-        if DEBUG:
-            print 'Cancelling all HTTP downloads for pieces:', pieces
+        logging.info('Cancelling all HTTP downloads for pieces: '+str(pieces))
         for d in self.downloads:
             if d.active and d.index in pieces:
                 d.cancelled = True

Modified: debtorrent/trunk/DebTorrent/BT1/NatCheck.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/BT1/NatCheck.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/BT1/NatCheck.py (original)
+++ debtorrent/trunk/DebTorrent/BT1/NatCheck.py Tue Jul 17 06:42:12 2007
@@ -6,7 +6,6 @@
 
 from cStringIO import StringIO
 from socket import error as socketerror
-from traceback import print_exc
 from DebTorrent.BTcrypto import Crypto, CRYPTO_OK
 from DebTorrent.__init__ import protocol_name
 
@@ -137,8 +136,6 @@
         return self.read_crypto_block4done()
 
     def read_crypto_block4done(self):
-        if DEBUG:
-            self._log_start()
         if self.cryptmode == 1:     # only handshake encryption
             if not self.buffer:  # oops; check for exceptions to this
                 return None

Modified: debtorrent/trunk/DebTorrent/BT1/Rerequester.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/BT1/Rerequester.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/BT1/Rerequester.py (original)
+++ debtorrent/trunk/DebTorrent/BT1/Rerequester.py Tue Jul 17 06:42:12 2007
@@ -17,6 +17,7 @@
 from random import shuffle
 from sha import sha
 from time import time
+import logging
 try:
     from os import getpid
 except ImportError:
@@ -28,8 +29,6 @@
 except:
     True = 1
     False = 0
-
-DEBUG = True
 
 mapbase64 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-'
 keys = {}
@@ -385,8 +384,7 @@
                 x = p[i]
                 peers.append(((x['ip'].strip(), x['port']),
                               x.get('peer id',0), cflags[i]))
-        if DEBUG:
-            print 'received from tracker:', peers
+        logging.debug('received from tracker: '+str(peers))
         ps = len(peers) + self.howmany()
         if ps < self.maxpeers:
             if self.doneflag.isSet():
@@ -409,7 +407,7 @@
             if self.excfunc:
                 self.excfunc(s)
             else:
-                print s
+                logging.error(s)
             callback()
         self.externalsched(r)
 

Modified: debtorrent/trunk/DebTorrent/BT1/Storage.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/BT1/Storage.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/BT1/Storage.py (original)
+++ debtorrent/trunk/DebTorrent/BT1/Storage.py Tue Jul 17 06:42:12 2007
@@ -6,10 +6,6 @@
 
 """Low-level writing of files.
 
-
-
- at type DEBUG: C{boolean}
- at var DEBUG: whether to enable printing of debug messages
 @type MAXREADSIZE: C{long}
 @var MAXREADSIZE: the maximum number of bytes that can be read at a time
 @type MAXLOCKSIZE: C{long}
@@ -29,7 +25,7 @@
 import os
 from os.path import exists, getsize, getmtime, basename, split
 from os import makedirs
-from traceback import print_exc
+import logging
 try:
     from os import fsync
 except ImportError:
@@ -41,8 +37,6 @@
 except:
     True = 1
     False = 0
-
-DEBUG = True
 
 MAXREADSIZE = 32768
 MAXLOCKSIZE = 1000000000L
@@ -420,17 +414,15 @@
                 assert newmtime <= oldmtime+1
                 assert newmtime >= oldmtime-1
             except:
-                if DEBUG:
-                    print ( file+' modified: '
+                logging.warning(file+' modified: '
                             +strftime('(%x %X)',localtime(self.mtimes[file]))
-                            +strftime(' != (%x %X) ?',localtime(getmtime(file))) )
+                            +strftime(' != (%x %X) ?',localtime(getmtime(file))))
                 raise IOError('modified during download')
         try:
             self.make_directories(file)
             return open(file, mode)
         except:
-            if DEBUG:
-                print_exc()
+            logging.exception('Error opening the file: '+file)
             raise
 
 
@@ -494,8 +486,7 @@
                     self.whandles[file] = 1
                     self.lock_file(file, f)
                 except (IOError, OSError), e:
-                    if DEBUG:
-                        print_exc()
+                    logging.exception('unable to reopen: '+file)
                     raise IOError('unable to reopen '+file+': '+str(e))
 
             if self.handlebuffer:
@@ -517,8 +508,7 @@
                     if self.lock_while_reading:
                         self.lock_file(file, f)
             except (IOError, OSError), e:
-                if DEBUG:
-                    print_exc()
+                logging.exception('unable to open: '+file)
                 raise IOError('unable to open '+file+': '+str(e))
             
             if self.handlebuffer is not None:
@@ -535,9 +525,8 @@
         for l in self.working_ranges:
             self.ranges.extend(l)
         self.begins = [i[0] for i in self.ranges]
-        if DEBUG:
-            print 'file ranges:', str(self.ranges)
-            print 'file begins:', str(self.begins)
+        logging.debug('file ranges: '+str(self.ranges))
+        logging.debug('file begins: '+str(self.begins))
 
     def get_file_range(self, index):
         """Get the file name and range that corresponds to this piece.
@@ -601,8 +590,7 @@
         
         r = PieceBuffer()
         for file, pos, end in self._intervals(pos, amount):
-            if DEBUG:
-                print 'reading '+file+' from '+str(pos)+' to '+str(end)
+            logging.debug('reading '+file+' from '+str(pos)+' to '+str(end))
             self.lock.acquire()
             h = self._get_file_handle(file, False)
             if flush_first and self.whandles.has_key(file):
@@ -632,8 +620,7 @@
         # might raise an IOError
         total = 0
         for file, begin, end in self._intervals(pos, len(s)):
-            if DEBUG:
-                print 'writing '+file+' from '+str(pos)+' to '+str(end)
+            logging.debug('writing '+file+' from '+str(pos)+' to '+str(end))
             self.lock.acquire()
             h = self._get_file_handle(file, True)
             h.seek(begin)
@@ -718,10 +705,9 @@
             return r
         start, end, offset, file = self.file_ranges[f]
         start_piece, end_piece = self.file_pieces[f]
-        if DEBUG:
-            print 'calculating disabled range for '+self.files[f][0]
-            print 'bytes: '+str(start)+'-'+str(end)
-            print 'file spans pieces '+str(start_piece)+'-'+str(end_piece)
+        logging.debug('calculating disabled range for '+self.files[f][0])
+        logging.debug('bytes: '+str(start)+'-'+str(end))
+        logging.debug('file spans pieces '+str(start_piece)+'-'+str(end_piece))
         pieces = range(start_piece, end_piece+1)
         offset = 0
         disabled_files = []
@@ -729,9 +715,8 @@
         working_range = [(start, end, offset, file)]
         update_pieces = []
 
-        if DEBUG:            
-            print 'working range:', str(working_range)
-            print 'update pieces:', str(update_pieces)
+        logging.debug('working range: '+str(working_range))
+        logging.debug('update pieces: '+str(update_pieces))
         r = (tuple(working_range), tuple(update_pieces), tuple(disabled_files))
         self.disabled_ranges[f] = r
         return r
@@ -757,8 +742,7 @@
         
         if not self.disabled[f]:
             return
-        if DEBUG:
-            print 'enabling file '+self.files[f][0]
+        logging.info('enabling file '+self.files[f][0])
         self.disabled[f] = False
         r = self.file_ranges[f]
         if not r:
@@ -784,8 +768,7 @@
         """
         if self.disabled[f]:
             return
-        if DEBUG:
-            print 'disabling file '+self.files[f][0]
+        logging.info('disabling file '+self.files[f][0])
         self.disabled[f] = True
         r = self._get_disabled_ranges(f)
         if not r:
@@ -907,13 +890,11 @@
                     continue
                 start, end, offset, file =r
                 start_piece, end_piece = self.file_pieces[i]
-                if DEBUG:
-                    print 'adding '+file
+                logging.debug('adding '+file)
                 for p in xrange(start_piece, end_piece+1):
                     valid_pieces[p] = 1
 
-            if DEBUG:
-                print 'Saved list of valid pieces:', valid_pieces.keys()
+            logging.info('Saved list of valid pieces: '+str(valid_pieces.keys()))
             
             def test(old, size, mtime):
                 """Test that the file has not changed since the status save.                
@@ -949,17 +930,14 @@
                      or not test(files[i],getsize(file),getmtime(file)) ):
                     start, end, offset, file = self.file_ranges[i]
                     start_piece, end_piece = self.file_pieces[i]
-                    if DEBUG:
-                        print 'removing '+file
+                    logging.debug('removing '+file)
                     for p in xrange(start_piece, end_piece+1):
                         if valid_pieces.has_key(p):
                             del valid_pieces[p]
         except:
-            if DEBUG:
-                print_exc()
+            logging.exception('Error unpickling data cache')
             return []
 
-        if DEBUG:
-            print 'Final list of valid pieces:', valid_pieces.keys()                        
+        logging.info('Final list of valid pieces: '+str(valid_pieces.keys()))
         return valid_pieces.keys()
 

Modified: debtorrent/trunk/DebTorrent/BT1/StorageWrapper.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/BT1/StorageWrapper.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/BT1/StorageWrapper.py (original)
+++ debtorrent/trunk/DebTorrent/BT1/StorageWrapper.py Tue Jul 17 06:42:12 2007
@@ -7,8 +7,8 @@
 from DebTorrent.bitfield import Bitfield
 from sha import sha
 from DebTorrent.clock import clock
-from traceback import print_exc
 from random import randrange
+import logging
 try:
     True
 except:
@@ -20,8 +20,6 @@
     def insort(l, item):
         l.append(item)
         l.sort()
-
-DEBUG = True
 
 STATS_INTERVAL = 0.2
 
@@ -164,8 +162,7 @@
                     x = next()
 
         self.statusfunc(fractionDone = 0)
-        if DEBUG:
-            print 'StorageWrapper old_style_init complete'
+        logging.info('StorageWrapper old_style_init complete')
         return True
 
 
@@ -192,8 +189,7 @@
         else:
             if not self.initialize_tasks:
                 self.initialize_done()
-                if DEBUG:
-                    print 'StorageWrapper initialize complete'
+                logging.info('StorageWrapper initialize complete')
                 return
             msg, done, init, next = self.initialize_tasks.pop(0)
             if init():
@@ -248,8 +244,7 @@
         return self.check_total > 0
 
     def _markgot(self, piece, pos):
-        if DEBUG:
-            print str(piece)+' at '+str(pos)
+        logging.debug(str(piece)+' at '+str(pos))
         self.places[piece] = pos
         self.have[piece] = True
         len = self._piecelen(piece)
@@ -570,8 +565,7 @@
 
     def _move_piece(self, index, newpos):
         oldpos = self.places[index]
-        if DEBUG:
-            print 'moving '+str(index)+' from '+str(oldpos)+' to '+str(newpos)
+        logging.debug('moving '+str(index)+' from '+str(oldpos)+' to '+str(newpos))
         assert oldpos != index
         assert oldpos != newpos
         assert index == newpos or not self.places.has_key(newpos)
@@ -659,8 +653,7 @@
         if not self.places.has_key(index):
             while self._clear_space(index):
                 pass
-            if DEBUG:
-                print 'new place for '+str(index)+' at '+str(self.places[index])
+            logging.debug('new place for '+str(index)+' at '+str(self.places[index]))
         if self.flag.isSet():
             return
 
@@ -1035,7 +1028,7 @@
 
             assert amount_obtained + amount_inactive == self.amount_desired
         except:
-#            print_exc()
+            logging.exception('Error unpickling data cache')
             return []   # invalid data, discard everything
 
         self.have = have

Modified: debtorrent/trunk/DebTorrent/BT1/StreamCheck.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/BT1/StreamCheck.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/BT1/StreamCheck.py (original)
+++ debtorrent/trunk/DebTorrent/BT1/StreamCheck.py Tue Jul 17 06:42:12 2007
@@ -8,17 +8,14 @@
 from binascii import b2a_hex
 from socket import error as socketerror
 from urllib import quote
-from traceback import print_exc
 from DebTorrent.__init__ import protocol_name
 import Connecter
+import logging
 try:
     True
 except:
     True = 1
     False = 0
-
-DEBUG = True
-
 
 option_pattern = chr(0)*8
 
@@ -65,31 +62,29 @@
 
     def read_header_len(self, s):
         if ord(s) != len(protocol_name):
-            print self.no, 'BAD HEADER LENGTH'
+            logging.warn(str(self.no)+' BAD HEADER LENGTH')
         return len(protocol_name), self.read_header
 
     def read_header(self, s):
         if s != protocol_name:
-            print self.no, 'BAD HEADER'
+            logging.warn(str(self.no)+' BAD HEADER')
         return 8, self.read_reserved
 
     def read_reserved(self, s):
         return 20, self.read_download_id
 
     def read_download_id(self, s):
-        if DEBUG:
-            print self.no, 'download ID ' + tohex(s)
+        logging.debug(str(self.no)+' download ID ' + tohex(s))
         return 20, self.read_peer_id
 
     def read_peer_id(self, s):
-        if DEBUG:
-            print self.no, 'peer ID' + make_readable(s)
+        logging.debug(str(self.no)+' peer ID' + make_readable(s))
         return 4, self.read_len
 
     def read_len(self, s):
         l = toint(s)
         if l > 2 ** 23:
-            print self.no, 'BAD LENGTH: '+str(l)+' ('+s+')'
+            logging.warn(str(self.no)+' BAD LENGTH: '+str(l)+' ('+s+')')
         return l, self.read_message
 
     def read_message(self, s):
@@ -97,30 +92,30 @@
             return 4, self.read_len
         m = s[0]
         if ord(m) > 8:
-            print self.no, 'BAD MESSAGE: '+str(ord(m))
+            logging.warn(str(self.no)+' BAD MESSAGE: '+str(ord(m)))
         if m == Connecter.REQUEST:
             if len(s) != 13:
-                print self.no, 'BAD REQUEST SIZE: '+str(len(s))
+                logging.warn(str(self.no)+' BAD REQUEST SIZE: '+str(len(s)))
                 return 4, self.read_len
             index = toint(s[1:5])
             begin = toint(s[5:9])
             length = toint(s[9:])
-            print self.no, 'Request: '+str(index)+': '+str(begin)+'-'+str(begin)+'+'+str(length)
+            logging.info(str(self.no)+' Request: '+str(index)+': '+str(begin)+'-'+str(begin)+'+'+str(length))
         elif m == Connecter.CANCEL:
             if len(s) != 13:
-                print self.no, 'BAD CANCEL SIZE: '+str(len(s))
+                logging.warn(str(self.no)+' BAD CANCEL SIZE: '+str(len(s)))
                 return 4, self.read_len
             index = toint(s[1:5])
             begin = toint(s[5:9])
             length = toint(s[9:])
-            print self.no, 'Cancel: '+str(index)+': '+str(begin)+'-'+str(begin)+'+'+str(length)
+            logging.info(str(self.no)+' Cancel: '+str(index)+': '+str(begin)+'-'+str(begin)+'+'+str(length))
         elif m == Connecter.PIECE:
             index = toint(s[1:5])
             begin = toint(s[5:9])
             length = len(s)-9
-            print self.no, 'Piece: '+str(index)+': '+str(begin)+'-'+str(begin)+'+'+str(length)
+            logging.info(str(self.no)+' Piece: '+str(index)+': '+str(begin)+'-'+str(begin)+'+'+str(length))
         else:
-            print self.no, 'Message '+str(ord(m))+' (length '+str(len(s))+')'
+            logging.info(str(self.no)+' Message '+str(ord(m))+' (length '+str(len(s))+')')
         return 4, self.read_len
 
     def write(self, s):

Modified: debtorrent/trunk/DebTorrent/BT1/T2T.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/BT1/T2T.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/BT1/T2T.py (original)
+++ debtorrent/trunk/DebTorrent/BT1/T2T.py Tue Jul 17 06:42:12 2007
@@ -9,7 +9,7 @@
 from threading import Event
 from random import randrange
 from string import lower
-import sys
+import sys, logging
 import __init__
 try:
     True
@@ -17,11 +17,9 @@
     True = 1
     False = 0
 
-DEBUG = True
-
 
 def excfunc(x):
-    print x
+    logging.error(x)
 
 R_0 = lambda: 0
 R_1 = lambda: 1
@@ -69,8 +67,7 @@
             return
         self.lastsuccessful = True
         self.newpeerdata = []
-        if DEBUG:
-            print 'contacting %s for info_hash=%s' % (self.tracker, quote(self.hash))
+        logging.info('contacting '+self.tracker+' for info_hash='+quote(self.hash))
         self.rerequester.snoop(self.peers, self.callback)
 
     def callback(self):
@@ -82,9 +79,7 @@
                 # I think I'm stripping from a regular tracker; boost the number of peers requested
                 self.peers = int(self.peers * (self.rerequester.announce_interval / self.interval))
             self.operatinginterval = self.rerequester.announce_interval
-            if DEBUG:
-                print ("%s with info_hash=%s returned %d peers" %
-                        (self.tracker, quote(self.hash), len(self.newpeerdata)))
+            logging.info(self.tracker+' with info_hash='+quote(self.hash)+' returned '+str(len(self.newpeerdata))+' peers')
             self.peerlists.append(self.newpeerdata)
             self.peerlists = self.peerlists[-10:]  # keep up to the last 10 announces
         if self.isactive():
@@ -96,26 +91,22 @@
         
     def errorfunc(self, r):
         self.lastsuccessful = False
-        if DEBUG:
-            print "%s with info_hash=%s gives error: '%s'" % (self.tracker, quote(self.hash), r)
+        logging.info(self.tracker+' with info_hash='+quote(self.hash)+' gives error: "'+r+'"')
         if r == self.rerequester.rejectedmessage + 'disallowed':   # whoops!
-            if DEBUG:
-                print ' -- disallowed - deactivating'
+            logging.info(' -- disallowed - deactivating')
             self.deactivate()
             self.disallow(self.tracker)   # signal other torrents on this tracker
             return
         if lower(r[:8]) == 'rejected': # tracker rejected this particular torrent
             self.rejected += 1
             if self.rejected == 3:     # rejected 3 times
-                if DEBUG:
-                    print ' -- rejected 3 times - deactivating'
+                logging.info(' -- rejected 3 times - deactivating')
                 self.deactivate()
             return
         self.errors += 1
         if self.errors >= 3:                         # three or more errors in a row
             self.operatinginterval += self.interval  # lengthen the interval
-            if DEBUG:
-                print ' -- lengthening interval to '+str(self.operatinginterval)+' seconds'
+            logging.info(' -- lengthening interval to '+str(self.operatinginterval)+' seconds')
 
     def harvest(self):
         x = []

Modified: debtorrent/trunk/DebTorrent/BT1/makemetafile.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/BT1/makemetafile.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/BT1/makemetafile.py (original)
+++ debtorrent/trunk/DebTorrent/BT1/makemetafile.py Tue Jul 17 06:42:12 2007
@@ -19,7 +19,7 @@
 from gzip import GzipFile
 from StringIO import StringIO
 from re import subn
-import binascii
+import binascii, logging
 try:
     from sys import getfilesystemencoding
     ENCODING = getfilesystemencoding()
@@ -315,8 +315,7 @@
 
     sub_pieces = getsubpieces(file, pieces_file)
 
-    if not sub_pieces:
-        print 'WARNING: Pieces file could not be found, not using sub-package pieces.'
+    logging.warning('WARNING: Pieces file could not be found, not using sub-package pieces.')
         
     file = abspath(file)
     f = open(file)

Modified: debtorrent/trunk/DebTorrent/BT1/track.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/BT1/track.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/BT1/track.py (original)
+++ debtorrent/trunk/DebTorrent/BT1/track.py Tue Jul 17 06:42:12 2007
@@ -36,7 +36,6 @@
 from os import rename, getpid
 from os.path import exists, isfile
 from cStringIO import StringIO
-from traceback import print_exc
 from time import time, gmtime, strftime, localtime
 from DebTorrent.clock import clock
 from random import shuffle, seed, randrange
@@ -44,11 +43,12 @@
 from types import StringType, IntType, LongType, ListType, DictType
 from binascii import b2a_hex, a2b_hex, a2b_base64
 from string import lower
-import sys, os
+import sys, os, logging
 import signal
 import re
 import DebTorrent.__init__
 from DebTorrent.__init__ import version, createPeerID
+from DebTorrent.ConfigDir import ConfigDir
 try:
     True
 except:
@@ -58,7 +58,6 @@
 
 defaults = [
     ('port', 80, "Port to listen on."),
-    ('dfile', None, 'file to store recent downloader info in'),
     ('bind', '', 'comma-separated list of ips/hostnames to bind to locally'),
 #    ('ipv6_enabled', autodetect_ipv6(),
     ('ipv6_enabled', 0,
@@ -66,7 +65,7 @@
     ('ipv6_binds_v4', autodetect_socket_style(),
         'set if an IPv6 server socket will also field IPv4 connections'),
     ('socket_timeout', 15, 'timeout for closing connections'),
-    ('save_dfile_interval', 5 * 60, 'seconds between saving dfile'),
+    ('save_state_interval', 5 * 60, 'seconds between saving state to a file'),
     ('timeout_downloaders_interval', 45 * 60, 'seconds between expiring downloaders'),
     ('reannounce_interval', 30 * 60, 'seconds downloaders should wait between reannouncements'),
     ('response_size', 50, 'number of peers to send in an info message'),
@@ -74,8 +73,6 @@
         'time to wait between checking if any connections have timed out'),
     ('nat_check', 3,
         "how many times to check if a downloader is behind a NAT (0 = don't check)"),
-    ('log_nat_checks', 0,
-        "whether to add entries to the log for nat-check results"),
     ('min_time_between_log_flushes', 3.0,
         'minimum time it must have been since the last flush to do another one'),
     ('min_time_between_cache_refreshes', 600.0,
@@ -105,7 +102,13 @@
              'file contains IP range data in the format: xxx:xxx:ip1-ip2'),
     ('only_local_override_ip', 2, "ignore the ip GET parameter from machines which aren't on local network IPs " +
              "(0 = never, 1 = always, 2 = ignore if NAT checking is not enabled)"),
-    ('logfile', '', 'file to write the tracker logs, use - for stdout (default)'),
+    ('cache_dir', '', 'the directory to use to get/store cache files, if not ' + 
+        'specified then a .DebTorrent directory in the user\'s home directory ' +
+        'will be used'),
+    ('log_dir', '',
+        'directory to write the logfiles to (default is to use the cache directory)'),
+    ('log_level', 30,
+        'level to write the logfiles at, varies from 10 (debug) to 50 (critical)'),
     ('allow_get', 0, 'use with allowed_dir; adds a /file?hash={hash} url that allows users to download the torrent file'),
     ('keep_dead', 0, 'keep dead torrents after they expire (so they still show up on your /scrape and web page)'),
     ('scrape_allowed', 'full', 'scrape access allowed (can be none, specific or full)'),
@@ -276,8 +279,8 @@
     @ivar config: the configuration parameters
     @type response_size: unknown
     @ivar response_size: unknown
-    @type dfile: C{string}
-    @ivar dfile: the state file to use when saving the current state
+    @type configdir: L{DebTorrent.ConfigDir.ConfigDir}
+    @ivar configdir: the configuration and cache directory manager
     @type natcheck: C{int}
     @ivar natcheck: how many times to check if a downloader is behind a NAT
     @type parse_dir_interval: C{int}
@@ -330,8 +333,8 @@
     @ivar trackerid: unknown
     @type reannounce_interval: C{int}
     @ivar reannounce_interval: seconds downloaders should wait between reannouncements
-    @type save_dfile_interval: C{int}
-    @ivar save_dfile_interval: seconds between saving the state file
+    @type save_state_interval: C{int}
+    @ivar save_state_interval: seconds between saving the state file
     @type show_names: C{boolean}
     @ivar show_names: whether to display names from allowed dir
     @type prevtime: unknown
@@ -373,19 +376,21 @@
     
     """
     
-    def __init__(self, config, rawserver):
+    def __init__(self, config, rawserver, configdir):
         """Initialize the instance.
         
         @type config: C{dictionary}
         @param config: the configuration parameters
         @type rawserver: L{DebTorrent.RawServer.RawServer}
         @param rawserver: the server to use for scheduling
+        @type configdir: L{DebTorrent.ConfigDir.ConfigDir}
+        @param configdir: the configuration and cache directory manager
         
         """
         
         self.config = config
         self.response_size = config['response_size']
-        self.dfile = config['dfile']
+        self.configdir = configdir
         self.natcheck = config['nat_check']
         favicon = config['favicon']
         self.parse_dir_interval = config['parse_dir_interval']
@@ -396,7 +401,7 @@
                 self.favicon = h.read()
                 h.close()
             except:
-                print "**warning** specified favicon file -- %s -- does not exist." % favicon
+                logging.warning("specified favicon file does not exist.")
         self.rawserver = rawserver
         self.cached = {}    # format: infohash: [[time1, l1, s1], [time2, l2, s2], ...]
         self.cached_t = {}  # format: infohash: [time, cache]
@@ -416,21 +421,19 @@
             self.only_local_override_ip = not config['nat_check']
 
         if CHECK_PEER_ID_ENCRYPTED and not CRYPTO_OK:
-            print ('**warning** crypto library not installed,' +
+            logging.warning('crypto library not installed,' +
                    ' cannot completely verify encrypted peers')
 
-        if exists(self.dfile):
+        tempstate = configdir.getState()
+        if tempstate:
             try:
-                h = open(self.dfile, 'rb')
-                ds = h.read()
-                h.close()
-                tempstate = bdecode(ds)
                 if not tempstate.has_key('peers'):
                     tempstate = {'peers': tempstate}
                 statefiletemplate(tempstate)
                 self.state = tempstate
+                logging.info('successfully loaded the previous state file')
             except:
-                print '**warning** statefile '+self.dfile+' corrupt; resetting'
+                logging.warning('statefile corrupt; resetting')
         self.downloads = self.state.setdefault('peers', {})
         self.completed = self.state.setdefault('completed', {})
 
@@ -468,9 +471,9 @@
         seed(self.trackerid)
                 
         self.reannounce_interval = config['reannounce_interval']
-        self.save_dfile_interval = config['save_dfile_interval']
+        self.save_state_interval = config['save_state_interval']
         self.show_names = config['show_names']
-        rawserver.add_task(self.save_state, self.save_dfile_interval)
+        rawserver.add_task(self.save_state, self.save_state_interval)
         self.prevtime = clock()
         self.timeout_downloaders_interval = config['timeout_downloaders_interval']
         rawserver.add_task(self.expire_downloaders, self.timeout_downloaders_interval)
@@ -484,8 +487,8 @@
 
         if config['allowed_list']:
             if config['allowed_dir']:
-                print '**warning** allowed_dir and allowed_list options cannot be used together'
-                print '**warning** disregarding allowed_dir'
+                logging.warning('allowed_dir and allowed_list options cannot be used together,'+
+                                ' disregarding allowed_dir')
                 config['allowed_dir'] = ''
             self.allowed = self.state.setdefault('allowed_list',{})
             self.allowed_list_mtime = 0
@@ -677,7 +680,7 @@
                 '</html>\n')
             return (200, 'OK', {'Content-Type': 'text/html; charset=iso-8859-1'}, s.getvalue())
         except:
-            print_exc()
+            logging.exception('Error generating info page')
             return (500, 'Internal Server Error', {'Content-Type': 'text/html; charset=iso-8859-1'}, 'Server Error')
 
 
@@ -1292,25 +1295,6 @@
                 bc[4][seed][peerid] = Bencached(bencode({'ip': ip, 'port': port}))
 
 
-    def natchecklog(self, peerid, ip, port, result):
-        """Log the results of any NAT checks performed.
-        
-        @type peerid: C{string}
-        @param peerid: the peer ID of the peer
-        @type ip: C{string}
-        @param ip: the IP address of the peer
-        @type port: C{int}
-        @param port: the port to contact the peer on
-        @type result: C{int}
-        @param result: the HTTP status code result of the NAT check
-        
-        """
-        
-        year, month, day, hour, minute, second, a, b, c = localtime(time())
-        print '%s - %s [%02d/%3s/%04d:%02d:%02d:%02d] "!natcheck-%s:%i" %i 0 - -' % (
-            ip, quote(peerid), day, months[month], year, hour, minute, second,
-            ip, port, result)
-
     def connectback_result(self, result, downloadid, peerid, ip, port):
         """Process a NAT check attempt and result.
         
@@ -1331,15 +1315,14 @@
         if ( record is None 
                  or (record['ip'] != ip and record.get('given ip') != ip)
                  or record['port'] != port ):
-            if self.config['log_nat_checks']:
-                self.natchecklog(peerid, ip, port, 404)
+            logging.info('natcheck on '+ip+' ('+quote(peerid)+'), '+str(port)+': '+str(404))
             return
         if self.config['log_nat_checks']:
             if result:
                 x = 200
             else:
                 x = 503
-            self.natchecklog(peerid, ip, port, x)
+            logging.info('natcheck on '+ip+' ('+quote(peerid)+'), '+str(port)+': '+str(x))
         if not record.has_key('nat'):
             record['nat'] = int(not result)
             if result:
@@ -1361,10 +1344,8 @@
 
     def save_state(self):
         """Save the state file to disk."""
-        self.rawserver.add_task(self.save_state, self.save_dfile_interval)
-        h = open(self.dfile, 'wb')
-        h.write(bencode(self.state))
-        h.close()
+        self.rawserver.add_task(self.save_state, self.save_state_interval)
+        self.configdir.saveState(self.state)
 
 
     def parse_allowed(self):
@@ -1392,7 +1373,7 @@
                 (self.allowed, added, garbage2) = r
                 self.state['allowed_list'] = self.allowed
             except (IOError, OSError):
-                print '**warning** unable to read allowed torrent list'
+                logging.warning('unable to read allowed torrent list')
                 return
             self.allowed_list_mtime = os.path.getmtime(f)
 
@@ -1413,7 +1394,7 @@
                 self.allowed_IPs.read_fieldlist(f)
                 self.allowed_ip_mtime = os.path.getmtime(f)
             except (IOError, OSError):
-                print '**warning** unable to read allowed_IP list'
+                logging.warning('unable to read allowed_IP list')
                 
         f = self.config['banned_ips']
         if f and self.banned_ip_mtime != os.path.getmtime(f):
@@ -1422,7 +1403,7 @@
                 self.banned_IPs.read_rangelist(f)
                 self.banned_ip_mtime = os.path.getmtime(f)
             except (IOError, OSError):
-                print '**warning** unable to read banned_IP list'
+                logging.warning('unable to read banned_IP list')
                 
 
     def delete_peer(self, infohash, peerid):
@@ -1465,32 +1446,59 @@
         self.rawserver.add_task(self.expire_downloaders, self.timeout_downloaders_interval)
 
 
-def track(args):
+def track(params):
     """Start the server and tracker.
     
-    @type args: C{list}
-    @param args: the command line arguments to the tracker
+    @type params: C{list}
+    @param params: the command line arguments to the tracker
     
     """
     
-    if len(args) == 0:
-        print formatDefinitions(defaults, 80)
-        return
+    configdefaults = {}
     try:
-        config, files = parseargs(args, defaults, 0, 0)
+        configdir = ConfigDir('debtorrent-client')
+        defaultsToIgnore = ['configfile']
+        configdir.setDefaults(defaults,defaultsToIgnore)
+        configdefaults = configdir.loadConfig(params)
+        defaults.append(('save_options',0,
+            "whether to save the current options as the new default configuration " +
+            "(only for debtorrent-tracker.py)"))
+        config, files = parseargs(params, defaults, 0, 0, configdefaults)
+        configdir.setCacheDir(config['cache_dir'], False)
+        if config['save_options']:
+            configdir.saveConfig(config)
+        if config['log_dir']:
+            outfilename = os.path.join(config['log_dir'], 'debtorrent-tracker.log')
+        else:
+            outfilename = os.path.join(configdir.cache_dir, 'debtorrent-tracker.log')
+        logging.basicConfig(level=config['log_level'],
+                            format='%(asctime)s %(levelname)s %(message)s',
+                            filename=outfilename,
+                            filemode='a')
     except ValueError, e:
         print 'error: ' + str(e)
-        print 'run with no arguments for parameter explanations'
-        return
+        print "Usage: debtorrent-tracker.py <global options>\n"
+        print formatDefinitions(defaults, 80)
+        sys.exit(1)
+
     r = RawServer(Event(), config['timeout_check_interval'],
                   config['socket_timeout'], ipv6_enable = config['ipv6_enabled'])
-    t = Tracker(config, r)
+    
+    t = Tracker(config, r, configdir)
+    
     r.bind(config['port'], config['bind'],
            reuse = True, ipv6_socket_style = config['ipv6_binds_v4'])
+
+    if config['log_dir']:
+        logfile = os.path.join(config['log_dir'], 'tracker-access.log')
+    else:
+        logfile = os.path.join(configdir.cache_dir, 'tracker-access.log')
+
     r.listen_forever(HTTPHandler(t.get, config['min_time_between_log_flushes'],
-                                 config['logfile'], config['hupmonitor']))
+                                 logfile, config['hupmonitor']))
+    
     t.save_state()
-    print '# Shutting down: ' + isotime()
+    logging.info('# Shutting down: ' + isotime())
 
 def size_format(s):
     """Format a byte size for reading by the user.

Modified: debtorrent/trunk/DebTorrent/ConfigDir.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/ConfigDir.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/ConfigDir.py (original)
+++ debtorrent/trunk/DebTorrent/ConfigDir.py Tue Jul 17 06:42:12 2007
@@ -202,11 +202,14 @@
             if self.config.has_key(k):
                 del self.config[k]
 
-    def setCacheDir(self, cache_dir):
+    def setCacheDir(self, cache_dir, create_dirs = True):
         """Sets the various cache directory locations.
         
         @type cache_dir: C{string}
         @param cache_dir: the directory to save cache files in
+        @type create_dirs: C{boolean}
+        @param create_dirs: whether to create the client cache directories
+            (optional, defaults to True)
         
         """
 
@@ -218,17 +221,18 @@
         if not os.path.isdir(self.cache_dir):
             os.mkdir(self.cache_dir)    # exception if failed
 
-        self.dir_torrentcache = os.path.join(self.cache_dir,'torrentcache')
-        if not os.path.isdir(self.dir_torrentcache):
-            os.mkdir(self.dir_torrentcache)
-
-        self.dir_datacache = os.path.join(self.cache_dir,'datacache')
-        if not os.path.isdir(self.dir_datacache):
-            os.mkdir(self.dir_datacache)
-
-        self.dir_piececache = os.path.join(self.cache_dir,'piececache')
-        if not os.path.isdir(self.dir_piececache):
-            os.mkdir(self.dir_piececache)
+        if create_dirs:
+            self.dir_torrentcache = os.path.join(self.cache_dir,'torrentcache')
+            if not os.path.isdir(self.dir_torrentcache):
+                os.mkdir(self.dir_torrentcache)
+    
+            self.dir_datacache = os.path.join(self.cache_dir,'datacache')
+            if not os.path.isdir(self.dir_datacache):
+                os.mkdir(self.dir_datacache)
+    
+            self.dir_piececache = os.path.join(self.cache_dir,'piececache')
+            if not os.path.isdir(self.dir_piececache):
+                os.mkdir(self.dir_piececache)
 
         self.statefile = os.path.join(self.cache_dir,'state'+self.config_type)
 

Modified: debtorrent/trunk/DebTorrent/HTTPCache.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/HTTPCache.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/HTTPCache.py (original)
+++ debtorrent/trunk/DebTorrent/HTTPCache.py Tue Jul 17 06:42:12 2007
@@ -14,19 +14,17 @@
 
 from httplib import HTTPConnection
 from threading import Thread
-from traceback import print_exc
 from DebTorrent.__init__ import product_name,version_short
 from os.path import join, split, getmtime, getsize, exists
 from os import utime, makedirs, listdir
 from time import strftime, strptime, gmtime
 from calendar import timegm
+import logging
 try:
     True
 except:
     True = 1
     False = 0
-
-DEBUG = True
 
 time_format = '%a, %d %b %Y %H:%M:%S'
 VERSION = product_name+'/'+version_short
@@ -81,14 +79,12 @@
         try:
             self.connection = HTTPConnection(self.server)
         except:
-            if DEBUG:
-                print 'cannot connect to http seed:', self.server
+            logging.error('cannot connect to http seed: '+self.server)
             return
         
         self.headers = {'User-Agent': VERSION}
         self.active = False
-        if DEBUG:
-            print 'CacheRequest: downloading ', self.url
+        logging.info('CacheRequest: downloading '+self.url)
         rq = Thread(target = self._request)
         rq.setDaemon(False)
         rq.start()
@@ -101,24 +97,18 @@
         import encodings.idna
         
         try:
-            if DEBUG:
-                print 'CacheRequest: sending request'
-                print 'GET', self.url, self.headers
+            logging.debug('CacheRequest: sending request GET '+self.url+', '+str(self.headers))
             self.connection.request('GET',self.url, None, self.headers)
             
             r = self.connection.getresponse()
                 
-            if DEBUG:
-                print 'CacheRequest: got response'
-                print r.status, r.reason, r.getheaders()
+            logging.debug('CacheRequest: got response '+str(r.status)+', '+r.reason+', '+str(r.getheaders()))
             self.connection_status = r.status
             self.connection_response = r.reason
             self.headers = dict(r.getheaders())
             self.received_data = r.read()
         except Exception, e:
-            if DEBUG:
-                print 'error accessing http server: '+str(e)
-                print_exc()
+            logging.exception('error accessing http server')
             self.connection_status = 500
             self.connection_response = 'Internal Server Error'
             self.headers = {}
@@ -170,8 +160,7 @@
         
         """
         
-        if DEBUG:
-            print 'Starting a HttpCache downloader for:', 'http://'+'/'.join(path)
+        logging.info('Starting a HttpCache downloader for: http://'+'/'.join(path))
         self.downloads.append(CacheRequest(self, path, func))
 
     def download_complete(self, d, path, func, r):
@@ -192,8 +181,7 @@
         
         """
         
-        if DEBUG:
-            print 'HttpCache download completed for:', 'http://'+'/'.join(path)
+        logging.info('HttpCache download completed for: http://'+'/'.join(path))
         self.downloads.remove(d)
 
         if r[0] in (200, 206):

Modified: debtorrent/trunk/DebTorrent/HTTPHandler.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/HTTPHandler.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/HTTPHandler.py (original)
+++ debtorrent/trunk/DebTorrent/HTTPHandler.py Tue Jul 17 06:42:12 2007
@@ -6,8 +6,6 @@
 
 """Handles incoming HTTP connections from other clients to this server.
 
- at type DEBUG: C{boolean}
- at var DEBUG: whether to print debugging messages
 @type weekdays: C{list} of C{string}
 @var weekdays: the days of the week
 @type months: C{list} of C{string}
@@ -20,14 +18,12 @@
 import time
 from clock import clock
 from gzip import GzipFile
-import signal
+import signal, logging
 try:
     True
 except:
     True = 1
     False = 0
-
-DEBUG = True
 
 weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
 
@@ -191,8 +187,7 @@
         except ValueError:
             return None
         self.headers[data[:i].strip().lower()] = data[i+1:].strip()
-        if DEBUG:
-            print data[:i].strip() + ": " + data[i+1:].strip()
+        logging.debug(data[:i].strip() + ": " + data[i+1:].strip())
         return self.read_header
 
     def answer(self, (responsecode, responsestring, headers, data)):
@@ -220,8 +215,7 @@
             if len(cdata) >= len(data):
                 self.encoding = 'identity'
             else:
-                if DEBUG:
-                   print "Compressed: %i  Uncompressed: %i\n" % (len(cdata),len(data))
+                logging.debug('Compressed: '+str(len(cdata))+'  Uncompressed: '+str(len(data)))
                 data = cdata
                 headers['Content-Encoding'] = 'gzip'
 
@@ -296,7 +290,7 @@
                 self.log = open(self.logfile,'a')
                 print >> self.log, "# Log Started: ", isotime()
             except:
-                print "**warning** could not open log file: ", exc_info()[0]
+                logging.exception('could not open log file')
             else:
                 if hupmonitor:
                     def huphandler(signum, frame, self = self):
@@ -305,7 +299,7 @@
                             self.log = open(self.logfile,'a')
                             print >> self.log, "# Log reopened: ", isotime()
                         except:
-                            print "**warning** could not reopen logfile"
+                            logging.exception('could not reopen log file')
                      
                     signal.signal(signal.SIGHUP, huphandler)            
 

Modified: debtorrent/trunk/DebTorrent/RateLimiter.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/RateLimiter.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/RateLimiter.py (original)
+++ debtorrent/trunk/DebTorrent/RateLimiter.py Tue Jul 17 06:42:12 2007
@@ -6,8 +6,6 @@
 
 """Limit the upload rate.
 
- at type DEBUG: C{boolean}
- at var DEBUG: whether to print debugging messages
 @type MAX_RATE_PERIOD: C{float}
 @var MAX_RATE_PERIOD: unknown
 @type MAX_RATE: C{float}
@@ -39,7 +37,7 @@
 
 """
 
-from traceback import print_exc
+import logging
 from binascii import b2a_hex
 from clock import clock
 from CurrentRateMeasure import Measure
@@ -55,8 +53,6 @@
     sum([1])
 except:
     sum = lambda a: reduce(lambda x,y: x+y, a, 0)
-
-DEBUG = True
 
 MAX_RATE_PERIOD = 20.0
 MAX_RATE = 10e10
@@ -236,15 +232,13 @@
         
         """
         
-        if DEBUG:
-            print 'ping delay:', delay
+        logging.debug('ping delay: '+str(delay))
         if not self.autoadjust:
             return
         self.pings.append(delay > PING_BOUNDARY)
         if len(self.pings) < PING_SAMPLES+PING_DISCARDS:
             return
-        if DEBUG:
-            print 'cycle'
+        logging.debug('cycle')
         pings = sum(self.pings[PING_DISCARDS:])
         del self.pings[:]
         if pings >= PING_THRESHHOLD:   # assume flooded
@@ -256,8 +250,7 @@
             self.upload_rate = max(int(self.upload_rate*ADJUST_DOWN),2)
             self.slots = int(sqrt(self.upload_rate*SLOTS_FACTOR))
             self.slotsfunc(self.slots)
-            if DEBUG:
-                print 'adjust down to '+str(self.upload_rate)
+            logging.debug('adjust down to '+str(self.upload_rate))
             self.lasttime = clock()
             self.bytes_sent = 0
             self.autoadjustup = UP_DELAY_FIRST
@@ -270,8 +263,7 @@
             self.upload_rate = int(self.upload_rate*ADJUST_UP)
             self.slots = int(sqrt(self.upload_rate*SLOTS_FACTOR))
             self.slotsfunc(self.slots)
-            if DEBUG:
-                print 'adjust up to '+str(self.upload_rate)
+            logging.debug('adjust up to '+str(self.upload_rate))
             self.lasttime = clock()
             self.bytes_sent = 0
             self.autoadjustup = UP_DELAY_NEXT

Modified: debtorrent/trunk/DebTorrent/RawServer.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/RawServer.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/RawServer.py (original)
+++ debtorrent/trunk/DebTorrent/RawServer.py Tue Jul 17 06:42:12 2007
@@ -21,7 +21,7 @@
 from time import sleep
 from clock import clock
 from signal import signal, SIGINT, SIG_DFL
-import sys
+import sys, logging
 try:
     True
 except:
@@ -307,8 +307,7 @@
         
         """
         
-        if self.noisy:
-            print "Received signal:", signalnum
+        logging.info("Received signal: "+str(signalnum))
         raise KeyboardInterrupt
 
     def listen_forever(self, handler):
@@ -340,7 +339,7 @@
                         if id in self.tasks_to_kill:
                             pass
                         try:
-#                            print func.func_name
+#                            logging.debug(func.func_name)
                             func()
                         except (SystemError, MemoryError), e:
                             self.failfunc(str(e))
@@ -422,7 +421,7 @@
             self.excflag.set()
         self.exccount += 1
         if self.errorfunc is None:
-            print_exc()
+            logging.exception('RawServer exception occurred')
         else:
             data = StringIO()
             print_exc(file = data)

Modified: debtorrent/trunk/DebTorrent/SocketHandler.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/SocketHandler.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/SocketHandler.py (original)
+++ debtorrent/trunk/DebTorrent/SocketHandler.py Tue Jul 17 06:42:12 2007
@@ -21,7 +21,7 @@
     timemult = 1
 from time import sleep
 from clock import clock
-import sys
+import sys, logging
 from random import shuffle, randrange
 # from BT1.StreamCheck import StreamCheck
 # import inspect
@@ -511,7 +511,7 @@
                     self.poll.unregister(s)
                     s.close()
                     del self.servers[sock]
-                    print "lost server socket"
+                    logging.error("lost server socket")
                 elif len(self.single_sockets) < self.max_connects:
                     try:
                         port = s.getsockname()[1]

Modified: debtorrent/trunk/DebTorrent/bencode.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/bencode.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/bencode.py (original)
+++ debtorrent/trunk/DebTorrent/bencode.py Tue Jul 17 06:42:12 2007
@@ -21,6 +21,7 @@
 """
 
 from types import IntType, LongType, StringType, ListType, TupleType, DictType
+import logging
 try:
     from types import BooleanType
 except ImportError:
@@ -471,7 +472,7 @@
     try:
         encode_func[type(x)](x, r)
     except:
-        print "*** error *** could not encode type %s (value: %s)" % (type(x), x)
+        logging.error('could not encode type '+str(type(x))+' (value: '+str(x)+')')
         assert 0
     return ''.join(r)
 

Modified: debtorrent/trunk/DebTorrent/download_bt1.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/download_bt1.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/download_bt1.py (original)
+++ debtorrent/trunk/DebTorrent/download_bt1.py Tue Jul 17 06:42:12 2007
@@ -6,8 +6,6 @@
 
 """Manage a single download.
 
- at type DEBUG: C{boolean}
- at var DEBUG: whether to print debugging information
 @type defaults: C{list} of C{tuple}
 @var defaults: the default configuration variables, including descriptions
 @type argslistheader: C{string}
@@ -78,6 +76,8 @@
         '(0 = disabled)'),
     ('log_dir', '',
         'directory to write the logfiles to (default is to use the cache directory)'),
+    ('log_level', 30,
+        'level to write the logfiles at, varies from 10 (debug) to 50 (critical)'),
     # Rate limits
     ('max_upload_rate', 0,
         'maximum kB/s to upload at (0 = no limit, -1 = automatic)'),

Modified: debtorrent/trunk/DebTorrent/inifile.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/inifile.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/inifile.py (original)
+++ debtorrent/trunk/DebTorrent/inifile.py Tue Jul 17 06:42:12 2007
@@ -28,14 +28,11 @@
 Booleans are written as integers.  Anything other than strings, integers, 
 and floats may have unpredictable results.
 
- at type DEBUG: C{boolean}
- at var DEBUG: whether to print debugging information
-
 '''
 
 from cStringIO import StringIO
-from traceback import print_exc
 from types import DictType, StringType
+import logging
 try:
     from types import BooleanType
 except ImportError:
@@ -46,8 +43,6 @@
 except:
     True = 1
     False = 0
-
-DEBUG = True
 
 def ini_write(f, d, comment=''):
     """Write the ini file.
@@ -70,8 +65,7 @@
             assert type(k) == StringType
             k = k.lower()
             if type(v) == DictType:
-                if DEBUG:
-                    print 'new section:' +k
+                logging.debug('new section:' +k)
                 if k:
                     assert not a.has_key(k)
                     a[k] = {}
@@ -85,8 +79,7 @@
                     if type(vv) == StringType:
                         vv = '"'+vv+'"'
                     aa[kk] = str(vv)
-                    if DEBUG:
-                        print 'a['+k+']['+kk+'] = '+str(vv)
+                    logging.debug('a['+k+']['+kk+'] = '+str(vv))
             else:
                 aa = a['']
                 assert not aa.has_key(k)
@@ -95,8 +88,7 @@
                 if type(v) == StringType:
                     v = '"'+v+'"'
                 aa[k] = str(v)
-                if DEBUG:
-                    print 'a[\'\']['+k+'] = '+str(v)
+                logging.debug('a[\'\']['+k+'] = '+str(v))
         r = open(f,'w')
         if comment:
             for c in comment.split('\n'):
@@ -114,8 +106,7 @@
                 r.write(kk+' = '+aa[kk]+'\n')
         success = True
     except:
-        if DEBUG:
-            print_exc()
+        logging.exception('Error writing config file: '+f)
         success = False
     try:
         r.close()
@@ -124,22 +115,19 @@
     return success
 
 
-if DEBUG:
-    def errfunc(lineno, line, err):
-        """Display an error message when reading the ini file fails.
-        
-        @type lineno: C{int}
-        @param lineno: the line number that caused the error 
-        @type line: C{string}
-        @param line: the line that caused the error
-        @type err: C{string}
-        @param err: the error that was generated
-        
-        """
-        
-        print '('+str(lineno)+') '+err+': '+line
-else:
-    errfunc = lambda lineno, line, err: None
+def errfunc(lineno, line, err):
+    """Display an error message when reading the ini file fails.
+    
+    @type lineno: C{int}
+    @param lineno: the line number that caused the error 
+    @type line: C{string}
+    @param line: the line that caused the error
+    @type err: C{string}
+    @param err: the error that was generated
+    
+    """
+    
+    logging.error('('+str(lineno)+') '+err+': '+line)
 
 def ini_read(f, errfunc = errfunc):
     """Read the ini file.
@@ -148,8 +136,7 @@
     @param f: the file name to read
     @type errfunc: C{function}
     @param errfunc: the function to call when an error occurs
-        (optional, default is to do nothing, unless debugging is enabled in
-        which case the error is printed to standard output)
+        (optional, default is to log an error message)
     @rtype: C{dictionary}
     @return: the data read from the ini file
     
@@ -201,11 +188,9 @@
                 errfunc(i,l,'duplicate entry')
                 continue
             d[k] = v
-        if DEBUG:
-            print dd
+        logging.debug(str(dd))
     except:
-        if DEBUG:
-            print_exc()
+        logging.exception('Error reading config file: '+f)
         dd = None
     try:
         r.close()

Modified: debtorrent/trunk/DebTorrent/iprangeparse.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/iprangeparse.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/iprangeparse.py (original)
+++ debtorrent/trunk/DebTorrent/iprangeparse.py Tue Jul 17 06:42:12 2007
@@ -13,6 +13,7 @@
 """
 
 from bisect import bisect, insort
+import logging
 
 try:
     True
@@ -340,7 +341,7 @@
                 ip2 = to_long_ipv4(ip2)
                 assert ip1 <= ip2
             except:
-                print '*** WARNING *** could not parse IP range: '+line
+                logging.warning('could not parse IP range: '+line)
             l.append((ip1,ip2))
         f.close()
         self._import_ipv4(l)

Modified: debtorrent/trunk/DebTorrent/launchmanycore.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/launchmanycore.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/launchmanycore.py (original)
+++ debtorrent/trunk/DebTorrent/launchmanycore.py Tue Jul 17 06:42:12 2007
@@ -27,7 +27,7 @@
 from clock import clock
 from __init__ import createPeerID, mapbase64, version
 from cStringIO import StringIO
-from traceback import print_exc
+import logging
 from DebTorrent.BT1.AptListener import AptListener
 from DebTorrent.HTTPHandler import HTTPHandler
 
@@ -36,8 +36,6 @@
 except:
     True = 1
     False = 0
-
-DEBUG = True
 
 def fmttime(n):
     """Formats seconds into a human-readable time.
@@ -378,18 +376,15 @@
             # Save the current state of the downloads
             self.configdir.saveState(self.pickle())
 
-            print 'shutting down'
+            logging.info('shutting down')
             self.hashcheck_queue = []
             for hash in self.torrent_list:
-                print 'dropped "'+self.torrent_cache[hash]['path']+'"'
+                logging.info('dropped "'+self.torrent_cache[hash]['path']+'"')
                 self.downloads[hash].shutdown()
             self.rawserver.shutdown()
 
         except:
-            data = StringIO()
-            print_exc(file = data)
-            print 'SYSTEM ERROR - EXCEPTION GENERATED'
-            print data.getvalue()
+            logging.exception('SYSTEM ERROR - EXCEPTION GENERATED')
 
 
     def gather_stats(self):
@@ -565,7 +560,7 @@
                 try:
                     os.makedirs(saveas)
                 except:
-                    print_exc()
+                    logging.exception('error creating the saveas directory: '+saveas)
                     raise OSError("couldn't create directory for "+x['path']
                                           +" ("+saveas+")")
         else:
@@ -596,8 +591,7 @@
         """
 
         file = '/'.join(path)
-        if DEBUG:
-            print 'Trying to find file:', file
+        logging.debug('Trying to find file: '+file)
             
         # Check each torrent in the cache
         for hash, data in self.torrent_cache.items():
@@ -611,12 +605,10 @@
                 
                 # Check that the file ends with the desired file name
                 if file.endswith('/'.join(f['path'])):
-                    if DEBUG:
-                        print 'Found file in:', binascii.b2a_hex(hash)
+                    logging.debug('Found file in: '+str(binascii.b2a_hex(hash)))
                     return self.downloads[hash].d, file_num
                 
-        if DEBUG:
-            print 'Failed to find file.'
+        logging.warning('Failed to find file: '+file)
         return None, None
 
 
@@ -656,7 +648,7 @@
         """
         
         if self.torrent_cache.has_key(hash):
-            print 'DIED: "'+self.torrent_cache[hash]['path']+'"'
+            logging.error('DIED: "'+self.torrent_cache[hash]['path']+'"')
         
     def has_torrent(self, hash):
         """Determine whether there is a downloader for the torrent.
@@ -695,7 +687,7 @@
         
         """
         
-        print 'FAILURE: '+s
+        logging.error('FAILURE: '+s)
 
     def exchandler(self, s):
         """Indicate to the Output that an exception has occurred.
@@ -705,8 +697,7 @@
         
         """
         
-        print 'SYSTEM ERROR - EXCEPTION GENERATED'
-        print s
+        logging.error('SYSTEM ERROR - EXCEPTION GENERATED\n'+s)
 
     def pickle(self):
         """Save the current state of the downloads to a writable state.

Modified: debtorrent/trunk/DebTorrent/parsedir.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/parsedir.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/parsedir.py (original)
+++ debtorrent/trunk/DebTorrent/parsedir.py Tue Jul 17 06:42:12 2007
@@ -4,26 +4,19 @@
 #
 # $Id$
 
-"""Parse a directory for torrent files.
-
- at type NOISY: C{boolean}
- at var NOISY: whether to print additional status messages about the parsing
-
-"""
+"""Parse a directory for torrent files."""
 
 from bencode import bencode, bdecode
 from download_bt1 import get_response
 from os.path import exists, isfile
 from sha import sha
-import sys, os
+import sys, os, logging
 
 try:
     True
 except:
     True = 1
     False = 0
-
-NOISY = True
 
 def _errfunc(x):
     """The default output printing function to use.
@@ -33,7 +26,7 @@
     
     """
     
-    print ":: "+x
+    logging.error(":: "+x)
 
 def parsedir(directory, parsed, files, blocked,
              exts = ['dtorrent'], 

Modified: debtorrent/trunk/DebTorrent/piecebuffer.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/piecebuffer.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/piecebuffer.py (original)
+++ debtorrent/trunk/DebTorrent/piecebuffer.py Tue Jul 17 06:42:12 2007
@@ -6,8 +6,6 @@
 
 """Store buffers of piece data.
 
- at type DEBUG: C{boolean}
- at var DEBUG: whether to print debugging messages
 @type _pool: L{BufferPool}
 @var _pool: example buffer pool
 @type PieceBuffer: L{SingleBuffer}
@@ -17,6 +15,7 @@
 
 from array import array
 from threading import Lock
+import logging
 # import inspect
 try:
     True
@@ -24,8 +23,6 @@
     True = 1
     False = 0
     
-DEBUG = False
-
 class SingleBuffer:
     """A single piece buffer.
     
@@ -38,21 +35,23 @@
     
     """
     
-    def __init__(self, pool):
+    def __init__(self, pool, count):
         """Initialize the instance.
         
         @type pool: L{BufferPool}
         @param pool: the pool of all piece buffers
+        @type count: L{BufferPool}
+        @param count: the pool of all piece buffers
         
         """
         
         self.pool = pool
         self.buf = array('c')
+        self.count = count
+        logging.debug('new/pooled buffer index: '+str(count))
 
     def init(self):
         """Initialize the new piece buffer."""
-        if DEBUG:
-            print 'new/pooled buffer index:', self.count
         self.length = 0
 
     def append(self, s):
@@ -110,8 +109,6 @@
 
     def release(self):
         """Release this buffer to the pool for reuse."""
-        if DEBUG:
-            print 'released buffer with index:', self.count
         self.pool.release(self)
 
 
@@ -131,8 +128,7 @@
         """Initialize the instance."""
         self.pool = []
         self.lock = Lock()
-        if DEBUG:
-            self.count = 0
+        self.count = 0
 
     def new(self):
         """Get a SingleBuffer from the pool, or create a new one.
@@ -146,10 +142,8 @@
         if self.pool:
             x = self.pool.pop()
         else:
-            x = SingleBuffer(self)
-            if DEBUG:
-                self.count += 1
-                x.count = self.count
+            self.count += 1
+            x = SingleBuffer(self, self.count)
         x.init()
         self.lock.release()
         return x

Modified: debtorrent/trunk/DebTorrent/subnetparse.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/subnetparse.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/subnetparse.py (original)
+++ debtorrent/trunk/DebTorrent/subnetparse.py Tue Jul 17 06:42:12 2007
@@ -17,6 +17,7 @@
 """
 
 from bisect import bisect, insort
+import logging
 
 try:
     True
@@ -275,7 +276,7 @@
                     depth = int(depth)
                 self._append(ip,depth)
             except:
-                print '*** WARNING *** could not parse IP range: '+line
+                logging.warning('could not parse IP range: '+line)
         f.close()
         self.ipv4list.sort()
         self.ipv6list.sort()

Modified: debtorrent/trunk/DebTorrent/torrentlistparse.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/DebTorrent/torrentlistparse.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/DebTorrent/torrentlistparse.py (original)
+++ debtorrent/trunk/DebTorrent/torrentlistparse.py Tue Jul 17 06:42:12 2007
@@ -7,6 +7,7 @@
 """Parse a file for a list of torrent hashes."""
 
 from binascii import unhexlify
+import logging
 
 try:
     True
@@ -43,7 +44,7 @@
                 raise ValueError, 'bad line'
             h = unhexlify(l)
         except:
-            print '*** WARNING *** could not parse line in torrent list: '+l
+            logging.warning('could not parse line in torrent list: '+l)
         if parsed.has_key(h):
             del removed[h]
         else:

Modified: debtorrent/trunk/debtorrent-client.conf
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/debtorrent-client.conf?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/debtorrent-client.conf (original)
+++ debtorrent/trunk/debtorrent-client.conf Tue Jul 17 06:42:12 2007
@@ -68,6 +68,20 @@
 
 # log_dir = ""
 
+#
+# Log Level
+#
+# The log level to write output logs at. The possible values are:
+#
+#         50 -- Critical
+#         40 -- Error
+#         30 -- Warn (default)
+#         20 -- Info
+#         10 -- Debug
+#
+
+# log_level = 30
+
 ###############################################################################
 #                              R A T E   L I M I T S
 ###############################################################################
@@ -300,12 +314,12 @@
 #                            A P T   R E Q U E S T S
 ###############################################################################
 #
-# Port
+# APT Port
 #
 # The port to listen for APT requests on.
 #
 
-# port = 9988
+# apt_port = 9988
 
 #
 # Show InfoPage
@@ -431,7 +445,7 @@
 #
 #         0 -- Don't separate, only run a single torrent for all packages
 #         1 -- Separate, and run the architecture:all torrent
-#         2 -- Separate, and run the the non-architecture:all torrent
+#         2 -- Separate, and run the non-architecture:all torrent
 #         3 -- Separate, and run both torrents
 #
 

Modified: debtorrent/trunk/debtorrent-client.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/debtorrent-client.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/debtorrent-client.py (original)
+++ debtorrent/trunk/debtorrent-client.py Tue Jul 17 06:42:12 2007
@@ -27,8 +27,8 @@
 from DebTorrent.download_bt1 import defaults, get_usage
 from DebTorrent.parseargs import parseargs
 from sys import argv, exit
-import os, sys
-from DebTorrent import version, report_email
+import os, logging
+from DebTorrent import version
 from DebTorrent.ConfigDir import ConfigDir
 
 def run(params):
@@ -59,16 +59,17 @@
             outfilename = os.path.join(config['log_dir'], 'debtorrent-client.log')
         else:
             outfilename = os.path.join(configdir.cache_dir, 'debtorrent-client.log')
-        outfile = open(outfilename, 'a', 0)
-        sys.stdout = outfile
-        sys.stderr = outfile
+        logging.basicConfig(level=config['log_level'],
+                            format='%(asctime)s %(levelname)s %(message)s',
+                            filename=outfilename,
+                            filemode='a')
     except ValueError, e:
         print 'error: ' + str(e) + '\n'
         print "Usage: debtorrent-client.py <global options>\n"
         print get_usage(defaults, 80, configdefaults)
         exit(1)
     except IOError, e:
-        print 'error: ' + str(e) + '\n'
+        logging.exception('error: ' + str(e))
         exit(2)
 
     LaunchMany(config, configdir)

Modified: debtorrent/trunk/test.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/trunk/test.py?rev=181&op=diff
==============================================================================
--- debtorrent/trunk/test.py (original)
+++ debtorrent/trunk/test.py Tue Jul 17 06:42:12 2007
@@ -404,6 +404,18 @@
     
     return os.path.join(CWD,'downloader' + str(num_down))
 
+def track_dir(num_track):
+    """Determine the working directory to use for a tracker.
+    
+    @type num_track: C{int}
+    @param num_track: the number of the tracker
+    @rtype: C{string}
+    @return: the tracker's directory
+    
+    """
+    
+    return os.path.join(CWD,'tracker' + str(num_track))
+
 def start_downloader(num_down, options = [], mirror = 'ftp.us.debian.org/debian', 
                      suites = 'main contrib non-free', clean = True):
     """Initialize a new downloader process.
@@ -466,6 +478,7 @@
 
     pid = start(DebTorrentClient.run, ['--cache_dir', downloader_dir,
                                        '--download_dir', downloader_dir,
+                                       '--log_level', '10',
                                        '--security', '0',
                                        '--apt_port', str(num_down) + '988', 
                                        '--max_upload_rate', '100',
@@ -487,7 +500,7 @@
     @param options: the arguments to pass to the tracker
         (optional, defaults to only using the default arguments)
     @type clean: C{boolean}
-    @param clean: whether to remove any previous tracker state files
+    @param clean: whether to remove any previous tracker files
         (optional, defaults to removing them)
     @rtype: C{int}
     @return: the PID of the downloader process
@@ -498,13 +511,16 @@
 
     print '************************** Starting Tracker ' + str(num_track) + ' **************************'
 
+    tracker_dir = track_dir(num_track)
+    
     if clean:
         try:
-            os.remove('tracker' + str(num_track) + '.state')
+            rmrf(tracker_dir)
         except:
             pass
 
-    pid = start(track, ['--dfile', 'tracker' + str(num_track) + '.state',
+    pid = start(track, ['--cache_dir', tracker_dir,
+                        '--log_level', '10',
                         '--port', str(num_track) + '969'] + options)
     return pid
 




More information about the Debtorrent-commits mailing list