Skip to content

Commit

Permalink
runlingo_setup.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Nelson committed May 17, 2017
1 parent be015cd commit 79582e7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
8 changes: 8 additions & 0 deletions runlingo_setup.py
Original file line number Diff line number Diff line change
@@ -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()

17 changes: 9 additions & 8 deletions ticdat/lingo.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
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.
:param input_tdf: A TicDatFactory defining the input schema
: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)
Expand Down Expand Up @@ -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))
Expand Down
14 changes: 14 additions & 0 deletions ticdat/testing/ticdattestutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected]"
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(())
Expand Down

0 comments on commit 79582e7

Please sign in to comment.