vartoxml.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #
  2. # Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
  3. # All rights reserved.
  4. # This component and the accompanying materials are made available
  5. # under the terms of the License "Eclipse Public License v1.0"
  6. # which accompanies this distribution, and is available
  7. # at the URL "http://www.eclipse.org/legal/epl-v10.html".
  8. #
  9. # Initial Contributors:
  10. # Nokia Corporation - initial contribution.
  11. #
  12. # Contributors:
  13. #
  14. # Description:
  15. # Raptor Binary Variation var file to xml spec generator
  16. # Given a set of .var files, this script will generate an xml specification file
  17. #
  18. import sys,os,re,fnmatch
  19. import xml.dom.minidom
  20. from optparse import OptionParser
  21. doc = xml.dom.minidom.Document()
  22. class VarFile:
  23. def __init__(self,aFile):
  24. self.virtual = False
  25. self.varname = ""
  26. self.varhrh = ""
  27. self.build_include = ""
  28. self.rom_include = ""
  29. self.extends = ""
  30. self.file = aFile
  31. # Parse the var file
  32. def ParseVarFile(self):
  33. file = open(self.file)
  34. vardata = file.readlines()
  35. for var in vardata:
  36. if re.match('VARIANT\s+(?P<VARIANTNAME>\w+)',var):
  37. self.varname = re.match('VARIANT\s+(?P<VARIANTNAME>\w+)',var)
  38. elif re.match('VARIANT_HRH\s+(?P<VARIANTHRH>.+)',var):
  39. self.varhrh = re.match('VARIANT_HRH\s+(?P<VARIANTHRH>.+)',var)
  40. elif re.match('VIRTUAL\s+$',var):
  41. self.virtual = True
  42. elif re.match('BUILD_INCLUDE\s+.+',var):
  43. self.build_include = re.match('BUILD_INCLUDE\s+(?P<PROPERTY>\w+)\s+(?P<LOCATION>.+)',var)
  44. elif re.match('ROM_INCLUDE\s+.+',var):
  45. self.rom_include = re.match('ROM_INCLUDE\s+(?P<PROPERTY>\w+)\s+(?P<LOCATION>.+)',var)
  46. elif re.match('EXTENDS\s+(?P<EXTENDS>\w+)',var):
  47. self.extends = re.match('EXTENDS\s+(?P<EXTENDSNODE>\w+)',var)
  48. if self.varname:
  49. self.varname = self.varname.group('VARIANTNAME')
  50. if self.varhrh:
  51. self.varhrh = self.varhrh.group('VARIANTHRH')
  52. if self.extends:
  53. self.extends = self.extends.group('EXTENDSNODE')
  54. file.close()
  55. # Write the specs for a variant object and attach it to a parent node
  56. def CreateSpec(self,parentNode):
  57. var = doc.createElement("var")
  58. parentNode.appendChild(var)
  59. # Set the FEATUREVARIANT name
  60. vname = doc.createElement("set")
  61. vname.setAttribute("name","FEATUREVARIANT")
  62. vname.setAttribute("value",self.varname)
  63. if self.virtual:
  64. vname.setAttribute("abstract","true")
  65. var.appendChild(vname)
  66. # Set the VARIANT_HRH name
  67. hrhname = doc.createElement("set")
  68. hrhname.setAttribute("name","VARIANT_HRH")
  69. hrhname.setAttribute("value",self.varhrh)
  70. var.appendChild(hrhname)
  71. # Set the build includes
  72. if self.build_include:
  73. buildincs = doc.createElement(self.build_include.group('PROPERTY'))
  74. buildincs.setAttribute("name","BUILD_INCLUDE")
  75. buildincs.setAttribute("value",self.build_include.group('LOCATION'))
  76. var.appendChild(buildincs)
  77. # Set the rom includes
  78. if self.rom_include:
  79. buildincs = doc.createElement(self.rom_include.group('PROPERTY'))
  80. buildincs.setAttribute("name","ROM_INCLUDE")
  81. buildincs.setAttribute("value",self.rom_include.group('LOCATION'))
  82. var.appendChild(buildincs)
  83. # Main function
  84. def main():
  85. parser = OptionParser(prog = "vartoxml.py")
  86. parser.add_option("-s","--sourcefile",action="append",dest="varfile",help="List of var files")
  87. parser.add_option("-o","--output",action="store",dest="outputxml",help="Output xml file")
  88. parser.add_option("-d","--folder",action="store",dest="folder",help="Folder names to search for var files")
  89. (options, leftover_args) = parser.parse_args(sys.argv[1:])
  90. childlist = []
  91. addedlist = []
  92. nodesList = []
  93. childnames = []
  94. i = 0
  95. # Get the list of .var file from the specified folder(s)
  96. if options.folder:
  97. for folder in options.folder:
  98. for fileName in os.listdir (folder):
  99. if fnmatch.fnmatch (fileName,'*.var'):
  100. if options.varfile:
  101. options.varfile.append(fileName)
  102. else:
  103. options.varfile = []
  104. options.varfile.append(fileName)
  105. # We need some source files for this script to work
  106. if not options.varfile:
  107. print "Error: No source files specified "
  108. sys.exit()
  109. # Set parent node to gibberish
  110. parentNode = doc.createElement("build")
  111. doc.appendChild(parentNode)
  112. newparentNode = ""
  113. # Removes duplicate elements in the arguments and iterate through them
  114. # to find the top-level abstract parent node
  115. for arg in list(set(options.varfile)):
  116. varobj = VarFile(arg)
  117. varobj.ParseVarFile()
  118. if varobj.extends:
  119. childlist.append(varobj)
  120. else:
  121. addedlist.append(varobj)
  122. conf = doc.createElement("config")
  123. conf.setAttribute("name",varobj.varname)
  124. parentNode.appendChild(conf)
  125. varobj.CreateSpec(conf)
  126. nodesList.append(conf)
  127. # Names of all the children need to be stored separately
  128. for c in childlist:
  129. childnames.append(c.varname)
  130. childlist2 = list(childlist)
  131. # Check the list is correct, and append orphan nodes to master BUILD node
  132. for ch in childlist2:
  133. if addedlist:
  134. if not ch.extends in addedlist[0].varname:
  135. if not ch.extends in childnames:
  136. conf = doc.createElement("config")
  137. conf.setAttribute("name",ch.varname)
  138. parentNode.appendChild(conf)
  139. varobj.CreateSpec(conf)
  140. nodesList.append(conf)
  141. addedlist.append(ch)
  142. childlist.remove(ch)
  143. else:
  144. if not ch.extends in childnames:
  145. conf = doc.createElement("config")
  146. conf.setAttribute("name",ch.varname)
  147. parentNode.appendChild(conf)
  148. varobj.CreateSpec(conf)
  149. nodesList.append(conf)
  150. addedlist.append(ch)
  151. childlist.remove(ch)
  152. # Make a copy of the new childlist
  153. childlist2 = list(childlist)
  154. # Go through all the children, and add them to the xml spec
  155. while (childlist2):
  156. # Refactor the childlist to remove elements which have been added
  157. for add in addedlist:
  158. if add in childlist:
  159. childlist.remove(add)
  160. for ch in childlist:
  161. if ch.extends == addedlist[i].varname:
  162. addedlist.append(ch)
  163. childlist2.remove(ch)
  164. conf = doc.createElement("config")
  165. conf.setAttribute("name",ch.varname)
  166. nodesList[i].appendChild(conf)
  167. nodesList.append(conf)
  168. ch.CreateSpec(conf)
  169. else:
  170. pass
  171. i = i + 1
  172. # If output xml file is specified, write to it otherwise print the xml to screen
  173. if options.outputxml:
  174. file = open(options.outputxml,"w")
  175. file.writelines(doc.toprettyxml(indent=" "))
  176. file.close()
  177. else:
  178. print doc.toprettyxml(indent=" ")
  179. if __name__ == "__main__":
  180. main()