def list_plugins(workers=4): """ List installed plugins. Returns a list of plugin dicts with basic information about the plugin and it's versions. Two additional fields exists in these dicts; installed_version and installed_file. """ fields = ('slug,plugin_name,versions.hard_dependencies,versions.type,' 'versions.version,versions.download,versions.filename,' 'versions.md5,versions.slug') plugins = parse_installed_plugins(workers) if plugins == []: return [] results = bukget.search( { 'field': 'versions.checksum', 'action': 'in', 'value': [plugin[0] for plugin in plugins] }, fields=fields) if len(results) < len(plugins): results += bukget.search( { 'field': 'main', 'action': 'in', 'value': [plugin[1] for plugin in plugins] }, fields=fields) if len(results) < len(plugins): results += bukget.search( { 'field': 'plugin_name', 'action': 'in', 'value': [plugin[2] for plugin in plugins] }, fields=fields) results = utils.remove_duplicate_plugins(results) for plugin in results: i_plugin = None for installed in plugins: if installed[2] == plugin['plugin_name']: plugin['installed_version'] = installed[3] plugin['installed_file'] = installed[4] i_plugin = installed if i_plugin is not None: plugins.remove(i_plugin) return results
def search(query, size): """ Search for plugins. This function will search for plugins on bukget by their slug and name. The results are ordered by a score calculated with their levenshtein edit distance from the search query and their popularity. Two parameters are required: query The query to search for. size The size of the returned results. It can be negative, and in that case the bottom of the results are returned. A list with the plugin dicts from BukGet is returned. It is sorted by the score system mentioned earlier. """ sorting = ('-' if size >= 0 else '')+'popularity.monthly' search_results = bukget.search( { 'field': 'plugin_name', 'action': 'like', 'value': query }, sort=sorting, fields='slug,plugin_name,description,popularity.monthly', size=abs(size)) search_results += bukget.search( { 'field': 'slug', 'action': 'like', 'value': query }, sort=sorting, fields='slug,plugin_name,description,popularity.monthly', size=abs(size)) search_results = utils.remove_duplicate_plugins(search_results) # Calculate scores results = list() for plugin in search_results: score = plugin['popularity']['monthly'] distance = common.levenshtein( query, plugin['plugin_name']) score -= distance results.append((score, plugin)) results.sort(key=lambda x: x[0], reverse=True) return [result[1] for result in results]
def search(query, size): """ Search for plugins. This function will search for plugins on bukget by their slug and name. The results are ordered by a score calculated with their levenshtein edit distance from the search query and their popularity. Two parameters are required: query The query to search for. size The size of the returned results. It can be negative, and in that case the bottom of the results are returned. A list with the plugin dicts from BukGet is returned. It is sorted by the score system mentioned earlier. """ sorting = ('-' if size >= 0 else '') + 'popularity.monthly' search_results = bukget.search( { 'field': 'plugin_name', 'action': 'like', 'value': query }, sort=sorting, fields='slug,plugin_name,description,popularity.monthly', size=abs(size)) search_results += bukget.search( { 'field': 'slug', 'action': 'like', 'value': query }, sort=sorting, fields='slug,plugin_name,description,popularity.monthly', size=abs(size)) search_results = utils.remove_duplicate_plugins(search_results) # Calculate scores results = list() for plugin in search_results: score = plugin['popularity']['monthly'] distance = common.levenshtein(query, plugin['plugin_name']) score -= distance results.append((score, plugin)) results.sort(key=lambda x: x[0], reverse=True) return [result[1] for result in results]
def dependencies(server, plugins, v_type='Latest', deps=True): """ Resolve dependencies. This function will return a list of plugin dictionaries of all plugins depending directly, or indirectly on the plugins in `plugins`. `plugins` may be a list of plugin slugs, or a single plugin slug. """ fields = ('slug,plugin_name,versions.hard_dependencies,versions.type,' 'versions.version,versions.download,versions.filename,' 'versions.md5') if type(plugins) is not list: plugins = [plugins] plugins, versions = utils.extract_name_version(plugins) results = bukget.search( { 'field': 'plugin_name', 'action': 'in', 'value': plugins }, fields=fields) results += bukget.search( { 'field': 'slug', 'action': 'in', 'value': plugins }, fields=fields) plugins = utils.remove_duplicate_plugins(results) for plugin in plugins: if not plugin['plugin_name'].lower() in versions: continue version = versions[plugin['plugin_name'].lower()] for ver in plugin['versions']: if ver['version'] == version: plugin['versions'] = [ver] break else: plugin['versions'] = [] if deps: return _dependencies(server, plugins, v_type=v_type) else: return plugins
def _dependencies(server, plugins, stack=None, v_type='Latest'): """ Undocumented recursive, internal function. """ fields = ('slug,plugin_name,versions.hard_dependencies,versions.type,' 'versions.version,versions.download,versions.filename,' 'versions.md5') # Sanitizing of parameters if stack is None: stack = plugins[:] v_type = v_type.capitalize() # Resolve all the dependencies of the plugins deps = utils.extract_dependencies(plugins, v_type) # Filter out dependencies allready in the stack and # add all dependencies to the stack # From this point on the stack contains all dependencies resolved so far, # and dependencies contains all dependencies that are new of this level. for dependency in deps: for installed in stack: if installed['plugin_name'] == dependency: deps.remove(dependency) break # If there are any plugins whose dependencies are not in the stack if len(deps) > 0: search_results = bukget.search( { 'field': 'plugin_name', 'action': 'in', 'value': deps }, fields=fields) for plugin in search_results: for in_stack in stack: if in_stack['slug'] == plugin['slug']: break else: stack.append(plugin) _dependencies(server, search_results, stack, v_type) return stack
def find_versions(plugins): """ Get plugin dictionaries from BukGet only with the version. This function takes a list of tuples containing plugin slugs in the first slot and version slugs in the second slot. Returned is a list of plugin dictionaries. Plugins which are not found, are just ignored. """ fields = ('versions.slug,versions.md5,versions.download,versions.filename,' 'plugin_name,slug') results = bukget.search( { 'field': 'slug', 'action': 'in', 'value': [plugin[0] for plugin in plugins] }, fields=fields) final = list() for plugin in results: target_version = None for slug, version in plugins: if slug == plugin['slug']: target_version = version break else: continue version = None for vers in plugin['versions']: if vers['slug'] == target_version: version = vers break else: continue plugin['versions'] = [version] final.append(plugin) return final