0

I am currently working on GIT automation using sub process. This is new for me . Trying out a basic status call as shown below, but keep getting error.

from subprocess import Popen, PIPE
import os
import subprocess
PIPE = subprocess.PIPE
branch = "C:/git-repos/Myfolder"
process = subprocess.Popen(["git status", branch], stdout=PIPE, stderr=PIPE)
stdoutput, stderroutput = process.communicate()
if 'fatal' in stdoutput:print "error"
else:print "Successful"

ERROR message::

Traceback (most recent call last):
  File "gitscript.py", line 9, in <module>
process = subprocess.Popen(["git status", branch], stdout=PIPE, stderr=PIPE)
 File "C:\Python25\Lib\subprocess.py",
line 594, in __init__
errread, errwrite)
 File "C:\Python25\Lib\subprocess.py",
line 818, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

What am I doing wrong?. Note: Myfolder is a local GIT repo. It is NOT a clone of any master repo.

Updated my question after following below comments::

process = subprocess.Popen(["git status", branch], stdout=PIPE, stderr=PIPE, shell=True)
stdoutput, stderroutput = process.communicate()
if 'fatal' in stderroutput:print "error"
else:print "Successful"   

1) The output is "Successful" . Stdout print shows blank. while the command(git status) on bash works fine prints out the status.

2) As part of project I have to use Python25, Windows and GIT version is 1.9.

3) Since subprocess did not work . I tried to install Gitpython. Installation does not work for python25. "setup.py" file does not list python25 and keeps throwing error on missing import files.

Request for any ideas or suggestion on how to approach this issue. With current environment setup can I achieve automation using python?

Help Please !

smi
  • 31
  • 5
  • Check out the answers here - http://stackoverflow.com/questions/2400878/why-subprocess-popen-doesnt-work-when-args-is-sequence – Kamehameha Nov 16 '15 at 18:16
  • Basically it should work when you use -`["git", "status", branch]` since the first arg in a sequence is the command str and rest are args – Kamehameha Nov 16 '15 at 18:17
  • I had tried ["git", "status", branch] before posting my query and read through most of GIT related posts. Appreciate your response. But the above command throws the same error – smi Nov 16 '15 at 18:51
  • I don't think it's a git issue. Maybe you can try simply executing `["ls"]` first and see if the error is raised. Then `["git", "status"]` without the path and then work your way up from there – Kamehameha Nov 16 '15 at 19:00
  • I did the following it worked this time import subprocess branch = 'C:/git-repos/Myfolder' p = subprocess.Popen(['git', 'status', branch], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = p.communicate() if 'fatal' in out: print "error" else:print "Success" – smi Nov 16 '15 at 20:18
  • Hmm... did you try printing out the `stdoutput` variable? I don't think it does what you intend for it to do. Using `shell=True` and passing `["git", "status", branch]` to Popen will simply execute `git` and not `git status /some/path` – Kamehameha Nov 17 '15 at 04:35
  • you are right . any pointers on how to get it working . – smi Nov 17 '15 at 18:34
  • Try simple - `subprocess.Popen("git status ./", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)` (Warning: Using user ip with shell is not a good idea) – Kamehameha Nov 17 '15 at 18:53

0 Answers0