comparison env/lib/python3.9/site-packages/planemo/tools.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 """Planemo-specific wrappers around galaxy-tool-util tool functionality."""
2 from __future__ import absolute_import
3
4 import os
5 import sys
6 import traceback
7
8 from galaxy.tool_util import loader_directory
9 from galaxy.tool_util.fetcher import ToolLocationFetcher
10
11 from planemo.io import error, info
12
13 is_tool_load_error = loader_directory.is_tool_load_error
14 SKIP_XML_MESSAGE = "Skipping XML file - does not appear to be a tool %s."
15 SHED_FILES = ["tool_dependencies.xml", "repository_dependencies.xml"]
16 LOAD_ERROR_MESSAGE = "Error loading tool with path %s"
17
18
19 def uri_to_path(ctx, uri):
20 """Fetch URI to a local path."""
21 fetcher = ToolLocationFetcher()
22 return fetcher.to_tool_path(uri)
23
24
25 def uris_to_paths(ctx, uris):
26 """Fetch multiple URIs to a local path."""
27 fetcher = ToolLocationFetcher()
28 paths = []
29 for uri in uris:
30 path = fetcher.to_tool_path(uri)
31 paths.append(path)
32 return paths
33
34
35 def yield_tool_sources_on_paths(ctx, paths, recursive=False, yield_load_errors=True, exclude_deprecated=False):
36 """Walk paths and yield ToolSource objects discovered."""
37 for path in paths:
38 for (tool_path, tool_source) in yield_tool_sources(ctx, path, recursive, yield_load_errors):
39 if exclude_deprecated and 'deprecated' in tool_path:
40 continue
41 yield (tool_path, tool_source)
42
43
44 def yield_tool_sources(ctx, path, recursive=False, yield_load_errors=True):
45 """Walk single path and yield ToolSource objects discovered."""
46 tools = load_tool_sources_from_path(
47 path,
48 recursive,
49 register_load_errors=True,
50 )
51 for (tool_path, tool_source) in tools:
52 if is_tool_load_error(tool_source):
53 if yield_load_errors:
54 yield (tool_path, tool_source)
55 else:
56 error(LOAD_ERROR_MESSAGE % tool_path)
57 continue
58
59 if not _is_tool_source(ctx, tool_path, tool_source):
60 continue
61 yield (tool_path, tool_source)
62
63
64 def load_tool_sources_from_path(path, recursive, register_load_errors=False):
65 """Generate a list for tool sources found down specified path."""
66 return loader_directory.load_tool_sources_from_path(
67 path,
68 _load_exception_handler,
69 recursive=recursive,
70 register_load_errors=register_load_errors,
71 )
72
73
74 def _load_exception_handler(path, exc_info):
75 error(LOAD_ERROR_MESSAGE % path)
76 traceback.print_exception(*exc_info, limit=1, file=sys.stderr)
77
78
79 def _is_tool_source(ctx, tool_path, tool_source):
80 if os.path.basename(tool_path) in SHED_FILES:
81 return False
82 root = getattr(tool_source, "root", None)
83 if root is not None:
84 if root.tag != "tool":
85 if ctx.verbose:
86 info(SKIP_XML_MESSAGE % tool_path)
87 return False
88 return True
89
90
91 __all__ = (
92 "is_tool_load_error",
93 "load_tool_sources_from_path",
94 "yield_tool_sources",
95 "yield_tool_sources_on_paths",
96 )