-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathutils.py
37 lines (32 loc) · 1.1 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Copyright (C) 2022 Anaconda, Inc
# Copyright (C) 2023 conda
# SPDX-License-Identifier: BSD-3-Clause
import sys
from pathlib import Path
from subprocess import CompletedProcess, run
from ruamel.yaml import YAML
def conda_subprocess(*args, explain=False, capture_output=True, **kwargs) -> CompletedProcess:
cmd = [sys.executable, "-m", "conda", *[str(a) for a in args]]
check = kwargs.pop("check", True)
if explain:
print("+", " ".join(cmd))
p = run(
cmd,
capture_output=capture_output,
text=kwargs.pop("text", capture_output),
check=False,
**kwargs,
)
if capture_output and (explain or p.returncode):
print(p.stdout)
print(p.stderr, file=sys.stderr)
if check:
p.check_returncode()
return p
def write_env_config(prefix, force=False, **kwargs):
condarc = Path(prefix) / ".condarc"
if condarc.is_file() and not force:
raise RuntimeError(f"File {condarc} already exists. Use force=True to overwrite.")
yaml = YAML(typ="unsafe", pure=True)
with open(condarc, "w") as f:
yaml.dump(kwargs, f)