Skip to content

Commit

Permalink
Refactor is_valid_prefix into validate_prefix (conda#11172)
Browse files Browse the repository at this point in the history
  • Loading branch information
kenodegard authored Jan 28, 2022
1 parent fc56fde commit f64f1f3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
11 changes: 10 additions & 1 deletion conda/cli/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,18 @@ def check_non_admin():
on your system for non-privileged users.
"""))

def is_valid_prefix(prefix):
def validate_prefix(prefix):
"""Verifies the prefix is a valid conda environment.
:raises EnvironmentLocationNotFound: Non-existent path or not a directory.
:raises DirectoryNotACondaEnvironmentError: Directory is not a conda environment.
:returns: Valid prefix.
:rtype: str
"""
if isdir(prefix):
if not isfile(join(prefix, 'conda-meta', 'history')):
raise DirectoryNotACondaEnvironmentError(prefix)
else:
raise EnvironmentLocationNotFound(prefix)

return prefix
14 changes: 9 additions & 5 deletions conda/cli/main_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from ..gateways.disk.delete import rm_rf
from ..common.compat import encode_environment
from ..gateways.subprocess import subprocess_call
from .common import is_valid_prefix
from .common import validate_prefix


def execute(args, parser):
Expand All @@ -19,11 +19,15 @@ def execute(args, parser):
call = args.executable_call
cwd = args.cwd
no_capture_output = args.no_capture_output
prefix = context.target_prefix or os.getenv("CONDA_PREFIX") or context.root_prefix
is_valid_prefix(prefix)

script_caller, command_args = wrap_subprocess_call(on_win, context.root_prefix, prefix,
args.dev, args.debug_wrapper_scripts, call)
script_caller, command_args = wrap_subprocess_call(
on_win,
context.root_prefix,
validate_prefix(context.target_prefix or os.getenv("CONDA_PREFIX") or context.root_prefix),
args.dev,
args.debug_wrapper_scripts,
call,
)
env = encode_environment(os.environ.copy())

response = subprocess_call(command_args, env=env, path=cwd, raise_on_error=False,
Expand Down
19 changes: 19 additions & 0 deletions news/gh-11172-validate-prefix
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### Enhancements

* Refactored `is_valid_prefix` into `validate_prefix`.

### Bug fixes

* <news item>

### Deprecations

* <news item>

### Docs

* <news item>

### Other

* <news item>

0 comments on commit f64f1f3

Please sign in to comment.