1

我正在编写一个简单的脚本,它将通过 python 同步一个 perforce 目录但是我不能这样做。它不允许我通过脚本执行该 [perforce] 目录中的任何内容。有人可以建议我如何在该目录中运行 p4 login、p4 sync 等命令吗?

4

6 回答 6

4

perforce有一个官方支持的python API 。

您应该使用它而不是subprocess因为它允许您将 p4 连接视为对象并使用 API 操作它,而不是手动解析 p4 命令的输出。

于 2014-01-25T06:40:13.947 回答
3

在没有任何其他参数的情况下运行“p4 sync”将同步当前客户端工作区映射到当前目录的文件。从 Python 脚本运行命令时,您需要了解脚本实际运行的位置。

出于调试目的,请先尝试运行“p4 info”:它将显示当前工作目录是什么,以及向您显示 Perforce 环境的其余详细信息。

如果您总是希望脚本同步同一个目录,无论您在哪里运行它,您都可能需要考虑指定同步路径。例如:

  • 仓库语法:p4 sync //depot/path/to/dir/...
  • 本地语法 (Windows): p4 sync c:\users\user\path\to\dir...
  • 本地语法 (*nix): p4 sync /home/user/path/to/dir/...

如果您是客户端工作区,用户或 perforce 服务器配置由 P4CONFIG 基于目录配置或由 P4V 设置,您可能希望将这些设置的全局选项作为全局选项添加到命令中。例如:

  • p4 -p server:1666 -c client_ws -u 用户同步 //depot/path/to/dir/...

当您从 Python 运行命令时,您可能会发现 P4Python 脚本 API 使配置更容易: https ://www.perforce.com/perforce/r14.2/manuals/p4script/python.programming.html

于 2013-01-25T13:48:54.473 回答
2

如果 P4Python 比您想要处理的更多,Perforce Workshop 中有一个 Python 类,它提供了使用 P4Python 接口包装命令行的功能。

用于 Python 的 P4Python 式 CLI 包装器

于 2014-01-27T18:36:06.523 回答
1

I have had good success running Perforce commands from Python, using the ActiveState Python 2.7, with the P4Python plugin. You will need a p4 definition line, like the following:

p4Params = {'Port': "perforce_server_name:1666", \
            'Pass': "mypassword", \
            'User': "myname", \
            'Client': "myclient"}

Plus you'll need the P4 and P4Exception libraries:

from P4 import P4,P4Exception

With that in hand, you can perform most any Perforce command with the following construct. This function syncs to Rev #0 to remove all controlled files from some depot location "depotPath":

  def removeWorkFiles(depotPath,p4Params):
  """Tell Perforce to remove the version files from the workspace"""
    p4 = P4(client=p4Params['Client'], port=p4Params['Port'], password=p4Params['Pass'])
    p4.user = p4Params['perfUser']  
    try:
      p4.connect()                 # Connect to the Perforce Server
      p4.run_login()
      deletePath = depotPath + "#0"
      p4.run("sync", "-f", deletePath) # force to version "0", ie remove from workspace
      p4.disconnect()
    except P4Exception:
      for e in p4.errors:          # Display errors
        logging.error( e)

I highly recommend you read Perforce's P4 Python help page at:

https://www.perforce.com/perforce/r14.2/manuals/p4script/python.programming.html

This will give you some perspective, though it is not complete. Hope this helps

于 2013-10-29T16:23:19.087 回答
0

如果您知道可以在 CMD shell 中运行的确切 perforce 命令,则可以使用 os.system('cmd')。你也可以看看os.popensubprocess.call来做同样的事情。

于 2013-01-25T13:31:22.757 回答
0

有 p4swamp,它也是 CLI 的包装器

from p4swamp import p4

result = p4('sync')
print(result)
于 2018-10-12T17:19:54.900 回答