CVS 文件自动移 tag 的 Python 脚本

CVS 文件自动移 tag 的 Python 脚本

背景

工作中使用的版本管理工具是 CVS,在两次发布中,如果修改的文件比较少,会选择用移 Tag 的方式来生成一个新 Tag 发布。文件比较少的情况下直接使用 CVS 客户端完成移 tag 操作。但是文件比较多的情况下就要考虑使用脚本来完成这个任务了。

首先,关于 CVS tag 的一些概念可以参考这篇文章 cvs tag 与 rtag, tag 与 branch 区别

命令

然后再弄清楚 CVS 移 tag 的命令。可以直接查阅 CVS 的文档 Modifying-tags
然后来看一个实例:
有一个文件名为 invoiceListingInc.jsp 所在路径是 Betty/jsp/invoiceListingInc.jsp,所在分支是 BR230_201411191736 其中的一个版本号是 1.1.2.2.14.4。本次需要移动的 tag 是 FAS_2_3-124

  • 把 tag 移动指定版本 1.1.2.2.14.2 上的命令是:
 cvs rtag -r 1.1.2.2.14.2 -F FAS_2_3-124 Betty/jsp/invoiceListingInc.jsp
  • 把 tag 移动指定分支 BR230_201411191736 上的最新版的命令是:
cvs rtag -r BR230_201411191736 -F FAS_2_3-124 Betty/jsp/invoiceListingInc.jsp

脚本

弄清楚了 CVS 的移 tag 命令,就可以开始写脚本了,脚本要做的工作就是处理一个文件列表,然后生成 CVS 命令并执行。Python 脚本如下:

# -- coding: utf-8 -- import sys
import os
import re
# 使用sys.stdin.readline()方法读入用户输入的一行

#分析可用的分支def showBranch(fileName, maps):
    file = open(fileName)
    index = 1
    while 1:
        line = file.readline()
        if not line:
            break
        print " %d : %s "%(index, line[:-1])
        maps[index] = line[:-1]
        index += 1#分析文件列表def showFile(fileName, maps, namelist, branch):
    file = open(fileName)
    while 1:
        line = file.readline()
        if not line:
            break
        text = line[:-1]
        text = text.replace('Betty/', '').replace('	', '')
        text = re.sub('s+', ' ', text)
        if len(text) > 1 :
            names = text.split(' ')
            fileName = 'Betty/' + names[0]
            namelist.append(fileName)
            if len(names) > 1 and len(names[1]) > 1 :
                maps[fileName] = names[1]
            else:
                maps[fileName] = branch

def arsk(title):
    if title:
        print title
    line = sys.stdin.readline()
    return line[:-1]

branchs = {}
branch = ''print 'Select Branch:'
showBranch("branchs.txt", branchs)
branchNum = int(arsk(''))
branch = branchs[branchNum]

print 'Input The Tag:'
line = sys.stdin.readline()
tag = line[:-1]

versions = {}
names = []
showFile("files.txt", versions, names, branch)
for key in names:
    print '%s;	version:%s;' %(key, versions[key])
print 'App will move these %d files to Tag : %s on Branch %s : ' % (len(names), tag, branch)
yes = arsk('Are you sure to do that ? y/n')
while 'y' not in yes and 'n' not in yes:
    yes = arsk('Are you sure to do that ? y/n')

if 'y' in yes:
    target = open('command.txt', 'w') #打开文件
    target.truncate()        #清空文件
    for key in names:
        cmd =  'cvs rtag -r %s -F %s %s' % (versions[key], tag, key)
        target.write(cmd)    #写入文件
        target.write('
')
        print cmd
        os.system(cmd)
    target.close()

脚本读取当前目录下的 file.txt 文件里的文件列表,使用 branchs.txt 文件来存储分支信息。如果需要移 tag 至指定版本号,则需要在 file.txt 里的文件名之后输入版本号。
在 CMD 输入输入

python movetag.py

来执行脚本。执行效果图如下:

最后输入 Y 确认移 tag 即可。

注意:移 tag 有风险,操作需谨慎。
另: 此脚本默认所在的运行环境是可以直接执行 cvs 命令的。 如果你对这个脚本不放心,可以注释掉自动执行 CVS 命令的那行脚本(倒数第二行):

#os.system(cmd)

这样就不会执行 CVS 的移 tag 命令了,而且脚本会在当前目录生成一个 command.txt 文件。里面就是处理好的 CVS 命令,你可以酌情自行执行。

完整的文件: 点击下载

原文地址:https://www.cnblogs.com/myfjd/p/4119323.html