Unverified Commit fa839565 authored by Reid's avatar Reid Committed by GitHub
Browse files

[Misc] Refactor: Improve argument handling for `conda` command (#20481)


Signed-off-by: default avatarreidliu41 <reid201711@gmail.com>
parent 75a99b98
...@@ -96,25 +96,30 @@ DEFAULT_PIP_PATTERNS = { ...@@ -96,25 +96,30 @@ DEFAULT_PIP_PATTERNS = {
def run(command): def run(command):
"""Return (return-code, stdout, stderr).""" """Return (return-code, stdout, stderr)."""
shell = True if type(command) is str else False shell = True if type(command) is str else False
p = subprocess.Popen(command, try:
stdout=subprocess.PIPE, p = subprocess.Popen(command,
stderr=subprocess.PIPE, stdout=subprocess.PIPE,
shell=shell) stderr=subprocess.PIPE,
raw_output, raw_err = p.communicate() shell=shell)
rc = p.returncode raw_output, raw_err = p.communicate()
if get_platform() == 'win32': rc = p.returncode
enc = 'oem' if get_platform() == 'win32':
else: enc = 'oem'
enc = locale.getpreferredencoding() else:
output = raw_output.decode(enc) enc = locale.getpreferredencoding()
if command == 'nvidia-smi topo -m': output = raw_output.decode(enc)
# don't remove the leading whitespace of `nvidia-smi topo -m` if command == 'nvidia-smi topo -m':
# because they are meaningful # don't remove the leading whitespace of `nvidia-smi topo -m`
output = output.rstrip() # because they are meaningful
else: output = output.rstrip()
output = output.strip() else:
err = raw_err.decode(enc) output = output.strip()
return rc, output, err.strip() err = raw_err.decode(enc)
return rc, output, err.strip()
except FileNotFoundError:
cmd_str = command if isinstance(command, str) else command[0]
return 127, '', f"Command not found: {cmd_str}"
def run_and_read_all(run_lambda, command): def run_and_read_all(run_lambda, command):
...@@ -148,7 +153,7 @@ def get_conda_packages(run_lambda, patterns=None): ...@@ -148,7 +153,7 @@ def get_conda_packages(run_lambda, patterns=None):
if patterns is None: if patterns is None:
patterns = DEFAULT_CONDA_PATTERNS patterns = DEFAULT_CONDA_PATTERNS
conda = os.environ.get('CONDA_EXE', 'conda') conda = os.environ.get('CONDA_EXE', 'conda')
out = run_and_read_all(run_lambda, "{} list".format(conda)) out = run_and_read_all(run_lambda, [conda, 'list'])
if out is None: if out is None:
return out return out
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment