2016-11-10 16:42:19 +01:00
import logging , sys , inspect
from optparse import OptionParser
2016-08-04 21:07:14 +02:00
#try to import setuptools
try :
from setuptools import setup , Extension
from setuptools . command import easy_install
except ImportError :
raise ImportError ( """ setuptools is not installed for \" """ + sys . executable + """ \"
2017-01-02 20:53:18 +01:00
Please follow this link for installing instructions :
2016-08-04 21:07:14 +02:00
https : / / pypi . python . org / pypi / setuptools
make sure you use \""" " + sys.executable + """ \" during the installation " " " )
raise SystemExit
2016-09-21 13:37:20 +02:00
from pkg_resources import parse_version
2017-01-02 20:53:18 +01:00
required_ortools_version = " 5.0.3919 "
required_protobuf_version = " 3.0.0 "
2016-09-22 14:55:50 +02:00
def notinstalled ( modulename ) :
return modulename + """ is not installed for \" """ + sys . executable + """ \"
2017-01-02 20:53:18 +01:00
Please run \""" " + str(sys.executable) + """ setup . py install - - user \""" "
2016-12-13 15:22:40 +01:00
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 + """
2017-01-02 20:53:18 +01:00
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 + """ . Remove it manually or by using pip. """
2016-09-22 14:55:50 +02:00
2016-12-13 15:22:40 +01:00
def log_error_and_exit ( error_message ) :
logging . error ( error_message )
raise SystemExit
2016-09-22 14:55:50 +02:00
if __name__ == ' __main__ ' :
2016-11-10 16:42:19 +01:00
parser = OptionParser ( ' Log level ' )
2016-09-22 14:55:50 +02:00
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 ( )
2016-12-13 15:22:40 +01:00
#Create the logger
2016-09-22 14:55:50 +02:00
try :
loglevel = getattr ( logging , options . log . upper ( ) )
except AttributeError :
loglevel = { 3 : logging . CRITICAL ,
2 : logging . ERROR ,
1 : logging . WARNING ,
0 : logging . INFO ,
- 1 : logging . DEBUG ,
} [ int ( options . log ) ]
2016-12-13 15:22:40 +01:00
logging . basicConfig ( format = ' [ %(levelname)s ] %(message)s ' , stream = sys . stdout , level = loglevel )
#Display Python Version and path
2016-09-22 14:55:50 +02:00
logging . info ( " Python path : " + sys . executable )
2016-12-13 15:22:40 +01:00
logging . info ( " Python version : " + sys . version )
2016-09-22 14:55:50 +02:00
2016-12-13 15:22:40 +01:00
#Choose the pypi package
if sys . version_info [ 0 ] > = 3 :
2016-09-22 14:55:50 +02:00
ortools_name = " py3-ortools "
else :
ortools_name = " ortools "
2016-12-13 15:22:40 +01:00
2016-09-22 14:55:50 +02:00
#try to import ortools
try :
import ortools
except ImportError :
2016-12-13 15:22:40 +01:00
log_error_and_exit ( notinstalled ( ortools_name ) )
2016-09-22 14:55:50 +02:00
#try to import protobuf
try :
import google . protobuf
except ImportError :
2016-12-13 15:22:40 +01:00
log_error_and_exit ( notinstalled ( " protobuf " ) )
2016-09-22 14:55:50 +02:00
#check ortools version
try :
2016-12-13 15:22:40 +01:00
if required_ortools_version != ortools . __version__ :
2016-09-22 14:55:50 +02:00
raise Exception
2016-12-13 15:22:40 +01:00
logging . info ( " or-tools version : " + ortools . __version__ + " \n " + inspect . getfile ( ortools ) )
2016-09-22 14:55:50 +02:00
except ( AttributeError , Exception ) :
2016-12-13 15:22:40 +01:00
log_error_and_exit ( wrong_version ( ortools , ortools_name , required_ortools_version , ortools . __version__ ) )
2016-09-22 14:55:50 +02:00
#check protobuf version
try :
2016-12-13 15:22:40 +01:00
if required_protobuf_version != google . protobuf . __version__ :
2016-09-22 14:55:50 +02:00
raise Exception
2016-12-13 15:22:40 +01:00
logging . info ( " protobuf version : " + google . protobuf . __version__ + " \n " + inspect . getfile ( google . protobuf ) )
2016-09-22 14:55:50 +02:00
except ( AttributeError , Exception ) :
2016-12-13 15:22:40 +01:00
log_error_and_exit ( wrong_version ( google . protobuf , " protobuf " , required_protobuf_version , google . protobuf . __version__ ) )
2016-08-04 21:07:14 +02:00
2016-12-09 19:07:14 +01:00
# Check if python can load the libraries' modules
# this is useful when the library architecture is not compatbile with the python executable,
# or when the library's dependencies are not available or not compatible.
from ortools . constraint_solver import _pywrapcp
from ortools . linear_solver import _pywraplp
from ortools . algorithms import _pywrapknapsack_solver
2016-12-13 15:22:40 +01:00
from ortools . graph import _pywrapgraph