python 如何在python中从perforce检出文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/184187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I check out a file from perforce in python?
提问by Matt Price
I would like to write some scripts in python that do some automated changes to source code. If the script determines it needs to change the file I would like to first check it out of perforce. I don't care about checking in because I will always want to build and test first.
我想用 python 编写一些脚本来对源代码进行一些自动更改。如果脚本确定它需要更改文件,我想首先从 perforce 中检查它。我不在乎签到,因为我总是想先构建和测试。
回答by Troy J. Farrell
Perforce has Python wrappers around their C/C++ tools, available in binary form for Windows, and source for other platforms:
Perforce 在其 C/C++ 工具周围有 Python 包装器,可用于 Windows 的二进制形式,以及用于其他平台的源代码:
http://www.perforce.com/perforce/loadsupp.html#api
http://www.perforce.com/perforce/loadsupp.html#api
You will find their documentation of the scripting API to be helpful:
您会发现他们的脚本 API 文档很有帮助:
http://www.perforce.com/perforce/doc.current/manuals/p4script/p4script.pdf
http://www.perforce.com/perforce/doc.current/manuals/p4script/p4script.pdf
Use of the Python API is quite similar to the command-line client:
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.']
>>>
I'll verify it from the command line:
我将从命令行验证它:
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
回答by Matt Price
Here's what I came up with:
这是我想出的:
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
回答by Syeberman
Perforce's P4 Python modulementioned in another answer is the way to go, but if installing this module isn't an option you can use the -G flag to help parse p4.exe output:
另一个答案中提到的Perforce 的 P4 Python 模块是可行的方法,但如果安装此模块不是一个选项,您可以使用 -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.
回答by Epu
Building from p4python source requires downloading and extracting the p4 api recommended for that version. For example, if building the Windows XP x86 version of P4Python 2008.2 for activepython 2.5:
从 p4python 源代码构建需要下载并解压为该版本推荐的 p4 api。例如,如果为 activepython 2.5 构建 Windows XP x86 版本的 P4Python 2008.2:
- download and extract both the p4pythonand p4api
- fixup the setup.cfg for p4python to point to the p4api directory.
To open files for edit (do a checkout), on the command line, see 'p4 help open'.
要打开文件进行编辑(进行签出),请在命令行上查看“p4 帮助打开”。
You can check out files without making a changelist if you add the file to the default changelist, but it's a good idea to make a changelist first.
如果将文件添加到默认更改列表,则无需创建更改列表即可检出文件,但最好先创建更改列表。
P4Python doesn't currently compile for activepython 2.6 without visual studio 2008; the provided libs are built with 2005 or 2003. Forcing p4python to build against mingw is nearly impossible, even with pexports of python26.dll and reimp/reassembly of the provided .lib files into .a files.
P4Python 目前不能在没有 Visual Studio 2008 的情况下为 activepython 2.6 编译;提供的库是用 2005 或 2003 构建的。强制 p4python 针对 mingw 构建几乎是不可能的,即使使用 python26.dll 的 pexports 并将提供的 .lib 文件重新导入/重组为 .a 文件也是如此。
In that case, you'll probably rather use subprocess, and return p4 results as marshalled python objects. You can write your own command wrapper that takes an arg array, constructs and runs the commands, and returns the results dictionary.
在这种情况下,您可能更愿意使用子进程,并将 p4 结果作为编组的 Python 对象返回。您可以编写自己的命令包装器,它接受一个 arg 数组,构造和运行命令,并返回结果字典。
You might try changing everything, testing, and on success, opening the files that are different with something equivalent to 'p4 diff -se //...'
您可以尝试更改所有内容,进行测试,并在成功时打开与“p4 diff -se //...”等效的文件不同的文件
回答by farhany
Remember guys to install the development package for Python for the p4api or it will complain about missing headers. In Ubuntu 10.10, just do a simple:
请记住,要为 p4api 安装 Python 的开发包,否则它会抱怨缺少标头。在 Ubuntu 10.10 中,只需做一个简单的:
apt-get install python2.6-dev
Or
或者
apt-get install python3.1-dev
回答by Nithin
You may want to check out the P4Python module. It's available on the perforce site and it makes things very simple.
您可能想查看 P4Python 模块。它在 perforce 站点上可用,它使事情变得非常简单。