0

With below clone command:

git clone https://github.com/android/media-samples.git

If run it from terminal window, below text output to screen:

Cloning into 'media-samples'...
remote: Enumerating objects: 57, done.
remote: Counting objects: 100% (57/57), done.
remote: Compressing objects: 100% (26/26), done.
remote: Total 871 (delta 16), reused 47 (delta 15), pack-reused 814
Receiving objects: 100% (871/871), 11.93 MiB | 2.54 MiB/s, done.
Resolving deltas: 100% (246/246), done.

But if I run it with Popen as below:

import os
import subprocess

os.system("rm -rf media-samples")
cmd = "git clone https://github.com/android/media-samples.git"
print(cmd)
cmd = cmd.split(" ")
sp = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = sp.communicate()
out = out.decode()
err = err.decode()
if out:
    print("standard output of subprocess:")
    print(out)
if err:
    print("standard error of subprocess:")
    print(err)
print("returncode of subprocess:")
print(sp.returncode)

I got below output:

git clone https://github.com/android/media-samples.git
standard error of subprocess:
Cloning into 'media-samples'...

returncode of subprocess:
0

Lots of output text missing (see below):

remote: Enumerating objects: 57, done.
remote: Counting objects: 100% (57/57), done.
remote: Compressing objects: 100% (26/26), done.
remote: Total 871 (delta 16), reused 47 (delta 15), pack-reused 814
Receiving objects: 100% (871/871), 11.93 MiB | 2.54 MiB/s, done.
Resolving deltas: 100% (246/246), done.

The code has logic for stdout and stderr already!

Captain Jack Sparrow
  • 971
  • 1
  • 11
  • 28
lucky1928
  • 8,708
  • 10
  • 43
  • 92

1 Answers1

1

Git checks if stderr goes to terminal before it prints the progress lines. When you run the command directly under the terminal the check returns true and the lines are printed, but when you call it via Popen() the check returns false and the lines are not printed.

The lines starting with remote: originate from this code, which calls the progress display function containing this code, which in turn uses the check is_foreground_fd() defined here.

md2perpe
  • 3,372
  • 2
  • 18
  • 22