Example #1
0
    def uninstall(self, package_name, ignore_missing=True):
        """
        Uninstall the plugin from the current virtualenv. By default this
        operation will fail when trying to uninstall a plugin that is not
        installed, use `ignore_missing` to change this behavior.

        :param package_name: the package name as stated in the setup.py file
        :param ignore_missing: ignore failures in uninstalling missing plugins.
        """

        if not ignore_missing:
            self.runner.run('{0} uninstall -y {1}'.format(
                utils.get_pip_path(), package_name))
        else:
            out = self.runner.run('{0} freeze'.format(
                utils.get_pip_path())).std_out
            packages = []
            for line in out.splitlines():
                packages.append(line.split('==')[0])
            if package_name in packages:
                self.runner.run('{0} uninstall -y {1}'.format(
                    utils.get_pip_path(), package_name))
            else:
                self.logger.info(
                    '{0} not installed. Nothing to do'.format(package_name))
Example #2
0
    def uninstall(self, package_name, ignore_missing=True):
        """
        Uninstall the plugin from the current virtualenv. By default this
        operation will fail when trying to uninstall a plugin that is not
        installed, use `ignore_missing` to change this behavior.

        :param package_name: the package name as stated in the setup.py file
        :param ignore_missing: ignore failures in uninstalling missing plugins.
        """

        if not ignore_missing:
            self.runner.run('{0} uninstall -y {1}'.format(
                utils.get_pip_path(), package_name))
        else:
            out = self.runner.run(
                '{0} freeze'.format(utils.get_pip_path())).std_out
            packages = []
            for line in out.splitlines():
                packages.append(line.split('==')[0])
            if package_name in packages:
                self.runner.run('{0} uninstall -y {1}'.format(
                    utils.get_pip_path(), package_name))
            else:
                self.logger.info('{0} not installed. Nothing to do'
                                 .format(package_name))
 def _assert_plugin_not_installed(self, plugin_name):
     out = self.runner.run('{0} freeze'.format(
         utils.get_pip_path())).std_out
     packages = []
     for line in out.splitlines():
         packages.append(line.split('==')[0])
     self.assertNotIn(plugin_name, packages)
Example #4
0
    def _list_plugin_files(self, plugin_name):

        """
        Retrieves python files related to the plugin.
        __init__ file are filtered out.

        :param plugin_name: The plugin name.

        :return: A list of file paths.
        :rtype: list of str
        """

        module_paths = []
        runner = LocalCommandRunner(self._logger)

        files = runner.run(
            '{0} show -f {1}'
            .format(utils.get_pip_path(), plugin_name)
        ).std_out.splitlines()
        for module in files:
            if self._is_valid_module(module):
                # the file paths are relative to the
                # package __init__.py file.
                prefix = '../' if os.name == 'posix' else '..\\'
                module_paths.append(
                    module.replace(prefix, '')
                    .replace(os.sep, '.').replace('.py', '').strip())
        return module_paths
 def _assert_plugin_not_installed(self, plugin_name):
     out = self.runner.run('{0} freeze'
                           .format(utils.get_pip_path())).std_out
     packages = []
     for line in out.splitlines():
         packages.append(line.split('==')[0])
     self.assertNotIn(plugin_name, packages)
Example #6
0
    def install(self, source, args=''):

        """
        Install the plugin to the current virtualenv.

        :param source: URL to the plugin. Any pip acceptable URL is ok.
        :param args: extra installation arguments passed to the pip command
        """

        plugin_dir = None
        try:
            if os.path.isabs(source):
                plugin_dir = source
            else:
                self.logger.debug('Extracting archive: {0}'.format(source))
                plugin_dir = extract_package_to_dir(source)
            self.logger.debug('Installing from directory: {0} '
                              '[args={1}]'.format(plugin_dir, args))
            command = '{0} install {1} {2}'.format(
                get_pip_path(), plugin_dir, args)
            self.runner.run(command, cwd=plugin_dir)
            package_name = extract_package_name(plugin_dir)
            self.logger.debug('Retrieved package name: {0}'
                              .format(package_name))
        finally:
            if plugin_dir and not os.path.isabs(source):
                self.logger.debug('Removing directory: {0}'
                                  .format(plugin_dir))
                shutil.rmtree(plugin_dir)

        return package_name
Example #7
0
def extract_package_to_dir(package_url):
    """
    Using a subprocess to extract a pip package to a temporary directory.
    :param: package_url: the URL of the package source.
    :return: the directory the package was extracted to.

    """
    plugin_dir = None
    archive_dir = tempfile.mkdtemp()
    runner = LocalCommandRunner()

    try:
        # We run `pip download` command in a subprocess to support
        # multi-threaded scenario (i.e snapshot restore).
        # We don't use `curl` because pip can handle different kinds of files,
        # including .git.
        command = [get_pip_path(), 'download', '-d',
                   archive_dir, '--no-deps', package_url]
        runner.run(command=command)
        archive = _get_archive(archive_dir, package_url)
        plugin_dir_parent = extract_archive(archive)
        plugin_dir = _get_plugin_path(plugin_dir_parent, package_url)

    except NonRecoverableError as e:
        if plugin_dir and os.path.exists(plugin_dir):
            shutil.rmtree(plugin_dir)
        raise e

    finally:
        if os.path.exists(archive_dir):
            shutil.rmtree(archive_dir)

    return plugin_dir
