From 3d6c833df60bcae0bb3aa809ea8c5813289e97f1 Mon Sep 17 00:00:00 2001 From: Cameron Dale Date: Tue, 15 Jan 2008 19:58:53 -0800 Subject: [PATCH] Change all print statements to log.msg calls. --- TODO | 9 --------- apt_dht_Khashmir/actions.py | 10 ++++++---- apt_dht_Khashmir/knode.py | 8 +++++--- apt_dht_Khashmir/krpc.py | 26 +++++++++++++++----------- apt_dht_Khashmir/ktable.py | 3 ++- test.py | 6 +++--- 6 files changed, 31 insertions(+), 31 deletions(-) diff --git a/TODO b/TODO index 10971a0..cb20020 100644 --- a/TODO +++ b/TODO @@ -1,12 +1,3 @@ -Change all print statements to log.msg() calls. - - -Add ability to search and hash and DHT-store other directories. - -The user should be able to specify a list of directories that will be -searched for files to hash and add to the DHT. - - Missing Kademlia implementation details are needed. The current implementation is missing some important features, mostly diff --git a/apt_dht_Khashmir/actions.py b/apt_dht_Khashmir/actions.py index 8da4431..7263711 100644 --- a/apt_dht_Khashmir/actions.py +++ b/apt_dht_Khashmir/actions.py @@ -2,6 +2,7 @@ # see LICENSE.txt for license information from twisted.internet import reactor +from twisted.python import log from khash import intify @@ -83,7 +84,8 @@ class FindNode(ActionBase): def makeMsgFailed(self, node): def defaultGotNodes(err, self=self, node=node): - print ">>> find failed (%s) %s/%s" % (self.config['PORT'], node.host, node.port), err + log.msg("find failed (%s) %s/%s" % (self.config['PORT'], node.host, node.port)) + log.err(err) self.caller.table.nodeFailed(node) self.outstanding = self.outstanding - 1 self.schedule() @@ -153,7 +155,7 @@ class GetValue(FindNode): try: f = getattr(node, self.findValue) except AttributeError: - print ">>> findValue %s doesn't have a %s method!" % (node, self.findValue) + log.msg("findValue %s doesn't have a %s method!" % (node, self.findValue)) else: df = f(self.target, self.caller.node.id) df.addCallback(self.handleGotNodes) @@ -206,7 +208,7 @@ class StoreValue(ActionBase): return t def storeFailed(self, t, node): - print ">>> store failed %s/%s" % (node.host, node.port) + log.msg("store failed %s/%s" % (node.host, node.port)) self.caller.nodeFailed(node) self.outstanding -= 1 if self.finished: @@ -233,7 +235,7 @@ class StoreValue(ActionBase): try: f = getattr(node, self.store) except AttributeError: - print ">>> %s doesn't have a %s method!" % (node, self.store) + log.msg("%s doesn't have a %s method!" % (node, self.store)) else: df = f(self.target, self.value, self.originated, self.caller.node.id) df.addCallback(self.storedValue, node=node) diff --git a/apt_dht_Khashmir/knode.py b/apt_dht_Khashmir/knode.py index eda1b01..fd6214c 100644 --- a/apt_dht_Khashmir/knode.py +++ b/apt_dht_Khashmir/knode.py @@ -1,6 +1,8 @@ ## Copyright 2002-2004 Andrew Loewenstern, All Rights Reserved # see LICENSE.txt for license information +from twisted.python import log + from node import Node, NULL_ID class KNodeBase(Node): @@ -8,17 +10,17 @@ class KNodeBase(Node): try: senderid = dict['rsp']['id'] except KeyError: - print ">>>> No peer id in response" + log.msg("No peer id in response") raise Exception, "No peer id in response." else: if self.id != NULL_ID and senderid != self.id: - print "Got response from different node than expected." + log.msg("Got response from different node than expected.") self.table.invalidateNode(self) return dict def errBack(self, err): - print ">>> ", err + log.err(err) return err def ping(self, id): diff --git a/apt_dht_Khashmir/krpc.py b/apt_dht_Khashmir/krpc.py index 441079c..1428545 100644 --- a/apt_dht_Khashmir/krpc.py +++ b/apt_dht_Khashmir/krpc.py @@ -8,6 +8,7 @@ from traceback import format_exception from twisted.internet.defer import Deferred from twisted.internet import protocol, reactor +from twisted.python import log from twisted.trial import unittest from khash import newID @@ -78,16 +79,17 @@ class KRPC: def datagramReceived(self, str, addr): if self.stopped: if self.noisy: - print "stopped, dropping message from", addr, str + log.msg("stopped, dropping message from %r: %s" % (addr, str)) # bdecode try: msg = bdecode(str) except Exception, e: if self.noisy: - print "response decode error: " + `e` + log.msg("response decode error: ") + log.err(e) else: if self.noisy: - print self.factory.port, "received from", addr, self.addr, ":", msg + log.msg("%d received from %r: %s" % (self.factory.port, addr, msg)) # look at msg type if msg[TYP] == REQ: ilen = len(str) @@ -104,12 +106,12 @@ class KRPC: olen = self._sendResponse(addr, msg[TID], RSP, ret) else: if self.noisy: - print "don't know about method %s" % msg[REQ] + log.msg("don't know about method %s" % msg[REQ]) # unknown method olen = self._sendResponse(addr, msg[TID], ERR, KRPC_ERROR_METHOD_UNKNOWN) if self.noisy: - print "%s %s >>> %s - %s %s %s" % (asctime(), addr, self.factory.node.port, - ilen, msg[REQ], olen) + log.msg("%s >>> %s - %s %s %s" % (addr, self.factory.node.port, + ilen, msg[REQ], olen)) elif msg[TYP] == RSP: # if response # lookup tid @@ -119,8 +121,9 @@ class KRPC: del(self.tids[msg[TID]]) df.callback({'rsp' : msg[RSP], '_krpc_sender': addr}) else: - print 'timeout ' + `msg[RSP]['id']` # no tid, this transaction timed out already... + if self.noisy: + log.msg('timeout: %r' % msg[RSP]['id']) elif msg[TYP] == ERR: # if error # lookup tid @@ -133,7 +136,8 @@ class KRPC: # day late and dollar short pass else: - print "unknown message type " + `msg` + if self.noisy: + log.msg("unknown message type: %r" % msg) # unknown message type df = self.tids[msg[TID]] # callback @@ -147,7 +151,7 @@ class KRPC: msg = {TID : tid, TYP : msgType, msgType : response} if self.noisy: - print self.factory.port, "responding to", addr, ":", msg + log.msg("%d responding to %r: %s" % (self.factory.port, addr, msg)) out = bencode(msg) self.transport.write(out, addr) @@ -160,7 +164,7 @@ class KRPC: # send it msg = {TID : newID(), TYP : REQ, REQ : method, ARG : args} if self.noisy: - print self.factory.port, "sending to", self.addr, ":", msg + log.msg("%d sending to %r: %s" % (self.factory.port, self.addr, msg)) str = bencode(msg) d = Deferred() self.tids[msg[TID]] = d @@ -168,7 +172,7 @@ class KRPC: if tids.has_key(id): df = tids[id] del(tids[id]) - print ">>>>>> KRPC_ERROR_TIMEOUT" + log.msg(">>>>>> KRPC_ERROR_TIMEOUT") df.errback(ProtocolError('timeout waiting for %r' % msg)) later = reactor.callLater(KRPC_TIMEOUT, timeOut) def dropTimeOut(dict, later_call = later): diff --git a/apt_dht_Khashmir/ktable.py b/apt_dht_Khashmir/ktable.py index 6dc5fd6..f3001a5 100644 --- a/apt_dht_Khashmir/ktable.py +++ b/apt_dht_Khashmir/ktable.py @@ -4,6 +4,7 @@ from datetime import datetime from bisect import bisect_left +from twisted.python import log from twisted.trial import unittest import khash @@ -133,7 +134,7 @@ class KTable: # this bucket is full and contains our node, split the bucket if len(self.buckets) >= self.config['HASH_LENGTH']: # our table is FULL, this is really unlikely - print "Hash Table is FULL! Increase K!" + log.err("Hash Table is FULL! Increase K!") return self._splitBucket(self.buckets[i]) diff --git a/test.py b/test.py index f66c7fb..9329b37 100755 --- a/test.py +++ b/test.py @@ -511,7 +511,7 @@ def apt_get(num_down, cmd): """ - print '************************** apt-get (' + str(num_down) + ') ' + ' '.join(cmd) + ' **************************' + print '*************** apt-get (' + str(num_down) + ') ' + ' '.join(cmd) + ' ****************' apt_conf = join([down_dir(num_down), 'etc', 'apt', 'apt.conf']) dpkg_status = join([down_dir(num_down), 'var', 'lib', 'dpkg', 'status']) args = ['-d', '-c', apt_conf, '-o', 'Dir::state::status='+dpkg_status] + cmd @@ -735,9 +735,9 @@ def run_test(bootstraps, downloaders, apt_get_queue): apt_get_results.append((elapsed, r_value)) if r_value == 0: - print '********************** apt-get completed successfully in ' + str(elapsed) + ' sec. **************************' + print '********** apt-get completed successfully in ' + str(elapsed) + ' sec. *****************' else: - print '********************** apt-get finished with status ' + str(r_value) + ' in ' + str(elapsed) + ' sec. **************************' + print '********** apt-get finished with status ' + str(r_value) + ' in ' + str(elapsed) + ' sec. ************' sleep(5) -- 2.39.5