Skip to content

Commit

Permalink
Add a global option to disable colors (pypa#4739)
Browse files Browse the repository at this point in the history
* Add a global option to disable colors

 This is a fix for issue pypa#2449

 All it does is simply add a global option --no-color

 Internally it switches ColorizedStreamHandler to StreamHandler if
 this flag is detected.

* Fix lint errors

 * not sure it makes the code more readable though ...

* Fix typo

* Choose logging class before assigning

 * As requested per review
 * Make the code shorter and easier to follow

* Be polite to followers, add commas

* Add functional test for the --no-color output

* Better detection of windows

* Fix fragile tests - can't trust script --quiet

 * The version found in Travis-CI does not respect this flag
   It added a prefix line and suffix line found if one does not
   add the flag --quiet (script from util-linux 2.26.2).

   As such the out  put is:

     Script started on Fri 27 Oct 2017 07:17:30 AM CEST
     \x1b[31mCannot uninstall requirement noSuchPackage, not installed\x1b[0m\n

     Script done on Fri 27 Oct 2017 07:17:31 AM CEST

  With this change, the test should pass, and is hopefully more stable.

* Simplify testing for color or no-color
  • Loading branch information
oz123 authored and kianasun committed Mar 28, 2018
1 parent ea8a1d4 commit eb6cef4
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 5 deletions.
4 changes: 3 additions & 1 deletion docs/reference/pip.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ Console logging
~~~~~~~~~~~~~~~

pip offers :ref:`-v, --verbose <--verbose>` and :ref:`-q, --quiet <--quiet>`
to control the console log level.
to control the console log level. By default, some messages (error and warnings)
are colored in the terminal. If you want to suppress the colored output use
:ref:`--no-color <--no-color>`.


.. _`FileLogging`:
Expand Down
2 changes: 2 additions & 0 deletions news/2449.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add `--no-color` to `pip`. All colored output is disabled
if this flag is detected.
11 changes: 7 additions & 4 deletions src/pip/_internal/basecommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ def main(self, args):
if options.log:
root_level = "DEBUG"

if options.no_color:
logger_class = "logging.StreamHandler"
else:
logger_class = "pip._internal.utils.logging.ColorizedStreamHandler"

logging.config.dictConfig({
"version": 1,
"disable_existing_loggers": False,
Expand All @@ -150,16 +155,14 @@ def main(self, args):
"handlers": {
"console": {
"level": level,
"class":
"pip._internal.utils.logging.ColorizedStreamHandler",
"class": logger_class,
"stream": self.log_streams[0],
"filters": ["exclude_warnings"],
"formatter": "indent",
},
"console_errors": {
"level": "WARNING",
"class":
"pip._internal.utils.logging.ColorizedStreamHandler",
"class": logger_class,
"stream": self.log_streams[1],
"formatter": "indent",
},
Expand Down
10 changes: 10 additions & 0 deletions src/pip/_internal/cmdoptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ def getname(n):
help='Give more output. Option is additive, and can be used up to 3 times.'
)

no_color = partial(
Option,
'--no-color',
dest='no_color',
action='store_true',
default=False,
help="Suppress colored output",
)

version = partial(
Option,
'-V', '--version',
Expand Down Expand Up @@ -566,6 +575,7 @@ def _merge_hash(option, opt_str, value, parser):
cache_dir,
no_cache,
disable_pip_version_check,
no_color,
]
}

Expand Down
41 changes: 41 additions & 0 deletions tests/functional/test_no_color.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""
Test specific for the --no-color option
"""
import os
import platform
import subprocess as sp
import sys

import pytest


@pytest.mark.skipif(sys.platform == 'win32',
reason="does not run on windows")
def test_no_color(script):

"""
Test uninstalling an existing package - should out put red error
We must use subprocess with the script command, since redirection
in unix platform causes text coloring to disapper. Thus, we can't
use the testing infrastructure that other options has.
"""

sp.Popen("script --flush --quiet --return /tmp/colored-output.txt"
" --command \"pip uninstall noSuchPackage\"", shell=True,
stdout=sp.PIPE, stderr=sp.PIPE).communicate()

with open("/tmp/colored-output.txt", "r") as result:
assert "\x1b[31m" in result.read()

os.unlink("/tmp/colored-output.txt")

sp.Popen("script --flush --quiet --return /tmp/no-color-output.txt"
" --command \"pip --no-color uninstall noSuchPackage\"",
shell=True,
stdout=sp.PIPE, stderr=sp.PIPE).communicate()

with open("/tmp/no-color-output.txt", "r") as result:
assert "\x1b[31m" not in result.read()

os.unlink("/tmp/no-color-output.txt")

0 comments on commit eb6cef4

Please sign in to comment.