如何在python中查看perforce中的文件?

时间:2008-10-08 18:33:08

标签: python scripting perforce

我想在python中编写一些脚本,对源代码进行一些自动更改。如果脚本确定需要更改文件,我想首先将其从perforce中检出。我不关心办理登机手续,因为我总是想先建立和测试。

6 个答案:

答案 0 :(得分:20)

Perforce在他们的C / C ++工具中有Python包装器,可以用于Windows的二进制形式,以及其他平台的源代码:

http://www.perforce.com/perforce/loadsupp.html#api

您会发现他们的脚本API文档很有帮助:

http://www.perforce.com/perforce/doc.current/manuals/p4script/p4script.pdf

使用Python API与命令行客户端非常相似:

PythonWin 2.5.1 (r251:54863, May  1 2007, 17:47:05) [MSC v.1310 32 bit (Intel)] on win32.
Portions Copyright 1994-2006 Mark Hammond - see 'Help/About PythonWin' for further copyright information.
>>> import P4
>>> p4 = P4.P4()
>>> p4.connect() # connect to the default server, with the default clientspec
>>> desc = {"Description": "My new changelist description",
...         "Change": "new"
...         }
>>> p4.input = desc
>>> p4.run("changelist", "-i")
['Change 2579505 created.']
>>> 

我将从命令行验证它:

P:\>p4 changelist -o 2579505
# A Perforce Change Specification.
#
#  Change:      The change number. 'new' on a new changelist.
#  Date:        The date this specification was last modified.
#  Client:      The client on which the changelist was created.  Read-only.
#  User:        The user who created the changelist.
#  Status:      Either 'pending' or 'submitted'. Read-only.
#  Description: Comments about the changelist.  Required.
#  Jobs:        What opened jobs are to be closed by this changelist.
#               You may delete jobs from this list.  (New changelists only.)
#  Files:       What opened files from the default changelist are to be added
#               to this changelist.  You may delete files from this list.
#               (New changelists only.)

Change: 2579505

Date:   2008/10/08 13:57:02

Client: MYCOMPUTER-DT

User:   myusername

Status: pending

Description:
        My new changelist description

答案 1 :(得分:7)

以下是我提出的建议:

import os

def CreateNewChangeList(description):
    "Create a new changelist and returns the changelist number as a string"
    p4in, p4out = os.popen2("p4 changelist -i")
    p4in.write("change: new\n")
    p4in.write("description: " + description)
    p4in.close()
    changelist = p4out.readline().split()[1]
    return changelist

def OpenFileForEdit(file, changelist = ""):
    "Open a file for edit, if a changelist is passed in then open it in that list"
    cmd = "p4 edit "
    if changelist:
        cmd += " -c " + changelist + " "
    ret = os.popen(cmd + file).readline().strip()
    if not ret.endswith("opened for edit"):
        print "Couldn't open", file, "for edit:"
        print ret
        raise ValueError

答案 2 :(得分:4)

另一个答案中提到的

Perforce's P4 Python module是要走的路,但是如果不能安装此模块,可以使用-G标志来帮助解析p4.exe输出:

p4 [ options ] command [ arg ... ]
    options:
            -c client -C charset -d dir -H host -G -L language
            -p port -P pass -s -Q charset -u user -x file
    The -G flag causes all output (and batch input for form commands
    with -i) to be formatted as marshalled Python dictionary objects.

答案 3 :(得分:3)

从p4python源构建需要下载并解压缩为该版本推荐的p4 api。例如,如果为activepython 2.5构建适用于P4Python 2008.2的Windows XP x86版本:

  • 下载并解压缩 p4python和 p4api
  • 将p4python的setup.cfg修复为 指向p4api目录。

要打开要编辑的文件(执行结帐),请在命令行中查看“p4 help open”。

如果您将文件添加到默认更改列表,则可以在不创建更改列表的情况下签出文件,但最好先制作更改列表。

P4Python目前没有为visual studio 2008编译activepython 2.6;所提供的库是用2005或2003构建的。强制p4python构建针对mingw几乎是不可能的,即使使用python26.dll的pexport和提供的.lib文件的reimp / reassembly为.a文件。

在这种情况下,您可能更愿意使用子进程,并将p4结果作为编组的python对象返回。您可以编写自己的命令包装器,它接受一个arg数组,构造并运行命令,并返回结果字典。

您可以尝试更改所有内容,测试并成功,打开与“p4 diff -se // ...”等同的文件不同的文件。

答案 4 :(得分:2)

您可能想要查看P4Python模块。它可以在perforce网站上找到,它使事情变得非常简单。

答案 5 :(得分:2)

请记住,为p4api安装Python的开发包,否则会抱怨缺少标题。在Ubuntu 10.10中,只需做一个简单的操作:

apt-get install python2.6-dev

或者

apt-get install python3.1-dev
相关问题