# -*- 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()