From 72618e374d6055d313f407a2948d50ef079a820d Mon Sep 17 00:00:00 2001 From: Hugo Osvaldo Barrera Date: Mon, 14 Jun 2021 22:50:22 +0200 Subject: [PATCH] Fix double-use of a generation The first use exhausted it, so the second iteration was empty. --- vdirsyncer/cli/fetchparams.py | 17 +++++++++-------- vdirsyncer/utils.py | 3 ++- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/vdirsyncer/cli/fetchparams.py b/vdirsyncer/cli/fetchparams.py index 3d15119..6a39785 100644 --- a/vdirsyncer/cli/fetchparams.py +++ b/vdirsyncer/cli/fetchparams.py @@ -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): diff --git a/vdirsyncer/utils.py b/vdirsyncer/utils.py index c2130a0..5883030 100644 --- a/vdirsyncer/utils.py +++ b/vdirsyncer/utils.py @@ -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