import os
import shutil
import uuid
from P4 import P4, P4Exception
defsync_workspace():""" sync from perforce"""
p4_workspace ="at_builder_01_dev_01"
p4_port ="p4d.xxx.com:1666"
p4_user ="at_builder_01"
p4_password ="xxx"
p4 = P4()
p4.port = p4_port
p4.user = p4_user
p4.password = p4_password
p4.connect()
temp_client(p4,"temp", p4_workspace)deftemp_client(p4, prefix, template):"""Creates a temporary workspace with a current root.
Will clean up the current root if exists.
And Will clean up client workspace after the block has finished.
The prefix is prepended to the workspace name and should be used in conjunction with
the SpecMap of a spec depot to avoid creating entries there.
"""
dst ="debug"
ws_dir ="DesignerWork"
name ="{prefix}_{id}".format(prefix=prefix,id=str(uuid.uuid1()))
ws = p4.fetch_client('-t', template, name)try:ifnot os.path.exists(dst):
os.mkdir(dst)
os.chdir(dst)if os.path.exists(ws_dir):
shutil.rmtree(ws_dir)
ws._root = os.getcwd()
p4.client = name
p4.save_client(ws)
p4.run_sync()except P4Exception:for e in p4.errors:print(e)finally:
p4.delete_client(name)if __name__ =='__main__':
sync_workspace()