如何在 python 中将 jenkins 作业配置 config.xml 转换为 YAML 格式以使用 jenkins

How to convert jenkins job configuration config.xml to YAML format in python to be used jenkins-job-builder?(如何在 python 中将 jenkins 作业配置 config.xml 转换为 YAML 格式以使用 jenkins-job-builder?)
本文介绍了如何在 python 中将 jenkins 作业配置 config.xml 转换为 YAML 格式以使用 jenkins-job-builder?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

jenkins-job-builder 是帮助我维持工作的好工具在 YAML 文件中.请参阅配置章节中的示例.

jenkins-job-builder is a nice tool to help me to maintain jobs in YAML files. see example in configuration chapter.

现在我有很多旧的 jenkins 作业,如果有一个 python 脚本 xml2yaml 将现有的 jenkins 作业 config.xml 转换为 YAML,那就太好了 文件格式.

Now I had lots of old jenkins jobs, it will be nice to have a python script xml2yaml to convert the existing jenkins job config.xml to YAML file format.

你有什么建议可以在 python 中快速解决吗?

Do you any suggestions to had a quick solution in python ?

我不需要直接在jenkins-job-builder中使用,直接转成YAML供参考即可.

I don't need it to be used in jenkins-job-builder directly, just can be converted it into YAML for reference.

对于转换,某些部分可以忽略,例如命名空间.

For the convert, some part can be ignored like namespace.

config.xml 段看起来像:

<project>
  <logRotator class="hudson.tasks.LogRotator">
    <daysToKeep>-1</daysToKeep>
    <numToKeep>20</numToKeep>
    <artifactDaysToKeep>-1</artifactDaysToKeep>
    <artifactNumToKeep>-1</artifactNumToKeep>
  </logRotator>
  ...
</project>

yaml 输出可能是:

- project:
   logrotate:
     daysToKeep: -1
     numToKeep: 20
     artifactDaysToKeep: -1
     artifactNumToKeep: -1

如果不熟悉jenkins中的config.xml,可以查看infra_backend-merge-all-repo 作业">https://ci.jenkins-ci.org

If you are not familiar with config.xml in jenkins, you can check infra_backend-merge-all-repo job in https://ci.jenkins-ci.org

推荐答案

很难从你的问题中确切地看出你在这里寻找什么,但假设你正在寻找基本结构:

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 在大多数平台上都对 XML 解析提供了良好的支持.您可能会想要使用一些简单易用的东西,例如 minidom.请参阅 Python 文档中的 XML 处理模块,了解您的 Python 版本.

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.

打开文件后,查找 project 然后从那里向下解析并使用简单的映射应该可以很好地工作,因为 yaml 格式很简单.

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)

此代码段将仅打印几行最终配置文件,但它为您提供了一个简单翻译器的正确方向.根据我所见,由于命名差异,自动翻译方案的空间不大.您可以在迭代更多选项并成为表驱动时简化代码,但这只是编程问题",这至少可以让您开始使用 python 中的 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.

这篇关于如何在 python 中将 jenkins 作业配置 config.xml 转换为 YAML 格式以使用 jenkins-job-builder?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

python arbitrarily incrementing an iterator inside a loop(python在循环内任意递增迭代器)
Joining a set of ordered-integer yielding Python iterators(加入一组产生 Python 迭代器的有序整数)
Iterating over dictionary items(), values(), keys() in Python 3(在 Python 3 中迭代字典 items()、values()、keys())
What is the Perl version of a Python iterator?(Python 迭代器的 Perl 版本是什么?)
How to create a generator/iterator with the Python C API?(如何使用 Python C API 创建生成器/迭代器?)
Python generator behaviour(Python 生成器行为)