Skip to content
Snippets Groups Projects
Commit a24bee9b authored by Nicola Vigano's avatar Nicola Vigano
Browse files

Python/matlab invocation: shared code for calling matlab

parent e84318c1
No related branches found
No related tags found
No related merge requests found
...@@ -66,47 +66,9 @@ class DCTLauncher(object): ...@@ -66,47 +66,9 @@ class DCTLauncher(object):
print(" # python %s matlab [options]" % os.path.basename(__file__)) print(" # python %s matlab [options]" % os.path.basename(__file__))
print(" Where [options] are the options for matlab") print(" Where [options] are the options for matlab")
else: else:
self.conf = dct.dct_io_xml.DCTConf(self.confPath) invoker = dct.dct_matlab_invocation.DCTMatlabInvocation(dct_dir = self.dct_dir, \
extra_args = self.args)
# Adding runtime libraries invoker.invoke()
try:
env_initial_libs = os.environ["LD_LIBRARY_PATH"]
except KeyError:
env_initial_libs = ""
os.environ["LD_LIBRARY_PATH"] = ""
libs = self.conf.getMatlabRuntimeLibraries()
for lib in libs:
env_var = os.environ["LD_LIBRARY_PATH"]
os.environ["LD_LIBRARY_PATH"] = ":".join([env_var, lib])
# Adding preload libraries
try:
env_initial_preload = os.environ["LD_PRELOAD"]
except KeyError:
env_initial_preload = ""
os.environ["LD_PRELOAD"] = ""
libs = self.conf.getMatlabPreloadLibraries()
for lib in libs:
env_var = os.environ["LD_PRELOAD"]
os.environ["LD_PRELOAD"] = ":".join([env_var, lib])
cmd = [os.path.join(self.conf.getMatlabPath(), "bin", "matlab")]
for arg in self.args:
cmd.append(arg)
cmd.append("-r")
ignore_id19 = self.conf.getIgnoreID19()
cmd.append("cd('%s');initialise_gt(%s);cd('%s');" \
% (self.dct_dir, ignore_id19, os.getcwd()) )
cmd_str = " ".join(cmd)
print("Calling: %s" % cmd_str)
subobj = subprocess.Popen(cmd)
while subobj.poll() is None:
try:
subobj.wait()
except KeyboardInterrupt:
pass
os.environ["LD_LIBRARY_PATH"] = env_initial_libs
os.environ["LD_PRELOAD"] = env_initial_preload
def _launchUpdate(self): def _launchUpdate(self):
util = dct.dct_utils_git.DCTGit(self.dct_dir) util = dct.dct_utils_git.DCTGit(self.dct_dir)
......
...@@ -4,6 +4,8 @@ Created on Feb 3, 2013 ...@@ -4,6 +4,8 @@ Created on Feb 3, 2013
@author: vigano @author: vigano
''' '''
import dct_matlab_invocation
import dct_io_xml import dct_io_xml
import dct_utils_git import dct_utils_git
......
...@@ -9,6 +9,8 @@ import string ...@@ -9,6 +9,8 @@ import string
from dct_io_xml import DCTConf, DCTOutput from dct_io_xml import DCTConf, DCTOutput
import dct_matlab_invocation
class FunctionsBuilder(object): class FunctionsBuilder(object):
submitMethods = [ 'OAR_make', submitMethods = [ 'OAR_make',
...@@ -31,7 +33,6 @@ class FunctionsBuilder(object): ...@@ -31,7 +33,6 @@ class FunctionsBuilder(object):
will_compile = True will_compile = True
will_generate = True will_generate = True
force_compile = False force_compile = False
ignore_id19 = conf.getIgnoreID19()
mfiles_to_consider = [ ] mfiles_to_consider = [ ]
for index in range(len(sys.argv)): for index in range(len(sys.argv)):
...@@ -67,13 +68,12 @@ class FunctionsBuilder(object): ...@@ -67,13 +68,12 @@ class FunctionsBuilder(object):
force_compile = force_compile, \ force_compile = force_compile, \
mfiles_to_consider = mfiles_to_consider, \ mfiles_to_consider = mfiles_to_consider, \
do_compile = will_compile, \ do_compile = will_compile, \
do_generate = will_generate, \ do_generate = will_generate)
ignore_id19 = ignore_id19)
def __init__(self, matlab_path, dct_dir = os.getcwd(), \ def __init__(self, matlab_path, dct_dir = os.getcwd(), \
script_dir = None, bin_dir = None, force_compile = False, \ script_dir = None, bin_dir = None, force_compile = False, \
mfiles_to_consider = [], do_compile = True, \ mfiles_to_consider = [], do_compile = True, \
do_generate = True, ignore_id19 = "true"): do_generate = True):
self.dct_dir = dct_dir self.dct_dir = dct_dir
if script_dir is None: if script_dir is None:
script_dir = os.path.join(self.dct_dir, 'bin', 'scripts') script_dir = os.path.join(self.dct_dir, 'bin', 'scripts')
...@@ -83,7 +83,6 @@ class FunctionsBuilder(object): ...@@ -83,7 +83,6 @@ class FunctionsBuilder(object):
self.script_dir = script_dir self.script_dir = script_dir
self.bin_dir = bin_dir self.bin_dir = bin_dir
self.ignore_id19
self.matlab_dir = matlab_path self.matlab_dir = matlab_path
self.force_compile = force_compile self.force_compile = force_compile
self.mfiles_to_consider = mfiles_to_consider self.mfiles_to_consider = mfiles_to_consider
...@@ -178,8 +177,8 @@ class FunctionsBuilder(object): ...@@ -178,8 +177,8 @@ class FunctionsBuilder(object):
script_content.append(" 'in_mfile', '%s' );\n" % in_mfile) script_content.append(" 'in_mfile', '%s' );\n" % in_mfile)
script_content.append("\ngtCompileFunctions(force, mfiles, varargin)\nend\n") script_content.append("\ngtCompileFunctions(force, mfiles, varargin)\nend\n")
DCTOutput.printSubJob("Writing to disk..")
script_loc = os.path.join(self.script_dir, 'compile.m') script_loc = os.path.join(self.script_dir, 'compile.m')
DCTOutput.printSubJob("Writing '%s' script to disk.." % script_loc)
fid = open(script_loc, "w") fid = open(script_loc, "w")
fid.writelines(script_content) fid.writelines(script_content)
fid.close() fid.close()
...@@ -189,26 +188,24 @@ class FunctionsBuilder(object): ...@@ -189,26 +188,24 @@ class FunctionsBuilder(object):
DCTOutput.printSubJob('Creating binaries directory') DCTOutput.printSubJob('Creating binaries directory')
os.makedirs(self.bin_dir) os.makedirs(self.bin_dir)
DCTOutput.printSubJob("Calling matlab..") DCTOutput.printSubJob("Calling matlab..")
matlab_path = os.path.join(self.matlab_dir, "bin", "matlab")
cmd = [matlab_path, '-nodesktop', '-nosplash', '-r'] extra_args = ['-nodesktop', '-nosplash']
args = ''
if self.force_compile is True: if self.force_compile is True:
args = "'force'" compile_args = "'force"
else: else:
args = "'no-force'" compile_args = "'no-force"
if len(self.mfiles_to_consider) is not 0: compile_args = "','".join([compile_args] + self.mfiles_to_consider) + "'"
args = args + ",'" + string.join(self.mfiles_to_consider,"','") + "'"
cmd = "compile(%s);quit;" % compile_args
cmd.append("cd('%s');initialise_gt(%s);cd('%s');compile(%s);quit;" \
% (self.dct_dir, self.ignore_id19, self.script_dir, args)) invoker = dct_matlab_invocation.DCTMatlabInvocation(dct_dir = self.dct_dir, \
DCTOutput.printSubSubJob("Command to execute", " ".join(cmd)) work_dir = self.script_dir, \
subobj = subprocess.Popen(cmd) extra_args = extra_args, \
while subobj.poll() is None: extra_cmds = cmd, \
try: ctrl_c_kill = True)
subobj.wait() invoker.invoke()
except KeyboardInterrupt:
print("Sending kill signal to matlab...")
subobj.kill()
print("") print("")
def generateMfile(self): def generateMfile(self):
......
...@@ -14,6 +14,7 @@ class DCTMakeInstallBundle(object): ...@@ -14,6 +14,7 @@ class DCTMakeInstallBundle(object):
'dct_io_xml.py', \ 'dct_io_xml.py', \
'dct_utils_git.py', \ 'dct_utils_git.py', \
'dct_utils_platform.py', \ 'dct_utils_platform.py', \
'dct_matlab_invocation.py', \
'dct_setup.py') 'dct_setup.py')
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
......
...@@ -87,6 +87,13 @@ class DCTConf(DCTXMLBase): ...@@ -87,6 +87,13 @@ class DCTConf(DCTXMLBase):
def getMatlabPath(self): def getMatlabPath(self):
return self.tree.findtext("matlab/path", os.path.join("/sware", "com", "matlab_2012a")) return self.tree.findtext("matlab/path", os.path.join("/sware", "com", "matlab_2012a"))
def getMatlabOptions(self):
elems = self.tree.findall("matlab/options/opt", [])
output = []
for elem in elems:
output.append(elem.text)
return output
def getIgnoreID19(self): def getIgnoreID19(self):
element = self.tree.find("dct/ignore_id19") element = self.tree.find("dct/ignore_id19")
return element.get('value') return element.get('value')
......
'''
Created on Feb 7, 2013
@author: ben
'''
import os
import subprocess
import dct_io_xml
class DCTMatlabInvocation(object):
def __init__(self, dct_dir, work_dir = os.getcwd(), extra_args = [], \
extra_cmds = "", ctrl_c_kill = False):
self.dct_dir = dct_dir
self.work_dir = work_dir
self.confPath = os.path.join(self.dct_dir, "conf.xml")
self.confPath = os.path.abspath(self.confPath)
self.args = extra_args
self.extra_cmds = extra_cmds
self.ctrl_c_kill = ctrl_c_kill
def invoke(self):
self.conf = dct_io_xml.DCTConf(self.confPath)
# Adding runtime libraries
try:
env_initial_libs = os.environ["LD_LIBRARY_PATH"]
except KeyError:
env_initial_libs = ""
os.environ["LD_LIBRARY_PATH"] = ""
libs = self.conf.getMatlabRuntimeLibraries()
for lib in libs:
env_var = os.environ["LD_LIBRARY_PATH"]
os.environ["LD_LIBRARY_PATH"] = ":".join([env_var, lib])
# Adding preload libraries
try:
env_initial_preload = os.environ["LD_PRELOAD"]
except KeyError:
env_initial_preload = ""
os.environ["LD_PRELOAD"] = ""
libs = self.conf.getMatlabPreloadLibraries()
for lib in libs:
env_var = os.environ["LD_PRELOAD"]
os.environ["LD_PRELOAD"] = ":".join([env_var, lib])
cmd = [os.path.join(self.conf.getMatlabPath(), "bin", "matlab")]
cmd = cmd + self.conf.getMatlabOptions()
cmd = cmd + self.args
cmd.append("-r")
ignore_id19 = self.conf.getIgnoreID19()
cmd.append("cd('%s');initialise_gt(%s);cd('%s');%s" \
% (self.dct_dir, ignore_id19, self.work_dir, \
"".join(self.extra_cmds)) )
print("Calling: %s" % " ".join(cmd))
subobj = subprocess.Popen(cmd)
while subobj.poll() is None:
try:
subobj.wait()
except KeyboardInterrupt:
if self.ctrl_c_kill is True:
print("Sending kill signal to matlab...")
subobj.kill()
os.environ["LD_LIBRARY_PATH"] = env_initial_libs
os.environ["LD_PRELOAD"] = env_initial_preload
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment