Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable spec colorization when redirecting stdout and add command line flag to re-enable #3013

Merged

Conversation

paulhopkins
Copy link
Contributor

@paulhopkins paulhopkins commented Feb 3, 2017

Fixes #1369.
Fixes #4761.

This PR disables spec colorization when piping or redirecting stdout and adds a global option to either enable it again, or always disable it. Here is an example using find

$ spack find -lvf libxml2 | cat -v
-- linux-ubuntu16-x86_64 / gcc@5.4.0 ----------------------------
ivzisvs libxml2@2.9.4%gcc~python
vmkp7vv libxml2@2.9.4%gcc~python
4sypony libxml2@2.9.4%gcc+python
$ spack --color find -lvf libxml2 | cat -v
-- ^[[0;35mlinux-ubuntu16-x86_64^[[0m / ^[[0;32mgcc@5.4.0^[[0m ----------------------------
^[[0;90mivzisvs^[[0m libxml2^[[0;36m@2.9.4^[[0m^[[0;32m%gcc^[[0m^[[0;32m^[[0m^[[0;94m~python^[[0m
^[[0;90mvmkp7vv^[[0m libxml2^[[0;36m@2.9.4^[[0m^[[0;32m%gcc^[[0m^[[0;32m^[[0m^[[0;94m~python^[[0m
^[[0;90m4sypony^[[0m libxml2^[[0;36m@2.9.4^[[0m^[[0;32m%gcc^[[0m^[[0;32m^[[0m^[[0;94m+python^[[0m

For users who always prefer colored output then having command defaults, #2705, could be useful.

bin/spack Outdated
if args.color:
tty.set_color(True)
elif args.no_color:
tty.set_color(False)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about not args.color and not args.no_color? What is the default? You can actually make these flags mutually exclusive. See spack edit -c edit for an example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default is to colorize if stdout.isatty() is True, therefore the behaviour depends on whether you are piping. have redirected stdout, or not. The command line arguments override the default behaviour to either always or never colorize.

Good call on the mutually exclusive arguments, I have changed the code and pushed. I did try to copy the ls or grep style which allows --color or e.g. --color always but it always consumed the subcommand e.g. invalid choice: 'find' choose from: alway, auto, never. Let me know if this not appropriate.

@adamjstewart
Copy link
Member

Can you fix the flake8 warnings?

@paulhopkins paulhopkins force-pushed the features/disable_colours_when_piping branch from 67a77e3 to 87daa86 Compare March 3, 2017 08:55
@paulhopkins
Copy link
Contributor Author

@adamjstewart Fixed the flake8 warning, didn't show up offline. Thanks

@eschnett
Copy link
Contributor

Can we pull this request? It seems ready to go.

@adamjstewart
Copy link
Member

One last request. Since you're adding new flags, can you update our tab-completion script? Just add -c --color --no-color to https://github.com/LLNL/spack/blob/develop/share/spack/spack-completion.bash#L115.

@paulhopkins paulhopkins force-pushed the features/disable_colours_when_piping branch from 87daa86 to cbdf546 Compare March 24, 2017 14:27
@paulhopkins
Copy link
Contributor Author

@adamjstewart I have changed the short form of the argument to '-C' because it clashes with #3142, and add -C --color --no-color to spack-completion.bash.

@adamjstewart
Copy link
Member

LGTM. I'm a little wary of merging core changes myself, so I'll let @tgamblin or someone else merge this one.

Copy link
Member

@tgamblin tgamblin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM but I have one request -- can the argument be reworked slightly to match the semantics of other command line tools like ls and grep?

Those support three options:

  1. --color=auto

  2. --color=never

  3. --color=always

@adamjstewart
Copy link
Member

I agree with @tgamblin's UI. In that case we can just drop the -C short option.

@paulhopkins paulhopkins force-pushed the features/disable_colours_when_piping branch 2 times, most recently from 6fe8ce0 to c079695 Compare March 28, 2017 11:16
@paulhopkins
Copy link
Contributor Author

I originally wanted to use the --color[=WHEN] approach by allowing an optional argument using nargs=?. However, if you use the plain --color version then it still consumes the next positional argument e.g. install, or find, etc. That's why I went with the --color, --no-color approach. I see now that you can also explicitly specify extra arguments such as --color=always, which does work though you have to enter the whole expression in otherwise it is not recognised.

I am afraid that this is not perfect but I have been going round and round with this, and I think it is sufficient without adding extra dummy arguments to get the correct usage statement.

@eschnett
Copy link
Contributor

eschnett commented May 9, 2017

ping -- is this ready to be applied? It's very inconvenient to run Spack on an HPC system and not being able to use less.

@tgamblin tgamblin force-pushed the features/disable_colours_when_piping branch from c079695 to f7b3ff1 Compare July 30, 2017 23:24
@tgamblin
Copy link
Member

@paulhopkins @adamjstewart: I rebased this on develop but noticed that the tests failed because Spec.format() now defaults to automatic colorization (color=None) instead of color=False, and this breaks places where Spec.format is used for programmatic things (like paths, module names, and variable values) and not for the user interface.

I went through the various usages of Spec.format, and here is what I found:

Wrong:

directory_layout.py:200         used in a path for directory_layout
modules.py:269                  used in module file short description
modules.py:293                  used in module name
modules.py:465                  used in an environment variable name
modules.py:466                  used in an environment variable value
modules.py:789                  used in a module name
spec.py:1363                    return string from Spec.short_spec
spec.py:2859                    return string from Spec.dep_string
spec.py:2862                    return from Spec.__str__()
cmd/mirror.py:178               used as a string sort key
cmd/module.py:152               used as output from `spack module loads`
test/directory_layout.py:59     used to validate directory_layout.py:200
test/graph.py:87                used in dot graph label
test/graph.py:88                used in dot graph label
test/graph.py:90                used in dot graph label
test/graph.py:92                used in dot graph label
test/graph.py:94                used in dot graph label
test/graph.py:96                used in dot graph label

Questionable:

spec.py:3421                    used in AmbiguousHashError msg string
package.py:922                  used in a FetchError msg string

Probably OK:

database.py:291                 passed to tty.warn
mirror.py:227                   used in tty.msg
mirror.py:234                   used in tty.msg
mirror.py:262                   used in tty.warn
package.py:908                  used in tty.warn
package.py:1653                 used in tty.msg
package.py:1712                 used in tty.msg
spec.py:1369                    return string from Spec.cshort_spec
spec.py:2939                    return from tree()
cmd/__init__.py:158             used in tty.die in disambiguate_specs()
cmd/__init__.py:202             returned as string from display_specs()
cmd/__init__.py:228             returned as string from display_specs()
cmd/__init__.py:238             returned as string from display_specs()
cmd/dependents.py:50            used in tty.msg
cmd/mirror.py:216               passed to colify() to be output
cmd/module.py:273               used as part of tty.error
cmd/uninstall.py:184            used in tty.error

The "wrong" ones are places where the return value of Spec.format is either always or sometimes are used programmatically. The "questionable" ones are error messages in exceptions, where color doesn't hurt, but seems weird to me, even though the error messages do eventually just get printed out. The "probably ok" ones are places where the specs are being output to the screen.

I think that to do this right, we should do a couple things:

  1. Retain the default of no color in Spec.format, and have the "probably ok" uses say they want automatic coloring explicitly.
  2. The color=None argument feels weird to me; I think the set_color and get_color calls in tty.color should just accept 'always', 'never', and 'auto', and we should pass those around.
  3. It might also make some sense to have a convenience method, Spec.cformat, which has a default color='auto' instead of default color='never', and use that in the "probably ok" places.

I'll go ahead and do the stuff above since it's relatively easy and doesn't hurt the current situation. If anyone has any objections let me know.

There is one more thing to consider. To really do this right, we should be deciding on whether to colorize something based on whether the stream we'll print to is a TTY. The logic in this PR decides universally based on whether sys.stdout is a TTY, so it won't catch weird cases like only redirecting sys.stderr. I think there should be some reworking of the various color output routines so that it's easier to know where they're printing, and easier to test whether it will eventually go to a TTY. That is a fair amount of work, and probably requires rethinking the color interface a bit, so I think it can wait.

Thoughts?

- Spec.format() was used programmatically (not for display) in many places.
- Revert its behavior to NOT colorize by default.
- Introduce cformat(), which defaults to auto-colorization.
@tgamblin tgamblin merged commit 1c7e572 into spack:develop Jul 31, 2017
tgamblin added a commit that referenced this pull request Aug 1, 2017
tgamblin added a commit that referenced this pull request Aug 1, 2017
tgamblin added a commit that referenced this pull request Aug 1, 2017
tgamblin added a commit that referenced this pull request Aug 1, 2017
tgamblin added a commit that referenced this pull request Aug 1, 2017
tgamblin added a commit that referenced this pull request Aug 1, 2017
tgamblin added a commit that referenced this pull request Aug 1, 2017
mamelara added a commit to NERSC/spack that referenced this pull request Aug 1, 2017
* satsuma2: new package (spack#4838)

* salmon: new package (spack#4833)

* New Package: C-Ares (spack#4840)

Adds the c-ares library, a C library for asynchronous DNS requests.

Required for the google gRPC library.

* sickle: new package (spack#4851)

* seqprep: new package (spack#4850)

* smalt: new package (spack#4853)

* singularity: new package (spack#4852)

* lmdb: Update to 0.9.21 (spack#4830)

Convert to MakefilePackage and add pkg-config file.

* added new pruners-ninja version (spack#4859)

* sortmerna: new package (spack#4866)

* revbayes: trying this again (spack#4861)

* new package: miniGMG (spack#4849)

* new package: miniGMG

* changed based on comments

* removed cuda version

* sparta: new package (spack#4867)

* sparta: new package

* fixing homepage

* Savanna (spack#4856)

Installing the stable version 0.5 through the checksummed tar.gz does not fetch the git submodule in the package.
The submodule appears as an empty directory.

Thus, clone the commit tagged as v0.5 using git to get around this issue

* savanna: modified adios dependency spec
* Replaced adios+staging with adios+flexpath+dataspaces
* savanna: Enabling fortran support in adios by default
* savanna: reverting to variant 'staging' for enabling all staging transports

* Make testing spack commands simpler (spack#4868)

Adds SpackCommand class allowing Spack commands to be easily in Python

Example usage:

    from spack.main import SpackCommand
    info = SpackCommand('info')
    out, err = info('mpich')
    print(info.returncode)

This allows easier testing of Spack commands.

Also:
* Simplify command tests
* Simplify mocking in command tests.
* Simplify module command test
* Simplify python command test
* Simplify uninstall command test
* Simplify url command test
* SpackCommand uses more compatible output redirection

* gBenchmark: Development Package (spack#4847)

* gBenchmark: Development Package

Add the development version (master branch) of `gBenchmark`

* gBenchmark: Remove Duplicate

Remove duplicate `gbenchmark` library
and keep its patch to remove the shipped
-Werror

* Perl - allow package activation without PERL5LIB variable (spack#4540)

* perl: prepend default perl @inc path to support package activation

* perl: remove stray comma from list of configure arguments

* perl: final comma in configure arguments makes adding arguments safer

This reverts commit fdc10cd.

* perl: add comment about modified @inc (thanks to George Hartzell)

* perl: use self.prefix.lib and self.prefix.bin for clarity

* perl: convert tabs added by editor to spaces for flake8

* perl: use new path syntax: prefix.lib.perl5

* perl: avoid line break before binary operator

* perl: use compact spack syntax for perl executable

* fix sphinx dependencies, add v1.6.3 (spack#4870)

* Add cuda variant for mvapich2. (spack#4800)

* Add cuda variant for mvapich2.

* Disable cuda for mvapich2 by default.

* Add a py-theano version from git repo (spack#4871)

* Change Version formatting properties and functions to return Version objects (spack#4834)

* Change version.up_to() to return Version() object
* Add unit tests for Version.up_to()
* Fix packages that expected up_to() to return a string
* Ensure that up_to() preserves separator characters
* Use version indexing instead of up_to
* Make all Version formatting properties return Version objects
* Update docs
* Tests need to test string representation

* stringtie: new package (spack#4878)

* added new version of cdo (spack#4877)

* subread: new package (spack#4882)

* structure: new package (spack#4879)

* structure: new pacakge

* fixing package structure (not a pun)

* swarm: new package (spack#4885)

* added new version of jdk 8u141-b15 (spack#4876)

* stacks: new package (spack#4875)

* fix GobjectIntrospection on Darwin (spack#4872)

* fix GobjectIntrospection on Darwin

* minor

* Rename the gpu variant to cuda, this is to be consistent with other (spack#4890)

packages.

* Update zstd version (spack#4873)

* Update zstd version

* Change order of versions

* Use MakefilePackage

* sumaclust: new package (spack#4884)

* sumaclust: new package

* tweaking url and make specs

* tabix: new package (spack#4886)

* tabix: new package

* fixed docs location

* cleaveland4: new package (spack#4894)

* cleaveland4: new package

* fixing return line in viennarna url_for_version

* fix config.guess patch for ppc64le (spack#4858)

* fix config.guess patch for ppc64le

* explicit patch for config.guess not required

* trimgalore: new package (spack#4899)

* transposome: new package (spack#4896)

* transdecoder: new package (spack#4895)

* transdecoder: new package

* fixed package structure

* fix callpath bug (spack#4659)

* fix callpath bug I found while testing env/cc

* fix hanging indent for flake

* tmux should not set PKG_CONFIG_PATH (spack#4901)

* fixes spack#967

* Version bump to 0.9.1

- Bugfixes for spack find
- 0.9.1 can read specs from current develop.

* Don't assume spack is in the path when building docs.

* Remove PKG_CONFIG_PATH from tmux configure

* Change tmux to AutotoolsPackage

* Correct link to libtinfo in tmux

* Add universal build_type variant to CMakePackage (spack#4797)

* Add universal build_type variant to CMakePackage
* Override build_type in some packages with different possible values
* Remove reference to no longer existent debug variant
* Update CBTF packages with new build_type variant
* Keep note on build size of LLVM

* shortstack: new package (spack#4905)

* added MPI dependency to Nekbone package (spack#4903)

* removed the tags as per comment in PR# 4749

* addressed above comments

* changed fortran compiler.

* added proxy application tags.

* added tags by removing them from description.

* addressed comments

* used join_path instead of path concat.

* removed the tags as per comment in PR# 4749

* addressed above comments

* changed fortran compiler.

* added proxy application tags.

* added tags by removing them from description.

* addressed comments

* used join_path instead of path concat.

* added tags.

* changes to use MPI as depedency.

* removed MPI as variant.

* changed pointer to filtered makenek file.

* flake 8 fix.

* Updated Namespace of BML Repository (spack#4910)

* Improve version detection for URLs with dynamic after version (spack#4902)

* snptest: new package (spack#4900)

* snptest: new package

* fixed version things

* fixed install phase

* openblas: add 0.2.20 (spack#4915)

* New Package: RSbench (spack#4752)

* New Package: RSbench

* minor change

* removed tags as per PR# 4749

* addressed comments and added gcc compiler.

* added proxy app tags to description.

* removed setting CC to pgicc through spec.

* removed compiler as depedency

* removed pgi variant.

* flake 8 fix.

* added mpi depedency with pgi compiler

* added  pgi compiler

* removed PGI compiler as depedency.

* added tags and addressed other code formattings.

* added tags and addressed other code formattings.

* addressed comments.

* Fix for Krell openspeedshop spack package bug.  New multi-value variant for GUI build. (spack#4880)

* Update the krell institute products to use the latest features of spack for building on cluster platforms.

* Address travis error messages and resubmit the pull request.

* Update the contents of openspeedshop package.py so it passes the flake8 tests.

* Fix flake8 error-whitespack issue in mrnet package.py file.

* Add updates based on spack reviewer feedback.

* More fixes based on comments from reviewers.  Switch using extend to using append, remove additional setting of PATH and LD_LIBRARY_PATH that should not be required due to RPATH.

* More review related changes.  Update MPIOption.append lines and take out xercesc references.

* Create a base options function for common openspeedshop base cmake options to reduce redundencies.

* Add libxml2+python depends on to get around issues with the libxml2 package file.

* Using boost over 1.60.0 causes compile errors.  This is a known boost bug. Also, dyninst-9.2.0 is set to be the vesrion of dyninst to use with OSS, as of now. The newer version fails to build.

* Fix bad syntax in specifying the boost version range.

* Update the version numbers for the krell institute components and tools: cbtf and openspeedshop.

* Do not build glib for qt3, it is not needed and causes build problems at this time anyway.

* A fix was added for setting LD_LIBRARY_PATH in the qt3 build, but if LD_LIBRARY_PATH is not set the qt build fails. So so check and set LD_LIBRARY_PATH if not set, update if it is set.

* Update the fix for qt3 build by setting LD_LIBRARY_PATH instead of checking for whether it is set or not per Adams comment that spack clears LD_LIBRARY_PATH.

* A fix was added for setting LD_LIBRARY_PATH in the qt3 build, but if LD_LIBRARY_PATH is not set the qt build fails. So so check and set LD_LIBRARY_PATH if not set, update if it is set.

* Trim comments to fit more concisely.

* Fix tabs versus spaces and swap if and else clause check from a negative to a positive check.

* Fix issues with the cbtf-argonavis build, update to use dyninst-9.3.2, fixes to openspeedshop package build.

* Fix issues with the cbtf-argonavis package.py files related to comments.

* Add changes for changing the krell packages from Package to CMakePackage.

* Add better changes for changing the krell packages from Package to CMakePackage.

* Add more modifications for changing the krell packages from Package to CMakePackage.

* Add additional modifications for changing the krell packages from Package to CMakePackage and fixing Travis erros

* Fix new travis errors.

* Fix new travis errors.

* Add more changes for PR 4765.

* Add more refinements to the conversion from Package to CMakePackage.

* Fix new travis errors.

* Add dependencies for MPI to be passed to cbtf-krell, so it can build the MPI collectors requested by the builder of openspeedshop.

* Remove extra unnecessary routine to adjust build arguments. Fix if-else clause issue.

* Fix more flake issues caused by last changes.

* Fix a bug where openspeedshop will not build when no mpi variants are specified.  Also switch to a multiple level variant for building the gui(s).  Use none, qt3, and qt4 as the variants with qt3 being the default.

* Add fix for spack issue spack#4843, where LTDL include files were not found.

* Add the build_type variant back into the openspeedshop package file.

* New Package: lcals (spack#4792)

* New Pacakge: lcals

* added logic for arch detection and compiler choice.

* fixes for comments.

* addressed comments.

* removed LCALS_ARCH and added flags though spack.

* addressed comments.

* flake 8 fix.

* reerted the changes along with comments.

* Added Proxy App tag (spack#4917)

* Added Proxy App tag

*  NO changes except proxy app tag

* bml: fix homepage (spack#4918)

* Adding QWT package. (spack#4911)

* Adding QWT package.

* Using builtin file filtering.

* Formatting.

* Fix for m4%clang (spack#4912)

* Fix for m4%clang

* Restricted condition to not subsitute rtlib on OSX

* Initial Spackage for qmd-progress library (spack#4924)

* Initial Spackage for qmd-progress library

PROGRESS is a library is focused on the development of general solvers
that are commonly used in quantum chemistry packages.

* Removed LA-CC from description to fix formatting

* Added Additional Formatting Requests

Added requested formatting changes and also ensured that graphlib and
mpi are disabled if not enabled

* gBenchmark: v1.2.0 (spack#4935)

Adds a the latest version of gBenchmark, release 1.2.0.

This is the first gBenchmark version with proper
[CMake config package installs](google/benchmark#363).
This is important for dependencies building against it, such as gRPC.

* zsh: add variant that skips tcsetpgrp test (spack#4923)

zsh's configure script fails if there's it tries to test for terminal
functionality if there's not a terminal (e.g. in a Jenkins build).

The configure script has a switch that asserts that tcsetpgrp works
and thereby avoids running that test.

This commit adds a variant that invokes that switch, defaulting to
True.

* Add latest version of apr (spack#4931)

* Add latest version of expat (spack#4930)

* Add missing dependencies to unixodbc (spack#4928)

* ZeroMQ: C++ Headers (cppzmq) (spack#4841)

Adds the cppzmq library, adding a C++ API to ZeroMQ (libzmq). In order to find the autotools-build libzmq, this requires the upcoming cppzmq release (or development branch).

* Add latest version of SCons (spack#4929)

* Add --color=[always|never|auto] argument; fix color when piping (spack#3013)

* Disable spec colorization when redirecting stdout and add command line flag to re-enable
* Add command line `--color` flag to control output colorization
* Add options to `llnl.util.tty.color` to allow color to be auto/always/never
* Add `Spec.cformat()` function to be used when `format()` should have auto-coloring

* Fix preference for X.Y version when mixed with X.Y.Z versions (spack#4922)

For packages which contain a mix of versions with formats X.Y and
X.Y.Z, if the user entered an X.Y version as a preference in
packages.yaml, Spack would get confused and favor any version A.B.Z
where X=A and Y=B. In the case where there is a mix of these version
types, this commit updates preferences so Spack will favor an exact
match.

* Clarify docs on using a hash in a spec (spack#4908)

* Fix xsdk build broken by petsc and trilinos (spack#4893)

* Fix xsdk build broken by petsc and trilinos

See spack#4891 for details

* Fix version conflict in trilinos package

Trilinos version 11 may conflict with superlu-dist.
The version "xsdk-0.2.0" was conflicting with superlu-dist,
even though it shouldn't.  I added a lower bound to the
comparison to fix this problem.

Thanks for the help @davydden!
tgamblin added a commit that referenced this pull request Aug 2, 2017
@tgamblin tgamblin added this to the v0.11.0 milestone Nov 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants