From c0a40605ad1ce5a85a670c5bee688f51751d7c3b Mon Sep 17 00:00:00 2001 From: Alex Grintsvayg Date: Fri, 31 Mar 2017 14:23:35 -0400 Subject: [PATCH] formatting and minor fixes on scripts --- scripts/create_network.py | 19 +++++++++++-------- scripts/dht_scripts.py | 7 +++---- scripts/dhttest.py | 12 ++++++++---- scripts/node_rpc_cli.py | 2 +- scripts/rpc_node.py | 2 -- 5 files changed, 23 insertions(+), 19 deletions(-) diff --git a/scripts/create_network.py b/scripts/create_network.py index ea814ea11..52726c4e1 100644 --- a/scripts/create_network.py +++ b/scripts/create_network.py @@ -8,7 +8,10 @@ # Thanks to Paul Cannon for IP-address resolution functions (taken from aspn.activestate.com) import argparse -import os, sys, time, signal +import os +import sys +import time +import signal amount = 0 @@ -18,8 +21,8 @@ def destroyNetwork(nodes): i = 0 for node in nodes: i += 1 - hashAmount = i*50/amount - hashbar = '#'*hashAmount + hashAmount = i * 50 / amount + hashbar = '#' * hashAmount output = '\r[%-50s] %d/%d' % (hashbar, i, amount) sys.stdout.write(output) time.sleep(0.15) @@ -28,7 +31,6 @@ def destroyNetwork(nodes): def main(): - parser = argparse.ArgumentParser(description="Launch a network of dht nodes") parser.add_argument("amount_of_nodes", @@ -53,17 +55,17 @@ def main(): print 'Network interface IP address omitted; using %s' % ipAddress startPort = 4000 - port = startPort+1 + port = startPort + 1 nodes = [] print 'Creating Kademlia network' try: node = os.spawnlp( os.P_NOWAIT, 'lbrynet-launch-node', 'lbrynet-launch-node', str(startPort)) nodes.append(node) - for i in range(amount-1): + for i in range(amount - 1): time.sleep(0.15) - hashAmount = i*50/amount - hashbar = '#'*hashAmount + hashAmount = i * 50 / amount + hashbar = '#' * hashAmount output = '\r[%-50s] %d/%d' % (hashbar, i, amount) sys.stdout.write(output) node = os.spawnlp( @@ -85,5 +87,6 @@ def main(): finally: destroyNetwork(nodes) + if __name__ == '__main__': main() diff --git a/scripts/dht_scripts.py b/scripts/dht_scripts.py index 6c3e503bb..fd37763fc 100644 --- a/scripts/dht_scripts.py +++ b/scripts/dht_scripts.py @@ -1,10 +1,10 @@ -from lbrynet.dht.node import Node import binascii -from twisted.internet import reactor, task import logging import sys -from lbrynet.core.utils import generate_id +from lbrynet.dht.node import Node +from twisted.internet import reactor, task +from lbrynet.core.utils import generate_id log = logging.getLogger(__name__) @@ -34,7 +34,6 @@ def join_network(udp_port, known_nodes): def get_hosts(node, h): - def print_hosts(hosts): print "Hosts returned from the DHT: " print hosts diff --git a/scripts/dhttest.py b/scripts/dhttest.py index 8470353b7..8d648f036 100644 --- a/scripts/dhttest.py +++ b/scripts/dhttest.py @@ -21,8 +21,9 @@ # Thanks to Paul Cannon for IP-address resolution functions (taken from aspn.activestate.com) - -import sys, hashlib, random +import binascii +import hashlib +import random import twisted.internet.reactor from lbrynet.dht.node import Node @@ -35,7 +36,7 @@ hash.update("key") KEY = hash.digest() # The value to store VALUE = random.randint(10000, 20000) -import binascii + lbryid = KEY @@ -68,6 +69,7 @@ def genericErrorCallback(failure): print 'An error has occurred:', failure.getErrorMessage() twisted.internet.reactor.callLater(0, stop) + def getValue(): """ Retrieves the value of the specified key (KEY) from the DHT """ global node, KEY @@ -100,8 +102,10 @@ def stop(): print '\nStopping Kademlia node and terminating script' twisted.internet.reactor.stop() + if __name__ == '__main__': import sys + if len(sys.argv) < 2: print 'Usage:\n%s UDP_PORT [KNOWN_NODE_IP KNOWN_NODE_PORT]' % sys.argv[0] print 'or:\n%s UDP_PORT [FILE_WITH_KNOWN_NODES]' % sys.argv[0] @@ -151,7 +155,7 @@ if __name__ == '__main__': # Schedule the node to join the Kademlia/Entangled DHT node.joinNetwork(knownNodes) # Schedule the "storeValue() call to be invoked after 2.5 seconds, - #using KEY and VALUE as arguments + # using KEY and VALUE as arguments twisted.internet.reactor.callLater(2.5, getValue) # Start the Twisted reactor - this fires up all networking, and # allows the scheduled join operation to take place diff --git a/scripts/node_rpc_cli.py b/scripts/node_rpc_cli.py index 7538e3842..43ffa6daf 100644 --- a/scripts/node_rpc_cli.py +++ b/scripts/node_rpc_cli.py @@ -2,10 +2,10 @@ CLI for sending rpc commands to a DHT node """ +import argparse from twisted.internet import reactor from txjsonrpc.web.jsonrpc import Proxy -import argparse def print_value(value): diff --git a/scripts/rpc_node.py b/scripts/rpc_node.py index 44bab16f5..ced4fc6e8 100644 --- a/scripts/rpc_node.py +++ b/scripts/rpc_node.py @@ -12,7 +12,6 @@ Launch a DHT node which can respond to RPC commands. """ - import argparse from lbrynet.dht.node import Node from txjsonrpc.web import jsonrpc @@ -38,7 +37,6 @@ class RPCNode(jsonrpc.JSONRPC): def main(): - parser = argparse.ArgumentParser(description="Launch a dht node which responds to rpc commands") parser.add_argument("node_port",