comparison env/lib/python3.7/site-packages/ephemeris/install_tool_deps.py @ 0:26e78fe6e8c4 draft

"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author shellac
date Sat, 02 May 2020 07:14:21 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:26e78fe6e8c4
1 #!/usr/bin/env python
2 '''Tool to install tool dependencies on a Galaxy instance.'''
3 import argparse
4 import logging as log
5 import os
6 import xml.etree.ElementTree as ET
7
8 import yaml
9 from bioblend import ConnectionError as ConnErr
10 from bioblend.galaxy.tools import ToolClient
11
12 from ephemeris import get_galaxy_connection
13 from ephemeris.common_parser import get_common_args
14
15 timeout_codes = (408, 502, 504)
16
17
18 def _parser():
19 parent = get_common_args()
20 parser = argparse.ArgumentParser(parents=[parent])
21 parser.add_argument("-t", "--tool", help='Path to a tool file, tool_conf file, or yaml file containing a sequence of tool ids', nargs='*')
22 parser.add_argument("-i", "--id", help='Space-separated list of tool ids', nargs='*')
23
24 return parser
25
26
27 def _install(tool_client, tool_id):
28 try:
29 tool_client.install_dependencies(tool_id)
30 except ConnErr as e:
31 if e.status_code in timeout_codes:
32 log.warning(e.body)
33 else:
34 raise
35
36
37 def main():
38 """
39 This script uses bioblend to trigger dependencies installations for the provided tools
40 """
41 args = _parser().parse_args()
42 gi = get_galaxy_connection(args)
43 tool_client = ToolClient(gi)
44
45 if args.verbose:
46 log.basicConfig(level=log.DEBUG)
47
48 if args.tool:
49 for tool_conf_path in args.tool: # type: str
50 _, ext = os.path.splitext(tool_conf_path)
51 if ext == '.xml':
52 log.info("tool_conf xml found, parsing..")
53 # install all
54 root = ET.ElementTree(file=tool_conf_path).getroot()
55 if root.tag == "toolbox":
56 # Install all from tool_conf
57 tool_path = root.get('tool_path', '')
58 tool_path = tool_path.replace('${tool_conf_dir}', os.path.abspath(os.path.dirname(tool_conf_path)))
59 if tool_path:
60 log.info("Searching for tools relative to " + tool_path)
61 tools = root.findall(".//tool[@file]")
62 if len(tools) == 0:
63 log.warning("No tools found in tool_conf")
64 continue
65
66 for tool in tools:
67 tool_id = ET.ElementTree(file=os.path.join(tool_path, tool.get('file'))).getroot().get('id')
68 if tool_id:
69 log.info("Installing tool dependencies for " + tool_id + " from: " + tool.get('file'))
70 _install(tool_client, tool_id)
71 elif root.tag == "tool" and root.get('id'):
72 # Install from single tool file
73 log.info("Tool xml found. Installing " + root.get('id') + " dependencies..")
74 _install(tool_client, root.get('id'))
75 else:
76 log.info("YAML tool list found, parsing..")
77 with open(tool_conf_path) as fh:
78 tool_ids = yaml.safe_load(fh)
79 for tool_id in tool_ids:
80 # Install from yaml file
81 log.info("Installing " + tool_id + " dependencies..")
82 _install(tool_client, tool_id)
83
84 if args.id:
85 for tool_id in args.id: # type: str
86 log.info("Installing " + tool_id + " dependencies..")
87 _install(tool_client, tool_id.strip())
88
89
90 if __name__ == '__main__':
91 main()