久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 python 中將 jenkins 作業配置 config.xml 轉換為

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?) - IT屋-程序員軟件開
本文介紹了如何在 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?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Troubles while parsing with python very large xml file(使用 python 解析非常大的 xml 文件時出現問題)
Find all nodes by attribute in XML using Python 2(使用 Python 2 在 XML 中按屬性查找所有節點)
Python - How to parse xml response and store a elements value in a variable?(Python - 如何解析 xml 響應并將元素值存儲在變量中?)
How to get XML tag value in Python(如何在 Python 中獲取 XML 標記值)
How to correctly parse utf-8 xml with ElementTree?(如何使用 ElementTree 正確解析 utf-8 xml?)
Parse XML from URL into python object(將 XML 從 URL 解析為 python 對象)
主站蜘蛛池模板: 亚洲最大福利网 | 国产精品一区在线播放 | 久久精品国产亚洲一区二区三区 | 久久久婷 | 性一交一乱一透一a级 | 啪啪网页| 久草在线 | 国产美女一区二区 | 国产精品久久久久aaaa九色 | 天天干天天爽 | 91精品久久久久久久久中文字幕 | 成人高清在线 | 最新超碰| 欧美日韩高清免费 | 国产精品爱久久久久久久 | 一本一道久久a久久精品蜜桃 | 91高清在线视频 | 国产中文字幕av | 成人免费大片黄在线播放 | 成人片免费看 | 欧美乱码精品一区二区三区 | 免费精品 | 成人在线免费视频 | 亚洲欧美日本在线 | 欧美成人精品二区三区99精品 | 在线观看 亚洲 | 一区二区三区四区在线视频 | 午夜视频在线视频 | 色吊丝2288sds中文字幕 | 久久91精品国产一区二区三区 | 国产免费福利小视频 | 美女爽到呻吟久久久久 | 国产高清一区二区三区 | 91国内精精品久久久久久婷婷 | 日韩中文字幕在线播放 | 免费成人高清在线视频 | 国产98色在线 | 中文字幕亚洲区一区二 | 91麻豆精品国产91久久久久久 | 国产精品99久久久久久久vr | 国产免费一区二区三区免费视频 |