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

Add a global option to disable colors #4739

Merged
merged 9 commits into from
Nov 5, 2017
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/reference/pip.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,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
61 changes: 61 additions & 0 deletions tests/functional/test_no_color.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""
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',
Copy link
Member

Choose a reason for hiding this comment

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

A cross-platform test would be nice to have.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pradyunsg , I would love that too, but it is unfair to ask me to implement this. I have no access to
a windows machine.

Copy link
Member

Choose a reason for hiding this comment

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

No issues then. :)

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
oz123 marked this conversation as resolved.
Show resolved Hide resolved
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:

red_lines = 0
reset_lines = 0

for line in result.readlines():
oz123 marked this conversation as resolved.
Show resolved Hide resolved
if line.startswith("\x1b[31m"):
red_lines += 1
if line.endswith("\x1b[0m\n"):
reset_lines += 1

assert red_lines >= 1
assert reset_lines >= 1

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:
red_lines = 0
reset_lines = 0
for line in result.readlines():
oz123 marked this conversation as resolved.
Show resolved Hide resolved
if line.startswith("\x1b[31m"):
red_lines += 1
if line.endswith("\x1b[0m\n"):
reset_lines += 1

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

assert red_lines == 0
assert reset_lines == 0