示例#1
0
def _prog(shell_cmd):
    """Extract the program run by a shell command.

    :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)
示例#2
0
 def test_path_surgery(self, mock_debug):
     from certbot.plugins.util import path_surgery
     all_path = {"PATH": "/usr/local/bin:/bin/:/usr/sbin/:/usr/local/sbin/"}
     with mock.patch.dict('os.environ', all_path):
         with mock.patch('certbot.util.exe_exists') as mock_exists:
             mock_exists.return_value = True
             self.assertEqual(path_surgery("eg"), True)
             self.assertEqual(mock_debug.call_count, 0)
             self.assertEqual(os.environ["PATH"], all_path["PATH"])
     no_path = {"PATH": "/tmp/"}
     with mock.patch.dict('os.environ', no_path):
         path_surgery("thingy")
         self.assertEqual(mock_debug.call_count, 2)
         self.assertTrue("Failed to find" in mock_debug.call_args[0][0])
         self.assertTrue("/usr/local/bin" in os.environ["PATH"])
         self.assertTrue("/tmp" in os.environ["PATH"])
示例#3
0
 def test_path_surgery(self, mock_debug):
     from certbot.plugins.util import path_surgery
     all_path = {"PATH": "/usr/local/bin:/bin/:/usr/sbin/:/usr/local/sbin/"}
     with mock.patch.dict('os.environ', all_path):
         with mock.patch('certbot.util.exe_exists') as mock_exists:
             mock_exists.return_value = True
             self.assertEqual(path_surgery("eg"), True)
             self.assertEqual(mock_debug.call_count, 0)
             self.assertEqual(os.environ["PATH"], all_path["PATH"])
     no_path = {"PATH": "/tmp/"}
     with mock.patch.dict('os.environ', no_path):
         path_surgery("thingy")
         self.assertEqual(mock_debug.call_count, 2)
         self.assertTrue("Failed to find" in mock_debug.call_args[0][0])
         self.assertTrue("/usr/local/bin" in os.environ["PATH"])
         self.assertTrue("/tmp" in os.environ["PATH"])
示例#4
0
 def test_path_surgery(self, mock_debug):
     from certbot.plugins.util import path_surgery
     all_path = {"PATH": "/usr/local/bin:/bin/:/usr/sbin/:/usr/local/sbin/"}
     with mock.patch.dict('os.environ', all_path):
         with mock.patch('certbot.util.exe_exists') as mock_exists:
             mock_exists.return_value = True
             self.assertEqual(path_surgery("eg"), True)
             self.assertEqual(mock_debug.call_count, 0)
             self.assertEqual(os.environ["PATH"], all_path["PATH"])
     if os.name != 'nt':
         # This part is specific to Linux since on Windows no PATH surgery is ever done.
         no_path = {"PATH": "/tmp/"}
         with mock.patch.dict('os.environ', no_path):
             path_surgery("thingy")
             self.assertEqual(mock_debug.call_count, 2 if os.name != 'nt' else 1)
             self.assertTrue("Failed to find" in mock_debug.call_args[0][0])
             self.assertTrue("/usr/local/bin" in os.environ["PATH"])
             self.assertTrue("/tmp" in os.environ["PATH"])
示例#5
0
def verify_exe_exists(exe, message=None):
    """Ensures an executable with the given name is available.

    If an executable isn't found for the given path or name, extra
    directories are added to the user's PATH to help find system
    utilities that may not be available in the default cron PATH.

    :param str exe: executable path or name
    :param str message: Error message to print.

    :raises .NoInstallationError: when the executable isn't found

    """
    if message is None:
        message = "Cannot find executable '{0}'.".format(exe)
    if not (certbot_util.exe_exists(exe) or plugins_util.path_surgery(exe)):
        raise errors.NoInstallationError(message)
示例#6
0
def verify_exe_exists(exe, message=None):
    """Ensures an executable with the given name is available.

    If an executable isn't found for the given path or name, extra
    directories are added to the user's PATH to help find system
    utilities that may not be available in the default cron PATH.

    :param str exe: executable path or name
    :param str message: Error message to print.

    :raises .NoInstallationError: when the executable isn't found

    """
    if message is None:
        message = "Cannot find executable '{0}'.".format(exe)
    if not (certbot_util.exe_exists(exe) or plugins_util.path_surgery(exe)):
        raise errors.NoInstallationError(message)