Vijay
Vijay

Reputation: 1817

Use Git commands within Python code

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?

Upvotes: 34

Views: 74052

Answers (7)

Catharina Oliveira
Catharina Oliveira

Reputation: 31

I had to use shlex on top of the run call because my command was too complex for the subprocess alone to understand.

import subprocess
import shlex
git_command = "git <command>"
subprocess.run(shlex.split(git_command))

Upvotes: 1

Trishayyyy
Trishayyyy

Reputation: 31

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.

Upvotes: 2

Joe Jacobs
Joe Jacobs

Reputation: 361

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.

Upvotes: 31

Ian Wetherbee
Ian Wetherbee

Reputation: 6109

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:

Upvotes: 54

Kenneth Hoste
Kenneth Hoste

Reputation: 2971

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

See here, for examples of how to use it.

Upvotes: 2

aychedee
aychedee

Reputation: 25569

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!

Upvotes: 25

user500944
user500944

Reputation:

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

Upvotes: -10

Related Questions