P4(Perforce): p4python实现同步数据到本地(二)

P4(Perforce): p4python实现同步数据到本地(二)

一、问题(p4 client 无法通过命令修改Client Root)

  1. 环境:centos 7, p4
  2. 现象:p4 client 无法通过命令修改Client Root,只能通过交互界面修改。
  3. 方案:复制已有的 workspace到一个临时的 (temp) workspace并同步到本地

二、解决方案:p4python

import os
import shutil
import uuid

from P4 import P4, P4Exception


def sync_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)


def temp_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:
        if not 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()

你可能感兴趣的:(Docker,CI:持续集成,Python)