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

在服务器中要做几个操作,使用命令操作 svn,svn 文件的创建,svn 文件更新,并把指定 demo 路径,移动到创建的文件夹中,进行提交,

 

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
85
86
87
88
89
90
# -*- coding:utf-8 -*-
import pysvn
import locale
import datetime
import os
import sys
 
def setlocale():
    language_code, encoding = locale.getdefaultlocale()
    if language_code is None:
        language_code = 'en_GB'
    if encoding is None:
        encoding = 'UTF-8'
    if encoding.lower == 'utf':
        encoding = 'UTF-8'
    locale.setlocale(locale.LC_ALL, '%s.%s' % (language_code, encoding))
 
 
def get_login(realm, username, may_save):
    return True, 'test', 'test', True
 
#获取svn地址,url指svn地址,path,指项目拉取到哪个地方
def svncheckout(url,path):
    client = pysvn.Client()
   # client.callback_get_login = get_login
    ret = client.checkout(url, path)
    print ret
 
#更新svn的地址
def svnupdate():
    client = pysvn.Client()
    ret = client.update(path)
    return ret
 
 
def svncheckin(url):
    client = pysvn.Client()
    #url=svnurl+"/"+projectname
   # os.makedirs(url)
    client.add(url)
    client.checkin(url,u'项目文件的创建')
 
 
#写入日志到本地,主要用于更新信息使用的
def svninfo(path):
    client = pysvn.Client()
    entry = client.info(path)
    Version = "Version: %s" % entry.commit_revision.number
    Author = "Author: %s" % entry.commit_author
    Update = "Update Date: %s" % str(datetime.datetime.fromtimestamp(entry.commit_time))[:-7]
    url=path + '\log.txt'
    print url
    f = file(url, 'a')
    f.write(Version + '\n' + Author + '\n' + Update + '\n' + '-' * 32 + '\n')
    f.close()
 
 
def copyFiles(sourceDir, targetDir):  #文件的复制
    if sourceDir.find(".svn") > 0:
        return
    for file in os.listdir(sourceDir):
        sourceFile = os.path.join(sourceDir, file)
        targetFile = os.path.join(targetDir, file)
        if os.path.isfile(sourceFile):
            if not os.path.exists(targetDir):
                os.makedirs(targetDir)
            if not os.path.exists(targetFile) or (
                os.path.exists(targetFile) and (os.path.getsize(targetFile) != os.path.getsize(sourceFile))):
                open(targetFile, "wb").write(open(sourceFile, "rb").read())
        if os.path.isdir(sourceFile):
            First_Directory = False
            copyFiles(sourceFile, targetFile)
 
def run(svnurl,path,projectname):
    #url为svn的仓库地址,path为本地路径,project为项目路径
   # svncheckout(svnurl, path)
    sourceDir=path+"/template"
    print sourceDir
    targetDir=path + "/" + projectname
    print targetDir
    copyFiles(sourceDir,targetDir)  #资源文件
    #svncheckin(targetDir)
 
 
 
if __name__ == "__main__":
    svnurl = "http://svn.egomsl.com/svn/repos/autotest.globalegrow.com/projectScript/uitest"
    #run(svnurl,"./script/ui",sys.argv[0]) #从控制台接收一个名称,进行文件夹的创建
    path='./script/ui';
    run(svnurl, path, sys.argv[0])