ソースコードに自動変更を加えるスクリプトをPythonで作成したいと思います。スクリプトがファイルを変更する必要があると判断した場合は、最初にPERFORCEからファイルをチェックアウトしたいと思います。私は常に最初にビルドしてテストしたいので、チェックインは気にしません。
6 に答える
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
これが私が思いついたものです:
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 用に P4Python 2008.2 の Windows XP x86 バージョンをビルドする場合:
コマンド ラインで編集 (チェックアウトを実行) するためにファイルを開くには、「p4 help open」を参照してください。
ファイルをデフォルトの変更リストに追加すると、変更リストを作成せずにファイルをチェックアウトできますが、最初に変更リストを作成することをお勧めします。
P4Python は現在、Visual Studio 2008 がないと、activepython 2.6 用にコンパイルされません。提供されているライブラリは 2005 または 2003 でビルドされています。ping26.dll の pexports と提供された .lib ファイルの .a ファイルへの再実装/再アセンブリを使用しても、p4python を強制的に mingw に対してビルドすることはほぼ不可能です。
その場合、おそらくサブプロセスを使用し、p4 の結果をマーシャリングされた Python オブジェクトとして返します。arg 配列を受け取り、コマンドを構築して実行し、結果の辞書を返す独自のコマンド ラッパーを作成できます。
すべてを変更してテストし、成功したら、「p4 diff -se //...」と同等のもので異なるファイルを開くことができます。
P4Python モジュールをチェックアウトすることをお勧めします。これは perforce サイトで入手でき、作業が非常に簡単になります。
p4api 用の Python の開発パッケージをインストールすることを忘れないでください。インストールしないと、ヘッダーが見つからないというエラーが表示されます。Ubuntu 10.10 では、次のように簡単に実行します。
apt-get install python2.6-dev
または
apt-get install python3.1-dev