How to convert jenkins job configuration config.xml to YAML format in python to be used jenkins-job-builder?

雨燕双飞 提交于 2019-12-04 09:57:59

It's hard to tell from your question exactly what you're looking for here, but assuming you're looking for the basic structure:

Python has good support on most platforms for XML Parsing. Chances are you'll want to use something simple and easy to use like minidom. See the XML Processing Modules in the python docs for your version of python.

Once you've opened the file, looking for project and then parsing down from there and using a simple mapping should work pretty well given the simplicity of the yaml format.

from xml.dom.minidom import parse

def getText(nodelist):
    rc = []
    for node in nodelist:
        if node.nodeType == node.TEXT_NODE:
            rc.append(node.data)
    return ''.join(rc)

def getTextForTag(nodelist,tag):
    elements = nodelist.getElementsByTagName(tag)
    if (elements.length>0):
        return getText( elements[0].childNodes)
    return ''

def printValueForTag(parent, indent, tag, valueName=''):
    value = getTextForTag( parent,tag)
    if (len(value)>0):
        if (valueName==''):
            valueName = tag
        print indent + valueName+": "+value

def emitLogRotate(indent, rotator):
    print indent+"logrotate:"
    indent+='  '
    printValueForTag( rotator,indent, 'daysToKeep')
    printValueForTag( rotator,indent, 'numToKeep')  
def emitProject(project):
    print "- project:"
    # all projects have log rotators, so no need to chec
    emitLogRotate("   ",project.getElementsByTagName('logRotator')[0])
    # next section...

dom = parse('config.xml')
emitProject(dom)

This snippet will print just a few lines of the eventual configuration file, but it puts you in the right direction for a simple translator. Based on what I've seen, there's not much room for an automatic translation scheme due to naming differences. You could streamline the code as you iterate for more options and to be table driven, but that's "just a matter of programming", this will at least get you started with the DOM parsers in python.

I'm writing a program that does this conversion from XML to YAML. It can dynamically query a Jenkins server and translate all the jobs to YAML.

https://github.com/ktdreyer/jenkins-job-wrecker

Right now it works for very simple jobs. I've taken a safe/pessimistic approach and the program will bail if it encounters XML that it cannot yet translate.

Jochen Hayek

I suggest querying and accessing the with expressions using on the command line and in shell scripts. No trouble with low-level programmatical access to XML. XMLStarlet is an XPath swiss-army knife on the command line.

"xmlstarlet el" shows you the element structure of the entire XML as XPath expressions.

"xmlstarlet sel -t -c XPath-expression" will extract exactly what you want.

Maybe you want to spend an hour (or two) on freshing up your XPath know-how in advance.

You will shed a couple of tears, once you recognize how much time you spent with programming XML access before you used XMLStarlet.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!