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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/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('\n')
        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()