[Tools] P4 애플리케이션

7445 단어 tools
PERFORCE 응용 사례를 살펴보겠습니다.
function: 현재 워크스페이스의 모든 파일 (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('
','') root_path = root + '/...' cmd = 'p4 info | perl -ne "print if s/Client name: //"' client = os.popen(cmd).read().replace('
','') pwd_path = root + '/' + ENV["MID_LEVEL"] pwd_path_tmp = pwd_path.replace('/','\/') user_name = os.popen("echo $P4USER").read.replace('
','') proj_name = os.popen("echo $USER").read.replace('
','') 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('
','') 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/<.>//" -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('
','') 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
") f.write("import os
") f.write("os.system('p4 sync @%s >/dev/null')
" % label_name) for i in range(0,len(chg_lst_queue)): f.write("os.system('p4 unshelve -s %s -f')" %chg_lst_queue[i] + "
" ) f.write("cmd = 'p4 info | perl -ne "print if s/Client root: //\"'"+"
") f.write("root = os.popen(cmd).read().replace('\
','')"+ "
") f.write("root_path = root + '/...'"+ "
") #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
") f.write("import os
") f.write("os.system('p4 shelve -d -c %s')" % shelve_number + "
") f.write("os.system('p4 revert -k -c %s //...')" % shelve_number + "
") f.write("os.system('p4 change -d %s')" % shelve_number + "
") f.write("os.system('p4 label -d %s')" % label_name +"
") f.write("os.system('rm -rf %s %s ') %(file_for_de,file_for_dv) +"
") 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 .....
") 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  -a
Revert only those files that haven't changed (in terms of content or filetype) since they were opened. -k
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.

좋은 웹페이지 즐겨찾기