Skip to content

Commit

Permalink
Remove -n and -p flags in favor of a positional argument like `co…
Browse files Browse the repository at this point in the history
…nda activate` (#12)
  • Loading branch information
jaimergp authored Jan 29, 2025
1 parent f8b8e6b commit 84158ba
Show file tree
Hide file tree
Showing 8 changed files with 3,515 additions and 7,564 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ jobs:
- uses: prefix-dev/setup-pixi@ba3bb36eb2066252b2363392b7739741bb777659 # v0.8.1
with:
environments: docs
- name: Setup project
run: |
pixi run --environment docs dev
- name: Build docs
run: pixi run docs
- name: Upload artifact
Expand Down
4 changes: 0 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ jobs:
environments: ${{ env.PIXI_ENV_NAME }}
- name: Setup project
run: |
pixi run --environment ${{ env.PIXI_ENV_NAME }} dev
echo "channels: [conda-forge]" > .pixi/envs/${{ env.PIXI_ENV_NAME }}/.condarc
pixi run --environment ${{ env.PIXI_ENV_NAME }} conda info
- name: Run tests
Expand All @@ -68,8 +67,5 @@ jobs:
- uses: prefix-dev/setup-pixi@ba3bb36eb2066252b2363392b7739741bb777659 # v0.8.1
with:
environments: build
- name: Setup project
run: |
pixi run --environment build dev
- name: Build recipe
run: pixi run --environment build build
20 changes: 15 additions & 5 deletions conda_spawn/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@
import os
from textwrap import dedent

from conda.exceptions import CondaError, ArgumentError
from conda.base.context import locate_prefix_by_name
from conda.exceptions import CondaError, ArgumentError, EnvironmentLocationNotFound
from conda.cli.conda_argparse import (
add_parser_help,
add_parser_prefix,
)


def configure_parser(parser: argparse.ArgumentParser):
from .shell import SHELLS

add_parser_help(parser)
add_parser_prefix(parser, prefix_required=True)

parser.add_argument(
"environment",
help="Environment to activate. Can be either a name or a path. Paths are only detected "
"if they contain a (back)slash. Use the ./env idiom environments in working directory.",
)
parser.add_argument(
"command",
metavar="COMMAND [args]",
Expand Down Expand Up @@ -74,7 +78,6 @@ def execute(args: argparse.Namespace) -> int:
from .main import (
hook,
spawn,
environment_speficier_to_path,
shell_specifier_to_shell,
)

Expand All @@ -95,15 +98,22 @@ def execute(args: argparse.Namespace) -> int:
dedent(
f"""
Detected active 'conda spawn' session{env_info}.
Nested activation is disallowed by default.
Please exit the current session before starting a new one by running 'exit'.
Alternatively, check the usage of --replace and/or --stack.
"""
).lstrip()
)

prefix = environment_speficier_to_path(args.name, args.prefix)
if "/" in args.environment or "\\" in args.environment:
prefix = os.path.expanduser(os.path.expandvars(args.environment))
if not os.path.isfile(os.path.join(args.environment, "conda-meta", "history")):
raise EnvironmentLocationNotFound(prefix)
else:
prefix = locate_prefix_by_name(args.environment)
shell = shell_specifier_to_shell(args.shell)

if args.hook:
if args.command:
raise ArgumentError("COMMAND cannot be provided with --hook.")
Expand Down
4 changes: 2 additions & 2 deletions docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The typical workflow looks like this:
python not found
~ $ which conda
/Users/user/Miniforge/condabin/conda
~ $ conda spawn -n base
~ $ conda spawn base
(base) ~ $ which python
/Users/user/Miniforge/bin/python
(base) ~ $ python my_project.py
Expand Down Expand Up @@ -45,7 +45,7 @@ After this, you might want to {ref}`shell-cleanup`.
To activate an environment named `my-project`:

```bash
conda spawn -n my-project
conda spawn my-project
```

To deactivate, exit the process with <kbd>Ctrl</kbd>+<kbd>D</kbd>, or run the command `exit`.
Expand Down
11,008 changes: 3,481 additions & 7,527 deletions pixi.lock

Large diffs are not rendered by default.

26 changes: 10 additions & 16 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,13 @@ conda = ">=23.9"
pexpect = "*"
shellingham = "*"

[tool.pixi.pypi-dependencies]
conda-spawn = { path = ".", editable = true }

[tool.pixi.feature.build]
dependencies = { conda-build = "*" }
tasks = { build = "conda build recipe" }

[tool.pixi.feature.dev.dependencies]
pip = "*"
hatchling = "*"
hatch-vcs = "*"

[tool.pixi.feature.dev.tasks]
dev = "python -m pip install -e . --no-deps --no-build-isolation"
spawn = "python -m conda spawn"

[tool.pixi.feature.docs.tasks]
docs = { cmd = "python -m sphinx.cmd.build -M dirhtml . _build", cwd = "docs" }
serve = { cmd = "python -m http.server", cwd = "docs/_build/dirhtml" }
Expand Down Expand Up @@ -99,13 +93,13 @@ python = "3.11.*"
python = "3.12.*"

[tool.pixi.environments]
dev = ["dev", "py39"]
build = ["dev", "build"]
docs = ["dev", "docs"]
test-py39 = ["dev", "test", "py39"]
test-py310 = ["dev", "test", "py310"]
test-py311 = ["dev", "test", "py311"]
test-py312 = ["dev", "test", "py312"]
dev = ["py39"]
build = ["build"]
docs = ["docs"]
test-py39 = ["test", "py39"]
test-py310 = ["test", "py310"]
test-py311 = ["test", "py311"]
test-py312 = ["test", "py312"]

[tool.hatch.version]
source = "vcs"
Expand Down
6 changes: 3 additions & 3 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ def test_cli(monkeypatch, conda_cli):

def test_nesting_disallowed(monkeypatch, conda_cli):
monkeypatch.setenv("CONDA_SPAWN", "1")
conda_cli("spawn", "-p", sys.prefix, "--hook", raises=CondaError)
conda_cli("spawn", sys.prefix, "--hook", raises=CondaError)


def test_nesting_replace(monkeypatch, conda_cli):
monkeypatch.setenv("CONDA_SPAWN", "1")
out, err, rc = conda_cli("spawn", "-p", sys.prefix, "--hook", "--replace")
out, err, rc = conda_cli("spawn", sys.prefix, "--hook", "--replace")
assert sys.prefix in out
assert not err
assert not rc


def test_nesting_stack(monkeypatch, conda_cli):
monkeypatch.setenv("CONDA_SPAWN", "1")
out, err, rc = conda_cli("spawn", "-p", sys.prefix, "--hook", "--stack")
out, err, rc = conda_cli("spawn", sys.prefix, "--hook", "--stack")
assert sys.prefix in out
assert not err
assert not rc
8 changes: 4 additions & 4 deletions tests/test_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_cmd(simple_env):


def test_hooks(conda_cli, simple_env):
out, err, rc = conda_cli("spawn", "--hook", "-p", simple_env)
out, err, rc = conda_cli("spawn", "--hook", simple_env)
print(out)
print(err, file=sys.stderr)
assert not rc
Expand All @@ -75,7 +75,7 @@ def test_hooks(conda_cli, simple_env):

@pytest.mark.skipif(sys.platform == "win32", reason="Only tested on Unix")
def test_hooks_integration_posix(simple_env, tmp_path):
hook = f"{sys.executable} -m conda spawn --hook --shell posix -p '{simple_env}'"
hook = f"{sys.executable} -m conda spawn --hook --shell posix '{simple_env}'"
script = f'eval "$({hook})"\nenv | sort'
script_path = tmp_path / "script-eval.sh"
script_path.write_text(script)
Expand All @@ -87,7 +87,7 @@ def test_hooks_integration_posix(simple_env, tmp_path):

@pytest.mark.skipif(sys.platform != "win32", reason="Powershell only tested on Windows")
def test_hooks_integration_powershell(simple_env, tmp_path):
hook = f"{sys.executable} -m conda spawn --hook --shell powershell -p {simple_env}"
hook = f"{sys.executable} -m conda spawn --hook --shell powershell {simple_env}"
script = f"{hook} | Out-String | Invoke-Expression\r\nls env:"
script_path = tmp_path / "script-eval.ps1"
script_path.write_text(script)
Expand All @@ -99,7 +99,7 @@ def test_hooks_integration_powershell(simple_env, tmp_path):

@pytest.mark.skipif(sys.platform != "win32", reason="Cmd.exe only tested on Windows")
def test_hooks_integration_cmd(simple_env, tmp_path):
hook = f"{sys.executable} -m conda spawn --hook --shell cmd -p {simple_env}"
hook = f"{sys.executable} -m conda spawn --hook --shell cmd {simple_env}"
script = f"FOR /F \"tokens=*\" %%g IN ('{hook}') do @CALL %%g\r\nset"
script_path = tmp_path / "script-eval.bat"
script_path.write_text(script)
Expand Down

0 comments on commit 84158ba

Please sign in to comment.