PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/Client/src/bkr/client/convert.py

https://github.com/beaker-project/beaker
Python | 197 lines | 183 code | 10 blank | 4 comment | 0 complexity | f4f6c231693efc2ea2ed3de3bd503079 MD5 | raw file
Possible License(s): GPL-2.0, CC-BY-SA-3.0
  1. # This program is free software; you can redistribute it and/or modify
  2. # it under the terms of the GNU General Public License as published by
  3. # the Free Software Foundation; either version 2 of the License, or
  4. # (at your option) any later version.
  5. import re
  6. import xml.dom.minidom
  7. __all__ = (
  8. "Convert",
  9. "rhts2beaker",
  10. )
  11. def rhts2beaker(jobfile):
  12. convert = Convert(xml.dom.minidom.parseString(jobfile))
  13. return convert.toxml()
  14. class Convert(object):
  15. doc = xml.dom.minidom.Document()
  16. rhts2beaker = staticmethod(rhts2beaker)
  17. invalidjobtags = ['submitter',
  18. 'workflow',
  19. ]
  20. invalidrecipetags = ['yumInstall',
  21. 'driverdisk',
  22. ]
  23. def __init__(self, jobxml):
  24. self.counter = 0
  25. self.jobxml = jobxml
  26. def toxml(self):
  27. self.handle_invalid(self.jobxml.getElementsByTagName("job"), self.invalidjobtags)
  28. self.handle_invalid(self.jobxml.getElementsByTagName("recipe"), self.invalidrecipetags)
  29. self.handle_invalid(self.jobxml.getElementsByTagName("guestrecipe"), self.invalidrecipetags)
  30. self.handle_tasks(self.jobxml)
  31. self.handle_recipes(self.jobxml.getElementsByTagName("recipe"))
  32. self.handle_recipes(self.jobxml.getElementsByTagName("guestrecipe"))
  33. return self.jobxml.toxml()
  34. def getText(self, nodelist):
  35. rc = ""
  36. for node in nodelist:
  37. if node.nodeType == node.TEXT_NODE:
  38. rc = rc + node.data
  39. return rc
  40. def handle_distroRequires(self, requires):
  41. require = None
  42. requires_search = re.compile(r'([^\s]+)\s+([^\s]+)\s+([^\s]+)')
  43. if requires_search.match(requires):
  44. (dummy, key, op, value, dummy) = requires_search.split(requires)
  45. if key in ['ARCH', 'FAMILY', 'NAME', 'VARIANT', 'METHOD']:
  46. require = self.doc.createElement('distro_%s' % key.lower())
  47. require.setAttribute('value', '%s' % value)
  48. else:
  49. require = self.doc.createElement('distro_tag')
  50. require.setAttribute('value', '%s' % key)
  51. require.setAttribute('op', '%s' % op)
  52. return require
  53. def handle_addrepo(self, addrepo):
  54. """
  55. Process repos
  56. """
  57. self.counter += 1
  58. repo = self.doc.createElement('repo')
  59. repo.setAttribute('name', 'myrepo_%s' % self.counter)
  60. repo.setAttribute('url', '%s' % addrepo)
  61. return repo
  62. def handle_addpackage(self, addpackage):
  63. """
  64. Process packages
  65. """
  66. package = self.doc.createElement('package')
  67. package.setAttribute('name', '%s' % addpackage)
  68. return package
  69. def handle_hostRequires(self, requires):
  70. require = None
  71. requires_search = re.compile(r'([^\s]+)\s+([^\s]+)\s+([^\s]+)')
  72. if requires_search.match(requires):
  73. (dummy, key, op, value, dummy) = requires_search.split(requires)
  74. if key == 'ARCH':
  75. require = self.doc.createElement('arch')
  76. elif key == 'LABCONTROLLER':
  77. require = self.doc.createElement('hostlabcontroller')
  78. elif key == 'HOSTNAME':
  79. require = self.doc.createElement('hostname')
  80. elif key == 'MEMORY':
  81. require = self.doc.createElement('memory')
  82. elif key == 'PROCESSORS':
  83. require = self.doc.createElement('cpu_count')
  84. elif key == 'FAMILY':
  85. # FAMILY doesn't make sense to beaker
  86. require = None
  87. elif key == 'CPUNAME':
  88. require = self.doc.createElement('cpu_codename')
  89. else:
  90. require = self.doc.createElement('key_value')
  91. require.setAttribute('key', '%s' % key)
  92. if require:
  93. require.setAttribute('op', '%s' % op)
  94. require.setAttribute('value', '%s' % value)
  95. return require
  96. def handle_tasks(self, nodes):
  97. for child in nodes.childNodes:
  98. if child.nodeType == child.ELEMENT_NODE:
  99. if child.tagName == 'test':
  100. child.tagName = 'task'
  101. else:
  102. self.handle_tasks(child)
  103. def handle_partition(self, node):
  104. partition = self.doc.createElement('partition')
  105. for child in node.childNodes:
  106. if child.nodeName == 'type':
  107. partition.setAttribute('type', self.getText(child.childNodes))
  108. if child.nodeName == 'name':
  109. partition.setAttribute('name', self.getText(child.childNodes))
  110. if child.nodeName == 'size':
  111. partition.setAttribute('size', self.getText(child.childNodes))
  112. if child.nodeName == 'fs':
  113. partition.setAttribute('fs', self.getText(child.childNodes))
  114. return partition
  115. def handle_recipes(self, recipes):
  116. for recipe in recipes:
  117. del_nodes = []
  118. partitions = self.doc.createElement('partitions')
  119. and_distro = self.doc.createElement('and')
  120. and_host = self.doc.createElement('and')
  121. repos = self.doc.createElement('repos')
  122. packages = self.doc.createElement('packages')
  123. kernel_options = ''
  124. if 'kernel_options' in recipe._attrs:
  125. kernel_options = '%s ' % recipe.getAttribute('kernel_options')
  126. if 'bootargs' in recipe._attrs:
  127. kernel_options = '%s%s' % (kernel_options, recipe.getAttribute('bootargs'))
  128. recipe.setAttribute('kernel_options', kernel_options)
  129. recipe.removeAttribute('bootargs')
  130. if 'testrepo' in recipe._attrs:
  131. recipe.removeAttribute('testrepo')
  132. for child in recipe.childNodes:
  133. if child.nodeType == child.ELEMENT_NODE and child.tagName == 'bootargs':
  134. del_nodes.append(child)
  135. kernel_options = '%s%s' % (kernel_options, self.getText(child.childNodes))
  136. recipe.setAttribute('kernel_options', kernel_options)
  137. if child.nodeType == child.ELEMENT_NODE and child.tagName == 'distroRequires':
  138. del_nodes.append(child)
  139. require = self.handle_distroRequires(self.getText(child.childNodes))
  140. if require:
  141. and_distro.appendChild(require)
  142. if child.nodeType == child.ELEMENT_NODE and child.tagName == 'hostRequires':
  143. del_nodes.append(child)
  144. require = self.handle_hostRequires(self.getText(child.childNodes))
  145. if require:
  146. and_host.appendChild(require)
  147. if child.nodeType == child.ELEMENT_NODE and child.tagName == 'partition':
  148. del_nodes.append(child)
  149. partitions.appendChild(self.handle_partition(child))
  150. if child.nodeType == child.ELEMENT_NODE and child.tagName == 'addrepo':
  151. del_nodes.append(child)
  152. repo = self.handle_addrepo(self.getText(child.childNodes))
  153. repos.appendChild(repo)
  154. if child.nodeType == child.ELEMENT_NODE and child.tagName == 'installPackage':
  155. del_nodes.append(child)
  156. package = self.handle_addpackage(self.getText(child.childNodes))
  157. packages.appendChild(package)
  158. distro = self.doc.createElement('distroRequires')
  159. distro.appendChild(and_distro)
  160. host = self.doc.createElement('hostRequires')
  161. host.appendChild(and_host)
  162. for child in del_nodes:
  163. recipe.removeChild(child)
  164. recipe.appendChild(packages)
  165. recipe.appendChild(repos)
  166. recipe.appendChild(distro)
  167. recipe.appendChild(host)
  168. recipe.appendChild(partitions)
  169. def handle_invalid(self, nodes, invalids):
  170. for invalid in invalids:
  171. for node in nodes:
  172. for child in node.getElementsByTagName(invalid):
  173. node.removeChild(child)