|
| 1 | +# coding=utf-8 |
| 2 | +"""Parse plugin metadata for plugin_dependencies |
| 3 | +
|
| 4 | +.. note:: This program is free software; you can redistribute it and/or modify |
| 5 | + it under the terms of the GNU General Public License as published by |
| 6 | + the Free Software Foundation; either version 2 of the License, or |
| 7 | + (at your option) any later version. |
| 8 | +
|
| 9 | +""" |
| 10 | + |
| 11 | +__author__ = 'elpaso@itopen.it' |
| 12 | +__date__ = '2018-05-29' |
| 13 | +__copyright__ = 'Copyright 2018, GISCE-TI S.L.' |
| 14 | + |
| 15 | +from configparser import NoOptionError, NoSectionError |
| 16 | +from .version_compare import compareVersions |
| 17 | +from . import installer as plugin_installer |
| 18 | +from qgis.utils import updateAvailablePlugins |
| 19 | + |
| 20 | + |
| 21 | +def __plugin_name_map(plugin_data_values): |
| 22 | + return { |
| 23 | + plugin['name']: plugin['id'] |
| 24 | + for plugin in plugin_data_values |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | +def __get_plugin_deps(plugin_id): |
| 29 | + |
| 30 | + result = {} |
| 31 | + parser = updateAvailablePlugins()[plugin_id] |
| 32 | + try: |
| 33 | + plugin_deps = parser.get('general', 'plugin_dependencies') |
| 34 | + except (NoOptionError, NoSectionError): |
| 35 | + return result |
| 36 | + |
| 37 | + for dep in plugin_deps.split(','): |
| 38 | + if dep.find('==') > 0: |
| 39 | + name, version_required = dep.split('==') |
| 40 | + else: |
| 41 | + name = dep |
| 42 | + version_required = None |
| 43 | + result[name] = version_required |
| 44 | + return result |
| 45 | + |
| 46 | + |
| 47 | +def find_dependencies(plugin_id, plugin_data=None, plugin_deps=None, installed_plugins=None): |
| 48 | + """Finds the plugin dependencies and checks if they can be installed or upgraded |
| 49 | +
|
| 50 | + :param plugin_id: plugin id |
| 51 | + :type plugin_id: str |
| 52 | + :param plugin_data: for testing only: dictionary of plugin data from the repo, defaults to None |
| 53 | + :param plugin_data: dict, optional |
| 54 | + :param plugin_deps: for testing only: dict of plugin id -> version_required, parsed from metadata value for "plugin_dependencies", defaults to None |
| 55 | + :param plugin_deps: dict, optional |
| 56 | + :param installed_plugins: for testing only: dict of plugin id -> version_installed |
| 57 | + :param installed_plugins: dict, optional |
| 58 | + :return: result dictionaries keyed by plugin name with: to_install, to_upgrade, not_found |
| 59 | + :rtype: tuple of dicts |
| 60 | + """ |
| 61 | + |
| 62 | + to_install = {} |
| 63 | + to_upgrade = {} |
| 64 | + not_found = {} |
| 65 | + |
| 66 | + if plugin_deps is None: |
| 67 | + plugin_deps = __get_plugin_deps(plugin_id) |
| 68 | + |
| 69 | + if installed_plugins is None: |
| 70 | + metadata_parser = updateAvailablePlugins() |
| 71 | + installed_plugins = {metadata_parser[k].get('general', 'name'): metadata_parser[k].get('general', 'version') for k, v in metadata_parser.items()} |
| 72 | + |
| 73 | + if plugin_data is None: |
| 74 | + plugin_data = plugin_installer.plugins.all() |
| 75 | + |
| 76 | + plugins_map = __plugin_name_map(plugin_data.values()) |
| 77 | + |
| 78 | + # Review all dependencies |
| 79 | + for name, version_required in plugin_deps.items(): |
| 80 | + try: |
| 81 | + p_id = plugins_map[name] |
| 82 | + except KeyError: |
| 83 | + not_found.update({name: { |
| 84 | + 'id': None, |
| 85 | + 'version_installed': None, |
| 86 | + 'version_required': None, |
| 87 | + 'version_available': None, |
| 88 | + 'action': None, |
| 89 | + 'error': 'missing_id' |
| 90 | + }}) |
| 91 | + continue |
| 92 | + |
| 93 | + affected_plugin = dict({ |
| 94 | + "id": p_id, |
| 95 | + # "version_installed": installed_plugins.get(p_id, {}).get('installed_plugins', None), |
| 96 | + "version_installed": installed_plugins.get(name, None), |
| 97 | + "version_required": version_required, |
| 98 | + "version_available": plugin_data[p_id].get('version_available', None), |
| 99 | + "action": None, |
| 100 | + }) |
| 101 | + |
| 102 | + # Install is needed |
| 103 | + if name not in installed_plugins: |
| 104 | + affected_plugin['action'] = 'install' |
| 105 | + destination_list = to_install |
| 106 | + # Upgrade is needed |
| 107 | + elif version_required is not None and compareVersions(installed_plugins[name], version_required) == 2: |
| 108 | + affected_plugin['action'] = 'upgrade' |
| 109 | + destination_list = to_upgrade |
| 110 | + # TODO @elpaso: review installed but not activated |
| 111 | + # No action is needed |
| 112 | + else: |
| 113 | + continue |
| 114 | + |
| 115 | + if affected_plugin['version_required'] == affected_plugin['version_available'] or affected_plugin['version_required'] is None: |
| 116 | + destination_list.update({name: affected_plugin}) |
| 117 | + else: |
| 118 | + affected_plugin['error'] = 'unavailable {}'.format(affected_plugin['action']) |
| 119 | + not_found.update({name: affected_plugin}) |
| 120 | + |
| 121 | + return to_install, to_upgrade, not_found |
0 commit comments