Newer
Older
'''
Created on Dec 27, 2011
@author: vigano
'''
Nicola Vigano
committed
import re
import datetime
import shutil
import fnmatch
try:
import lxml.etree as ET
except ImportError as ex:
print("Couldn't import lxml. Trying with standard distribution.")
try:
import xml.etree.ElementTree as ET
except ImportError as ex1:
print(ex1)
raise ex1
class DCTOutput(object):
Nicola Vigano
committed
def __init__(self, verboseLevel = 1):
if verboseLevel is True:
verboseLevel = 1;
self.verboseLevel = verboseLevel;
@staticmethod
def printError(args):
print("\n\033[31;1mError: %s\033[39;0m\n" % "".join([str(x) for x in args]))
@staticmethod
def printWarning(args):
print("\n\033[33;1mWarning: %s\033[39;0m\n" % "".join(args))
Nicola Vigano
committed
def printInfoRecord(self, title, args):
if self.verboseLevel >= 1:
print(" %s: \033[36;1m%s\033[39;0m" % (title, "".join(args)))
Nicola Vigano
committed
def printJob(self, args):
if self.verboseLevel >= 1:
print("\033[39;1m%s\033[39;0m" % "".join(args))
Nicola Vigano
committed
def printSubJob(self, args):
if self.verboseLevel >= 1:
print(" * \033[32;1m%s\033[39;0m" % "".join(args))
Nicola Vigano
committed
def printSubSubJob(self, title, args):
if self.verboseLevel >= 1:
print(" - \033[32m%s\033[39;0m: %s" % (title, "".join(args)))
def printSubSubSubJob(self, title, args):
if self.verboseLevel >= 1:
print(" + %s: %s" % (title, "".join(args)))
class DCTXMLBase(object):
def __init__(self, xmlFile):
self.xmlFile = xmlFile
Nicola Vigano
committed
self.tree = ET.parse(xmlFile)
def save(self, newName = None):
if newName is not None:
self.xmlFile = newName
Nicola Vigano
committed
self.tree.write(self.xmlFile, pretty_print = True)
@staticmethod
def checkCwdWithDCTDir(dctdir = os.getenv('DCT_DIR')):
print("Checking if current directory matches with DCT_DIR environment variable...")
indir = os.getcwd()
try:
os.chdir(dctdir)
testdir = os.getcwd()
except:
raise EnvironmentError("DCT_DIR environment variable (%s) points to inexistent directory!" % dctdir)
os.chdir(indir)
if indir == testdir:
else:
raise EnvironmentError("Current working directory does not match with DCT_DIR environment variable!\n%s != %s" % (indir, dctdir))
class DCTConf(DCTXMLBase):
def __init__(self, conf_xml = "conf.xml"):
DCTXMLBase.__init__(self, conf_xml)
def getVersion(self):
return self.tree.findtext("version", "0")
def getMatlabVersion(self):
return self.tree.findtext("matlab/version", "2013a")
def getMatlabPath(self):
return self.tree.findtext("matlab/path", os.path.join("/sware", "com", "matlab_2013a"))
def getMatlabOptions(self):
elems = self.tree.findall("matlab/options/opt")
output = []
for elem in elems:
output.append(elem.text)
return output
Nicola Vigano
committed
def getIgnoreID19(self):
element = self.tree.find("dct/ignore_id19")
return element.get('value')
def getMatlabRuntimeLibraries(self, hostname = ""):
elems = self.tree.findall("matlab/libraries/path")
output = []
for elem in elems:
if (hostname != "") and ("hostname" in elem.attrib.keys()):
hosts = elem.attrib.get("hostname")
for pattern in hosts.split(","):
if fnmatch.fnmatch(hostname, pattern):
output.append(elem.text)
break
else:
output.append(elem.text)
return output
def getMatlabPreloadLibraries(self, hostname = ""):
elems = self.tree.findall("matlab/preload/path")
output = []
for elem in elems:
if (hostname != "") and ("hostname" in elem.attrib.keys()):
hosts = elem.attrib.get("hostname")
for pattern in hosts.split(","):
if fnmatch.fnmatch(hostname, pattern):
output.append(elem.text)
break
else:
output.append(elem.text)
return output
def getRootDir(self):
return self.tree.findtext("root_dir")
def setRootDir(self, newRoot):
self.tree.find("root_dir").text = newRoot
def getMexFiles(self):
return MexConf(self.tree.find("mex_files"))
def setConf(self, conf_section, value):
self.tree.find(conf_section).text = value
def getSmtpServer(self):
return self.tree.findtext("notifications/email/smtp_server", "smtp.esrf.fr")
def getUserEmail(self):
return self.tree.findtext("notifications/email/address", "")
class MexConf(object):
def __init__(self, tree):
self.tree = tree
def isExcluded(self, test_file, dct_dir):
for exclude in self.tree.findall("exclude_list/path"):
exclude_path = os.path.join(dct_dir, exclude.text)
if fnmatch.fnmatch(test_file, exclude_path):
return True
else:
return False
def getFileProperties(self, filePath):
for fileEntry in self.tree.findall("file"):
if fileEntry.findtext("path") in filePath:
output = { }
output["includes"] = []
for include in fileEntry.findall("includes/path"):
output["includes"].append(include.text)
output["lib_paths"] = []
for lib_path in fileEntry.findall("libraries/path"):
output["lib_paths"].append(lib_path.text)
output["libs"] = []
for lib in fileEntry.findall("libraries/lib"):
output["libs"].append(lib.text)
output["defines"] = []
for lib in fileEntry.findall("preprocessor/define"):
output["defines"].append(lib.text)
output["options"] = []
for lib in fileEntry.findall("preprocessor/option"):
output["options"].append(lib.text)
return output
else:
return None
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
class DCTConfUpdater(object):
def __init__(self, base_dir):
self.base_dir = base_dir
def _getNewFilename(self, filepath):
timestamp = datetime.datetime.fromtimestamp(os.path.getmtime(filepath))
pieces = os.path.splitext(filepath)
return (pieces[0] + "-" + timestamp.strftime("%Y-%m-%d-%H-%M") + pieces[1])
def backup(self, filename):
filepath = os.path.join(self.base_dir, filename)
if not os.path.exists(filepath):
raise ValueError("No such file %s" % filename)
newFilePath = self._getNewFilename(filepath)
os.rename(filepath, newFilePath)
print("Made backup copy of file: '%s' -> '%s'" % (filepath, newFilePath))
def copy(self, src_path, dest_filename):
newfilepath = os.path.join(self.base_dir, dest_filename)
print("New file path: %s" % newfilepath)
shutil.copy2(src_path, newfilepath)
def safelyInstallNewFile(self, src_path, dest_filename):
try:
self.backup(dest_filename)
except:
print("Creating new file: '%s'" % dest_filename)
self.copy(src_path, dest_filename)
def updateXMLConfFile(self, conf_section, value, path_conf_xml = "conf.xml"):
path_conf_xml = os.path.join(self.base_dir, path_conf_xml)
conf = DCTConf(path_conf_xml)
conf.setConf(conf_section, value)
print("Updated '%s' with: '%s' <- '%s'" % (path_conf_xml, conf_section, value))