21
votes

I have been asked to write a script that pulls the latest code from Git, makes a build, and performs some automated unit tests.

I found that there are two built-in Python modules for interacting with Git that are readily available: GitPython and libgit2.

What approach/module should I use?

6

6 Answers

38
votes

An easier solution would be to use the Python subprocess module to call git. In your case, this would pull the latest code and build:

import subprocess
subprocess.call(["git", "pull"])
subprocess.call(["make"])
subprocess.call(["make", "test"])

Docs:

21
votes

I agree with Ian Wetherbee. You should use subprocess to call git directly. If you need to perform some logic on the output of the commands then you would use the following subprocess call format.

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!
12
votes

So with Python 3.5 and later, the .call() method has been deprecated.

https://docs.python.org/3.6/library/subprocess.html#older-high-level-api

The current recommended method is to use the .run() method on subprocess.

import subprocess
subprocess.run(["git", "pull"])
subprocess.run(["make"])
subprocess.run(["make", "test"])

Adding this as when I went to read the docs, the links above contradicted the accepted answer and I had to do some research. Adding my 2 cents to hopefully save someone else a bit of time.

3
votes

In EasyBuild, we rely on GitPython, and that's working out fine.

See here, for examples of how to use it.

0
votes

If GitPython package doesn't work for you there are also the PyGit and Dulwich packages. These can be easily installed through pip.

But, I have personally just used the subprocess calls. Works perfect for what I needed, which was just basic git calls. For something more advanced, I'd recommend a git package.

-8
votes

If you're on Linux or Mac, why use python at all for this task? Write a shell script.

#!/bin/sh
set -e
git pull
make
./your_test #change this line to actually launch the thing that does your test