comparison env/lib/python3.9/site-packages/ephemeris/shed_tools_methods.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 from bioblend.toolshed import ToolShedInstance
2
3
4 VALID_KEYS = [
5 "name",
6 "owner",
7 "changeset_revision",
8 "tool_panel_section_id",
9 "tool_panel_section_label",
10 "tool_shed_url",
11 "install_repository_dependencies",
12 "install_resolver_dependencies",
13 "install_tool_dependencies"
14 ]
15
16
17 def complete_repo_information(tool,
18 default_toolshed_url,
19 require_tool_panel_info,
20 default_install_tool_dependencies,
21 default_install_repository_dependencies,
22 default_install_resolver_dependencies,
23 force_latest_revision):
24 repo = dict()
25 # We need those values. Throw a KeyError when not present
26 repo['name'] = tool['name']
27 repo['owner'] = tool['owner']
28 repo['tool_panel_section_id'] = tool.get('tool_panel_section_id')
29 repo['tool_panel_section_label'] = tool.get('tool_panel_section_label')
30 if require_tool_panel_info and repo['tool_panel_section_id'] is None and repo[
31 'tool_panel_section_label'] is None and 'data_manager' not in repo.get('name'):
32 raise KeyError("Either tool_panel_section_id or tool_panel_section_name must be defined for tool '{0}'.".format(
33 repo.get('name')))
34 repo['tool_shed_url'] = format_tool_shed_url(tool.get('tool_shed_url', default_toolshed_url))
35 repo['changeset_revision'] = tool.get('changeset_revision')
36 repo = get_changeset_revisions(repo, force_latest_revision)
37 repo['install_repository_dependencies'] = tool.get('install_repository_dependencies',
38 default_install_repository_dependencies)
39 repo['install_resolver_dependencies'] = tool.get('install_resolver_dependencies',
40 default_install_resolver_dependencies)
41 repo['install_tool_dependencies'] = tool.get('install_tool_dependencies', default_install_tool_dependencies)
42 return repo
43
44
45 def format_tool_shed_url(tool_shed_url):
46 formatted_tool_shed_url = tool_shed_url
47 if not formatted_tool_shed_url.endswith('/'):
48 formatted_tool_shed_url += '/'
49 if not formatted_tool_shed_url.startswith('http'):
50 formatted_tool_shed_url = 'https://' + formatted_tool_shed_url
51 return formatted_tool_shed_url
52
53
54 def get_changeset_revisions(repository, force_latest_revision=False):
55 """
56 Select the correct changeset revision for a repository,
57 and make sure the repository exists
58 (i.e a request to the tool shed with name and owner returns a list of revisions).
59 Return repository or None, if the repository could not be found on the specified tool shed.
60 """
61 # Do not connect to the internet when not necessary
62 if repository.get('changeset_revision') is None or force_latest_revision:
63 ts = ToolShedInstance(url=repository['tool_shed_url'])
64 # Get the set revision or set it to the latest installable revision
65 installable_revisions = ts.repositories.get_ordered_installable_revisions(repository['name'],
66 repository['owner'])
67 if not installable_revisions: #
68 raise LookupError("Repo does not exist in tool shed: {0}".format(repository))
69 repository['changeset_revision'] = installable_revisions[-1]
70
71 return repository
72
73
74 def flatten_repo_info(repositories):
75 """
76 Flatten the dict containing info about what tools to install.
77 The tool definition YAML file allows multiple revisions to be listed for
78 the same tool. To enable simple, iterative processing of the info in this
79 script, flatten the `tools_info` list to include one entry per tool revision.
80
81 :type repositories: list of dicts
82 :param repositories: Each dict in this list should contain info about a tool.
83 :rtype: list of dicts
84 :return: Return a list of dicts that correspond to the input argument such
85 that if an input element contained `revisions` key with multiple
86 values, those will be returned as separate list items.
87 """
88
89 flattened_list = []
90 for repo_info in repositories:
91 new_repo_info = dict()
92 for key, value in repo_info.items():
93 if key in VALID_KEYS:
94 new_repo_info[key] = value
95 if 'revisions' in repo_info:
96 revisions = repo_info.get('revisions', [])
97 if not revisions: # Revisions are empty list or None
98 flattened_list.append(new_repo_info)
99 else:
100 for revision in revisions:
101 # A new dictionary must be created, otherwise there will
102 # be aliasing of dictionaries. Which leads to multiple
103 # repos with the same revision in the end result.
104 new_revision_dict = dict(**new_repo_info)
105 new_revision_dict['changeset_revision'] = revision
106 flattened_list.append(new_revision_dict)
107 else: # Revision was not defined at all
108 flattened_list.append(new_repo_info)
109 return flattened_list