[tor-commits] [nyx/master] Simplifying run_nyx and fixing prerequisite checks

atagar at torproject.org atagar at torproject.org
Sun Jun 7 20:44:26 UTC 2015


commit 9d705ff9cc5955e5468126f3494da66dc1328ae1
Author: Damian Johnson <atagar at torproject.org>
Date:   Fri Jun 5 08:51:09 2015 -0700

    Simplifying run_nyx and fixing prerequisite checks
    
    Yeah, that wasn't gonna work. We imported nyx, *then* ran a function to check
    for ImportErrors. If we were missing a prereqisite then the initial import
    would've failed making our _check_prereq() function pointless.
    
    Stem requires Python 2.6 so that check is pointless. As for the rest it's both
    simpler and safer to import nyx then see what breaks.
    
    This also makes our run_nyx script dead simple (main() method is now in our
    __init__.py).
---
 nyx/__init__.py |   34 +++++++++++++++++++++++++++++++--
 run_nyx         |   56 ++-----------------------------------------------------
 2 files changed, 34 insertions(+), 56 deletions(-)

diff --git a/nyx/__init__.py b/nyx/__init__.py
index b81c0fb..c38b4f7 100644
--- a/nyx/__init__.py
+++ b/nyx/__init__.py
@@ -2,6 +2,9 @@
 Tor curses monitoring application.
 """
 
+__version__ = '1.4.6_dev'
+__release_date__ = 'April 28, 2011'
+
 __all__ = [
   'arguments',
   'config_panel',
@@ -13,5 +16,32 @@ __all__ = [
   'torrc_panel',
 ]
 
-__version__ = '1.4.6_dev'
-__release_date__ = 'April 28, 2011'
+import distutils.spawn
+import sys
+
+
+def main():
+  try:
+    import nyx.starter
+    nyx.starter.main()
+  except ImportError as exc:
+    if exc.message == 'No module named stem':
+      if distutils.spawn.find_executable('pip') is not None:
+        advice = ", try running 'sudo pip install stem'"
+      elif distutils.spawn.find_executable('apt-get') is not None:
+        advice = ", try running 'sudo apt-get install python-stem'"
+      else:
+        advice = ', you can find it at https://stem.torproject.org/download.html'
+
+      print 'nyx requires stem' + advice
+    elif exc.message == 'No module named curses':
+      if distutils.spawn.find_executable('apt-get') is not None:
+        advice = ", try running 'sudo apt-get install python-curses'"
+      else:
+        advice = ''  # not sure what to do for other platforms
+
+      print 'nyx requires curses' + advice
+    else:
+      print 'Unable to start nyx: %s' % exc
+
+    sys.exit(1)
diff --git a/run_nyx b/run_nyx
index c3ba49b..866e050 100755
--- a/run_nyx
+++ b/run_nyx
@@ -2,59 +2,7 @@
 # Copyright 2014, Damian Johnson and The Tor Project
 # See LICENSE for licensing information
 
-import distutils.spawn
-import sys
-
-import nyx.starter
-
-def main():
-  try:
-    _check_prereq()
-  except ImportError as exc:
-    print exc
-    sys.exit(1)
-
-  nyx.starter.main()
-
-
-def _check_prereq():
-  """
-  Checks for nyx's prerequisistes...
-
-    * python 2.6 or later
-    * stem
-    * curses
-
-  :raises: **ImportError** if any of our prerequisites aren't met
-  """
-
-  major_version, minor_version = sys.version_info[0:2]
-
-  if major_version < 2 or (major_version == 2 and minor_version < 6):
-    raise ImportError('nyx requires python version 2.6 or greater')
-
-  try:
-    import stem
-  except ImportError:
-    if distutils.spawn.find_executable('pip') is not None:
-      advice = "try running 'sudo pip install stem'"
-    elif distutils.spawn.find_executable('apt-get') is not None:
-      advice = "try running 'sudo apt-get install python-stem'"
-    else:
-      advice = 'you can find it at https://stem.torproject.org/download.html'
-
-    raise ImportError('nyx requires stem, %s' % advice)
-
-  try:
-    import curses
-  except ImportError:
-    if distutils.spawn.find_executable('apt-get') is not None:
-      advice = ", try running 'sudo apt-get install python-curses'"
-    else:
-      advice = ''  # not sure what to do for other platforms
-
-    raise ImportError('nyx requires curses' + advice)
-
+import nyx
 
 if __name__ == '__main__':
-  main()
+  nyx.main()





More information about the tor-commits mailing list