批量修改cvs下面所有Root文件的python脚本

CVS服务器端用户名和密码都变了,只能通过修改所有文件夹中的CVS下的Root才可以,手动用UltraEdit可以修改,但是用了一个下午和晚上,因为代码太多了,很想写个东西解决了,因为太急没有写。静下来居然没用多少时间就OK了,把代码贴下大家共享吧。希望别人不要像我浪费这么多时间。

原理:针对每个Root文件替换账户名称,利用临时文件

 

代码:

 import os

import sys

import shutil

from os.path import isdir

from os import listdir

 

root_old='abc'

root_new='def'

 

path='F://Code'

 

k=0

 

filelist=listdir(path)

filelist2=[]

for root,dirs,files in os.walk(path):

      for file in files:

          if file.find('Root')>=0:

            filelist2.append(file)

            k=k+1

            temp_name=root+'//Root.bak'

       source_name=root+'//'+file

     shutil.copy(source_name,temp_name)

src_file=open(temp_name)

dst_file=open(source_name,'w')

temp=src_file.read().replace(root_old,root_new)

dst_file.write(temp)

dst_file.close()

src_file.close()

os.remove(temp_name)

 

print('files count:',k)

 

 

使用时替换path和 root_old,root_new

 

以上代码很简单,不过功能还可以

#!/usr/bin/python #coding=gb18030 # ============================================== # autoupdate - automate update cvs and svn repositories # ============================================== # by liude67596@126.com # 23:29 2008-9-21 from os.path import basename, isdir from os import chdir, getcwd, listdir, system def traverse(path, depth=0): status = 255 if (depth > 5): print "depth = ", depth, " > 5" return 254 prefix = depth * '| ' + '|_' if (isdir(path)): print prefix, basename(path) logging.info(prefix + basename(path)) if (isdir(path + "/cvs")): status = cvsupdate(path) elif (isdir(path + "/.svn")): status = svnupdate(path) else: for item in listdir(path): traverse(path + '/' + item, depth + 1) print depth * '.', status return status def cvsupdate(path): _curpath = getcwd() chdir(path) status = system("cvs update") chdir(_curpath) return status def svnupdate(path): _curpath = getcwd() chdir(path) #system("TortoiseProc.exe /command:update /path:\"" + path + "\" /notempfile /closeonend:1") status = system("svn update") chdir(_curpath) return status # 执行shell命令的方式1 # os.system(my_command) // it return 0 or 1 .. 1 on failure # 执行shell命令的方式2 # (stdout, stderr) = popen2.popen3(my_command) # stdout.read() // console output # stderr.read() // error output if __name__ == '__main__': #Well, to get the path + script file do something like: import os, sys print os.path.join(sys.path[0], sys.argv[0]) import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s', filename='autoupdate.log', filemode='w') logging.debug('A debug message') logging.info('Some information') logging.warning('A shot across the bows') traverse('./')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值