Newer
Older
#!/usr/bin/env python
'''
Created on Feb 3, 2013
@author: vigano
'''
import sys
import os
import subprocess
import zUtil_Python as dct
class DCTLauncher(object):
def __init__(self, args):
self.initialized = False
self.dct_dir = os.path.abspath(os.path.dirname(args[0]))
self.confPath = os.path.join(self.dct_dir, "conf.xml")
self.confPath = os.path.abspath(self.confPath)
try:
if len(args) is 1:
print("Defaulting to matlab")
args.append("matlab")
cmd = args[1]
if cmd is "-h" or cmd is "--help":
self.command = "help"
elif cmd in ("matlab", "update", "compile_mex", "compile_matlab", \
"update_conf", "make"):
self.command = cmd
else:
raise ValueError("Command '%s' not recognized" % cmd)
self.args = args[2:]
self.initialized = True
except ValueError as ex:
dct.dct_io_xml.DCTOutput.printError(ex.args)
def run(self):
if self.command == "help":
self.printHelp()
elif self.command == "matlab":
self._launchMatlab()
elif self.command == "update":
self._launchUpdate()
elif self.command == "compile_mex":
self._launchCompileMex()
elif self.command == "compile_matlab":
self._launchCompileMatlab()
elif self.command == "update_conf":
self._launchUpdateConf()
elif self.command == "make":
self._launchMake()
else:
raise ValueError("Not recognized command: %s" % self.command)
def _launchMatlab(self):
if os.path.exists(self.confPath) is False:
raise SystemError("File conf.xml not found in DCT directory")
if (len(self.args) is not 0) and self.args[0] in ("-h", "--help"):
print(" Usage:")
print(" # python %s matlab [options]" % __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 = os.environ["LD_LIBRARY_PATH"]
except KeyError:
env_initial = ""
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
libs = self.conf.getRuntimeLibraries()
for lib in libs:
env_var = os.environ["LD_LIBRARY_PATH"]
os.environ["LD_LIBRARY_PATH"] = ":".join([env_var, lib])
cmd = [self.conf.getMatlabCommand()]
for arg in self.args:
cmd.append(arg)
cmd.append("-r")
cmd.append("cd('%s');initialise_gt();cd('%s');" % (self.dct_dir, 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
def _launchUpdate(self):
util = dct.dct_utils_git.DCTGit(self.dct_dir)
util.updateRepo()
def _launchCompileMex(self):
from zUtil_Python.dct_compile_mex_functions import MexBuilder
try:
args_compile = [""]
for arg in self.args:
args_compile.append(arg)
mex_builder = MexBuilder.getInstanceFromArgs(args_compile, dct_dir = self.dct_dir)
mex_builder.findMexs()
mex_builder.compileFuncs()
except ValueError as ex:
if (len(ex.args) is not 0) and (ex.args[0] is not ""):
dct.dct_io_xml.DCTOutput.printError(ex.args)
MexBuilder("").printHelp();
except BaseException as ex:
dct.dct_io_xml.DCTOutput.printError(ex.args)
def _launchCompileMatlab(self):
from zUtil_Python.dct_compile_matlab_functions import FunctionsBuilder
try:
func_builder = FunctionsBuilder.getInstanceFromArgs(sys.argv, dct_dir = self.dct_dir)
if func_builder.do_generate is True:
func_builder.generateMfile()
if func_builder.do_compile is True:
func_builder.compileFuncs()
except ValueError as ex:
dct.dct_io_xml.DCTOutput.printError(ex.args)
def _launchUpdateConf(self):
upd = dct.dct_io_xml.DCTConfUpdater(self.dct_dir)
confexamplefile = os.path.join(self.dct_dir, "zUtil_Conf", "conf.example.xml")
upd.safelyInstallNewFile(confexamplefile, "conf.xml")
def _launchMake(self):
if len(self.args) is 0:
raise ValueError("Not enough arguments")
if self.args[0] == "install_zip":
builder = dct.dct_distrib.DCTMakeInstallBundle()
builder.run(self.dct_dir)
else:
raise ValueError("Unsupported make option: '%s'" % " ".join(self.args))
def printHelp(self):
print("\"%s\" launches dct or one of the maintenance routines in the DCT directory: \"%s\"" %
(__file__, self.dct_dir) )
print(" Usage:")
print(" # python %s command [options]" % __file__)
print(" # python %s command -h | --help : to show command's help" % __file__)
print(" # python %s -h | --help : to show this help" % __file__)
print(" Commands:")
print(" matlab\n update\n compile_mex\n compile_matlab\n update_conf")
if __name__ == '__main__':
launcher = DCTLauncher(sys.argv)
if launcher.initialized is False:
launcher.printHelp()
sys.exit(1)
try:
launcher.run()
except SystemError as ex:
dct.dct_io_xml.DCTOutput.printError(ex.args)
print("Try running:\n # python %s update_conf" % __file__)
except ValueError as ex:
dct.dct_io_xml.DCTOutput.printError(ex.args)