ParseConfig2sedcmd.py

 python  python   2016-11-01   评论次 

这是一个将配置文件变成sed的python脚本。

由于是工作中写的临时用的,并不能确保万无一失,要求配置文件的格式类似以下(java配置文件):

# log4j config
log4j.rootLogger=info,logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%-5p]%-d{HH:mm:ss} [%c{1}] %X{ip}-%X{sid} %m%n

log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile.File=/home/dsp/app/mobile_monitora/logs/monitor.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=[%-5p]%-d{yyyy-MM-dd HH:mm:ss} [%c] %X{ip}-%X{sid} %m%n

log4j.logger.org.springframework=ERROR
log4j.logger.org.springframework.jdbc.core.simple.SimpleJdbcTemplate=ERROR
log4j.logger.java.sql.Connection=ERROR
log4j.logger.java.sql.Statement=ERROR
log4j.logger.java.sql.PreparedStatement=ERROR
log4j.logger.java.sql.ResultSet=ERROR
log4j.logger.com.opensymphony.xwork2=ERROR

最终生成效果类似于:

#************************************************** log4j.properties **************************************************
#hz b
sed -i 's#log4j.rootLogger=.*#log4j.rootLogger=info,logfile#g' log4j.properties
sed -i 's#log4j.appender.stdout=.*#log4j.appender.stdout=org.apache.log4j.ConsoleAppender#g' log4j.properties
sed -i 's#log4j.appender.stdout.layout=.*#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout#g' log4j.properties
sed -i 's#log4j.appender.stdout.layout.ConversionPattern=.*#log4j.appender.stdout.layout.ConversionPattern=[%-5p]%-d{HH:mm:ss} [%c{1}] %X{ip}-%X{sid} %m%n#g' log4j.properties
sed -i 's#log4j.appender.logfile=.*#log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender#g' log4j.properties
sed -i 's#log4j.appender.logfile.File=.*#log4j.appender.logfile.File=/home/dsp/app/mobile_monitorb/logs/monitor.log#g' log4j.properties
sed -i 's#log4j.appender.logfile.layout=.*#log4j.appender.logfile.layout=org.apache.log4j.PatternLayout#g' log4j.properties
sed -i 's#log4j.appender.logfile.layout.ConversionPattern=.*#log4j.appender.logfile.layout.ConversionPattern=[%-5p]%-d{yyyy-MM-dd HH:mm:ss} [%c] %X{ip}-%X{sid} %m%n#g' log4j.properties
sed -i 's#log4j.logger.org.springframework=.*#log4j.logger.org.springframework=ERROR#g' log4j.properties
sed -i 's#log4j.logger.org.springframework.jdbc.core.simple.SimpleJdbcTemplate=.*#log4j.logger.org.springframework.jdbc.core.simple.SimpleJdbcTemplate=ERROR#g' log4j.properties
sed -i 's#log4j.logger.java.sql.Connection=.*#log4j.logger.java.sql.Connection=ERROR#g' log4j.properties
sed -i 's#log4j.logger.java.sql.Statement=.*#log4j.logger.java.sql.Statement=ERROR#g' log4j.properties
sed -i 's#log4j.logger.java.sql.PreparedStatement=.*#log4j.logger.java.sql.PreparedStatement=ERROR#g' log4j.properties
sed -i 's#log4j.logger.java.sql.ResultSet=.*#log4j.logger.java.sql.ResultSet=ERROR#g' log4j.properties
sed -i 's#log4j.logger.com.opensymphony.xwork2=.*#log4j.logger.com.opensymphony.xwork2=ERROR#g' log4j.properties


脚本如下(GITHUB链接是https://github.com/staugur/scripts/blob/master/other/ParseConfig2sedcmd.py):

#!/usr/bin/python

import os
import sys
import commands

#BASEDIR=os.path.dirname(os.path.abspath(__file__))

def init(filename=None):
    if filename:
        cmd1="dos2unix %s"%filename
        cmd2="sed -i -e '/^#/d' -e '/^$/d' %s"%filename
    else:
        cmd1="dos2unix *.properties"
        cmd2="sed -i -e '/^#/d' -e '/^$/d' *.properties"
    commands.getoutput(cmd1)
    commands.getoutput(cmd2)

def parse(files=None):
    d={}
    if files:
        files=(files,)
    else:
        files=os.listdir('.')
    for f in files:
        if f == 'done.py':continue
        with open(f, 'r') as _f:
            v=[ _ for _ in _f.readlines() if _ ]
            #print v
        d[f]=v
    return d

def main(d):
    for k,v in d.iteritems():
        print "*" * 50, k, "*" * 50
        i=0
        for _ in v:
            if _.count('#') == 0:
                line=_.strip()
                #print line
                try:
                    key,value=line.split('=')
                except Exception, e:
                    if line.split('=')[0] == 'http.p3p.policy.header':
                        key,value='http.p3p.policy.header',line.split('http.p3p.policy.header=')[-1]
                    else:
                        raise
                cmd="sed -i 's#%s=.*#%s=%s#g' %s" %(key, key, value, k)
                print cmd
                commands.getoutput(cmd)
                i+=1
        print i,len(v),'\n'

if __name__ == "__main__":
    try:
        filename=sys.argv[1]
    except:
        filename=None
    init()
    main(parse(filename))


点赞 打赏
取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

交流分享

QQ扫一扫或点击二维码加入,群号577543189
微信扫一扫,手滑关注它
微信扫一扫,进入小程序

推荐文章

标签云集

友情链接  交换友链

推广位