diff --git a/certbot/hooks.py b/certbot/hooks.py index 6757c5a72..ae190a930 100644 --- a/certbot/hooks.py +++ b/certbot/hooks.py @@ -20,13 +20,19 @@ def validate_hooks(config): validate_hook(config.renew_hook, "renew") def _prog(shell_cmd): - """Extract the program run by a shell command""" - cmd = util.which(shell_cmd) - if not cmd: - plug_util.path_surgery(shell_cmd) - cmd = util.which(shell_cmd) + """Extract the program run by a shell command. - return os.path.basename(cmd) if cmd else None + :param str shell_cmd: command to be executed + + :returns: basename of command or None if the command isn't found + :rtype: str or None + + """ + if not util.exe_exists(shell_cmd): + plug_util.path_surgery(shell_cmd) + if not util.exe_exists(shell_cmd): + return None + return os.path.basename(shell_cmd) def validate_hook(shell_cmd, hook_name): @@ -117,6 +123,7 @@ def execute(shell_cmd): logger.error('Hook command "%s" returned error code %d', shell_cmd, cmd.returncode) if err: - logger.error('Error output from %s:\n%s', _prog(shell_cmd), err) + base_cmd = os.path.basename(shell_cmd.split(None, 1)[0]) + logger.error('Error output from %s:\n%s', base_cmd, err) return (err, out) diff --git a/certbot/tests/hook_test.py b/certbot/tests/hook_test.py index f799ed74f..a8f49745b 100644 --- a/certbot/tests/hook_test.py +++ b/certbot/tests/hook_test.py @@ -27,13 +27,13 @@ class HookTest(unittest.TestCase): config = mock.MagicMock(pre_hook="explodinator", post_hook="", renew_hook="") self.assertRaises(errors.HookCommandNotFound, hooks.validate_hooks, config) - @mock.patch('certbot.hooks.util.which') + @mock.patch('certbot.hooks.util.exe_exists') @mock.patch('certbot.hooks.plug_util.path_surgery') - def test_prog(self, mock_ps, mockwhich): - mockwhich.return_value = "/very/very/funky" + def test_prog(self, mock_ps, mock_exe_exists): + mock_exe_exists.return_value = True self.assertEqual(hooks._prog("funky"), "funky") self.assertEqual(mock_ps.call_count, 0) - mockwhich.return_value = None + mock_exe_exists.return_value = False self.assertEqual(hooks._prog("funky"), None) self.assertEqual(mock_ps.call_count, 1)