Example #8
0
    def _pip_install(self, source, args, constraint):
        plugin_dir = None
        try:
            if os.path.isabs(source):
                plugin_dir = source
            else:
                self.logger.debug('Extracting archive: {0}'.format(source))
                plugin_dir = extract_package_to_dir(source)
            package_name = extract_package_name(plugin_dir)
            if self._package_installed_in_agent_env(constraint, package_name):
                self.logger.warn('Skipping source plugin {0} installation, '
                                 'as the plugin is already installed in the '
                                 'agent virtualenv.'.format(package_name))
                return
            self.logger.debug('Installing from directory: {0} '
                              '[args={1}, package_name={2}]'
                              .format(plugin_dir, args, package_name))
            command = [get_pip_path(), 'install'] + args + [plugin_dir]

            self.runner.run(command=command, cwd=plugin_dir)
            self.logger.debug('Retrieved package name: {0}'
                              .format(package_name))
        except CommandExecutionException as e:
            self.logger.debug('Failed running pip install. Output:\n{0}'
                              .format(e.output))
            raise exceptions.PluginInstallationError(
                'Failed running pip install. ({0})'.format(e.error))
        finally:
            if plugin_dir and not os.path.isabs(source):
                self.logger.debug('Removing directory: {0}'
                                  .format(plugin_dir))
                self._rmtree(plugin_dir)
def extract_package_to_dir(package_url):
    """
    Using a subprocess to extract a pip package to a temporary directory.
    :param: package_url: the URL of the package source.
    :return: the directory the package was extracted to.

    """
    plugin_dir = None
    archive_dir = tempfile.mkdtemp()
    runner = LocalCommandRunner()

    try:
        # We run `pip download` command in a subprocess to support
        # multi-threaded scenario (i.e snapshot restore).
        # We don't use `curl` because pip can handle different kinds of files,
        # including .git.
        command = [get_pip_path(), 'download', '-d',
                   archive_dir, '--no-deps', package_url]
        runner.run(command=command)
        archive = _get_archive(archive_dir, package_url)
        plugin_dir_parent = extract_archive(archive)
        plugin_dir = _get_plugin_path(plugin_dir_parent, package_url)

    except NonRecoverableError as e:
        if plugin_dir and os.path.exists(plugin_dir):
            shutil.rmtree(plugin_dir)
        raise e

    finally:
        if os.path.exists(archive_dir):
            shutil.rmtree(archive_dir)

    return plugin_dir
    def _pip_install(self, source, args, constraint):
        plugin_dir = None
        try:
            if os.path.isabs(source):
                plugin_dir = source
            else:
                self.logger.debug('Extracting archive: {0}'.format(source))
                plugin_dir = extract_package_to_dir(source)
            package_name = extract_package_name(plugin_dir)
            if self._package_installed_in_agent_env(constraint, package_name):
                self.logger.warn('Skipping source plugin {0} installation, '
                                 'as the plugin is already installed in the '
                                 'agent virtualenv.'.format(package_name))
                return
            self.logger.debug('Installing from directory: {0} '
                              '[args={1}, package_name={2}]'
                              .format(plugin_dir, args, package_name))
            command = [get_pip_path(), 'install'] + args + [plugin_dir]

            self.runner.run(command=command, cwd=plugin_dir)
            self.logger.debug('Retrieved package name: {0}'
                              .format(package_name))
        except CommandExecutionException as e:
            self.logger.debug('Failed running pip install. Output:\n{0}'
                              .format(e.output))
            raise exceptions.PluginInstallationError(
                'Failed running pip install. ({0})'.format(e.error))
        finally:
            if plugin_dir and not os.path.isabs(source):
                self.logger.debug('Removing directory: {0}'
                                  .format(plugin_dir))
                self._rmtree(plugin_dir)
Example #11
0
 def _pip_install(self, source, args, constraint):
     plugin_dir = None
     try:
         if os.path.isabs(source):
             plugin_dir = source
         else:
             self.logger.debug('Extracting archive: {0}'.format(source))
             plugin_dir = extract_package_to_dir(source)
         package_name = extract_package_name(plugin_dir)
         if self._package_installed_in_agent_env(constraint, package_name):
             self.logger.warn('Skipping source plugin {0} installation, '
                              'as the plugin is already installed in the '
                              'agent virtualenv.'.format(package_name))
             return
         self.logger.debug('Installing from directory: {0} '
                           '[args={1}, package_name={2}]'
                           .format(plugin_dir, args, package_name))
         command = '{0} install {1} {2}'.format(
             get_pip_path(), plugin_dir, args)
         self.runner.run(command, cwd=plugin_dir)
         self.logger.debug('Retrieved package name: {0}'
                           .format(package_name))
     finally:
         if plugin_dir and not os.path.isabs(source):
             self.logger.debug('Removing directory: {0}'
                               .format(plugin_dir))
             self._rmtree(plugin_dir)
