使用python 的svn,一

本文介绍了如何在服务器环境中利用Python进行SVN操作,包括创建 SVN 文件、更新文件、移动文件及提交更改。通过Python脚本自动化这些任务,提升工作效率。

在服务器中要做几个操作,使用命令操作svn,svn文件创建,svn文件更新,文件移动和提交

# -*- 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])
以下是几种使用 Python 编写 SVN diff 工具的方法: ### 方法使用`subprocess`模块调用`svn diff`命令 此方法通过`subprocess`模块调用系统的`svn diff`命令,并对可能出现的乱码问题进行处理。 ```python from urllib.parse import unquote import subprocess # 定义开始日期和 SVN 路径 startDate = "your_start_date" SVNpath = "your_svn_path" def getsvnfilelist(): svn_cmd_run = subprocess.run("svn diff -r %s --summarize %s" % (startDate, SVNpath), shell=True, stdout=subprocess.PIPE).stdout svn_cmd_stdout = unquote(svn_cmd_run.decode('gbk')) print(svn_cmd_stdout) getsvnfilelist() ``` 上述代码中,`startDate`和`SVNpath`需要根据实际情况进行替换。通过`subprocess.run`调用`svn diff`命令获取差异信息,然后使用`unquote`和`decode('gbk')`处理可能的乱码,最后将结果打印输出。 ### 方法二:使用`pysvn`库获取两个版本之间的 diff `pysvn`是个用于操作 SVNPython 库,可以直接在 Python 代码中操作 SVN 仓库获取差异信息。 ```python from pysvn import * # 定义 SVN 用户名、密码、前个版本号和本地路径 g_svnUserName = "your_username" g_svnPassword = "your_password" g_preVersion = 0 g_localPath = "your_local_path" client = Client() client.set_default_username(g_svnUserName) client.set_default_password(g_svnPassword) currentRevision = Revision(opt_revision_kind.committed) if g_preVersion == 0: olderRevision = Revision(opt_revision_kind.previous) else: olderRevision = Revision(opt_revision_kind.number, g_preVersion) versionDiff = client.diff(os.path.join(os.environ["temp"], "1.tmp"), g_localPath, olderRevision, g_localPath, currentRevision) ``` 上述代码中,`g_svnUserName`、`g_svnPassword`、`g_preVersion`和`g_localPath`需要根据实际情况进行替换。通过`pysvn.Client`创建SVN 客户端对象,设置用户名和密码,确定当前版本和前个版本,最后调用`client.diff`方法获取两个版本之间的差异信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值