svn批量的添加ignore

svn没有批量添加ignore file的功能,只能一个个目录去设置,故写了脚本自动化的执行

使用了svn propset,故改了ignore file,可以反复的执行此脚本

 

#!/usr/bin/python
# -*- coding: utf8 -*-
# antsmallant 2018.09.05

import time
import os
import sys
import commands

def print_usage():
    myname = os.path.basename(sys.argv[0])
    help = '''
#######################################################
usage  : {0} ignore_file
e.g    : {0} ../server/ignore.txt

ignore_file looks like:
bin/3rd/a
bin/b
bin/c/d/e


#######################################################
'''.format(myname)
    print(help)

#exe shell cmd, return True if no error, otherwise False
def exe_cmd(cmdstr):
    print("exe_cmd, cmdstr={0}".format(cmdstr))
    status, output = commands.getstatusoutput(cmdstr)
    if status != 0:
        print("exe_cmd fail, error={0}, errorcode={1}, cmdstr={2}".format(output, status>>8, cmdstr))
        return False
    print("exe_cmd success, output={0}".format(output))
    return True

#retrun dict 
def parse_ignore_file(file_path):
    f = open(file_path, "r")
    allline = f.readlines()
    f.close()
    ret = {}
    for line in allline:
        line = line.strip()
        line = line.replace("\n", "")
        line = line.replace("\r", "")
        if len(line) == 0:
            continue
        dn = os.path.dirname(line)
        bn = os.path.basename(line)
        if ret.has_key(dn):
            ret[dn].append(bn)
        else:
            ret[dn] = []
            ret[dn].append(bn)
    return ret

def set_ignore(dn, filelist):
    assert len(filelist) > 0, "filelist has no data"+str(filelist)
    print("set_ignore" + dn)
    #make temp ignore file
    temp_ignore = os.path.join(dn, "temp_ignore.txt")
    f = open(temp_ignore, "w")
    if not f:
        return False
    for i in filelist:
        f.write(i+"\n")
    f.flush()
    f.close()
    cmd = "cd {0} && svn up && svn propset svn:ignore ./ -F {1}".format(dn, temp_ignore)
    ok = exe_cmd(cmd)
    if not ok:
        os.remove(temp_ignore)
        return False
    cmd = "cd {0}/../ && svn up && svn ci -m 'add ignore' ./".format(dn)
    ok = exe_cmd(cmd)
    if not ok:
        os.remove(temp_ignore)
        return False
    os.remove(temp_ignore)
    return True


def main():
    if len(sys.argv) < 2:
        print("err: arg not given")
        print_usage()
        return False
    ignore_file = os.path.abspath(sys.argv[1])
    print("ignore_file: " + ignore_file)
    if not os.path.exists(ignore_file):
        print("ignore_file not exists")
        return False
    dn = os.path.dirname(ignore_file)
    print("dirname: " + dn)
    file_2_ignore = parse_ignore_file(ignore_file)
    for k, v in file_2_ignore.items():
        ok = set_ignore(os.path.join(dn, k), v)
        if not ok:
            print("something wrong when doing: "+k+" "+str(v))
            return False
    return True




if __name__ == '__main__':
    main()

 

posted @ 2018-09-05 11:31  antsmallant  阅读(654)  评论(0编辑  收藏  举报