diff --git a/examples/python/check_dependencies.py b/examples/python/check_dependencies.py index b8f1c0c257..ba2b7114e5 100644 --- a/examples/python/check_dependencies.py +++ b/examples/python/check_dependencies.py @@ -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, diff --git a/tools/check_python_deps.py b/tools/check_python_deps.py index 9c04329360..669d837da9 100644 --- a/tools/check_python_deps.py +++ b/tools/check_python_deps.py @@ -7,35 +7,35 @@ 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 from pkg_resources import parse_version - -current_ortools_version = "VVVV" -minimum_protobuf_version = "PROTOBUF_TAG" +required_ortools_version = "VVVV" +required_protobuf_version = "PROTOBUF_TAG" def notinstalled(modulename): return modulename + """ is not installed for \"""" + sys.executable + """\" -Please run \"""" + str(sys.executable) + """ setup.py install --user\"""" +Run \"""" + sys.executable + """ setup.py install --user\" to install it""" -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.""" +def wrong_version(module, modulename, required_version, installed_version): + return """You are using """ + modulename + """-""" + installed_version + """ : """ + inspect.getfile(module) + """, while the required version is : """ + required_version + """ +Run \"""" + sys.executable + """ setup.py install --user\" to upgrade. +If the problem persists, remove the site-package that contains \"""" + inspect.getfile(module) + """\". You can do so either manually or by using pip.""" + +def log_error_and_exit(error_message): + logging.error(error_message) + raise SystemExit if __name__ == '__main__': parser = OptionParser('Log level') parser.add_option('-l','--log',type='string',help='Available levels are CRITICAL (3), ERROR (2), WARNING (1), INFO (0), DEBUG (-1)',default='INFO') options,args = parser.parse_args() + #Create the logger try: loglevel = getattr(logging,options.log.upper()) except AttributeError: @@ -46,48 +46,45 @@ if __name__ == '__main__': -1:logging.DEBUG, }[int(options.log)] - logging.basicConfig(stream=sys.stdout, level=loglevel) - - logging.info("Python path : " + sys.executable) - logging.info("Python version : " + sys.version + "\n") + logging.basicConfig(format='[%(levelname)s] %(message)s', stream=sys.stdout, level=loglevel) - if version_info[0] >= 3: + #Display Python Version and path + logging.info("Python path : " + sys.executable) + logging.info("Python version : " + sys.version) + + #Choose the pypi package + if sys.version_info[0] >= 3: ortools_name = "py3-ortools" else: ortools_name = "ortools" + #try to import ortools try: import ortools except ImportError: - logging.error (notinstalled(ortools_name)) - raise SystemExit + log_error_and_exit(notinstalled(ortools_name)) #try to import protobuf try: import google.protobuf except ImportError: - logging.error (notinstalled("protobuf")) - raise SystemExit + log_error_and_exit(notinstalled("protobuf")) #check ortools version try: - if parse_version(current_ortools_version) > parse_version(ortools.__version__): + if required_ortools_version != ortools.__version__: raise Exception - logging.info("or-tools version : " + ortools.__version__) - logging.info(inspect.getfile(ortools) + "\n") + logging.info("or-tools version : " + ortools.__version__ + "\n" + inspect.getfile(ortools)) except (AttributeError, Exception): - logging.error (wrong_version(ortools, ortools_name, current_ortools_version, ortools.__version__)) - raise SystemExit + log_error_and_exit(wrong_version(ortools, ortools_name, required_ortools_version, ortools.__version__)) #check protobuf version try: - if parse_version(minimum_protobuf_version) > parse_version(google.protobuf.__version__): + if required_protobuf_version != google.protobuf.__version__: raise Exception - logging.info("protobuf version : " + google.protobuf.__version__) - logging.info(inspect.getfile(google.protobuf) + "\n") + logging.info("protobuf version : " + google.protobuf.__version__+ "\n" + inspect.getfile(google.protobuf) ) except (AttributeError, Exception): - logging.error(wrong_version(google.protobuf, "protobuf", minimum_protobuf_version, google.protobuf.__version__)) - raise SystemExit + log_error_and_exit(wrong_version(google.protobuf, "protobuf", required_protobuf_version, google.protobuf.__version__)) # Check if python can load the libraries' modules # this is useful when the library architecture is not compatbile with the python executable, @@ -95,6 +92,4 @@ if __name__ == '__main__': from ortools.constraint_solver import _pywrapcp from ortools.linear_solver import _pywraplp from ortools.algorithms import _pywrapknapsack_solver - from ortools.graph import _pywrapgraph - - + from ortools.graph import _pywrapgraph \ No newline at end of file diff --git a/tools/setup.py b/tools/setup.py index 08321f10ca..62e04fa3a2 100644 --- a/tools/setup.py +++ b/tools/setup.py @@ -36,7 +36,7 @@ setup( 'ortools.linear_solver',], ext_modules = [dummy_module], install_requires = [ - 'protobuf >= PROTOBUF_TAG'], + 'protobuf == PROTOBUF_TAG'], package_data = { 'ortools.constraint_solver' : ['_pywrapcp.dll'], 'ortools.linear_solver' : ['_pywraplp.dll'], diff --git a/tools/setup_data.py b/tools/setup_data.py index 91505f5b5e..ad0f58cca9 100644 --- a/tools/setup_data.py +++ b/tools/setup_data.py @@ -23,9 +23,9 @@ def read(fname): return open(pjoin(dirname(__file__), fname)).read() if version_info[0] >= 3: - install_requires = ["py3-ortools >= VVVV"] + install_requires = ["py3-ortools == VVVV"] else: - install_requires = ["ortools >= VVVV"] + install_requires = ["ortools == VVVV"] setup( name='ortools_examples',