def updateReport(reportFile, newReportInstance): """ _updateReport_ Given a file containing several reports: reportFile, find the report in there whose name matches the newReportInstance's name and replace that report with the new Report instance. Returns a boolean: True if report name was matched and updated, False if the report was not found and updated. (False may indicate that the new report file needs to be merged with the main report file) """ if not os.path.exists(reportFile): existingReports = [] else: existingReports = readJobReport(reportFile) updatedReport = False output = IMProvDoc("JobReports") for report in existingReports: if report.name == newReportInstance.name: output.addNode(newReportInstance.save()) updatedReport = True else: output.addNode(report.save()) handle = open(reportFile, 'w') handle.write(output.makeDOMDocument().toprettyxml()) handle.close() return updatedReport
def saveToFile(self, filename): """ _saveToFile_ Save this instance to the file provided """ doc = IMProvDoc("ProdCommonConfig") doc.addNode(self.save()) handle = open(filename, 'w') handle.write(doc.makeDOMDocument().toprettyxml()) handle.close() return
def writeSpec(filename, *relValInstances): """ _writeSpec_ Write out the RelValidation instances provided to the filename given. Will overwrite file if present """ masterDoc = IMProvDoc("ReleaseValidation") for item in relValInstances: masterDoc.addNode(item.save()) handle = open(filename, 'w') handle.write(masterDoc.makeDOMDocument().toprettyxml()) handle.close() return
def writeSpecFile(filename, *specInstances): """ _writeSpecFile_ Util to write multiple specs to a single file """ doc = IMProvDoc("RequestSpecs") for spec in specInstances: doc.addNode(spec.save()) handle = open(filename, 'w') handle.write(doc.makeDOMDocument().toprettyxml()) handle.close() return
def save(self): """ _save_ Convert this object into an IMProvNode structure """ result = IMProvDoc("RequestSpec") details = IMProvNode("RequestDetails") for key, val in self.requestDetails.items(): details.addNode(IMProvNode(key, str(val))) policies = IMProvNode("Policies") for key, val in self.policies.items(): policies.addNode(IMProvNode(key, str(val))) result.addNode(details) result.addNode(policies) result.addNode(self.workflow.makeIMProv()) return result
def startElement(self, name, attrs): """ _startElement_ Override SAX startElement handler """ plainAttrs = {} for key, value in attrs.items(): plainAttrs[str(key)] = str(value) if self._ParentDoc == None: self._ParentDoc = IMProvDoc(str(name)) self._ParentDoc.attrs.update(plainAttrs) self._NodeStack.append(self._ParentDoc) return self._CharCache = [] newnode = IMProvNode(str(name)) for key, value in attrs.items(): newnode.attrs[key] = value self._NodeStack[-1].addNode(newnode) self._NodeStack.append(newnode) return
def mergeReports(reportFile1, reportFile2): """ _mergeReports_ Load job reports from both files, and combine them into a single file. The output will be written to the first file provided. (IE JobReports from reportFile2 will be added to reportFile1) If reportFile1 does not exist, a new report will be created, containing the contents of reportFile2. If reportFile2 does not exist, then a RuntimeError is thrown. """ if not os.path.exists(reportFile1): reports1 = [] else: reports1 = readJobReport(reportFile1) if not os.path.exists(reportFile2): msg = "Report file to be merged does not exist:\n" msg += reportFile2 raise RuntimeError, msg reports2 = readJobReport(reportFile2) reports1.extend(reports2) output = IMProvDoc("JobReports") for item in reports1: output.addNode(item.save()) handle = open(reportFile1, 'w') handle.write(output.makeDOMDocument().toprettyxml()) handle.close() return
def combineReports(reportFile, reportNames, newReportInstance): """ Combine reports, take some fields from report with given name in reportFile and then overwrite with newReportInstance Note: newReportInstance is modified, and should be written back as the task fjr - else subsequent tasks will take the wrong one!!! """ if not os.path.exists(reportFile): existingReports = [] else: existingReports = readJobReport(reportFile) if not isinstance(reportNames, list): reportNames = [reportNames] reportFound = False output = IMProvDoc("JobReports") #wipe old values ready for new ones newReportInstance.inputFiles = [] newReportInstance.generatorInfo = {} #how to handle multiple? for report in existingReports: if report.name in reportNames: reportFound = True # copy some values across from old report newReportInstance.inputFiles.extend(report.inputFiles) newReportInstance.skippedEvents.extend(report.skippedEvents) newReportInstance.skippedFiles.extend(report.skippedFiles) # loop over output files and change provenance to 1st node's for outfile in newReportInstance.files: oldinputfiles = outfile.inputFiles outfile.inputFiles = [ ] #clear ready for correct provenance info for infile in oldinputfiles: # find the ancestor input files in previous report for ancestor in report.files: if ancestor['LFN'] == infile['LFN']: outfile.inputFiles.extend(ancestor.inputFiles) print "Updated InputFiles %s for %s" % ( ancestor.inputFiles, outfile['LFN']) # No LFN, use PFN (Needed for parent forwarding) elif not infile['LFN'] and \ ancestor['PFN'] == infile['PFN']: outfile.inputFiles.extend(ancestor.inputFiles) print "Updated InputFiles %s for %s" % ( ancestor.inputFiles, outfile['LFN']) if report.timing.has_key('AppStartTime') and \ report.timing['AppStartTime'] < newReportInstance.timing.get('AppStartTime', time.time()): newReportInstance.timing['AppStartTime'] = report.timing[ 'AppStartTime'] continue # // if here either this report is not one of the inputs # // or the report contained a staged out file #// - in either case it must be saved output.addNode(report.save()) if not reportFound: raise RuntimeError, "Reports not combined: %s not found in %s" % \ (str(reportNames), reportFile) output.addNode(newReportInstance.save()) handle = open(reportFile, 'w') handle.write(output.makeDOMDocument().toprettyxml()) handle.close() return newReportInstance
# longer a session leader, preventing the daemon from ever acquiring # a controlling terminal. pid = os.fork() # Fork a second child. except OSError, e: raise Exception, "%s [%d]" % (e.strerror, e.errno) if (pid == 0): # The second child. # Since the current working directory may be a mounted filesystem, we # avoid the issue of not being able to unmount the filesystem at # shutdown time by changing it to the root directory. os.chdir(workdir) # We probably don't want the file mode creation mask inherited from # the parent, so we give the child complete control over permissions. os.umask(UMASK) doc = IMProvDoc("Daemon") doc.addNode(IMProvNode("ProcessID", None, Value=os.getpid())) doc.addNode(IMProvNode("ParentProcessID", None, Value=os.getppid())) doc.addNode(IMProvNode("ProcessGroupID", None, Value=os.getpgrp())) doc.addNode(IMProvNode("UserID", None, Value=os.getuid())) doc.addNode(IMProvNode("EffectiveUserID", None, Value=os.geteuid())) doc.addNode(IMProvNode("GroupID", None, Value=os.getgid())) doc.addNode( IMProvNode("EffectiveGroupID", None, Value=os.getegid())) open("Daemon.xml", "w").write(doc.makeDOMDocument().toprettyxml()) print "Started Daemon: Process %s" % os.getpid() else: