從SVN一鍵對比版本

公司的部署程序太多,每次部署安裝完後,還得從SVN上對比版本,手工作實在太麻煩。python

好比下面的一個版本web

思路:apache

將須要檢查的部件及安裝的位置、SVN相關信息寫入配置文件,而後程序讀取配置文件tomcat

配置文件內容以下:服務器

[server1]
ipaddr=192.168.3.2
password=qqqqq
port=22
dir=/home/gx/    ----##部件按裝的位置
compent=sc,cag,ghomeAgent,IfTImg,Stream,TransDownload  ---###服務器上安裝的部件

[Server2]
ipaddr=192.168.14.31
password=qqqqq
port=22
dir=/home/gx/
compent=bms,bussinessAgent

[SVN]
svnserver=192.168.3.185
user=nenenen
password=xxxx
url=http://192.168.3.185/TM/VS/VSP/04_Build
~

 

因設計到公司內部的產品,代碼屏蔽了一些東西:app

#!/usr/bin/python
#coding:utf-8
import paramiko
import sys
import subprocess
import ConfigParser
import os,time
import re
import urllib2
from urlparse import urlparse
import unittest

#從SVN獲取組件及版本號
class snvparser():

    aa={}
    def __init__(self,url,server,user,passwd):
        
        auth_handler = urllib2.HTTPBasicAuthHandler()
        auth_handler.add_password(realm='TM Repository',
                          uri=server,
                          user=user,
                          passwd=passwd)
        self.opener = urllib2.build_opener(auth_handler)
        urllib2.install_opener(self.opener)
        self.url=url
        
        #print self.url
        a=self.opener.open(self.url)
        data=a.read()
        patten='href="(\w*?)/"'
        self.compentlist=re.findall(patten,data)
        #print self.compentlist
        if  'mvp' in "".join(self.compentlist) or 'GXVCP' in ''.join(self.compentlist):
            urldd = urlparse(self.url)
            result=urldd.path.split('/')[-1]
            self.compentlist.sort(reverse=True)
            #print self.compentlist
            #print self.compentlist[0]
            self.aa[result]=self.compentlist[0]
        else:
            for self.subb in self.compentlist:
                #print self.subb
                #print '-'*50
                self.suburl=self.url+'/'+self.subb
                #print self.suburl
                self.openurl(self.suburl)
   
    def openurl(self,url):
        #self.url=url
        #print url
        a=self.opener.open(url)
        data=a.read()
        patten='href="(\w*?)/"'
        self.compentlist=re.findall(patten,data)

        if  'mvp' in "".join(self.compentlist) or 'GXVCP' in ''.join(self.compentlist):
            urldd = urlparse(url)
            result=urldd.path.split('/')[-1]
            self.compentlist.sort(reverse=True)
           # print self.compentlist
           # print self.compentlist[0]
            self.aa[result]=self.compentlist[0]
        else:
            for self.subb in self.compentlist:
                #print self.subb
                #print '-'*50
                self.suburl=url+'/'+self.subb
                #print self.suburl
                self.openurl(self.suburl)

    def result(self):
        return self.aa



#配置文件判斷及讀取
config=ConfigParser.ConfigParser()
if not os.path.exists('config.ini'):
    print "config.ini is not exists,please check it!!"
    exit(-1)

config.read('config.ini')
print 
print "You want to check follow Component:"
print config.get('server1','compent')
print config.get('Server2','compent')

#SSH遠程操做函數
def ssh2(ip,pt,pw,us,comm):
    try:
        ssh = paramiko.SSHClient()
        ssh.load_system_host_keys()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(ip,port=int(pt),username=us,password=pw)
        #for COMM in comm:
        stdin,stdout,stderr = ssh.exec_command(comm)
        print "The Installed Version is          :",
        read = stdout.read()
        #print read
        compi=re.compile('Implementation-Version:(.*)')
        qq=re.search(compi,read).group(1)
        #print '======================'
        print "\033[36m %s \033[0m" % (qq.strip())
        print '-'*30
        ssh.close()
    except Exception,e:
        print "\033[36mError\033[0m is :",ip,e

