From a24bee9b396e48fc94a19d6cf21944185daf1b08 Mon Sep 17 00:00:00 2001
From: Nicola Vigano <nicola.vigano@esrf.fr>
Date: Thu, 7 Feb 2013 15:27:16 +0100
Subject: [PATCH] Python/matlab invocation: shared code for calling matlab

Signed-off-by: Nicola Vigano <nicola.vigano@esrf.fr>
---
 dct_launch.py                                | 44 +----------
 zUtil_Python/__init__.py                     |  2 +
 zUtil_Python/dct_compile_matlab_functions.py | 45 ++++++------
 zUtil_Python/dct_distrib.py                  |  1 +
 zUtil_Python/dct_io_xml.py                   |  7 ++
 zUtil_Python/dct_matlab_invocation.py        | 77 ++++++++++++++++++++
 6 files changed, 111 insertions(+), 65 deletions(-)
 create mode 100755 zUtil_Python/dct_matlab_invocation.py

diff --git a/dct_launch.py b/dct_launch.py
index a48ebe2b..6b035192 100755
--- a/dct_launch.py
+++ b/dct_launch.py
@@ -66,47 +66,9 @@ class DCTLauncher(object):
             print("   # python %s matlab [options]" % os.path.basename(__file__))
             print(" Where [options] are the options for matlab")
         else:
-            self.conf = dct.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")]
-            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
+            invoker = dct.dct_matlab_invocation.DCTMatlabInvocation(dct_dir = self.dct_dir, \
+                                                                    extra_args = self.args)
+            invoker.invoke()
 
     def _launchUpdate(self):
         util = dct.dct_utils_git.DCTGit(self.dct_dir)
diff --git a/zUtil_Python/__init__.py b/zUtil_Python/__init__.py
index 884f9d5b..eea837b1 100755
--- a/zUtil_Python/__init__.py
+++ b/zUtil_Python/__init__.py
@@ -4,6 +4,8 @@ Created on Feb 3, 2013
 @author: vigano
 '''
 
+import dct_matlab_invocation
+
 import dct_io_xml
 
 import dct_utils_git
diff --git a/zUtil_Python/dct_compile_matlab_functions.py b/zUtil_Python/dct_compile_matlab_functions.py
index e4d8ec77..c3adf358 100755
--- a/zUtil_Python/dct_compile_matlab_functions.py
+++ b/zUtil_Python/dct_compile_matlab_functions.py
@@ -9,6 +9,8 @@ import string
 
 from dct_io_xml import DCTConf, DCTOutput
 
+import dct_matlab_invocation
+
 class FunctionsBuilder(object):
 
     submitMethods = [ 'OAR_make',
@@ -31,7 +33,6 @@ class FunctionsBuilder(object):
         will_compile = True
         will_generate = True
         force_compile = False
-        ignore_id19 = conf.getIgnoreID19()
 
         mfiles_to_consider = [ ]
         for index in range(len(sys.argv)):
@@ -67,13 +68,12 @@ class FunctionsBuilder(object):
                                 force_compile = force_compile, \
                                 mfiles_to_consider = mfiles_to_consider, \
                                 do_compile = will_compile, \
-                                do_generate = will_generate, \
-                                ignore_id19 = ignore_id19)
+                                do_generate = will_generate)
 
     def __init__(self, matlab_path, dct_dir = os.getcwd(), \
                  script_dir = None, bin_dir = None, force_compile = False, \
                  mfiles_to_consider = [], do_compile = True, \
-                 do_generate = True, ignore_id19 = "true"):
+                 do_generate = True):
         self.dct_dir = dct_dir
         if script_dir is None:
             script_dir = os.path.join(self.dct_dir, 'bin', 'scripts')
@@ -83,7 +83,6 @@ class FunctionsBuilder(object):
         self.script_dir = script_dir
         self.bin_dir = bin_dir
 
-        self.ignore_id19
         self.matlab_dir = matlab_path
         self.force_compile = force_compile
         self.mfiles_to_consider = mfiles_to_consider
@@ -178,8 +177,8 @@ class FunctionsBuilder(object):
             script_content.append("    'in_mfile', '%s' );\n" % in_mfile)
         script_content.append("\ngtCompileFunctions(force, mfiles, varargin)\nend\n")
 
-        DCTOutput.printSubJob("Writing to disk..")
         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.writelines(script_content)
         fid.close()
@@ -189,26 +188,24 @@ class FunctionsBuilder(object):
             DCTOutput.printSubJob('Creating binaries directory')
             os.makedirs(self.bin_dir)
         DCTOutput.printSubJob("Calling matlab..")
-        matlab_path = os.path.join(self.matlab_dir, "bin", "matlab") 
-        cmd = [matlab_path, '-nodesktop', '-nosplash', '-r']
-        args = ''
+
+        extra_args = ['-nodesktop', '-nosplash']
+
         if self.force_compile is True:
-            args = "'force'"
+            compile_args = "'force"
         else:
-            args = "'no-force'"
-        if len(self.mfiles_to_consider) is not 0:
-            args = args + ",'" + string.join(self.mfiles_to_consider,"','") + "'"
-
-        cmd.append("cd('%s');initialise_gt(%s);cd('%s');compile(%s);quit;" \
-                   % (self.dct_dir, self.ignore_id19, self.script_dir, args))
-        DCTOutput.printSubSubJob("Command to execute", " ".join(cmd))
-        subobj = subprocess.Popen(cmd)
-        while subobj.poll() is None:
-            try:
-                subobj.wait()
-            except KeyboardInterrupt:
-                print("Sending kill signal to matlab...")
-                subobj.kill()
+            compile_args = "'no-force"
+        compile_args = "','".join([compile_args] + self.mfiles_to_consider) + "'"
+
+        cmd = "compile(%s);quit;" % compile_args
+
+        invoker = dct_matlab_invocation.DCTMatlabInvocation(dct_dir = self.dct_dir, \
+                                                            work_dir = self.script_dir, \
+                                                            extra_args = extra_args, \
+                                                            extra_cmds = cmd, \
+                                                            ctrl_c_kill = True)
+        invoker.invoke()
+
         print("")
 
     def generateMfile(self):
diff --git a/zUtil_Python/dct_distrib.py b/zUtil_Python/dct_distrib.py
index f1e1c70a..61576b47 100755
--- a/zUtil_Python/dct_distrib.py
+++ b/zUtil_Python/dct_distrib.py
@@ -14,6 +14,7 @@ class DCTMakeInstallBundle(object):
                             'dct_io_xml.py', \
                             'dct_utils_git.py', \
                             'dct_utils_platform.py', \
+                            'dct_matlab_invocation.py', \
                             'dct_setup.py')
 
     def __init__(self, *args, **kwargs):
diff --git a/zUtil_Python/dct_io_xml.py b/zUtil_Python/dct_io_xml.py
index b84afb8f..6a905524 100755
--- a/zUtil_Python/dct_io_xml.py
+++ b/zUtil_Python/dct_io_xml.py
@@ -87,6 +87,13 @@ class DCTConf(DCTXMLBase):
     def getMatlabPath(self):
         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):
         element = self.tree.find("dct/ignore_id19")
         return element.get('value')
diff --git a/zUtil_Python/dct_matlab_invocation.py b/zUtil_Python/dct_matlab_invocation.py
new file mode 100755
index 00000000..00faace9
--- /dev/null
+++ b/zUtil_Python/dct_matlab_invocation.py
@@ -0,0 +1,77 @@
+'''
+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
+
-- 
GitLab