当前位置: 首页 > 工具软件 > dict_build > 使用案例 >

2021-10-07-build_info

夏和雅
2023-12-01
#!/usr/bin/python
# coding:utf-8

import re
import jenkins
from tools  import  Tools

def LoginJenkins(master_url,username=None,password=None):
    return jenkins.Jenkins(master_url,username,password)

class BuildInfo(object):
    def __init__(self,InstanceJenkins,job_name,buildnumber):
        self.jenkins = InstanceJenkins
        self.job_name = job_name
        self.buildnumber = buildnumber
        self.detail_Dict = InstanceJenkins.get_build_info(job_name,buildnumber)
        self.initializa()

    @classmethod
    def init_URL(cls,url,username=None,password=None):
        # print url
        # print username
        # print password

        url_master,job_name,buildnumber = Tools.get_params_use_url(url)

        InstanceJenkins = jenkins.Jenkins(url_master,username,password)
        return cls(InstanceJenkins,job_name,buildnumber)

    def initializa(self):
        self.is_building = self.detail_Dict.get('building','')
        self.build_result = self.detail_Dict.get('result','') # 有几种状态 暂时未知   ABORTED  SUCCESS  FAILURE null,已经"building": true, 正在构建
        self.build_start_time = int(self.detail_Dict.get('timestamp',0)/1000)
        self.build_use_time = int(self.detail_Dict.get('duration',0)/1000)
        self.build_end_time = self.build_start_time + self.build_use_time
        self.build_url = self.detail_Dict.get('url','')

    def __getitem__(self, item):
        return getattr(self,item)



    # 获取本次构建的额某一个参数
    def get_build_parmas(self):
        self.parmas_dict = {}
        if 'actions' in self.detail_Dict.keys():
            build_actions_list = self.detail_Dict.get('actions',[])
            for item_dict in build_actions_list:
                if 'parameters' in item_dict.keys():
                    parameters_list = item_dict.get('parameters',[])
                    for parmas_dict_child_dict  in parameters_list :
                        self.parmas_dict[parmas_dict_child_dict.get('name','')] = parmas_dict_child_dict.get('value','')
        # 获取本地构建的某一个参数
        return self.parmas_dict


    # 获取本次所有的构建参数
    def get_build_one_parmas(self,parma_name):
        if 'actions' in self.detail_Dict.keys():
            build_actions_list = self.detail_Dict.get('actions',[])
            for item_dict in build_actions_list:
                if 'parameters' in item_dict.keys():
                    parameters_list = item_dict.get('parameters',[])
                    for parmas_dict_child_dict  in parameters_list :
                        if parmas_dict_child_dict.get('name','') == parma_name:
                            return  parmas_dict_child_dict.get('value','')


    # 获取buildoutput 内容:未加入从初始化中
    def get_build_output(self):
        print  self.job_name, self.buildnumber
        self.build_output = self.jenkins.get_build_console_output(self.job_name,self.buildnumber)
        return self.build_output

    #  获取当前build的节点信息。
    def get_build_volume_info(self):
        self.get_build_output()
        ret = Tools.ReFindall('\[apply and node (?P<node>.*?) ageny\] container (?P<volume>.*?) is running now',self.build_output)
        return ret



if __name__ == '__main__' :
    # InstanceJenkins = LoginJenkins('http://10.0.0.40:8080/','admin','xxxxx')
    # build = BuildInfo(InstanceJenkins,'diff_changed_failed',20)
    # print build.get_build_one_parmas('tag')
    # print build.get_build_output()
    # print build.build_output
    build = BuildInfo.init_URL('http://10.0.0.40:8080/job/diff_changed_failed/91/','admin','xxxx')
    # print build.get_build_one_parmas('GERRIT_CHANGE_ID')
    print  build.get_build_parmas()
 类似资料: