#markdown测试
yangqian0817 / blog Goto Github PK
View Code? Open in Web Editor NEW11
11
#markdown测试
#coding=utf-8
import subprocess
import logging
import re
import uuid
import os
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
class p4:
class server:
def init(self,p4port,user,passwd):
self.p4port=p4port
self.user=user
self.passwd=passwd
def __init__(self,p4_server,work_space,client_view):
self.p4_server=p4_server
self.client_view=client_view
self.work_space=work_space
self.perforce_name = self.__get_perforce_name()
if os.path.exists(work_space) and os.path.isdir(work_space):
logging.info('Directory already exists without initialization...')
else:
os.makedirs(work_space)
#根据client名称生成配置文件唯一标识
def __get_perforce_name(self):
return str(uuid.uuid3(uuid.NAMESPACE_DNS,self.client_view))
#替换默认root地址为指定地址
def __replace_root_path(self):
f = open(self.perforce_name, 'r+')
regex = '^Root:\t([^\r\n]*)'
result=[]
pattern=re.compile(regex)
for line in f.readlines():
match=pattern.match(line)
if match:
logging.info(line)
default_path=match.group(1)
line=line.replace(default_path,self.work_space)
logging.info(line)
result.append(line)
f.seek(0, os.SEEK_SET)
f.writelines(result)
f.close()
def create(self):
template_base_cmd='p4 -p {0} -C utf8 -u {1} -P {2} client -o {3} > %s' % self.perforce_name
self.delete()
self.login()
logging.info('init perforce_config:' + self.perforce_name)
#生成client模板文件
template_cmd = template_base_cmd.format(self.p4_server.p4port, self.p4_server.user, self.p4_server.ticket, self.client_view)
return_code, stdin, stderr= self.execute_shell(template_cmd)
if return_code == 0:
#替换client模板文件中的默认路径为指定的路径
self.__replace_root_path()
#执行创建client命令
create_base_cmd = 'p4 -p {0} -C utf8 -u {1} -P {2} client -i < %s' % self.perforce_name
create_cmd = create_base_cmd.format(self.p4_server.p4port, self.p4_server.user, self.p4_server.ticket)
return_code, stdin, stderr = self.execute_shell(create_cmd)
if return_code == 0:
return True
else:
logging.info('create:' + stdin)
logging.info('create:' + stderr)
raise RuntimeError('create p4 client[%s] failed:' % self.client_view)
else:
logging.info('create:'+stdin)
logging.info('create:' + stderr)
raise RuntimeError('init p4 client[%s] template failed:'%self.client_view)
def delete(self):
base_cmd = 'p4 -p {0} -C utf8 -u {1} -P {2} client -d {3} '
self.login()
cmd=base_cmd.format(self.p4_server.p4port,self.p4_server.user,self.p4_server.ticket,self.client_view)
return_code,stdin,stderr=self.execute_shell(cmd)
#如果删除成功或者当前client不存在则返回True,负责抛出异常
if return_code==0 or 'doesn\'t exist' in stderr:
return True
else:
logging.info('delete stdin:' + stdin)
logging.info('delete stderr:' + stderr)
raise RuntimeError(('delete p4 client[%s] failed:' % self.client_view) + stdin)
def login(self):
base_cmd = 'p4 -p {0} -C utf8 -u {1} login -p'
cmd = base_cmd.format(self.p4_server.p4port, self.p4_server.user)
pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE,
shell=True, universal_newlines=True)
result=''
#因为此命令执行后未为交互命令,无换行符则按一个字符依次读取到'Enter password: '为止
while pipe.poll() is None:
result += pipe.stdout.read(1)
if result == 'Enter password: ':
pipe.stdin.write(self.p4_server.passwd+'\r\n')
pipe.stdin.flush()
result=None
break
# 通过正则匹配到Ticket
while pipe.poll() is None:
line=pipe.stdout.readline().strip()
if result!= '' and re.match('[0-9a-zA-Z]+',line):
result=line
if result ==None :
raise RuntimeError('create p4 client[%s] failed:'%self.client_view + line)
else:
self.p4_server.ticket=result
def sync(self,path,label=None,force=True,thread_num=10):
base_cmd = 'p4 -p {0} -C utf8 -u {1} -P {2} -c {3} sync {4} {5}'
sync_option='--parallel='+str(thread_num)
sync_path=path
if force:
sync_option+=' -f'
if label:
sync_path+='@'+label
sync_path+='...'
cmd = base_cmd.format(self.p4_server.p4port, self.p4_server.user,self.p4_server.ticket,self.client_view,sync_option,sync_path)
return_code, stdin, stderr = self.execute_shell(cmd)
# 如果删除成功或者当前client不存在则返回True,负责抛出异常
if return_code == 0:
logging.info(path+('@'+label if label else'')+ 'sync success!')
return True
else:
logging.info('sync stdin:' + stdin)
logging.info('sync stderr:' + stderr)
raise RuntimeError(('delete p4 client[%s] failed:' % self.client_view) + stdin)
def execute_shell(self,shell):
print 'execute shell:' + shell
pipe = subprocess.Popen(shell, stdout=subprocess.PIPE, stdin=subprocess.PIPE,stderr=subprocess.PIPE,shell=True, universal_newlines=True)
returncode = 0
lines = []
while pipe.poll() is None:
line = pipe.stdout.readline()
line = line.strip()
if line != None and len(line) != 0:
line = line + '\n'
lines.append(line)
stdin = ''.join(lines)
stderr = ''.join(pipe.stderr.readlines())
if pipe.returncode == 0:
returncode = 0
else :
returncode = 1
return returncode, stdin,stderr
if name == 'main':
p4_server=p4.server('lfghelix03-ap:1666','pCituEngDeskTop','JP8q#HB5')
p4=p4(p4_server,'E:\WorkSpace\p4_test2','my_perforce')
logging.info(p4.create())
logging.info(p4.sync('//HW-APK/IRD/ChnTelecomAutoReg/EMUI9.0-Dev-ALL-Global-Full-None/'))
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.