00001
00002
00003
00004
00005
00006
00007
00008 INDEX_DTD = """
00009 <!DOCTYPE diagnostic_index [
00010
00011 <!ELEMENT diagnostic_index (run*)>
00012 <!ELEMENT run (runnumber,runindex?,detector*,figure*)>
00013 <!ELEMENT runnumber (#PCDATA)>
00014 <!ELEMENT runindex (#PCDATA)>
00015 <!ELEMENT detector (detname, figure*, channel*)>
00016 <!ELEMENT detname (#PCDATA)>
00017 <!ELEMENT channel (channelname, figure*)>
00018 <!ELEMENT channelname (#PCDATA)>
00019 <!ELEMENT figure (path,figname,rootPath,figtitle)>
00020 <!ELEMENT path (#PCDATA)>
00021 <!ELEMENT figname (#PCDATA)>
00022 <!ELEMENT rootPath (#PCDATA)>
00023 <!ELEMENT figtitle (#PCDATA)>
00024
00025 <!ATTLIST run id ID #REQUIRED>
00026 <!ATTLIST detector id ID #REQUIRED>
00027 <!ATTLIST channel id ID #REQUIRED>
00028 <!ATTLIST figure id ID #REQUIRED>
00029
00030 ]>
00031 """
00032
00033 COPY = False
00034
00035 PATH_TO_INDEX = 'diagnostics/xml/'
00036
00037 XML_INDEX_INDEX = 0;
00038 XML_COLLECTION_INDEX = 1;
00039
00040 DIAGNOSTIC_INDEX_TAG = 'diagnostic_index'
00041 RUN_TAG = 'run'
00042 RUN_NUMBER_TAG = 'runnumber'
00043 RUN_INDEX_TAG = 'runindex'
00044
00045 ID_ATTR = 'id'
00046
00047 def createRunFromValues(document, runIdValue, runNumberValue, runIndexValue):
00048 """Create a run element from the provided values"""
00049 runNode = document.createElement(RUN_TAG)
00050 runId = document.createAttribute(ID_ATTR)
00051 runId.value = runIdValue
00052 runNode.setAttributeNode(runId)
00053 runNumberNode = document.createElement(RUN_NUMBER_TAG)
00054 runNumber = document.createTextNode(runNumberValue)
00055 runNode.appendChild(document.createTextNode('\n '))
00056 runNumberNode.appendChild(runNumber)
00057 runNode.appendChild(runNumberNode)
00058 runIndexNode = document.createElement(RUN_INDEX_TAG)
00059 path = PATH_TO_INDEX + runIndexValue
00060 runIndex = document.createTextNode(path)
00061 runIndexNode.appendChild(runIndex)
00062 runNode.appendChild(document.createTextNode('\n '))
00063 runNode.appendChild(runIndexNode)
00064 runNode.appendChild(document.createTextNode('\n '))
00065 return runNode
00066
00067
00068 def createRunFromElement(document,
00069 node,
00070 runIndexValue):
00071 """Create a run element from the provided run Element"""
00072 number = node.getElementsByTagName(RUN_NUMBER_TAG).item(0)
00073 return createRunFromValues(document,
00074 node.getAttribute(ID_ATTR),
00075 number.firstChild.data,
00076 runIndexValue)
00077
00078
00079 def collectIndexes(existingContent,
00080 freshContent,
00081 xmlIndex):
00082 root = existingContent.getElementsByTagName(DIAGNOSTIC_INDEX_TAG).item(0)
00083 entries = {}
00084 existing = existingContent.getElementsByTagName(RUN_TAG)
00085 index = 0
00086 for node in existing:
00087 for number in node.getElementsByTagName(RUN_NUMBER_TAG):
00088 entries[number.firstChild.data] = index
00089 index += 1
00090
00091 for node in freshContent.getElementsByTagName(RUN_TAG):
00092 number = node.getElementsByTagName(RUN_NUMBER_TAG).item(0)
00093 key = number.firstChild.data
00094 if key in entries:
00095 print 'Updating entry for run ' + key
00096 if COPY:
00097 runNode = node
00098 else:
00099 runNode = createRunFromElement(existingContent,
00100 node,
00101 os.path.basename(xmlIndex))
00102 root.replaceChild(runNode,
00103 existing.item(entries[key]))
00104 else:
00105 pass
00106 idValue = key
00107 while 7 > len(idValue):
00108 idValue = '0' + idValue
00109 runNode = createRunFromValues(existingContent,
00110 'run_' + idValue,
00111 key,
00112 os.path.basename(xmlIndex))
00113 root.appendChild(existingContent.createTextNode(' '))
00114 root.appendChild(runNode)
00115 root.appendChild(existingContent.createTextNode('\n'))
00116 return existingContent
00117
00118
00119 if __name__ == "__main__":
00120
00121 import os
00122 import sys
00123 from optparse import OptionParser, OptionGroup, IndentedHelpFormatter
00124 from xml.dom.minidom import parse, parseString
00125
00126 parser = OptionParser(usage="xmlCollect xmlIndex xmlCollection",
00127 version="%prog 1.0")
00128
00129 parser.add_option('-o',
00130 '--output',
00131 dest='OUTPUT',
00132 help='file into which to write the output',
00133 default=None)
00134
00135 parser.add_option('-p',
00136 '--path-to-index',
00137 dest='PATH_TO_INDEX',
00138 help='relative path from collection to index',
00139 default=None)
00140 import sys
00141 (options, args) = parser.parse_args()
00142
00143 if None != options.PATH_TO_INDEX:
00144 PATH_TO_INDEX = options.PATH_TO_INDEX+'/'
00145
00146 if XML_INDEX_INDEX >= len(args):
00147 sys.exit(1)
00148 else:
00149 xmlIndex = args[XML_INDEX_INDEX]
00150 print "Collecting runs from " + xmlIndex
00151 fresh = parse(xmlIndex)
00152
00153 if XML_COLLECTION_INDEX >= len(args):
00154 empty = '<?xml version="1.0" ?>\n' + INDEX_DTD + "<diagnostic_index/>"
00155 existing = parseString(empty)
00156 else:
00157 input = args[XML_COLLECTION_INDEX]
00158 print "Updating " + input
00159 existing = parse(input)
00160
00161 if None != options.OUTPUT:
00162 xmlFile = open(options.OUTPUT,
00163 'w')
00164 if not xmlFile:
00165 print "Failed to open XML index output file:", outFile
00166 sys.exit(1)
00167 else:
00168 xmlFile = sys.stdout
00169
00170 collection = collectIndexes(existing,
00171 fresh,
00172 xmlIndex)
00173 collection.writexml(xmlFile)
00174
00175 if sys.stdout != xmlFile:
00176 xmlFile.close();