Skip to content

Commit e31a9e5

Browse files
authored
fix: Disable rich interactivity when running in CI (#14204)
See added code comments for rationale.
1 parent 6294f20 commit e31a9e5

5 files changed

Lines changed: 43 additions & 33 deletions

File tree

news/13354.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Stop animating progress bars and status spinners when running on CI, even
2+
if ``FORCE_COLOR`` is set.

src/pip/_internal/network/session.py

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
from pip._internal.utils.glibc import libc_ver
4747
from pip._internal.utils.misc import (
4848
build_url_from_netloc,
49+
looks_like_ci,
4950
parse_netloc,
5051
redact_auth_from_url,
5152
)
@@ -79,35 +80,6 @@
7980
]
8081

8182

82-
# These are environment variables present when running under various
83-
# CI systems. For each variable, some CI systems that use the variable
84-
# are indicated. The collection was chosen so that for each of a number
85-
# of popular systems, at least one of the environment variables is used.
86-
# This list is used to provide some indication of and lower bound for
87-
# CI traffic to PyPI. Thus, it is okay if the list is not comprehensive.
88-
# For more background, see: https://github.com/pypa/pip/issues/5499
89-
CI_ENVIRONMENT_VARIABLES = (
90-
# Azure Pipelines
91-
"BUILD_BUILDID",
92-
# Jenkins
93-
"BUILD_ID",
94-
# AppVeyor, CircleCI, Codeship, Gitlab CI, Shippable, Travis CI
95-
"CI",
96-
# Explicit environment variable.
97-
"PIP_IS_CI",
98-
)
99-
100-
101-
def looks_like_ci() -> bool:
102-
"""
103-
Return whether it looks like pip is running under CI.
104-
"""
105-
# We don't use the method of checking for a tty (e.g. using isatty())
106-
# because some CI systems mimic a tty (e.g. Travis CI). Thus that
107-
# method doesn't provide definitive information in either direction.
108-
return any(name in os.environ for name in CI_ENVIRONMENT_VARIABLES)
109-
110-
11183
@functools.lru_cache(maxsize=1)
11284
def user_agent() -> str:
11385
"""

src/pip/_internal/utils/logging.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from pip._internal.utils._log import VERBOSE, getLogger
3030
from pip._internal.utils.compat import WINDOWS
3131
from pip._internal.utils.deprecation import DEPRECATION_MSG_PREFIX
32-
from pip._internal.utils.misc import StreamWrapper, ensure_dir
32+
from pip._internal.utils.misc import StreamWrapper, ensure_dir, looks_like_ci
3333

3434
_log_state = threading.local()
3535
_stdout_console = None
@@ -320,8 +320,16 @@ def setup_logging(verbosity: int, no_color: bool, user_log_file: str | None) ->
320320
["user_log"] if include_user_log else []
321321
)
322322
global _stdout_console, stderr_console
323-
_stdout_console = PipConsole(file=sys.stdout, no_color=no_color, soft_wrap=True)
324-
_stderr_console = PipConsole(file=sys.stderr, no_color=no_color, soft_wrap=True)
323+
console_options: dict[str, Any] = {"no_color": no_color, "soft_wrap": True}
324+
if looks_like_ci():
325+
# Don't animate progress bars and status spinners when running
326+
# in CI. A lot of CI workflows use FORCE_COLOR and similar envvars
327+
# so their CI output remains colorized, but this causes rich to
328+
# assume a fully interactive terminal, resulting in any animated
329+
# output to span many lines which is distracting (and unhelpful).
330+
console_options["force_interactive"] = False
331+
_stdout_console = PipConsole(file=sys.stdout, **console_options)
332+
_stderr_console = PipConsole(file=sys.stderr, **console_options)
325333

326334
logging.config.dictConfig(
327335
{

src/pip/_internal/utils/misc.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"ensure_dir",
5353
"remove_auth_from_url",
5454
"check_externally_managed",
55+
"looks_like_ci",
5556
"ConfiguredBuildBackendHookCaller",
5657
]
5758

@@ -65,6 +66,23 @@
6566
OnErr = Callable[[FunctionType, Path, ExcInfo], Any]
6667

6768
FILE_CHUNK_SIZE = 1024 * 1024
69+
# These are environment variables present when running under various
70+
# CI systems. For each variable, some CI systems that use the variable
71+
# are indicated. The collection was chosen so that for each of a number
72+
# of popular systems, at least one of the environment variables is used.
73+
# This list is used to provide some indication of and lower bound for
74+
# CI traffic to PyPI. Thus, it is okay if the list is not comprehensive.
75+
# For more background, see: https://github.com/pypa/pip/issues/5499
76+
CI_ENVIRONMENT_VARIABLES = (
77+
# Azure Pipelines
78+
"BUILD_BUILDID",
79+
# Jenkins
80+
"BUILD_ID",
81+
# AppVeyor, CircleCI, Codeship, Gitlab CI, Shippable, Travis CI
82+
"CI",
83+
# Explicit environment variable.
84+
"PIP_IS_CI",
85+
)
6886

6987

7088
def get_pip_version() -> str:
@@ -785,3 +803,13 @@ def warn_if_run_as_root() -> None:
785803
"Use the --root-user-action option if you know what you are doing and "
786804
"want to suppress this warning."
787805
)
806+
807+
808+
def looks_like_ci() -> bool:
809+
"""
810+
Return whether it looks like pip is running under CI.
811+
"""
812+
# We don't use the method of checking for a tty (e.g. using isatty())
813+
# because some CI systems mimic a tty (e.g. Travis CI). Thus that
814+
# method doesn't provide definitive information in either direction.
815+
return any(name in os.environ for name in CI_ENVIRONMENT_VARIABLES)

tests/unit/test_network_session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@
3434
)
3535
from pip._internal.models.link import Link
3636
from pip._internal.network.session import (
37-
CI_ENVIRONMENT_VARIABLES,
3837
HTTPAdapter,
3938
PipSession,
4039
user_agent,
4140
)
41+
from pip._internal.utils.misc import CI_ENVIRONMENT_VARIABLES
4242

4343
from tests.lib.output import render_to_text
4444
from tests.lib.server import make_mock_server, server_running

0 commit comments

Comments
 (0)