您的位置:首页 > 其它

jenkins上添加text parameter, 用脚本读取并存到配置文件中

2016-11-11 09:48 525 查看
1  功能需求

             在jenkins上设置这些参数  即类似于设置系统变量   只不过设置name:custom_config / value : text parameter 而不是string parameter

            自定义设置配置文件中一些session/value,可以不用再创建一个配置文件,以及在运行时载入这个配置文件( eg....  run.bat  -C custom_config.cfg)

            现直接在jenkins上设置text parameter ,  再通过脚本读取text paramenter 并与已有的配置文件自动合并 运行



2  达到效果



              最终生成的配置文件:



#!/usr/bin/env python
# -*- coding: utf-8 -*-

from os import environ, linesep
from ConfigParser import ConfigParser
from StringIO import StringIO
from pdb import set_trace
import StringIO
default_version = '0.0.0'
def add_val2section(cfg, section, name, value):
if value and value != default_version:
cfg.add_section(section)
cfg.set(section, name, value)
else:
print('[warning] {0}\'s version is not set in environment!'.format(section))

def build_sal_regression_cfg(env):
productname = env.get('SAL_product_name', 'sc')
sal_pattern_version = env.get('SAL_PATTERN_VERSION', default_version)
sal_build_version = env.get('SAL_BUILD_VERSION', default_version)
bep_build_version = env.get('BEP_BUILD_VERSION', default_version)
bep_pattern_version = env.get('BEP_PATTERN_VERSION', default_version)
custom_config = env.get('CUSTOM_CONFIG', '')
cfg_name = r'{0}.cfg'.format(productname)
cfg = ConfigParser()
# read the configuration of the product
cfg.read(cfg_name)
add_val2section(cfg, 'test_info', 'productname', productname)
add_val2section(cfg, 'pattern_sal', 'version', sal_pattern_version)
add_val2section(cfg, 'pattern_bep', 'version', bep_pattern_version)
add_val2section(cfg, 'build_win32_sal', 'version', sal_build_version)
add_val2section(cfg, 'build_win64_sal', 'version', sal_build_version)
add_val2section(cfg, 'build_win32_bep', 'version', bep_build_version)
add_val2section(cfg, 'build_win64_bep', 'version', bep_build_version)
# read custom_config
if custom_config:
buf = StringIO.StringIO(custom_config)
cfg.readfp(buf)
return cfg

def build_cfg(env):
test_name = env.get('SAL_test_name', 'sal_regression')
return build_sal_regression_cfg(env)
# raise Exception('SAL_test_name({0}) is not supported'.format(test_name))

def main(cfg_file_path):
#print environ
with open(cfg_file_path, 'w') as f:
build_cfg(environ).write(f)
pass

if __name__ == '__main__':
main('temp.cfg')
print 'creat-cfg-py'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