[tor-commits] [nyx/master] Move interaction of --interface and --socket to arguments

atagar at torproject.org atagar at torproject.org
Tue Sep 12 18:47:18 UTC 2017


commit 305f575eb060ed31e51f5e9b537cbb5f13e96bf7
Author: Damian Johnson <atagar at torproject.org>
Date:   Tue Sep 5 11:22:01 2017 -0700

    Move interaction of --interface and --socket to arguments
    
    When interface is set we unset the socket, and vic versa. Doing this in the
    arguments module both makes sense and lets us clean up the starter a bit.
---
 nyx/arguments.py         | 29 +++++++++++++++++------------
 nyx/panel/connection.py  |  4 ++--
 nyx/starter.py           | 15 ++-------------
 test/arguments.py        | 25 ++++++++++++++++++++-----
 web/changelog/index.html |  1 +
 5 files changed, 42 insertions(+), 32 deletions(-)

diff --git a/nyx/arguments.py b/nyx/arguments.py
index e55b653..698328f 100644
--- a/nyx/arguments.py
+++ b/nyx/arguments.py
@@ -15,11 +15,8 @@ import nyx.log
 import stem.util.connection
 
 DEFAULT_ARGS = {
-  'control_address': '127.0.0.1',
-  'control_port': 9051,
-  'user_provided_port': False,
+  'control_port': ('127.0.0.1', 9051),
   'control_socket': '/var/run/tor/control',
-  'user_provided_socket': False,
   'config': os.path.join(os.path.expanduser('~/.nyx'), 'config'),
   'debug_path': None,
   'logged_events': 'NOTICE,WARN,ERR,NYX_NOTICE,NYX_WARNING,NYX_ERROR',
@@ -81,27 +78,27 @@ def parse(argv):
   except getopt.GetoptError as exc:
     raise ValueError('%s (for usage provide --help)' % exc)
 
+  has_port_arg, has_socket_arg = False, False
+
   for opt, arg in recognized_args:
     if opt in ('-i', '--interface'):
       if ':' in arg:
         address, port = arg.split(':', 1)
       else:
-        address, port = None, arg
+        address, port = args['control_port'][0], arg
 
       if address is not None:
         if not stem.util.connection.is_valid_ipv4_address(address):
           raise ValueError("'%s' isn't a valid IPv4 address" % address)
 
-        args['control_address'] = address
-
       if not stem.util.connection.is_valid_port(port):
         raise ValueError("'%s' isn't a valid port number" % port)
 
-      args['control_port'] = int(port)
-      args['user_provided_port'] = True
+      args['control_port'] = (address, int(port))
+      has_port_arg = True
     elif opt in ('-s', '--socket'):
       args['control_socket'] = arg
-      args['user_provided_socket'] = True
+      has_socket_arg = True
     elif opt in ('-c', '--config'):
       args['config'] = arg
     elif opt in ('-d', '--debug'):
@@ -113,6 +110,14 @@ def parse(argv):
     elif opt in ('-h', '--help'):
       args['print_help'] = True
 
+  # If the user explicitely specified an endpoint then just try to connect to
+  # that.
+
+  if has_socket_arg and not has_port_arg:
+    args['control_port'] = None
+  elif has_port_arg and not has_socket_arg:
+    args['control_socket'] = None
+
   # translates our args dict into a named tuple
 
   Args = collections.namedtuple('Args', args.keys())
@@ -127,8 +132,8 @@ def get_help():
   """
 
   return HELP_OUTPUT.format(
-    address = DEFAULT_ARGS['control_address'],
-    port = DEFAULT_ARGS['control_port'],
+    address = DEFAULT_ARGS['control_port'][0],
+    port = DEFAULT_ARGS['control_port'][1],
     socket = DEFAULT_ARGS['control_socket'],
     config_path = DEFAULT_ARGS['config'],
   )
diff --git a/nyx/panel/connection.py b/nyx/panel/connection.py
index 41577b6..4683dcc 100644
--- a/nyx/panel/connection.py
+++ b/nyx/panel/connection.py
@@ -5,11 +5,11 @@
 Listing of the currently established connections tor has made.
 """
 
-import re
-import time
 import collections
 import curses
 import itertools
+import re
+import time
 
 import nyx.curses
 import nyx.panel
diff --git a/nyx/starter.py b/nyx/starter.py
index 077ca36..88cf9f5 100644
--- a/nyx/starter.py
+++ b/nyx/starter.py
@@ -65,20 +65,9 @@ def main(config):
 
   _load_user_nyxrc(args.config)
 
-  control_port = (args.control_address, args.control_port)
-  control_socket = args.control_socket
-
-  # If the user explicitely specified an endpoint then just try to connect to
-  # that.
-
-  if args.user_provided_socket and not args.user_provided_port:
-    control_port = None
-  elif args.user_provided_port and not args.user_provided_socket:
-    control_socket = None
-
   controller = init_controller(
-    control_port = control_port,
-    control_socket = control_socket,
+    control_port = args.control_port,
+    control_socket = args.control_socket,
     password_prompt = True,
     chroot_path = config.get('tor_chroot', ''),
   )
diff --git a/test/arguments.py b/test/arguments.py
index c146e7d..454c3ca 100644
--- a/test/arguments.py
+++ b/test/arguments.py
@@ -16,12 +16,10 @@ class TestArgumentParsing(unittest.TestCase):
 
   def test_that_we_load_arguments(self):
     args = parse(['--interface', '10.0.0.25:80'])
-    self.assertEqual('10.0.0.25', args.control_address)
-    self.assertEqual(80, args.control_port)
+    self.assertEqual(('10.0.0.25', 80), args.control_port)
 
     args = parse(['--interface', '80'])
-    self.assertEqual(DEFAULT_ARGS['control_address'], args.control_address)
-    self.assertEqual(80, args.control_port)
+    self.assertEqual((DEFAULT_ARGS['control_port'][0], 80), args.control_port)
 
     args = parse(['--socket', '/tmp/my_socket', '--config', '/tmp/my_config'])
     self.assertEqual('/tmp/my_socket', args.control_socket)
@@ -41,7 +39,7 @@ class TestArgumentParsing(unittest.TestCase):
 
   def test_examples(self):
     args = parse(['-i', '1643'])
-    self.assertEqual(1643, args.control_port)
+    self.assertEqual((DEFAULT_ARGS['control_port'][0], 1643), args.control_port)
 
     args = parse(['-l', 'WARN,ERR', '-c', '/tmp/cfg'])
     self.assertEqual('WARN,ERR', args.logged_events)
@@ -50,6 +48,23 @@ class TestArgumentParsing(unittest.TestCase):
   def test_that_we_reject_unrecognized_arguments(self):
     self.assertRaises(ValueError, parse, ['--blarg', 'stuff'])
 
+  def test_that_port_and_socket_unset_other(self):
+    args = parse([])
+    self.assertEqual(DEFAULT_ARGS['control_port'], args.control_port)
+    self.assertEqual(DEFAULT_ARGS['control_socket'], args.control_socket)
+
+    args = parse(['--interface', '10.0.0.25:80'])
+    self.assertEqual(('10.0.0.25', 80), args.control_port)
+    self.assertEqual(None, args.control_socket)
+
+    args = parse(['--socket', '/tmp/my_socket'])
+    self.assertEqual(None, args.control_port)
+    self.assertEqual('/tmp/my_socket', args.control_socket)
+
+    args = parse(['--interface', '10.0.0.25:80', '--socket', '/tmp/my_socket'])
+    self.assertEqual(('10.0.0.25', 80), args.control_port)
+    self.assertEqual('/tmp/my_socket', args.control_socket)
+
   def test_that_we_reject_invalid_interfaces(self):
     invalid_inputs = (
       '',
diff --git a/web/changelog/index.html b/web/changelog/index.html
index 3bc4641..7977afd 100644
--- a/web/changelog/index.html
+++ b/web/changelog/index.html
@@ -60,6 +60,7 @@
         <li><span class="component">Startup</span>
           <ul>
             <li>Startup is several seconds faster when ran for the first time</li>
+            <li>Halved startup time and reduced memory by caching consensus data to disk</li>
           </ul>
         </li>
 





More information about the tor-commits mailing list