来点PERFORCE 应用实例。
function : 把当前workspace中的所有文件(local file & P4 file)打成label
cmd: p4 labelsync , p4 shelve, p4 unshelve, p4 revert , p4 change , p4 info, p4 changes, p4 label
#!/usr/bin/env python3
import sys,time,shutil,getopt
import os
import subprocess
import re,datetime
form threading import Thread
label_user = "";
def get_options():
global label_user;
try:
opts,args = getopt.getopt(sys.argv[1:],"hl:",["help","label="])
except getopt.GetoptError:
print_help()
sys.exit()
for op,para in opts:
if op in("-h","--help"):
print_help()
sys.exit()
if op in ("-l","--label"):
label_user = '.'+ para
get_options();
cmd = 'p4 info | perl -ne "print if s/Client root: //"'
root = os.popen(cmd).read().replace('\n','')
root_path = root + '/...'
cmd = 'p4 info | perl -ne "print if s/Client name: //"'
client = os.popen(cmd).read().replace('\n','')
pwd_path = root + '/' + ENV["MID_LEVEL"]
pwd_path_tmp = pwd_path.replace('/','\/')
user_name = os.popen("echo $P4USER").read.replace('\n','')
proj_name = os.popen("echo $USER").read.replace('\n','')
date = datetime.datetime.now().strftime('%F %T')
date_str = date.replace(' ','.')
label_name = 'label'+ '.' + proj_name + '.' + date_str + label_user
proj_sim_path = os.getenv('SIMENV_SIM')
cmd = 'p4 changelists -c %s -s pending' % client #查看现有的pending list
chg_lst_queue = []
cmd_result_queue = os.popen(cmd).readlines()
for i in range(0,len(cmd_result_queue)):
cmd_result = cmd_result_queue[i].replace('\n','')
change_num = re.match('\w+\s(\d+)\s',cmd_result.group(1))
chg_lst_queue.append(change_num)
def father_thread():
global script_done;
global flag;
script_done = 0
flag = 0
os.chdir(pwd_path)
cmd_queue = []
cmd_queue.append('p4 label -o %s > label.txt' % label_name)#create label
cmd_queue.append('p4 label -i < label.txt')
cmd_queue.append('rm -rf label.txt')
cmd_queue.append('p4 labelsync -l %s %s >/dev/null' %(label_name,root_path))#把当前workspace中在P4上的文件打入label
cmd_queue.append('p4 change -o > change_spec.txt')#创建一个新的change list
cmd_queue.append('sed "s/<.*>/<%s>/" -i change_spec.txt')#把描述改为label name
cmd_queue.append('p4 change -i < change_spec.txt')
cmd_queue.append('rm -rf change_spec.txt')
cmd_queue.append('p4 changes -m 1 -u %s' % user_name)#8 , 找到最新的change list
cmd_queue.append('find . -type f | grep -v '.*\/\..*'>%s/all_files.sh' %proj_sim_path) #找到当前workspace下的所有文件,不包括隐藏文件
cmd_queue.append('chmod +x %s/all_files.sh'%proj_sim_path)
cmd_queue.append('%s/all_files.sh' %proj_sim_path)
cmd_queue.append('rm -rf %s/all_files.sh ' % proj_sim_path)#12
for i in range(0,len(cmd_queue)):
if(i==8):
latest_changes = os.popen(cmd_queue[i]).read().replace('\n','')
matchobj = re.match('\w+\s(\d+)\s',latest_changes)
shelve_number = matchobj.group(1)
chg_lst_queue.append(shelve_num)
flag = 1
os.system('p4 edit -c %s %s >/dev/null'%(shelve_number,root_path))
os.system('p4 revert -a -c %s >/dev/null'%shelve_number)# revert unchanged file
elif(i==9):#通过P4 add 把所有local file 加入最新的change list
os.system(cmd_queue[i])
os.system("sed -i \'s/^./p4 add -c %s %s/\' %s/all_files.sh" % (shelve_number,pwd_path_tmp,proj_sim_path))
os.system("sed -i 's/$/ >\/dev\/null /' %s/all_files.sh" % proj_sim_path)
elif(i==12):
script_done = 1
os.system(cmd_queue[i])
for j in range(0,len(chg_lst_queue)): # 把所有pending list 进行shelve
os.system('p4 shelve -c %s -f' %chg_lst_queue[i])
else:
os.system(cmd_queue[i])
file_for_de = "%s/file_for_de_%s" %(proj_sim_path,shelve_number)
if(os.path.exists(file_for_de)):
os.remove(file_for_de)
with open(file_for_de,'a') as f:
f.write("#!/usr/bin/env python3\n")
f.write("import os\n")
f.write("os.system('p4 sync @%s >/dev/null')\n" % label_name)
for i in range(0,len(chg_lst_queue)):
f.write("os.system('p4 unshelve -s %s -f')" %chg_lst_queue[i] + "\n" )
f.write("cmd = 'p4 info | perl -ne "print if s/Client root: //\"'"+"\n")
f.write("root = os.popen(cmd).read().replace('\\n','')"+ "\n")
f.write("root_path = root + '/...'"+ "\n")
#f.write("os.system('p4 revert -k %s ' %root_path')")
os.system('chmod 777 %s' %file_for_de)
file_for_dv = "%s/file_for_dv_%s" %(proj_sim_path,shelve_number)
if(os.path.exists(file_for_dv)):
os.remove(file_for_dv)
with open(file_for_dv,'a') as f:
f.write("#!/usr/bin/env python3\n")
f.write("import os\n")
f.write("os.system('p4 shelve -d -c %s')" % shelve_number + "\n")
f.write("os.system('p4 revert -k -c %s //...')" % shelve_number + "\n")
f.write("os.system('p4 change -d %s')" % shelve_number + "\n")
f.write("os.system('p4 label -d %s')" % label_name +"\n")
f.write("os.system('rm -rf %s %s ') %(file_for_de,file_for_dv) +"\n")
os.system('chmod 777 %s' %file_for_dv)
def son_thread():
global script_done;
global flag;
cnt = 0
while(1):
if(script_done == 1 or cnt == 100):
exit(0)
else:
if(flag==1):
cnt= cnt+1
if(cnt == 1):
print("wait .....\n")
sys.stdout.write('> ')
sys.stdout.flush()
time.sleep(2)
t1 = Thread(target=father_thread)
t1.start()
t2 = Thread(target=son_thread)
t2.start()
perl -pi -e "s/you/me/g" file
-e 参数允许执行 Perl 代码
-p 参数确保处理文本的每一行并且将处理后的结果打印出来
-i 参数将修改的结果应用到文件中(所以看不到 -p 打印的内容了)
-n 表示遍历所有行(类似 -p 但不负责打印结果)
p4 revert
|
Revert only those files that haven't changed (in terms of content or filetype) since they were opened. |
|
Keep workspace files; the file(s) are removed from any changelists and Perforce records that the files as being no longer open, but the file(s) are unchanged in the client workspace. |