Skip to content

Commit 4370a5a

Browse files
oz123pradyunsg
authored andcommitted
Add a global option to disable colors (#4739)
* Add a global option to disable colors This is a fix for issue #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
1 parent cf989e5 commit 4370a5a

File tree

5 files changed

+63
-5
lines changed

5 files changed

+63
-5
lines changed

docs/reference/pip.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ Console logging
2222
~~~~~~~~~~~~~~~
2323

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

2729

2830
.. _`FileLogging`:

news/2449.feature

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add `--no-color` to `pip`. All colored output is disabled
2+
if this flag is detected.

src/pip/_internal/basecommand.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ def main(self, args):
132132
if options.log:
133133
root_level = "DEBUG"
134134

135+
if options.no_color:
136+
logger_class = "logging.StreamHandler"
137+
else:
138+
logger_class = "pip._internal.utils.logging.ColorizedStreamHandler"
139+
135140
logging.config.dictConfig({
136141
"version": 1,
137142
"disable_existing_loggers": False,
@@ -150,16 +155,14 @@ def main(self, args):
150155
"handlers": {
151156
"console": {
152157
"level": level,
153-
"class":
154-
"pip._internal.utils.logging.ColorizedStreamHandler",
158+
"class": logger_class,
155159
"stream": self.log_streams[0],
156160
"filters": ["exclude_warnings"],
157161
"formatter": "indent",
158162
},
159163
"console_errors": {
160164
"level": "WARNING",
161-
"class":
162-
"pip._internal.utils.logging.ColorizedStreamHandler",
165+
"class": logger_class,
163166
"stream": self.log_streams[1],
164167
"formatter": "indent",
165168
},

src/pip/_internal/cmdoptions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ def getname(n):
102102
help='Give more output. Option is additive, and can be used up to 3 times.'
103103
)
104104

105+
no_color = partial(
106+
Option,
107+
'--no-color',
108+
dest='no_color',
109+
action='store_true',
110+
default=False,
111+
help="Suppress colored output",
112+
)
113+
105114
version = partial(
106115
Option,
107116
'-V', '--version',
@@ -566,6 +575,7 @@ def _merge_hash(option, opt_str, value, parser):
566575
cache_dir,
567576
no_cache,
568577
disable_pip_version_check,
578+
no_color,
569579
]
570580
}
571581

tests/functional/test_no_color.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Test specific for the --no-color option
3+
"""
4+
import os
5+
import platform
6+
import subprocess as sp
7+
import sys
8+
9+
import pytest
10+
11+
12+
@pytest.mark.skipif(sys.platform == 'win32',
13+
reason="does not run on windows")
14+
def test_no_color(script):
15+
16+
"""
17+
Test uninstalling an existing package - should out put red error
18+
19+
We must use subprocess with the script command, since redirection
20+
in unix platform causes text coloring to disapper. Thus, we can't
21+
use the testing infrastructure that other options has.
22+
"""
23+
24+
sp.Popen("script --flush --quiet --return /tmp/colored-output.txt"
25+
" --command \"pip uninstall noSuchPackage\"", shell=True,
26+
stdout=sp.PIPE, stderr=sp.PIPE).communicate()
27+
28+
with open("/tmp/colored-output.txt", "r") as result:
29+
assert "\x1b[31m" in result.read()
30+
31+
os.unlink("/tmp/colored-output.txt")
32+
33+
sp.Popen("script --flush --quiet --return /tmp/no-color-output.txt"
34+
" --command \"pip --no-color uninstall noSuchPackage\"",
35+
shell=True,
36+
stdout=sp.PIPE, stderr=sp.PIPE).communicate()
37+
38+
with open("/tmp/no-color-output.txt", "r") as result:
39+
assert "\x1b[31m" not in result.read()
40+
41+
os.unlink("/tmp/no-color-output.txt")

0 commit comments

Comments
 (0)