refactor the code of checking the python dependencies
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import logging, sys, inspect, os
|
||||
import logging, sys, inspect
|
||||
from os.path import dirname, abspath
|
||||
from optparse import OptionParser
|
||||
|
||||
@@ -8,7 +8,7 @@ try:
|
||||
from setuptools.command import easy_install
|
||||
except ImportError:
|
||||
raise ImportError("""setuptools is not installed for \"""" + sys.executable + """\"
|
||||
Please follow this link for installing instructions :
|
||||
Follow this link for installing instructions :
|
||||
https://pypi.python.org/pypi/setuptools
|
||||
make sure you use \"""" + sys.executable + """\" during the installation""")
|
||||
raise SystemExit
|
||||
@@ -17,22 +17,23 @@ from pkg_resources import parse_version
|
||||
|
||||
def notinstalled(modulename):
|
||||
return modulename + """ could not be imported for \"""" + sys.executable + """\"
|
||||
Please set PYTHONPATH to the output of this command \"make print-OR_TOOLS_PYTHONPATH\" before running the examples"""
|
||||
|
||||
def wrong_version(module, modulename, minimum_version, installed_version):
|
||||
return """
|
||||
You are using """ + modulename + """-""" + installed_version + """ : """ + inspect.getfile(module) + """
|
||||
The minimum required version is : """ + minimum_version + """
|
||||
Please run \"""" + str(sys.executable) + """ setup.py install --user\" to upgrade
|
||||
If the problem persists, then """ + inspect.getfile(module) + """ is binding the newely installed version of """ + modulename + """
|
||||
You should either remove it, or use PYTHONPATH to manage your sys.path. If you decide to use PYTHONPATH, do it to run the ortools examples as well.
|
||||
Check https://docs.python.org/3/tutorial/modules.html#the-module-search-path from more information."""
|
||||
Set PYTHONPATH to the output of this command \"make print-OR_TOOLS_PYTHONPATH\" before running the examples"""
|
||||
|
||||
def wrong_module(module_file, modulename):
|
||||
return """
|
||||
The python examples are not importing the """ + modulename + """ module from the sources because it's binded by \"""" + module_file + """\".
|
||||
Please delete the binding module and rerun this script again.
|
||||
"""
|
||||
The python examples are not importing the """ + modulename + """ module from the sources.
|
||||
Remove the site-package that contains \"""" + module_file + """\", either manually or by using pip, and rerun this script again."""
|
||||
|
||||
def log_error_and_exit(error_message):
|
||||
logging.error(error_message)
|
||||
raise SystemExit
|
||||
|
||||
# Returns the n_th parent of file
|
||||
def n_dirname(n, file):
|
||||
directory = file
|
||||
for x in xrange(0, n):
|
||||
directory = dirname(directory)
|
||||
return directory
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = OptionParser('Log level')
|
||||
@@ -49,10 +50,10 @@ if __name__ == '__main__':
|
||||
-1:logging.DEBUG,
|
||||
}[int(options.log)]
|
||||
|
||||
logging.basicConfig(stream=sys.stdout, level=loglevel)
|
||||
logging.basicConfig(format='[%(levelname)s] %(message)s',stream=sys.stdout, level=loglevel)
|
||||
|
||||
logging.info("Python path : " + sys.executable)
|
||||
logging.info("Python version : " + sys.version + "\n")
|
||||
logging.info("Python version : " + sys.version)
|
||||
|
||||
#try to import ortools
|
||||
try:
|
||||
@@ -63,24 +64,22 @@ if __name__ == '__main__':
|
||||
|
||||
#check if we're using ortools from the sources or it's binded by pypi's module
|
||||
ortools_module_file = inspect.getfile(ortools)
|
||||
ortools_module_path = dirname(dirname(dirname(ortools_module_file)))
|
||||
ortools_project_path = dirname(dirname(dirname(abspath(inspect.getfile(inspect.currentframe())))))
|
||||
ortools_module_path = n_dirname(2, ortools_module_file)
|
||||
ortools_project_path = n_dirname(3, abspath(inspect.getfile(inspect.currentframe())))
|
||||
try:
|
||||
if(ortools_module_path == ortools_project_path):
|
||||
logging.info("Good! The python examples are importing the ortools module from the sources. The imported module is : " + inspect.getfile(ortools))
|
||||
else:
|
||||
raise Exception
|
||||
except (Exception):
|
||||
logging.error(wrong_module(ortools_module_file, "ortools"))
|
||||
raise SystemExit
|
||||
log_error_and_exit(wrong_module(ortools_module_file, "ortools"))
|
||||
|
||||
#try to import protobuf
|
||||
try:
|
||||
import google.protobuf
|
||||
logging.info("Protobuf is imported from " + inspect.getfile(google.protobuf))
|
||||
except ImportError:
|
||||
logging.error (notinstalled("protobuf"))
|
||||
raise SystemExit
|
||||
log_error_and_exit(notinstalled("protobuf"))
|
||||
|
||||
# Check if python can load the libraries' modules
|
||||
# this is useful when the library architecture is not compatbile with the python executable,
|
||||
|
||||
Reference in New Issue
Block a user