Skip to content

Commit

Permalink
Allow force-enabling or force-disabling colorized output
Browse files Browse the repository at this point in the history
`click` allows emitted colored text via `click.style`,
which is used by pip-compile to emit colorized e.g. comments to stderr.
click uses auto-detection to only enable colors when writing to a TTY.

At Lyft, we operate pip-compile as a service:
pip-compile invocations are forwarded to a set of remote machines,
which leverage a shared cache for much faster compiles.
However, they run pip-compile as a subprocess without a TTY attached
meaning the output we stream back to the user is not colorized.

Therefore, add a `--color` and matching `--no-color` argument
to force-enable as well as force-disable color
(the default remains click's auto-detection).
This could also be used for e.g. running pip-compile in CI.

AFAIK pip-sync does not emit colored output,
so I did not add the `--color/--no-color` options there.
  • Loading branch information
aneeshusa committed Jan 3, 2024
1 parent e02d186 commit b96be7d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions piptools/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def _determine_linesep(
@click.command(context_settings={"help_option_names": options.help_option_names})
@click.pass_context
@options.version
@options.color
@options.verbose
@options.quiet
@options.dry_run
Expand Down Expand Up @@ -115,6 +116,7 @@ def _determine_linesep(
@options.only_build_deps
def cli(
ctx: click.Context,
color: bool | None,
verbose: int,
quiet: int,
dry_run: bool,
Expand Down Expand Up @@ -162,6 +164,8 @@ def cli(
Compiles requirements.txt from requirements.in, pyproject.toml, setup.cfg,
or setup.py specs.
"""
if color is not None:
ctx.color = color
log.verbosity = verbose - quiet

if all_build_deps and build_deps_targets:
Expand Down
6 changes: 6 additions & 0 deletions piptools/scripts/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ def _get_default_option(option_name: str) -> Any:

version = click.version_option(package_name="pip-tools")

color = click.option(
"--color/--no-color",
default=None,
help="Force output to be colorized or not, instead of auto-detecting color support",
)

verbose = click.option(
"-v",
"--verbose",
Expand Down

0 comments on commit b96be7d

Please sign in to comment.