commit: 0431aa7db28e69af309a8175ec9e13a23439e2fa Author: Siddhanth Rathod <xsiddhanthrathod <AT> gmail <DOT> com> AuthorDate: Sat Mar 4 19:29:28 2023 +0000 Commit: Sam James <sam <AT> gentoo <DOT> org> CommitDate: Tue Mar 21 02:35:55 2023 +0000 URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=0431aa7d
emerge: respect NO_COLOR Bug: https://bugs.gentoo.org/898224 Signed-off-by: Siddhanth Rathod <xsiddhanthrathod <AT> gmail.com> Closes: https://github.com/gentoo/portage/pull/1003 Signed-off-by: Sam James <sam <AT> gentoo.org> NEWS | 7 ++++++- bin/ebuild | 7 +++---- bin/egencache | 9 +++------ bin/etc-update | 1 + bin/isolated-functions.sh | 11 +++++++++-- bin/portageq | 6 ++---- bin/save-ebuild-env.sh | 2 +- lib/_emerge/AbstractEbuildProcess.py | 7 +++---- lib/_emerge/actions.py | 12 ++++++------ lib/portage/_emirrordist/main.py | 2 +- lib/portage/dbapi/_MergeProcess.py | 5 ++--- lib/portage/package/ebuild/_config/special_env_vars.py | 2 ++ lib/portage/package/ebuild/_ipc/QueryCommand.py | 6 ++---- lib/portage/tests/__init__.py | 3 ++- lib/portage/tests/resolver/ResolverPlayground.py | 4 ++-- lib/portage/tests/runTests.py | 2 +- lib/portage/util/__init__.py | 11 +++++++++++ 17 files changed, 57 insertions(+), 40 deletions(-) diff --git a/NEWS b/NEWS index 03e262c69..01209c9fb 100644 --- a/NEWS +++ b/NEWS @@ -48,7 +48,12 @@ Cleanups: in terms of priority for providers. Features: -* TODO +* Respect the NO_COLOR environment variable for disabling color. + + The new quasi-standard for disabling ANSI color is to check for the NO_COLOR + environment variable and disable color when the variable has a nonempty value. + See bug #898224. Portage previously used NOCOLOR. It continues to support NOCOLOR + for a time for compatibility. Bug fixes: * TODO diff --git a/bin/ebuild b/bin/ebuild index 8d1908795..8fccbea89 100755 --- a/bin/ebuild +++ b/bin/ebuild @@ -67,7 +67,6 @@ try: from _emerge.RootConfig import RootConfig portage.process.sanitize_fds() - description = "See the ebuild(1) man page for more info" usage = "Usage: ebuild <ebuild file> <command> [command] ..." parser = argparse.ArgumentParser(description=description, usage=usage) @@ -128,14 +127,14 @@ try: if not opts.color == "y" and ( opts.color == "n" - or portage.settings.get("NOCOLOR") in ("yes", "true") + or portage.util.no_color(portage.settings) or portage.settings.get("TERM") == "dumb" or not sys.stdout.isatty() ): portage.output.nocolor() portage.settings.unlock() - portage.settings["NOCOLOR"] = "true" - portage.settings.backup_changes("NOCOLOR") + portage.settings["NO_COLOR"] = "true" + portage.settings.backup_changes("NO_COLOR") portage.settings.lock() apply_priorities(portage.settings) diff --git a/bin/egencache b/bin/egencache index 1a6118bde..671df3014 100755 --- a/bin/egencache +++ b/bin/egencache @@ -67,7 +67,7 @@ try: from portage.package.ebuild._parallel_manifest.ManifestScheduler import ( ManifestScheduler, ) - from portage.util import cmp_sort_key, writemsg_level + from portage.util import cmp_sort_key, writemsg_level, no_color from portage.util._async.AsyncFunction import AsyncFunction from portage.util._async.run_main_scheduler import run_main_scheduler from portage.util._async.TaskScheduler import TaskScheduler @@ -1105,12 +1105,9 @@ try: if "PATH" in os.environ: env["PATH"] = os.environ["PATH"] - if not sys.stdout.isatty() or os.environ.get("NOCOLOR", "").lower() in ( - "yes", - "true", - ): + if not sys.stdout.isatty() or no_color(os.environ): portage.output.nocolor() - env["NOCOLOR"] = "true" + env["NO_COLOR"] = "true" parser, options, atoms = parse_args(args) diff --git a/bin/etc-update b/bin/etc-update index 59e709168..14bd80b84 100755 --- a/bin/etc-update +++ b/bin/etc-update @@ -798,6 +798,7 @@ portage_vars=( EROOT USERLAND NOCOLOR + NO_COLOR ) if type -P portageq > /dev/null; then diff --git a/bin/isolated-functions.sh b/bin/isolated-functions.sh index 882789132..06be030fb 100644 --- a/bin/isolated-functions.sh +++ b/bin/isolated-functions.sh @@ -434,14 +434,21 @@ RC_INDENTATION='' RC_DEFAULT_INDENT=2 RC_DOT_PATTERN='' -case "${NOCOLOR:-false}" in + + +if [[ -z ${NO_COLOR} ]] ; then + case ${NOCOLOR:-false} in yes|true) __unset_colors ;; no|false) __set_colors ;; -esac + esac +else + __unset_colors +fi + if [[ -z ${USERLAND} ]] ; then case $(uname -s) in diff --git a/bin/portageq b/bin/portageq index e9b6b5d38..df2b1a872 100755 --- a/bin/portageq +++ b/bin/portageq @@ -70,7 +70,7 @@ try: portage._internal_caller = True from portage import os from portage.eapi import eapi_has_repo_deps - from portage.util import writemsg, writemsg_stdout + from portage.util import writemsg, writemsg_stdout, no_color portage.proxy.lazyimport.lazyimport( globals(), @@ -1451,9 +1451,7 @@ try: def main(argv): argv = portage._decode_argv(argv) - - nocolor = os.environ.get("NOCOLOR") - if nocolor in ("yes", "true"): + if no_color(os.environ): portage.output.nocolor() parser = argparse.ArgumentParser(add_help=False) diff --git a/bin/save-ebuild-env.sh b/bin/save-ebuild-env.sh index eb57c9208..6dc0bf77f 100644 --- a/bin/save-ebuild-env.sh +++ b/bin/save-ebuild-env.sh @@ -93,7 +93,7 @@ __save_ebuild_env() { ECLASS_DEPTH ENDCOL FAKEROOTKEY \ HOME \ LAST_E_CMD LAST_E_LEN LD_PRELOAD MISC_FUNCTIONS_ARGS MOPREFIX \ - NOCOLOR PKGDIR PKGUSE PKG_LOGDIR PKG_TMPDIR \ + NOCOLOR NO_COLOR PKGDIR PKGUSE PKG_LOGDIR PKG_TMPDIR \ PORTAGE_BASHRC_FILES PORTAGE_BASHRCS_SOURCED \ PORTAGE_COLOR_BAD PORTAGE_COLOR_BRACKET PORTAGE_COLOR_ERR \ PORTAGE_COLOR_GOOD PORTAGE_COLOR_HILITE PORTAGE_COLOR_INFO \ diff --git a/lib/_emerge/AbstractEbuildProcess.py b/lib/_emerge/AbstractEbuildProcess.py index 0e6786eed..fe2ef426a 100644 --- a/lib/_emerge/AbstractEbuildProcess.py +++ b/lib/_emerge/AbstractEbuildProcess.py @@ -18,7 +18,7 @@ from portage.package.ebuild._ipc.ExitCommand import ExitCommand from portage.package.ebuild._ipc.QueryCommand import QueryCommand from portage import os from portage.util.futures import asyncio -from portage.util import apply_secpass_permissions +from portage.util import apply_secpass_permissions, no_color portage.proxy.lazyimport.lazyimport( globals(), @@ -170,6 +170,7 @@ class AbstractEbuildProcess(SpawnProcess): # Automatically prevent color codes from showing up in logs, # since we're not displaying to a terminal anyway. self.settings["NOCOLOR"] = "true" + self.settings["NO_COLOR"] = "true" start_ipc_daemon = False if self._enable_ipc_daemon: @@ -390,9 +391,7 @@ class AbstractEbuildProcess(SpawnProcess): elog_func = getattr(elog_messages, elog_funcname) global_havecolor = portage.output.havecolor try: - portage.output.havecolor = self.settings.get( - "NOCOLOR", "false" - ).lower() in ("no", "false") + portage.output.havecolor = not no_color(self.settings) for line in lines: elog_func(line, phase=phase, key=self.settings.mycpv, out=out) finally: diff --git a/lib/_emerge/actions.py b/lib/_emerge/actions.py index 3945afce2..6017579c8 100644 --- a/lib/_emerge/actions.py +++ b/lib/_emerge/actions.py @@ -2757,7 +2757,7 @@ def adjust_config(myopts, settings): settings["PORTAGE_DEBUG"] = str(PORTAGE_DEBUG) settings.backup_changes("PORTAGE_DEBUG") - if settings.get("NOCOLOR") not in ("yes", "true"): + if not portage.util.no_color(settings): portage.output.havecolor = 1 # The explicit --color < y | n > option overrides the NOCOLOR environment @@ -2765,15 +2765,15 @@ def adjust_config(myopts, settings): if "--color" in myopts: if "y" == myopts["--color"]: portage.output.havecolor = 1 - settings["NOCOLOR"] = "false" + settings["NO_COLOR"] = "" else: portage.output.havecolor = 0 - settings["NOCOLOR"] = "true" - settings.backup_changes("NOCOLOR") + settings["NO_COLOR"] = "true" + settings.backup_changes("NO_COLOR") elif settings.get("TERM") == "dumb" or not sys.stdout.isatty(): portage.output.havecolor = 0 - settings["NOCOLOR"] = "true" - settings.backup_changes("NOCOLOR") + settings["NO_COLOR"] = "true" + settings.backup_changes("NO_COLOR") if "--pkg-format" in myopts: settings["PORTAGE_BINPKG_FORMAT"] = myopts["--pkg-format"] diff --git a/lib/portage/_emirrordist/main.py b/lib/portage/_emirrordist/main.py index 1383b4652..736a60555 100644 --- a/lib/portage/_emirrordist/main.py +++ b/lib/portage/_emirrordist/main.py @@ -238,7 +238,7 @@ def emirrordist_main(args): if not sys.stdout.isatty(): portage.output.nocolor() - env["NOCOLOR"] = "true" + env["NO_COLOR"] = "true" parser, options, args = parse_args(args) diff --git a/lib/portage/dbapi/_MergeProcess.py b/lib/portage/dbapi/_MergeProcess.py index 97ec8ca34..7c018222f 100644 --- a/lib/portage/dbapi/_MergeProcess.py +++ b/lib/portage/dbapi/_MergeProcess.py @@ -11,6 +11,7 @@ from portage import os, _unicode_decode from portage.util._ctypes import find_library import portage.elog.messages from portage.util._async.ForkProcess import ForkProcess +from portage.util import no_color class MergeProcess(ForkProcess): @@ -200,9 +201,7 @@ class MergeProcess(ForkProcess): os.close(self._elog_reader_fd) counter = self._counter mylink = self._dblink - - portage.output.havecolor = self.settings.get("NOCOLOR") not in ("yes", "true") - + portage.output.havecolor = not no_color(self.settings) # Avoid wastful updates of the vdb cache. self.vartree.dbapi._flush_cache_enabled = False diff --git a/lib/portage/package/ebuild/_config/special_env_vars.py b/lib/portage/package/ebuild/_config/special_env_vars.py index 37fc2a290..beb411188 100644 --- a/lib/portage/package/ebuild/_config/special_env_vars.py +++ b/lib/portage/package/ebuild/_config/special_env_vars.py @@ -114,6 +114,7 @@ environ_whitelist = frozenset( "HOME", "MERGE_TYPE", "NOCOLOR", + "NO_COLOR", "P", "PATH", "PF", @@ -358,4 +359,5 @@ validate_commands = ( case_insensitive_vars = ( "AUTOCLEAN", "NOCOLOR", + "NO_COLOR", ) diff --git a/lib/portage/package/ebuild/_ipc/QueryCommand.py b/lib/portage/package/ebuild/_ipc/QueryCommand.py index 71d608e43..faf1baa0a 100644 --- a/lib/portage/package/ebuild/_ipc/QueryCommand.py +++ b/lib/portage/package/ebuild/_ipc/QueryCommand.py @@ -10,7 +10,7 @@ from portage.eapi import eapi_has_repo_deps from portage.elog import messages as elog_messages from portage.exception import InvalidAtom from portage.package.ebuild._ipc.IpcCommand import IpcCommand -from portage.util import normalize_path +from portage.util import normalize_path, no_color from portage.versions import best @@ -149,9 +149,7 @@ class QueryCommand(IpcCommand): elog_func = getattr(elog_messages, elog_funcname) global_havecolor = portage.output.havecolor try: - portage.output.havecolor = self.settings.get( - "NOCOLOR", "false" - ).lower() in ("no", "false") + portage.output.havecolor = not no_color(self.settings) for line in lines: elog_func(line, phase=phase, key=self.settings.mycpv, out=out) finally: diff --git a/lib/portage/tests/__init__.py b/lib/portage/tests/__init__.py index 2aa97830d..d8e0cc78f 100644 --- a/lib/portage/tests/__init__.py +++ b/lib/portage/tests/__init__.py @@ -12,6 +12,7 @@ from unittest.runner import TextTestResult as _TextTestResult import portage from portage import os +from portage.util import no_color from portage import _encodings from portage import _unicode_decode from portage.output import colorize @@ -78,7 +79,7 @@ def main(): options = parser.parse_args(args=sys.argv) if ( - os.environ.get("NOCOLOR") in ("yes", "true") + no_color(os.environ) or os.environ.get("TERM") == "dumb" or not sys.stdout.isatty() ): diff --git a/lib/portage/tests/resolver/ResolverPlayground.py b/lib/portage/tests/resolver/ResolverPlayground.py index 2b197ca6d..7f97d3100 100644 --- a/lib/portage/tests/resolver/ResolverPlayground.py +++ b/lib/portage/tests/resolver/ResolverPlayground.py @@ -587,8 +587,8 @@ class ResolverPlayground: "PORTAGE_TMPDIR": os.path.join(self.eroot, "var/tmp"), } - if os.environ.get("NOCOLOR"): - make_conf["NOCOLOR"] = os.environ["NOCOLOR"] + if portage.util.no_color(os.environ): + make_conf["NO_COLOR"] = os.environ["NO_COLOR"] # Pass along PORTAGE_USERNAME and PORTAGE_GRPNAME since they # need to be inherited by ebuild subprocesses. diff --git a/lib/portage/tests/runTests.py b/lib/portage/tests/runTests.py index cfb4082c0..bf4c2a7c5 100755 --- a/lib/portage/tests/runTests.py +++ b/lib/portage/tests/runTests.py @@ -40,7 +40,7 @@ portage._internal_caller = True # work the same regardless of global configuration file state/existence. portage._disable_legacy_globals() -if os.environ.get("NOCOLOR") in ("yes", "true"): +if portage.util.no_color(os.environ): portage.output.nocolor() import portage.tests as tests diff --git a/lib/portage/util/__init__.py b/lib/portage/util/__init__.py index 4d50088e7..31aba6702 100644 --- a/lib/portage/util/__init__.py +++ b/lib/portage/util/__init__.py @@ -56,6 +56,7 @@ __all__ = [ "writemsg", "writemsg_level", "writemsg_stdout", + "no_color", ] from contextlib import AbstractContextManager @@ -71,6 +72,7 @@ import string import sys import traceback import glob +from typing import Optional import portage @@ -2006,3 +2008,12 @@ def getlibpaths(root, env=None): rval.append("/lib") return [normalize_path(x) for x in rval if x] + + +def no_color(settings: Optional[dict]) -> bool: + # In several years (2026+), we can cleanup NOCOLOR support, and just support NO_COLOR. + has_color: str = settings.get("NO_COLOR") + nocolor: str = settings.get("NOCOLOR", "false").lower() + if has_color is None: + return nocolor in ("yes", "true") + return bool(has_color)
