博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python 一键commit文件、目录到SVN服务器
阅读量:4315 次
发布时间:2019-06-06

本文共 3711 字,大约阅读时间需要 12 分钟。

一键commit文件、目录到SVN服务器

 

by:授客 QQ1033553122

 

 

实现功能

一键提交文件、目录到svn

 

测试环境

Win7 64

 

Python 3.3.2

 

TortoiseSVN 1.9.6-64 Bit

 

代码show

#!/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)
    # 报错
    # dirpath = 'd:/svn/dir_out_of_svn_workpath'
    # if svn_client.add(dirpath)[0]:
    #     svn_client.commit(dirpath)

 

注意:

例中,svn工作路径为:'D:\svn\myfolder',即执行checkout时选择的目录

1、只能添加并提交位于svn工作目录下文件目录,否则会报错,如下:

if __name__ == '__main__':

    svn_client = SVNClient()
    svn_client.update()
    dirpath = 'd:/svn/dir_out_of_svn_workpath'
    if svn_client.add(dirpath)[0]:
        svn_client.commit(dirpath)

 

 

 

2、如果未对给定目录执行过add类函数,那么执行add函数后,执行commit函数,将会把该目录下的文件、目录及其下子文件、子目录,一起提交svn;否则不会做任何提交操作;所以,给add传递参数,最好是通过遍历的方式,传递每个文件、目录的绝对路径。

 

 

3、安装svn时,第二项,必须选择图示红色选框框选项,否则运行会报错:svn不是内部或外部命令,也不是可运行的程序

Python <wbr>一键commit文件、目录到SVN服务器 

 

 

 

转载于:https://www.cnblogs.com/shouke/p/10157541.html

你可能感兴趣的文章
js算法之最常用的排序
查看>>
Python——交互式图形编程
查看>>
经典排序——希尔排序
查看>>
团队编程项目作业2-团队编程项目代码设计规范
查看>>
英特尔公司将停止910GL、915GL和915PL芯片组的生产
查看>>
团队编程项目作业2-团队编程项目开发环境搭建过程
查看>>
Stax解析XML示例代码
查看>>
cookie
查看>>
二级图片导航菜单
查看>>
<Using parquet with impala>
查看>>
OpenGL渲染流程
查看>>
委托异步回调
查看>>
扩展欧几里得算法
查看>>
いつでもどこでも本格的に麻雀&チュートリアルが充実!iPhone/iPod touch/iPad向け「雀龍門Mobile」をiPadで遊んでみました...
查看>>
如何重置mysql中的root密码
查看>>
bzoj 3171: [Tjoi2013]循环格 最小费用最大流
查看>>
关于IO的一些数字
查看>>
高放的c++学习笔记之模板与泛型编程
查看>>
bzoj 1089: [SCOI2003]严格n元树
查看>>
mybatis 日期比较
查看>>