I am trying to call "git clone" command in Python. I hope the Python script can display the GIT command output to the screen the same as running it in a terminal. For example the percentage information of the clone process. Is it there anyway to do it in Python?
Asked
Active
Viewed 1,821 times
1 Answers
0
Take a look at Python's sub process module, you can capture the output to a variable and then work with it. There are ways to intercept steer and stout. It's available as of 2.4 and should do the trick. I've used this for scripts running system commands at capturing the output for things at work. Reply if you need an example and I can dig one up from my work computer tomorrow morning...
http://docs.python.org/2/library/subprocess.html
try:
# Needs to all be one argument for acme command...
cmd = ["acme nw -proj " + self.based_on]
p = subprocess.Popen(cmd,
cwd=place,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output, error_msgs = p.communicate()
except (OSError) as err:
sys.stdout.write("ERROR {0} ({1})\n".format(err.strerror,
err.errno))
sys.exit(err.errno)
if len(error_msgs) > 0 and error_msgs != "Scanning the source base and preparing the workspace, please wait ...\n":
sys.stdout.write("There were errors, during view create\n")
sys.stdout.write(error_msgs)
else:
sys.stdout.write("SUCCESS\n")

pcm
- 397
- 6
- 12
-
I have read the document. And tried the solutions in the duplicated questions. But nothing really work properly. I can only get one line of git clone command output. All the statistic information are missing from git command output. – enchanter Dec 17 '13 at 10:13
-
For example:
I can only catch 1 line
>>> Cloning into 'suba' ...
But the output from the terminal should be:
Cloning into 'suba'... remote: Counting objects: 6, done. remote: Compressing objects: 100% (3/3), done. remote: Total 6 (delta 0), reused 0 (delta 0) Receiving objects: 100% (6/6), done. Checking connectivity... done.
– enchanter Dec 17 '13 at 00:14