Merge pull request #904 from pimutils/improve-error-msg

Fix double-use of a generator
This commit is contained in:
Hugo Osvaldo Barrera 2021-06-19 13:11:24 +02:00 committed by GitHub
commit 722dace828
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 9 deletions

View file

@ -74,20 +74,21 @@ def _fetch_value(opts, key):
return rv
def _strategy_command(*command):
def _strategy_command(*command: str):
"""Execute a user-specified command and return its output."""
import subprocess
# normalize path of every path member
# if there is no path specified then nothing will happen
command = map(expand_path, command)
# Normalize path of every path member.
# If there is no path specified then nothing will happen.
# Makes this a list to avoid it being exhausted on the first iteration.
expanded_command = list(map(expand_path, command))
try:
stdout = subprocess.check_output(command, universal_newlines=True)
stdout = subprocess.check_output(expanded_command, universal_newlines=True)
return stdout.strip("\n")
except OSError as e:
raise exceptions.UserError(
"Failed to execute command: {}\n{}".format(" ".join(command), str(e))
)
cmd = " ".join(expanded_command)
raise exceptions.UserError(f"Failed to execute command: {cmd}\n{str(e)}")
def _strategy_prompt(text):

View file

@ -19,7 +19,8 @@ SAFE_UID_CHARS = (
_missing = object()
def expand_path(p):
def expand_path(p: str) -> str:
"""Expand $HOME in a path and normalise slashes."""
p = os.path.expanduser(p)
p = os.path.normpath(p)
return p