run SVN commands using python



# -*- coding:utf-8 -*-
import subprocess
import json
import os

class SVNCommands(object):
    userName = 'username'
    password = 'password'
    def __init__(self,dirPath='',fileName='',ciMessage='',svnServerUrl=''):
        self.__dirPath = dirPath
        if(len(dirPath.strip())):
            self.setLocalPath(dirPath)
        self.__fileName = fileName
        if(len(fileName.strip())):
            self.setFileName(fileName)
        self.__svnUrl = svnServerUrl
        if(len(svnServerUrl.strip())):
            self.setSVNUrl(svnServerUrl)
        self.__svnCmd = ''
        self.__ciMessage = ciMessage
        if(len(ciMessage.strip())):
            self.setCiMessage(ciMessage)
        self.__outInfo = 'ss'
        self.__errInfo = 'ss'
    def setLocalPath(self,path):
        '''
        path: local directory path string
        '''
        path = path.replace('\\','/').strip()
        if(os.path.isdir(path)):
            if(path.endswith('/') == False):
                path = path + '/'
            self.__dirPath = path
        else:
            raise TypeError('It must be a real directory')
    def getLocalPath(self):
        return self.__dirPath
    def setFileName(self,file):
        '''
        file whick you want to up,delete,commite,contains the extension
        '''
        self.__fileName = file.strip()

    def setSVNUrl(self,url):
        '''
        the url in SVN server
        '''
        self.__svnUrl = url.strip()
        
    def setCiMessage(self,msg):
        if(isinstance(msg,str)):
            self.__ciMessage = msg.strip()
        else:
            raise TypeError('It need string type')
    
    def svnCheckout(self):
        if(len(self.__dirPath) and len(self.__svnUrl)):
            self.__svnCmd = 'svn checkout ' + self.__svnUrl + ' ' + self.__dirPath + ' --username ' + self.userName + ' --password ' + self.password
            self.displayCmd()  # display
            self.execCmd()
        else:
            raise ValueError('Please check the SVN url and local path')
    def svnUpdate(self):
        if(len(self.__dirPath)):
            self.__svnCmd = 'svn up ' + self.__dirPath
            self.displayCmd()  # display
            self.execCmd()
        else:
            raise ValueError('Please check local path')
    def svnAdd(self):
        if(os.path.isfile(self.__dirPath + self.__fileName)):
            self.__svnCmd = 'svn add '+ self.__dirPath + self.__fileName
            self.displayCmd()  # display
            self.execCmd()
        else:
            raise ValueError('Please check you file')
    def svnDel(self):
        if(os.path.isfile(self.__dirPath + self.__fileName)):
            self.__svnCmd = 'svn delete ' + self.__dirPath + self.__fileName
            self.displayCmd()  # display
            self.execCmd()
        else:
            raise ValueError('Please check you file')
    def svnCommit(self):
        if(os.path.isfile(self.__dirPath + self.__fileName)):
            self.__svnCmd = 'svn ci -m "' + self.__ciMessage + '" ' + self.__dirPath + self.__fileName
            self.displayCmd()  # display
            self.execCmd()
        elif(os.path.isdir(self.__dirPath)):
            self.__svnCmd = 'svn ci -m "' + self.__ciMessage + '" ' + self.__dirPath
            self.displayCmd()  # display
            self.execCmd()
        else:
            raise ValueError('Please check you file')
    def svnCleanup(self):
        if(len(self.__dirPath)):
            self.__svnCmd = 'svn cleanup ' + self.__dirPath
            self.displayCmd()  # display
            self.execCmd()
        else:
           raise ValueError('Please check local path') 
    def execCmd(self):
        p = subprocess.Popen(self.__svnCmd)
        try:
            self.__outInfo,self.__errInfo = p.communicate(timeout = 20)
        except subprocess.TimeoutExpired:
            p.kill()
            self.__outInfo,self.__errInfo = p.communicate()
    def displayInfo(self):
        print({'Revision':self.__outInfo,'svnErr':self.__errInfo})
    def displayCmd(self):
        print('Command is '+ self.__svnCmd)

if __name__ == '__main__':
    '''
    svn = SVNCommands('d:\\tmp\\svn_test1')
    svn.setSVNUrl('https://xxxxxxxxx')
    svn.svnCheckout()
    '''
    #exit()
    svn = SVNCommands()
    svn.setLocalPath('d:\\tmp\\svn_test1')
    svn.setFileName('ab.txt')
    svn.svnAdd()
    svn.setCiMessage('+ add this file')
    svn.svnCommit()
    #svn.displayInfo()

    exit()



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值