# Environment and version management class # # Author: Sean Dobbs (s-dobbs@northwestern.edu), 2015 import os from subprocess import Popen, PIPE class HDJobConfig: """ Stores information about the environment and software versions used for a set of jobs """ def __init__(self,filename,config_filename): self.filename = filename self.config_filename = config_filename self.VERBOSE = 1 self.config_loaded = False def ExtractVersionFromPath(self, prefix, tokens): """ Input: list of directory derived from a full pathname (e.g. fullpathname.split('/')) Output: Version number Looks for a directory that begins with a specified prefix, and returns the rest of the directory name as the version number. This works for all package names currently used in Hall D """ prefix_length = len(prefix) for el in tokens[1].split('/'): if len(el)0: print "In BuildConfig..." print " configuration file = " + self.config_filename process = Popen(['./print_env.csh',self.config_filename], stdout=PIPE, stderr=PIPE) stdout, stderr = process.communicate() if self.VERBOSE>1: print stdout for line in stdout.split('\n'): tokens = line.split('=') if len(tokens) < 2: continue if tokens[0]=='HDDS_REV': self.hdds_ver=tokens[1] elif tokens[0]=='SIM_RECON_REV': self.sim_recon_ver=tokens[1] elif tokens[0]=='CERN_LEVEL': self.cern_level=tokens[1] elif tokens[0]=='JANA_HOME': self.jana_ver=self.ExtractVersionFromPath('jana_',tokens) #for el in tokens[1].split('/'): # if len(el)<5: # continue # if el[:5]=='jana_': # self.jana_ver = el[5:] # break elif tokens[0]=='EVIOROOT': self.evio_ver=self.ExtractVersionFromPath('evio-',tokens) elif tokens[0]=='XERCESCROOT': self.xerces_ver=self.ExtractVersionFromPath('xerces-c-',tokens) elif tokens[0]=='ROOTSYS': self.root_ver=self.ExtractVersionFromPath('root_',tokens) elif tokens[0]=='CCDB_HOME': self.ccdb_ver=self.ExtractVersionFromPath('ccdb_',tokens) # we're done! self.config_loaded = True def DumpConfig(self): """ Build an XML file that stores the versions of software that are being used and various other configuration settings for this set of jobs """ if not self.config_loaded: print "Trying to dump config file before the configuration has been loaded, stopping..." return if self.VERBOSE>0: print "Building config file %s ..."%(self.filename) if os.path.isfile(self.filename): raise RuntimeError("Config file already exists!") output_str = "\n" output_str += "\n"%(self.jana_ver) output_str += "\n"%(self.sim_recon_ver) output_str += "\n"%(self.hdds_ver) output_str += "\n"%(self.evio_ver) output_str += "\n"%(self.cern_level) output_str += "\n"%(self.xerces_ver) output_str += "\n"%(self.root_ver) output_str += "\n"%(self.ccdb_ver) #output_str += "" output_str += "\n" # # # # # # # # # if self.nthreads: output_str += "\n"%(self.nthreads) if self.mem_requested: output_str += "\n"%(self.mem_requested) if self.disk_space: output_str += "\n"%(self.disk_space) if self.time_limit: output_str += "\n"%(self.time_limit) output_str += "" if self.VERBOSE>0: print "------------------------------------------------" print "Contents of XML file:" print "------------------------------------------------" print output_str print "Creating version file %s ..."%self.filename with open(self.filename,"w") as f: print>>f,output_str