class testCheckVersion(unittest.TestCase):
    def setUp(self):
        pass
    def tearDown(self):
        pass
    def testVersion(self):
        #self.assert(1==2)
        pass

 
if __name__ == '__main__':
    #print config.sections()
    server=config.get('SVN','svnserver')
    user=config.get('SVN','user')
    password=config.get('SVN','password')
    url=config.get('SVN','url')
    try:

        cc=snvparser(url,server,user,password)
        result=cc.result()
    except Exception,e:
        print e
        exit(-1)
    #print result
    #print '='*70
   
    for section_name in config.sections():
        if not section_name=='SVN':
            print '='*70
            configdd=dict(config.items(section_name))
        #configdd=getconfig(section_name)
       # print configdd
            for compent in configdd['compent'].split(","):
                try:
                    if compent=='sc' or compent=='bussinessAgent' or compent=='cag':
                        comm='cat %s%s-vcp/META-INF/MANIFEST.MF' % (configdd['dir'],compent)
                        print "The SVN \033[31m %-13s \033[0m Version is:" % compent ,
                        print "\033[36m %s \033[0m" % result[compent]
                        ssh2(configdd['ipaddr'],configdd['port'],configdd['password'],us='root',comm=comm)
                    if compent=='ghomeAgent':
                        comm='cat %s%s/META-INF/MANIFEST.MF' % (configdd['dir'],compent)
                        print "The SVN \033[31m %-13s \033[0m Version is:" % compent,
                        print "\033[36m %s \033[0m" % result[compent]
                        ssh2(configdd['ipaddr'],configdd['port'],configdd['password'],us='root',comm=comm)
                    if compent=='bms':
                        comm='cat %sbms/apache-tomcat-6.0.36/webapps/bms/META-INF/MANIFEST.MF' % configdd['dir']
                        print "The SVN \033[31m %-13s \033[0m Version is:" % compent,
                        print "\033[36m %s \033[0m" % result[compent]
                        ssh2(configdd['ipaddr'],configdd['port'],configdd['password'],us='root',comm=comm)
                    #if compent=='IfTImg' or compent=='Stream' or compent=='TransDownload':
                        #comm='ls /home/gx'
                        #print result
                        
                        
                except KeyError,e:
                    print "Please Check input !",e
                except Exception,e:
                    print "Error",e
    print '='*70

在寫完程序後才發現,公司的SVN 命令太不規範,甚至單詞也有拼錯;目錄安裝也很是隨意,查看版本方式多樣花。一時沒心情再將程序往下寫了,有須要的能夠根據實際狀況更改ssh

程序運行以下:webapp

[root@localhost tools]# python checkVersion.py 
['sc', 'bussinessAgent', 'cag', 'bms', 'ghomeAgent', 'server1', 'Server2']
======================================================================
 sc 
20160218 14:01:01 The Installed Version is: 'NoneType' object has no attribute 'group'
The SVN NESTEST Version is : GXVCP_sc_D10V200_alpha1 
======================================================================
bussinessAgent
[Errno None] Unable to connect to port 22 on  or 192.168.14.31  --#服務器密碼被更改了
The SVN NESTEST Version is : GXVCP_businessAgent_D10V200_alpha1 
======================================================================
cag
20160218 14:01:12 The Installed Version is: 'NoneType' object has no attribute 'group'   ---文件已更名了
The SVN NESTEST Version is : GXVCP_cag_D10V200_alpha1 
======================================================================
bms
[Errno None] Unable to connect to port 22 on  or 192.168.14.31
The SVN NESTEST Version is : GXVCP_bms_D10V200_alpha1 
======================================================================
ghomeAgent
20160218 14:01:22 The Installed Version is:  GXVCP_ghomeAgent_D10V200_alpha1 
------------------------------
The SVN NESTEST Version is : GXVCP_ghomeAgent_D10V200_alpha1 
======================================================================
相關文章
相關標籤/搜索