首页 > 解决方案 > p4python 创建并提交一个新文件

问题描述

如何使用 p4python 创建和提交新文件?

create_and_submit_file(full_path_in_depot, new_file_text_content):
    logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)
    p4 = get_p4() # implemented
    try:  # Catch exceptions with try/except
        connect_and_login_p4(p4) # implemented

        # .. implementation here .. p4.some_call() 

        LOGGER.info('done')
    except P4Exception:
        for e in p4.errors:  # Display errors
            LOGGER.error(e)

标签: perforcep4python

解决方案


如果该文件已经存在于工作空间内的本地文件系统中,那么您需要做的就是p4.run_add(file)p4.run_submit('-d', 'this is my awesome file').

如果文件不存在,您需要创建它,如果您没有工作区,您也需要创建它。为简洁起见,以下是您从命令行完全从头开始执行此操作的方法(这可以直接映射到 P4Python,但我对您的环境知之甚少,无法为您提供开箱即用的代码,所以我不会尝试翻译):

echo "my awesome file content" > my_awesome_file
p4 set P4CLIENT=my_awesome_client
p4 --field "View=//depot/... //my_awesome_client/..." client -o | p4 client -i
p4 add my_awesome_file
p4 submit -d "this is my awesome file"

查看示例p4.save_client以查看如何使用 P4Python 创建/修改客户端规范并修改字段以适应您的环境的简单示例(类似于我如何使用--field标志来设置Viewmy_awesome_client对应的//depot/...):

https://www.perforce.com/perforce/r14.2/manuals/p4script/python.p4.html#python.p4.save_spectype


推荐阅读