Skip to content

Commit

Permalink
Print all env vars in pipx environment (#1073)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomiBelan authored Oct 6, 2023
1 parent 4c84be5 commit b489691
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- Add `pipx install --preinstall` to support preinstalling build requirements
- Pass `--no-input` to pip when output is not piped to parent stdout
- Fix program name in generated manual page
- Print all environment variables in `pipx environment`

## 1.2.0

Expand Down
31 changes: 25 additions & 6 deletions src/pipx/commands/environment.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from pipx.constants import (
EXIT_CODE_OK,
LOCAL_BIN_DIR,
Expand All @@ -9,27 +11,44 @@
PIPX_VENV_CACHEDIR,
ExitCode,
)
from pipx.emojis import EMOJI_SUPPORT
from pipx.interpreter import DEFAULT_PYTHON
from pipx.util import PipxError


def environment(value: str) -> ExitCode:
"""Print a list of variables used in `pipx.constants`"""
environment_variables = {
"""Print a list of environment variables and paths used by pipx"""
environment_variables = [
"PIPX_HOME",
"PIPX_BIN_DIR",
"PIPX_SHARED_LIBS",
"PIPX_DEFAULT_PYTHON",
"USE_EMOJI",
]
derived_values = {
"PIPX_HOME": PIPX_HOME,
"PIPX_BIN_DIR": LOCAL_BIN_DIR,
"PIPX_SHARED_LIBS": PIPX_SHARED_LIBS,
"PIPX_LOCAL_VENVS": PIPX_LOCAL_VENVS,
"PIPX_LOG_DIR": PIPX_LOG_DIR,
"PIPX_TRASH_DIR": PIPX_TRASH_DIR,
"PIPX_VENV_CACHEDIR": PIPX_VENV_CACHEDIR,
"PIPX_DEFAULT_PYTHON": DEFAULT_PYTHON,
"USE_EMOJI": str(EMOJI_SUPPORT).lower(),
}
if value is None:
print("Environment variables (set by user):")
print("")
for env_variable in environment_variables:
print(f"{env_variable}={environment_variables[env_variable]}")
env_value = os.getenv(env_variable, "")
print(f"{env_variable}={env_value}")
print("")
print("Derived values (computed by pipx):")
print("")
print("Only PIPX_HOME and PIPX_BIN_DIR can be set by users in the above list.")
elif value in environment_variables:
print(environment_variables[value])
for env_variable in derived_values:
print(f"{env_variable}={derived_values[env_variable]}")
elif value in derived_values:
print(derived_values[value])
else:
raise PipxError("Variable not found.")

Expand Down
11 changes: 6 additions & 5 deletions src/pipx/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,15 +668,16 @@ def _add_environment(subparsers: argparse._SubParsersAction) -> None:
p = subparsers.add_parser(
"environment",
formatter_class=LineWrapRawTextHelpFormatter,
help=("Print a list of variables used in pipx.constants."),
help="Print a list of environment variables and paths used by pipx.",
description=textwrap.dedent(
"""
Prints the names and current values of environment variables used by pipx,
followed by internal pipx variables which are derived from the environment
variables and platform specific default values.
Available variables:
PIPX_HOME, PIPX_BIN_DIR, PIPX_SHARED_LIBS, PIPX_LOCAL_VENVS, PIPX_LOG_DIR,
PIPX_TRASH_DIR, PIPX_VENV_CACHEDIR
Only PIPX_HOME and PIPX_BIN_DIR can be set by users in the above list.
PIPX_TRASH_DIR, PIPX_VENV_CACHEDIR, PIPX_DEFAULT_PYTHON, USE_EMOJI
"""
),
)
Expand Down
9 changes: 5 additions & 4 deletions tests/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ def test_cli(monkeypatch, capsys):
assert "PIPX_LOG_DIR" in captured.out
assert "PIPX_TRASH_DIR" in captured.out
assert "PIPX_VENV_CACHEDIR" in captured.out
assert (
"Only PIPX_HOME and PIPX_BIN_DIR can be set by users in the above list."
in captured.out
)
assert "PIPX_DEFAULT_PYTHON" in captured.out
assert "USE_EMOJI" in captured.out
assert "Environment variables (set by user):" in captured.out


def test_cli_with_args(monkeypatch, capsys):
Expand All @@ -25,6 +24,8 @@ def test_cli_with_args(monkeypatch, capsys):
assert not run_pipx_cli(["environment", "--value", "PIPX_LOG_DIR"])
assert not run_pipx_cli(["environment", "--value", "PIPX_TRASH_DIR"])
assert not run_pipx_cli(["environment", "--value", "PIPX_VENV_CACHEDIR"])
assert not run_pipx_cli(["environment", "--value", "PIPX_DEFAULT_PYTHON"])
assert not run_pipx_cli(["environment", "--value", "USE_EMOJI"])

assert run_pipx_cli(["environment", "--value", "SSS"])
captured = capsys.readouterr()
Expand Down

0 comments on commit b489691

Please sign in to comment.