Python提交文件到SVN服务器

Python提交文件到SVN服务器

代码

#!/usr/bin/env/ python
# -*- coding:utf-8 -*-

__author__ = 'shouke'

import subprocess
import os.path


class SVNClient:
    def __init__(self):
        self.svn_work_path = 'D:\svn\myfolder'
        if not os.path.exists(self.svn_work_path):
            print('svn工作路径:%s 不存在,退出程序' % self.svn_work_path)
            exit()
        self.try_for_filure = 1 # 提交失败,重试次数

    def get_svn_work_path(self):
        return self.svn_work_path

    def set_svn_work_path(self, svn_work_path):
        self.svn_work_path  = svn_work_path

    def update(self):
        args = 'cd /d ' + self.svn_work_path + ' & svn update'
        with subprocess.Popen(args, shell=True, universal_newlines = True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc:
            output = proc.communicate()
            print('执行svn update命令输出:%s' % str(output))
            if not output[1]:
                 print('svn update命令执行成功' )
                 return [True,'执行成功']
            else:
                print('svn update命令执行失败:%s' % str(output))
                return  [False, str(output)]

    def add(self, path):
        args = 'cd /d ' + self.svn_work_path + ' & svn add ' + path
        with subprocess.Popen(args, shell=True, universal_newlines = True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc:
            output = proc.communicate()
            print('执行svn add命令输出:%s' %  str(output))
            if not output[1] or ( not str(output) and str(output).find('is already under version control') != -1):
                 print('svn add命令执行成功' )
                 return [True,'执行成功']
            else:
                print('svn add命令执行失败:%s' % str(output))
                return  [False, 'svn add命令执行失败:%s' % str(output)]

    def commit(self, path):
        args = 'cd /d ' + self.svn_work_path + ' & svn commit -m "添加版本文件"' + path
        with subprocess.Popen(args, shell=True, universal_newlines = True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc:
            output = proc.communicate()
            print('执行svn commit命令输出:%s' % str(output))
            if not output[1]:
                 print('svn commit命令执行成功' )
                 return [True,'执行成功']
            else:
                print('svn commit命令执行失败,正在重试:%s' % str(output))
                if self.try_for_filure != 0:
                    self.commit(path)
                    self.try_for_filure = self.try_for_filure - 1
                return  [False, str(output)]



filepath_list = []

# 获取目标目录下的文件|子目录|子文件路径
def get_subdir_or_subfile_paths(dirpath, excludes):
    global  filepath_list
    if not os.path.exists(dirpath):
        print('路径:%s 不存在,退出程序' % dirpath)
        exit()
    elif not os.path.isdir(dirpath):
        print('路径:%s 不为目录' % dirpath)
        return  []

    for name in os.listdir(dirpath):
        for exclude in excludes.strip(',').split(','):
            if not name.endswith(exclude):
                full_path = os.path.join(dirpath, name)
                filepath_list.append(full_path)
                if os.path.isdir(full_path):
                    get_subdir_or_subfile_paths(full_path, exclude)

    return filepath_list

if __name__ == '__main__':
    svn_client = SVNClient()
    svn_client.update()
    dirpath = 'dirname'  # 'D:\svn\myfolder\dirname'
    if svn_client.add(dirpath)[0]:
        svn_client.commit(dirpath)

    dirpath = 'D:\svn\myfolder\dirname'  # ''
    # 传递每个文件、目录的绝对路径,确保重复执行时,给定目录下新增的文件也可以被提交
    paths = get_subdir_or_subfile_paths(dirpath, '.svn') # .svn文件需要被过滤掉,因为无法提交成功
    for path in paths:
        if svn_client.add(path)[0]:
            svn_client.commit(dirpath)

    filepath = 'myfile.txt' # 'D:\svn\myfolder\dirname\myfile.txt'
    if svn_client.add(filepath)[0]:
        svn_client.commit(filepath)

获取文件路径方式

# 获取上级目录
print(os.path.abspath(os.path.join(os.getcwd(), "..")))
# 获取上上级目录
print(os.path.abspath(os.path.join(os.getcwd(), "../..")))

使用思路

  • 配置SVN基础目录,获取当前目录
  • 定义空列表,以当前目录开始遍历,获取上一级目录,加入到列表中,直至上一级目录与基础目录一致
  • 将上述列表上传SVN
  • 将当前目录下所有文件上传SVN
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值