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

[tor-commits] [stem/master] Adding module to check for requirements



commit b84a75638942e61a58b2af8edd97448ea72857d7
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Wed Jun 13 09:20:34 2012 -0700

    Adding module to check for requirements
    
    Stem requires at least python 2.5, and beyond that our python version and
    modules does impact what functionality we have available (yay). Adding a module
    for tracking what we have available.
---
 run_tests.py                              |    9 +++
 stem/__init__.py                          |    2 +-
 stem/descriptor/server_descriptor.py      |   11 +---
 stem/prereq.py                            |   86 +++++++++++++++++++++++++++++
 test/unit/descriptor/server_descriptor.py |    5 +-
 5 files changed, 102 insertions(+), 11 deletions(-)

diff --git a/run_tests.py b/run_tests.py
index a6d604c..5c41f40 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -45,6 +45,7 @@ import test.integ.util.system
 import test.integ.process
 import test.integ.version
 
+import stem.prereq
 import stem.util.conf
 import stem.util.enum
 import stem.util.log as log
@@ -218,6 +219,14 @@ def load_user_configuration(test_config):
     sys.exit(1)
 
 if __name__ == '__main__':
+  try:
+    stem.prereq.check_requriements()
+  except ImportError, exc:
+    print exc
+    print
+    
+    sys.exit(1)
+  
   start_time = time.time()
   
   # override flag to indicate at the end that testing failed somewhere
diff --git a/stem/__init__.py b/stem/__init__.py
index 61374a6..8f5add1 100644
--- a/stem/__init__.py
+++ b/stem/__init__.py
@@ -8,5 +8,5 @@ __contact__ = 'atagar@xxxxxxxxxxxxxx'
 __url__ = 'http://www.atagar.com/stem/'
 __license__ = 'LGPLv3'
 
-__all__ = ["descriptor", "response", "util", "connection", "control", "process", "socket", "version"]
+__all__ = ["descriptor", "response", "util", "connection", "control", "prereq", "process", "socket", "version"]
 
diff --git a/stem/descriptor/server_descriptor.py b/stem/descriptor/server_descriptor.py
index 99080ab..765a5ba 100644
--- a/stem/descriptor/server_descriptor.py
+++ b/stem/descriptor/server_descriptor.py
@@ -32,6 +32,7 @@ import base64
 import hashlib
 import datetime
 
+import stem.prereq
 import stem.descriptor
 import stem.descriptor.extrainfo_descriptor
 import stem.version
@@ -39,13 +40,6 @@ import stem.util.log as log
 import stem.util.connection
 import stem.util.tor_tools
 
-try:
-  import rsa
-  IS_RSA_AVAILABLE = True
-except ImportError:
-  log.info("Unable to import the rsa module. Because of this we'll be unable to verify descriptor signature integrity.")
-  IS_RSA_AVAILABLE = False
-
 # relay descriptors must have exactly one of the following
 REQUIRED_FIELDS = (
   "router",
@@ -572,7 +566,8 @@ class RelayDescriptor(ServerDescriptor):
     # if we have a fingerprint then checks that our fingerprint is a hash of
     # our signing key
     
-    if IS_RSA_AVAILABLE and validate and self.fingerprint:
+    if validate and self.fingerprint and stem.prereq.is_rsa_available():
+      import rsa
       pubkey = rsa.PublicKey.load_pkcs1(self.signing_key)
       der_encoded = pubkey.save_pkcs1(format = "DER")
       key_hash = hashlib.sha1(der_encoded).hexdigest()
diff --git a/stem/prereq.py b/stem/prereq.py
new file mode 100644
index 0000000..e443078
--- /dev/null
+++ b/stem/prereq.py
@@ -0,0 +1,86 @@
+"""
+Checks for stem dependencies. We require python 2.5 or greater (in the 2.x
+series). Other requirements for complete functionality are...
+
+* Python 2.6
+
+  * os.walk's followlinks argument
+
+* rsa module
+
+  * validating descriptor signagure integrity
+
+::
+
+  check_requriements - checks for minimum requirements for running stem
+  
+  is_python_26 - checks if python 2.6 or later is available
+  is_python_27 - checks if python 2.7 or later is available
+  
+  is_rsa_available - checks if the rsa module is available
+"""
+
+import sys
+
+import stem.util.log as log
+
+IS_RSA_AVAILABLE = None
+
+def check_requriements():
+  """
+  Checks that we meet the minimum requirements to run stem. If we don't then
+  this raises an ImportError with the issue.
+  
+  :raises: ImportError with the problem if we don't meet stem's requirements
+  """
+  
+  major_version, minor_version = sys.version_info[0:2]
+  
+  if major_version > 2:
+    raise ImportError("stem isn't compatible beyond the python 2.x series")
+  elif major_version < 2 or minor_version < 5:
+    raise ImportError("stem requires python version 2.5 or greater")
+
+def is_python_26():
+  """
+  Checks if we're in the 2.6 - 2.x range.
+  
+  :returns: bool that is True if we meet this requirement and False otherwise
+  """
+  
+  return _check_version(6)
+
+def is_python_27():
+  """
+  Checks if we're in the 2.7 - 2.x range.
+  
+  :returns: bool that is True if we meet this requirement and False otherwise
+  """
+  
+  return _check_version(7)
+
+def is_rsa_available():
+  global IS_RSA_AVAILABLE
+  
+  if IS_RSA_AVAILABLE == None:
+    try:
+      import rsa
+      IS_RSA_AVAILABLE = True
+    except ImportError:
+      IS_RSA_AVAILABLE = False
+      
+      msg = "Unable to import the rsa module. Because of this we'll be unable to verify descriptor signature integrity."
+      log.log_once("stem.prereq.is_rsa_availabe", log.INFO, msg)
+  
+  return IS_RSA_AVAILABLE
+
+def _check_version(minor_req):
+  major_version, minor_version = sys.version_info[0:2]
+  
+  if major_version > 2:
+    return False
+  elif major_version < 2 or minor_version < minor_req:
+    return False
+  
+  return True
+
diff --git a/test/unit/descriptor/server_descriptor.py b/test/unit/descriptor/server_descriptor.py
index 3bb2c7a..8502b56 100644
--- a/test/unit/descriptor/server_descriptor.py
+++ b/test/unit/descriptor/server_descriptor.py
@@ -6,6 +6,7 @@ import datetime
 import StringIO
 import unittest
 
+import stem.prereq
 import stem.descriptor.server_descriptor
 from stem.descriptor.server_descriptor import RelayDescriptor, BridgeDescriptor
 
@@ -312,7 +313,7 @@ class TestServerDescriptor(unittest.TestCase):
     Checks that a fingerprint matching the hash of our signing key will validate.
     """
     
-    if not stem.descriptor.server_descriptor.IS_RSA_AVAILABLE:
+    if not stem.prereq.is_rsa_available():
       self.skipTest("(rsa module unavailable)")
     
     fingerprint = "4F0C 867D F0EF 6816 0568 C826 838F 482C EA7C FE44"
@@ -326,7 +327,7 @@ class TestServerDescriptor(unittest.TestCase):
     it doesn't match the hash of our signing key.
     """
     
-    if not stem.descriptor.server_descriptor.IS_RSA_AVAILABLE:
+    if not stem.prereq.is_rsa_available():
       self.skipTest("(rsa module unavailable)")
     
     fingerprint = "4F0C 867D F0EF 6816 0568 C826 838F 482C EA7C FE45"



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