comparison env/lib/python3.7/site-packages/galaxy/tool_util/lint.py @ 2:6af9afd405e9 draft

"planemo upload commit 0a63dd5f4d38a1f6944587f52a8cd79874177fc1"
author shellac
date Thu, 14 May 2020 14:56:58 -0400
parents 26e78fe6e8c4
children
comparison
equal deleted inserted replaced
1:75ca89e9b81c 2:6af9afd405e9
1 """This modules contains the functions that drive the tool linting framework."""
2 from __future__ import print_function
3
4 import inspect
5
6 from galaxy.tool_util.parser import get_tool_source
7 from galaxy.util import submodules
8 from galaxy.util.getargspec import getfullargspec
9
10
11 LEVEL_ALL = "all"
12 LEVEL_WARN = "warn"
13 LEVEL_ERROR = "error"
14
15
16 def lint_tool_source(tool_source, level=LEVEL_ALL, fail_level=LEVEL_WARN, extra_modules=[], skip_types=[], name=None):
17 lint_context = LintContext(level=level, skip_types=skip_types, object_name=name)
18 lint_tool_source_with(lint_context, tool_source, extra_modules)
19
20 return not lint_context.failed(fail_level)
21
22
23 def lint_xml(tool_xml, level=LEVEL_ALL, fail_level=LEVEL_WARN, extra_modules=[], skip_types=[], name=None):
24 lint_context = LintContext(level=level, skip_types=skip_types, object_name=name)
25 lint_xml_with(lint_context, tool_xml, extra_modules)
26
27 return not lint_context.failed(fail_level)
28
29
30 def lint_tool_source_with(lint_context, tool_source, extra_modules=[]):
31 import galaxy.tool_util.linters
32 tool_xml = getattr(tool_source, "xml_tree", None)
33 linter_modules = submodules.import_submodules(galaxy.tool_util.linters, ordered=True)
34 linter_modules.extend(extra_modules)
35 for module in linter_modules:
36 tool_type = tool_source.parse_tool_type() or "default"
37 lint_tool_types = getattr(module, "lint_tool_types", ["default"])
38 if not ("*" in lint_tool_types or tool_type in lint_tool_types):
39 continue
40
41 for (name, value) in inspect.getmembers(module):
42 if callable(value) and name.startswith("lint_"):
43 # Look at the first argument to the linter to decide
44 # if we should lint the XML description or the abstract
45 # tool parser object.
46 first_arg = getfullargspec(value).args[0]
47 if first_arg == "tool_xml":
48 if tool_xml is None:
49 # XML linter and non-XML tool, skip for now
50 continue
51 else:
52 lint_context.lint(name, value, tool_xml)
53 else:
54 lint_context.lint(name, value, tool_source)
55
56
57 def lint_xml_with(lint_context, tool_xml, extra_modules=[]):
58 tool_source = get_tool_source(xml_tree=tool_xml)
59 return lint_tool_source_with(lint_context, tool_source, extra_modules=extra_modules)
60
61
62 # TODO: Nothing inherently tool-y about LintContext and in fact
63 # it is reused for repositories in planemo. Therefore, it should probably
64 # be moved to galaxy.util.lint.
65 class LintContext(object):
66
67 def __init__(self, level, skip_types=[], object_name=None):
68 self.skip_types = skip_types
69 self.level = level
70 self.object_name = object_name
71 self.found_errors = False
72 self.found_warns = False
73
74 def lint(self, name, lint_func, lint_target):
75 name = name.replace("tsts", "tests")[len("lint_"):]
76 if name in self.skip_types:
77 return
78 self.printed_linter_info = False
79 self.valid_messages = []
80 self.info_messages = []
81 self.warn_messages = []
82 self.error_messages = []
83 lint_func(lint_target, self)
84 # TODO: colorful emoji if in click CLI.
85 if self.error_messages:
86 status = "FAIL"
87 elif self.warn_messages:
88 status = "WARNING"
89 else:
90 status = "CHECK"
91
92 def print_linter_info():
93 if self.printed_linter_info:
94 return
95 self.printed_linter_info = True
96 print("Applying linter %s... %s" % (name, status))
97
98 for message in self.error_messages:
99 self.found_errors = True
100 print_linter_info()
101 print(".. ERROR: %s" % message)
102
103 if self.level != LEVEL_ERROR:
104 for message in self.warn_messages:
105 self.found_warns = True
106 print_linter_info()
107 print(".. WARNING: %s" % message)
108
109 if self.level == LEVEL_ALL:
110 for message in self.info_messages:
111 print_linter_info()
112 print(".. INFO: %s" % message)
113 for message in self.valid_messages:
114 print_linter_info()
115 print(".. CHECK: %s" % message)
116
117 def __handle_message(self, message_list, message, *args):
118 if args:
119 message = message % args
120 message_list.append(message)
121
122 def valid(self, message, *args):
123 self.__handle_message(self.valid_messages, message, *args)
124
125 def info(self, message, *args):
126 self.__handle_message(self.info_messages, message, *args)
127
128 def error(self, message, *args):
129 self.__handle_message(self.error_messages, message, *args)
130
131 def warn(self, message, *args):
132 self.__handle_message(self.warn_messages, message, *args)
133
134 def failed(self, fail_level):
135 found_warns = self.found_warns
136 found_errors = self.found_errors
137 if fail_level == LEVEL_WARN:
138 lint_fail = (found_warns or found_errors)
139 else:
140 lint_fail = found_errors
141 return lint_fail