Git から最新のコードを取得し、ビルドを作成し、いくつかの自動化された単体テストを実行するスクリプトを作成するように依頼されました。
Git と対話するための 2 つの組み込み Python モジュールがすぐに利用できることがわかりました:GitPython
とlibgit2
.
どのアプローチ/モジュールを使用すればよいですか?
より簡単な解決策は、Pythonsubprocess
モジュールを使用して git を呼び出すことです。あなたの場合、これは最新のコードをプルしてビルドします:
import subprocess
subprocess.call(["git", "pull"])
subprocess.call(["make"])
subprocess.call(["make", "test"])
ドキュメント:
私はイアン・ウェザビーに同意します。subprocess を使用して git を直接呼び出す必要があります。コマンドの出力に対して何らかのロジックを実行する必要がある場合は、次のサブプロセス呼び出し形式を使用します。
import subprocess
PIPE = subprocess.PIPE
branch = 'my_branch'
process = subprocess.Popen(['git', 'pull', branch], stdout=PIPE, stderr=PIPE)
stdoutput, stderroutput = process.communicate()
if 'fatal' in stdoutput:
# Handle error case
else:
# Success!
Linux または Mac を使用している場合、なぜこのタスクに Python を使用するのでしょうか? シェルスクリプトを書きます。
#!/bin/sh
set -e
git pull
make
./your_test #change this line to actually launch the thing that does your test