Pythonでperforceからファイルをチェックアウトするにはどうすればよいですか?
質問
Pythonでソースコードの自動変更を行うスクリプトを作成したいと思います。スクリプトがファイルを変更する必要があると判断した場合、最初にperforceからチェックアウトしたいと思います。私は常に最初にビルドとテストを行いたいので、チェックインを気にしません。
解決
Perforceには、Windowsのバイナリ形式で利用可能なC / C ++ツールのPythonラッパーと、他のプラットフォームのソースがあります:
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
他のヒント
ここに私が思いついたものがあります:
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
別の回答で言及されている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.
p4pythonソースからビルドするには、そのバージョンに推奨されるp4 apiをダウンロードして抽出する必要があります。たとえば、activepython 2.5用にWindows XP x86バージョンのP4Python 2008.2をビルドする場合:
編集のためにファイルを開く(チェックアウトを行う)には、コマンドラインで「p4 help open」を参照してください。
デフォルトのチェンジリストにファイルを追加すると、チェンジリストを作成せずにファイルをチェックアウトできますが、最初にチェンジリストを作成することをお勧めします。
P4Pythonは現在、Visual Studio 2008なしでactivepython 2.6用にコンパイルしません。提供されたライブラリは2005または2003でビルドされます。python26.dllのpexportsおよび提供された.libファイルの.aファイルへの再アセンブリ/再アセンブリーであっても、mingwに対してp4pythonを強制的にビルドすることはほとんど不可能です。
その場合、おそらくサブプロセスを使用し、マーシャルされたpythonオブジェクトとしてp4の結果を返します。 arg配列を受け取り、コマンドを構築して実行し、結果辞書を返す独自のコマンドラッパーを作成できます。
すべてを変更してテストし、成功したら、「p4 diff -se // ...」に相当するものとは異なるファイルを開くことができます
P4Pythonモジュールをチェックアウトすることもできます。 PERFORCEサイトで利用でき、非常にシンプルになります。
p4api用のPythonの開発パッケージをインストールすることを忘れないでください。そうしないと、ヘッダーの欠落について不満を言うでしょう。 Ubuntu 10.10では、次の簡単な操作を行います。
apt-get install python2.6-dev
または
apt-get install python3.1-dev