Example #12
0
 def _pip_install(self, source, args, constraint):
     plugin_dir = None
     try:
         if os.path.isabs(source):
             plugin_dir = source
         else:
             self.logger.debug('Extracting archive: {0}'.format(source))
             plugin_dir = extract_package_to_dir(source)
         package_name = extract_package_name(plugin_dir)
         if self._package_installed_in_agent_env(constraint, package_name):
             self.logger.warn('Skipping source plugin {0} installation, '
                              'as the plugin is already installed in the '
                              'agent virtualenv.'.format(package_name))
             return
         self.logger.debug('Installing from directory: {0} '
                           '[args={1}, package_name={2}]'
                           .format(plugin_dir, args, package_name))
         command = '{0} install {1} {2}'.format(
             get_pip_path(), plugin_dir, args)
         self.runner.run(command, cwd=plugin_dir)
         self.logger.debug('Retrieved package name: {0}'
                           .format(package_name))
     finally:
         if plugin_dir and not os.path.isabs(source):
             self.logger.debug('Removing directory: {0}'
                               .format(plugin_dir))
             self._rmtree(plugin_dir)
Example #13
0
    def _list_plugin_files(self, plugin_name):
        """
        Retrieves python files related to the plugin.
        __init__ file are filtered out.

        :param plugin_name: The plugin name.

        :return: A list of file paths.
        :rtype: list of str
        """

        module_paths = []
        runner = LocalCommandRunner(self._logger)

        files = runner.run('{0} show -f {1}'.format(
            utils.get_pip_path(), plugin_name)).std_out.splitlines()
        for module in files:
            if self._is_valid_module(module):
                # the file paths are relative to the
                # package __init__.py file.
                prefix = '../' if os.name == 'posix' else '..\\'
                module_paths.append(
                    module.replace(prefix,
                                   '').replace(os.sep,
                                               '.').replace('.py', '').strip())
        return module_paths
 def _assert_plugin_installed(self, plugin_name, dependencies=None):
     if not dependencies:
         dependencies = []
     out = self.runner.run('{0} freeze'.format(
         utils.get_pip_path())).std_out
     packages = []
     for line in out.splitlines():
         packages.append(line.split('==')[0])
     self.assertIn(plugin_name, out)
     for dependency in dependencies:
         self.assertIn(dependency, packages)
 def _assert_plugin_installed(self, plugin_name, dependencies=None):
     if not dependencies:
         dependencies = []
     out = self.runner.run('{0} freeze'
                           .format(utils.get_pip_path())).std_out
     packages = []
     for line in out.splitlines():
         packages.append(line.split('==')[0])
     self.assertIn(plugin_name, out)
     for dependency in dependencies:
         self.assertIn(dependency, packages)
Example #16
0
 def _pip_install(self, source, args):
     plugin_dir = None
     try:
         if os.path.isabs(source):
             plugin_dir = source
         else:
             self.logger.debug('Extracting archive: {0}'.format(source))
             plugin_dir = extract_package_to_dir(source)
         self.logger.debug('Installing from directory: {0} '
                           '[args={1}]'.format(plugin_dir, args))
         command = '{0} install {1} {2}'.format(get_pip_path(), plugin_dir,
                                                args)
         self.runner.run(command, cwd=plugin_dir)
         package_name = extract_package_name(plugin_dir)
         self.logger.debug(
             'Retrieved package name: {0}'.format(package_name))
     finally:
         if plugin_dir and not os.path.isabs(source):
             self.logger.debug('Removing directory: {0}'.format(plugin_dir))
             shutil.rmtree(plugin_dir, ignore_errors=True)
     return package_name
Example #17
0
 def _pip_install(self, source, args):
     plugin_dir = None
     try:
         if os.path.isabs(source):
             plugin_dir = source
         else:
             self.logger.debug('Extracting archive: {0}'.format(source))
             plugin_dir = extract_package_to_dir(source)
         self.logger.debug('Installing from directory: {0} '
                           '[args={1}]'.format(plugin_dir, args))
         command = '{0} install {1} {2}'.format(
             get_pip_path(), plugin_dir, args)
         self.runner.run(command, cwd=plugin_dir)
         package_name = extract_package_name(plugin_dir)
         self.logger.debug('Retrieved package name: {0}'
                           .format(package_name))
     finally:
         if plugin_dir and not os.path.isabs(source):
             self.logger.debug('Removing directory: {0}'
                               .format(plugin_dir))
             shutil.rmtree(plugin_dir, ignore_errors=True)
     return package_name
 def _pip_freeze(self):
     try:
         return self.runner.run([get_pip_path(), 'freeze', '--all']).std_out
     except CommandExecutionException as e:
         raise exceptions.PluginInstallationError(
             'Failed running pip freeze. ({0})'.format(e))
Example #19
0
 def _pip_freeze(self):
     try:
         return self.runner.run('{0} freeze'.format(get_pip_path())).std_out
     except CommandExecutionException as e:
         raise exceptions.PluginInstallationError(
             'Failed running pip freeze. ({0})'.format(e))