summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIsis Lovecruft <isis@torproject.org>2013-02-16 23:36:07 +0000
committerIsis Lovecruft <isis@torproject.org>2013-02-16 23:36:07 +0000
commitbc57191ce66b66b20fd21871a136ad84721b3a7b (patch)
treea65cd2cd1a67d785372755a777d05ea11cb7542f
parentb712b428c595686b187b1c188552b5c66ad684a7 (diff)
Add new version of leap/mx/util/runner.py file.
-rw-r--r--src/leap/mx/runner.py83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/leap/mx/runner.py b/src/leap/mx/runner.py
new file mode 100644
index 0000000..daf956e
--- /dev/null
+++ b/src/leap/mx/runner.py
@@ -0,0 +1,83 @@
+#-*- coding: utf-8 -*-
+"""
+runner
+------
+A module containing application and daemon process utilities.
+
+@author Isis Agora Lovecruft <isis@leap.se>, 0x2cdb8b35
+@version 0.0.1
+
+"""
+
+from os import path as ospath
+
+import re
+
+
+class CheckRequirements(ImportError):
+ """
+ Raised when we're missing something from requirements.pip.
+ """
+ def __init__(self, package_name, pipfile, message=None):
+ """
+ Display an error message with instructions for obtaining missing
+ dependencies.
+
+ @param message: A string describing the error.
+ @param missing: A string indicating which dependency is missing.
+ @param pipfile: The path and filename of the pip requirements file,
+ relative to the top-level repository directory.
+ """
+ if message:
+ self.message = message
+ return self
+
+ self.package_name = package_name
+ self.pipfile = pipfile
+ self.dependencies = self.__read_pip_requirements__()
+ self.missing = []
+
+ for package, version in self.dependencies:
+ pkg = package.lower() if package == "Twisted" else package
+ try:
+ __import__(pkg)
+ except ImportError:
+ self.missing.append(package)
+
+ if len(self.missing) > 0:
+ self.message = self.package_name + " requires "
+ elif len(self.missing) <= 0:
+ return None
+
+ if len(self.missing) >= 1:
+ for missed in self.missing[:-1]:
+ self.message += missed + ", "
+ self.message += "and "
+
+ if len(self.missing) == 1:
+ self.message += self.missing[0] + "."
+ self.message += "\nPlease see %s for ".format(self.pipfile)
+ self.message += "instruction on installing dependencies."
+ raise self(self.message)
+
+ def __read_pip_requirements__(self, file=None):
+ """
+ Check the pip requirements file to determine our dependencies.
+
+ @param file: The full path of the pip requirements.txt file.
+ @returns: A list of tuple(package_name, package_version).
+ """
+ if not file:
+ file = self.pipfile
+
+ requirement = re.compile('[^0-9=><]+')
+ dependencies = []
+
+ with open(file) as pipfile:
+ for line in pipfile.readlines():
+ shortened = line.strip()
+ matched = requirement.match(shortened)
+ package_name = matched.group()
+ package_version = shortened.split(package_name, 1)[1]
+ dependencies.append((package_name, package_version))
+ return dependencies