From 79582e797da7afdf3df5d0e908ff4209c021fac6 Mon Sep 17 00:00:00 2001 From: Sam Nelson Date: Wed, 17 May 2017 12:47:59 -0700 Subject: [PATCH] runlingo_setup.py --- runlingo_setup.py | 8 ++++++++ ticdat/lingo.py | 17 +++++++++-------- ticdat/testing/ticdattestutils.py | 14 ++++++++++++++ 3 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 runlingo_setup.py diff --git a/runlingo_setup.py b/runlingo_setup.py new file mode 100644 index 0000000..f73f462 --- /dev/null +++ b/runlingo_setup.py @@ -0,0 +1,8 @@ +print "This script configures a lingorun_path.txt file in order to enable use with runlingo." +print "You need to run this script from the directory containing runlingo " +print "(or runlingo.exe on windows)" +print "This is a one-time post 'pip install' setup step." + +import ticdat.testing.ticdattestutils as tdu +tdu.configure_runlingo_path() + diff --git a/ticdat/lingo.py b/ticdat/lingo.py index 234b734..f10c843 100644 --- a/ticdat/lingo.py +++ b/ticdat/lingo.py @@ -11,7 +11,7 @@ def _code_dir(): return os.path.dirname(os.path.abspath(inspect.getsourcefile(_code_dir))) -def lingo_run(lng_file, input_tdf, input_dat, soln_tdf, infinity=INFINITY, lingorun_path=None): +def lingo_run(lng_file, input_tdf, input_dat, soln_tdf, infinity=INFINITY, runlingo_path=None): """ solve an optimization problem using an Lingo .lng file :param lng_file: An Lingo .lng file. @@ -19,6 +19,7 @@ def lingo_run(lng_file, input_tdf, input_dat, soln_tdf, infinity=INFINITY, lingo :param input_dat: A TicDat object consistent with input_tdf :param soln_tdf: A TicDatFactory defining the solution variables :param infinity: A number used to represent infinity in Lingo + :param runlingo_path: A path to the Lingo executable :return: a TicDat object consistent with soln_tdf, or None if no solution found """ verify(os.path.isfile(lng_file), "lng_file %s is not a valid file."%lng_file) @@ -83,18 +84,18 @@ def lingo_run(lng_file, input_tdf, input_dat, soln_tdf, infinity=INFINITY, lingo ] with open(commandsfile, "w") as f: f.write("\n".join(commands)) - if not lingorun_path: + if not runlingo_path: if 'TICDAT_LINGO_PATH' in os.environ: - lingorun_path = os.environ['TICDAT_LINGO_PATH'] + runlingo_path = os.environ['TICDAT_LINGO_PATH'] else: verify(os.path.isfile(os.path.join(_code_dir(),"lingo_run_path.txt")), - "need to either pass lingorun_path argument or run lingo_run_setup.py") - with open(os.path.join(_code_dir(),"lingorun_path.txt"),"r") as f: - lingorun_path = f.read().strip() - verify(os.path.isfile(lingorun_path), "%s not a valid path to lingorun"%lingorun_path) + "need to either pass runlingo_path argument or run lingo_run_setup.py") + with open(os.path.join(_code_dir(),"runlingo_path.txt"),"r") as f: + runlingo_path = f.read().strip() + verify(os.path.isfile(runlingo_path), "%s not a valid path to runlingo"%runlingo_path) output = '' try: - output = subprocess.check_output([lingorun_path, commandsfile], stderr=subprocess.STDOUT, cwd=working_dir) + output = subprocess.check_output([runlingo_path, commandsfile], stderr=subprocess.STDOUT, cwd=working_dir) except subprocess.CalledProcessError as err: if tu.development_deployed_environment: raise Exception("runlingo failed to complete: " + str(err.output)) diff --git a/ticdat/testing/ticdattestutils.py b/ticdat/testing/ticdattestutils.py index 8537415..c13340c 100644 --- a/ticdat/testing/ticdattestutils.py +++ b/ticdat/testing/ticdattestutils.py @@ -46,6 +46,20 @@ def configure_oplrun_path(): with open(os.path.join(opl_dir, "oplrun_path.txt"), "w") as f: f.write(oplrun_path) +def configure_runlingo_path(): + if sys.platform in ['win32']: + runlingo_name = os.path.abspath('runlingo.exe') + else: + runlingo_name = os.path.abspath('runlingo') + verify(os.path.isfile(runlingo_name), "You need to be in the directory containing runlingo") + lingo_dir = os.path.abspath(os.path.join(_codeDir(), "..")) + v_str = "Contact ticdat support at ticdat@opalytics.com" + verify(os.path.isdir(lingo_dir), "%s is strangely not a directory. %s"%(lingo_dir, v_str)) + verify(os.path.isfile(os.path.join(lingo_dir,"lingo.py")), "opl.py is missing. %s"%v_str) + runlingo_path = os.path.abspath(runlingo_name) + with open(os.path.join(lingo_dir, "runlingo_path.txt"), "w") as f: + f.write(runlingo_path) + _debug = [] def _asserting() : _debug.append(())