使用python 操作liunx的svn,方案二

在对liunx操作svn的方式,做了改动,使用python的,subprocess进行操作

在第一种方案中,我使用了先拉到本地,然后再创建,在进行上传,实际在svn中可以直接创建文件,并进行文件复制,具体代码如下

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# CreateDate: 2018-01-17

import os
import re
import subprocess
import locale
import sys

class SvnCommand(object):
    def __init__(self, project):
        self.cmd = 'http://svn.egomsl.com/svn/repos/autotest.globalegrow.com/projectScript/uitest'
        self.project = project

    # 获取文件路径

    def checkout(self): #下载目录
        """
        checkout code from SVN respoitory.
        :params url: svn url.
        :params path: target path.
        """
        command = 'svn checkout ' + self.cmd
        message = 'checkout code success'
        subprocess.check_output(command, shell=True)
        return {'code': 0, 'msg': message}


    # 新建文件
    def crate(self):
        # 新建svn目录
        project = self.cmd + '/' + self.project
        command = 'svn mkdir -m "making"  ' + project
        print command
        message = 'create file success'
        subprocess.check_output(command, shell=True)
        s.copyfile();
        return {'code': 0, 'msg': message}


    def copyfile(self):
        targetDir = self.cmd + '/' + self.project     #要复制的文件
        url = 'svn list  http://svn.egomsl.com/svn/repos/autotest.globalegrow.com/projectScript/uitest/template'  #模板文件
        address = subprocess.check_output(url, shell=True)
        pri_list = address.split('
')
        print pri_list
        for i in range(len(pri_list)):
            sourceDir = self.cmd + "/template" + "/" + pri_list[i]
            command2 = 'svn copy ' + sourceDir + ' ' + targetDir + " -m 'copy project' "
            print command2
            subprocess.check_output(command2, shell=True)
    def update(self): #更新项目
        """
        update latest code.
        """
        self.cmd = 'svn update'
        message = 'update code success'
        try:
            subprocess.check_output(self.cmd, shell=True)
        except Exception:
            self.cmd = 'svn cleanup'
            subprocess.check_output(self.cmd, shell=True)
            self.cmd = 'svn update'
            subprocess.check_output(self.cmd, shell=True)
            return {'code': 0, 'msg': message}

    # 更新svn时需要获取svn的地址,这样只更新自己的项目
    def svncommit(self):
        project = self.cmd + '/' + self.project
        print u"开始提交svn地址"
        command = "svn ci -m commit 'commit' " + project
        print command
        message = 'commit code success'
        subprocess.check_output(command, shell=True)
        return {'code': 0, 'msg': message}




if __name__ == "__main__":
    s = SvnCommand(sys.argv[1])
    s.crate()
    s.copyfile()

  

原文地址:https://www.cnblogs.com/chongyou/p/8336653.html