conda-pack icon indicating copy to clipboard operation
conda-pack copied to clipboard

WISH: Output messages and progress to stderr (instead of stdout)

Open HenrikBengtsson opened this issue 2 years ago • 4 comments

It looks like conda-pack outputs messages and progress to standard output;

$ conda activate myenv
(myenv) $ conda-pack --version
conda-pack 0.7.0
(myenv) $ conda-pack > stdout 2> stderr
(myenv) $ ls -l stdout stderr
-rw-rw-r-- 1 hb hb     0 Apr 14 12:08 stderr
-rw-rw-r-- 1 hb hb 20171 Apr 14 12:09 stdout
(myenv) $ cat stdout
Collecting packages...
Packing environment at '/home/hb/.conda/envs/myenv' to 'myenv.tar.gz'
[########################################] | 100% Completed | 30.0s

I think it would be more in line with the Unix-philosophy for such output, meant for human consumption, to go to standard error, and leave output to stdout to be consumed by software, e.g. via Unix pipe and likes. This way, you can safely do things such as:

mytool() {
  conda-pack
  echo "DONE"
}
$ res=$(mytool)
Collecting packages...
Packing environment at '/home/hb/.conda/envs/myenv' to 'myenv.tar.gz'
[########################################] | 100% Completed | 30.0s
$ echo "res=$res"
DONE

As it is now, res contains also those messages and the progress bar.

HenrikBengtsson avatar Apr 14 '22 19:04 HenrikBengtsson

Forgot to say, in case someone runs into this, a workaround is to redirect stdout ("1") to stderr ("2") using:

$ conda-pack 1>&2

Proof:

(myenv) $ (conda-pack 1>&2) > stdout 2> stderr
(myenv) $ ls -l stdout stderr
-rw-rw-r-- 1 hb hb 18835 Apr 14 12:49 stderr
-rw-rw-r-- 1 hb hb     0 Apr 14 12:48 stdout

HenrikBengtsson avatar Apr 14 '22 19:04 HenrikBengtsson

Smarter applications will also output different things depending on whether output is printed to the terminal or piped/redirected:

Normal behavior

$ git clone [email protected]:conda/conda-pack.git
Cloning into 'conda-pack'...
remote: Enumerating objects: 2010, done.
remote: Counting objects: 100% (521/521), done.
remote: Compressing objects: 100% (202/202), done.
remote: Total 2010 (delta 292), reused 464 (delta 281), pack-reused 1489
Receiving objects: 100% (2010/2010), 849.49 KiB | 404.00 KiB/s, done.
Resolving deltas: 100% (1303/1303), done.

Piped behavior

$ git clone [email protected]:conda/conda-pack.git > stdout 2> stderr
$ cat stdout
$ cat stderr
Cloning into 'conda-pack'...

kenodegard avatar Apr 26 '22 07:04 kenodegard