Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trying to execute git command using python script

Tags:

git

python

I am trying to execute a simple git command using following python script.

#!/usr/bin/python

import commands
import subprocess
import os
import sys

pr = subprocess.Popen( "/usr/bin/git log" , cwd = os.path.dirname( '/ext/home/rakesh.kumar/workspace/myproject' ), shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE )
(out, error) = pr.communicate()


print "Error : " + str(error) 
print "out : " + str(out)

but I am getting the following error even though I am running the python script in the same directory where git reposetory is.

Error : fatal: Not a git repository (or any of the parent directories): .git

I suspected the git might got correputed, but git files are fine and git commands work if I execute on normal command prompt.

I tried to search on net but couldn't get useful information. Please help it will be greatly appreciated.

like image 445
Rakesh Avatar asked Mar 19 '12 17:03

Rakesh


2 Answers

The problem is your use of os.path.dirname():

os.path.dirname( '/ext/home/rakesh.kumar/workspace/myproject' )

will give you:

>>> os.path.dirname( '/ext/home/rakesh.kumar/workspace/myproject' )
'/ext/home/rakesh.kumar/workspace'

which, I bet, is not what you want.

like image 97
Santa Avatar answered Nov 20 '22 21:11

Santa


Try this:

#!/usr/bin/python

import commands
import subprocess
import os
import sys

pr = subprocess.Popen( "/usr/bin/git log" , cwd = os.path.dirname( '/ext/home/rakesh.kumar/workspace/myproject/' ), shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE )
(out, error) = pr.communicate()


print "Error : " + str(error) 
print "out : " + str(out)

Directory path should have '/' at the end.

like image 4
Adam Avatar answered Nov 20 '22 22:11

Adam