[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [arm/master] Breaking main() method into helper functions



commit 06b61ed911ce095ca277dc409ee5d5f27265c900
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Fri Dec 27 10:04:50 2013 -0800

    Breaking main() method into helper functions
    
    Our main method is way too long. This isn't all the trimming I'd like to do,
    but it's a start.
---
 arm/config/strings.cfg |    3 +-
 arm/starter.py         |  115 ++++++++++++++++++++++++++++++------------------
 arm/util/tracker.py    |    2 +-
 3 files changed, 74 insertions(+), 46 deletions(-)

diff --git a/arm/config/strings.cfg b/arm/config/strings.cfg
index cb1f7cf..7451cde 100644
--- a/arm/config/strings.cfg
+++ b/arm/config/strings.cfg
@@ -12,7 +12,7 @@
 #
 ################################################################################
 
-msg.config.unable_to_load_settings Unable to load arm's internal configuration ({path}): {error}
+msg.config.unable_to_load_settings Unable to load arm's internal configurations: {error}
 msg.config.unable_to_read_file Failed to load configuration (using defaults): "{error}"
 msg.config.nothing_loaded No armrc loaded, using defaults. You can customize arm by placing a configuration file at {path} (see the armrc.sample for its options).
 
@@ -36,6 +36,7 @@ msg.setup.unable_to_determine_pid Unable to determine Tor's pid. Some informatio
 msg.setup.unknown_event_types arm doesn't recognize the following event types: {event_types} (log 'UNKNOWN' events to see them)
 
 msg.tracker.abort_getting_resources Failed three attempts to get process resource usage from {resolver}, {response} ({exc})
+msg.tracker.lookup_rate_increased connection lookup time increasing to {seconds} seconds per call
 msg.tracker.unable_to_get_resources Unable to query process resource usage from {resolver} ({exc})
 msg.tracker.unable_to_use_all_resolvers We were unable to use any of your system's resolvers to get tor's connections. This is fine, but means that the connections page will be empty. This is usually permissions related so if you would like to fix this then run arm with the same user as tor (ie, "sudo -u <tor user> arm").
 msg.tracker.unable_to_use_resolver Unable to query connections with {old_resolver}, trying {new_resolver}
diff --git a/arm/starter.py b/arm/starter.py
index af760f7..a8824a7 100644
--- a/arm/starter.py
+++ b/arm/starter.py
@@ -34,8 +34,6 @@ import stem.util.system
 
 from arm.util import init_controller, msg, trace, info, notice, warn, load_settings
 
-SETTINGS_PATH = os.path.join(os.path.dirname(__file__), 'settings.cfg')
-
 CONFIG = stem.util.conf.config_dict("arm", {
   'tor.chroot': '',
   'tor.password': None,
@@ -49,11 +47,12 @@ def main():
   try:
     load_settings()
   except IOError as exc:
-    print msg('config.unable_to_load_settings', path = SETTINGS_PATH, error = exc)
+    print msg('config.unable_to_load_settings', error = exc)
     sys.exit(1)
 
   try:
     args = arm.arguments.parse(sys.argv[1:])
+    config.set('startup.events', args.logged_events)
   except getopt.GetoptError as exc:
     print msg('usage.invalid_arguments', error = exc)
     sys.exit(1)
@@ -76,27 +75,8 @@ def main():
       print msg('debug.unable_to_write_file', path = args.debug_path, error = exc.strerror)
       sys.exit(1)
 
-  # loads user's personal armrc if available
-
-  if os.path.exists(args.config):
-    try:
-      config.load(args.config)
-    except IOError as exc:
-      warn('config.unable_to_read_file', error = exc.strerror)
-  else:
-    notice('config.nothing_loaded', path = args.config)
-
-  config.set('startup.events', args.logged_events)
-
-  # check that the chroot exists and strip trailing slashes
-
-  chroot = CONFIG['tor.chroot'].strip().rstrip(os.path.sep)
-
-  if chroot and not os.path.exists(chroot):
-    stem.util.log.notice(msg('setup.chroot_doesnt_exist', path = chroot))
-    config.set('tor.chroot', '')
-  else:
-    config.set('tor.chroot', chroot)  # use the normalized path
+  _load_armrc(args.config)
+  _validate_chroot()
 
   # validates and expands log event flags
 
@@ -121,17 +101,8 @@ def main():
 
   config.set('tor.password', '')
 
-  # Give a notice if tor or arm are running with root. Querying connections
-  # usually requires us to have the same permissions as tor so if tor is
-  # running as root then drop this notice (they're already then being warned
-  # about tor being root anyway).
-
-  tor_user = controller.get_user(None)
-
-  if tor_user == "root":
-    notice('setup.tor_is_running_as_root')
-  elif os.getuid() == 0:
-    notice('setup.arm_is_running_as_root', tor_user = tor_user if tor_user else "<tor user>")
+  _setup_freebsd_chroot(controller)
+  _warn_if_root(controller)
 
   # fetches descriptions for tor's configuration options
 
@@ -154,15 +125,6 @@ def main():
   except ValueError:
     warn('setup.unable_to_determine_pid')
 
-  # If we're running under FreeBSD then check the system for a chroot path.
-
-  if not CONFIG['tor.chroot'] and platform.system() == 'FreeBSD':
-    jail_chroot = stem.util.system.get_bsd_jail_path(controller.get_pid(0))
-
-    if jail_chroot and os.path.exists(jail_chroot):
-      info('setup.set_freebsd_chroot', path = jail_chroot)
-      config.set('tor.chroot', jail_chroot)
-
   # If using our LANG variable for rendering multi-byte characters lets us
   # get unicode support then then use it. This needs to be done before
   # initializing curses.
@@ -307,6 +269,71 @@ def _setup_debug_logging(args):
   )
 
 
+def _load_armrc(path):
+  """
+  Loads user's personal armrc if it's available.
+  """
+
+  if os.path.exists(path):
+    try:
+      config = stem.util.conf.get_config('arm')
+      config.load(path)
+    except IOError as exc:
+      warn('config.unable_to_read_file', error = exc.strerror)
+  else:
+    notice('config.nothing_loaded', path = path)
+
+
+def _validate_chroot():
+  """
+  Ensure that the chroot exists and strip trailing slashes.
+  """
+
+  config = stem.util.conf.get_config('arm')
+  chroot = CONFIG['tor.chroot'].strip().rstrip(os.path.sep)
+
+  if chroot and not os.path.exists(chroot):
+    notice('setup.chroot_doesnt_exist', path = chroot)
+    config.set('tor.chroot', '')
+  else:
+    config.set('tor.chroot', chroot)  # use the normalized path
+
+
+def _setup_freebsd_chroot(controller):
+  """
+  If we're running under FreeBSD then check the system for a chroot path.
+
+  :param stem.control.Controller controller: tor controller connection
+  """
+
+  if not CONFIG['tor.chroot'] and platform.system() == 'FreeBSD':
+    jail_chroot = stem.util.system.get_bsd_jail_path(controller.get_pid(0))
+
+    if jail_chroot and os.path.exists(jail_chroot):
+      info('setup.set_freebsd_chroot', path = jail_chroot)
+
+      config = stem.util.conf.get_config('arm')
+      config.set('tor.chroot', jail_chroot)
+
+
+def _warn_if_root(controller):
+  """
+  Give a notice if tor or arm are running with root. Querying connections
+  usually requires us to have the same permissions as tor so if tor is
+  running as root then just warning about that.
+
+  :param stem.control.Controller controller: tor controller connection
+  """
+
+  tor_user = controller.get_user(None)
+
+  if tor_user == "root":
+    notice('setup.tor_is_running_as_root')
+  elif os.getuid() == 0:
+    tor_user = tor_user if tor_user else "<tor user>"
+    notice('setup.arm_is_running_as_root', tor_user = tor_user)
+
+
 def _shutdown_daemons(controller):
   """
   Stops and joins on worker threads.
diff --git a/arm/util/tracker.py b/arm/util/tracker.py
index 3874d4c..f9e60f8 100644
--- a/arm/util/tracker.py
+++ b/arm/util/tracker.py
@@ -360,7 +360,7 @@ class ConnectionTracker(Daemon):
           min_rate += 1  # little extra padding so we don't frequently update this
           self.set_rate(min_rate)
           self._rate_too_low_count = 0
-          log.debug("connection lookup time increasing to %0.1f seconds per call" % min_rate)
+          debug('tracker.lookup_rate_increased', seconds = "%0.1f" % min_rate)
       else:
         self._rate_too_low_count = 0
 

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits