Mercurial > repos > bcclaywell > argo_navis
comparison venv/lib/python2.7/site-packages/planemo/tool_lint.py @ 0:d67268158946 draft
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
author | bcclaywell |
---|---|
date | Mon, 12 Oct 2015 17:43:33 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d67268158946 |
---|---|
1 import os | |
2 | |
3 from planemo.io import info | |
4 from planemo.io import error | |
5 | |
6 import planemo.linters.xsd | |
7 | |
8 from planemo.tools import load_tool_elements_from_path, is_tool_load_error | |
9 from galaxy.tools.lint import lint_xml | |
10 | |
11 SKIP_XML_MESSAGE = "Skipping XML file - does not appear to be a tool %s." | |
12 LINTING_TOOL_MESSAGE = "Linting tool %s" | |
13 SHED_FILES = ["tool_dependencies.xml", "repository_dependencies.xml"] | |
14 | |
15 | |
16 def lint_tools_on_path(ctx, paths, lint_args, **kwds): | |
17 assert_tools = kwds.get("assert_tools", True) | |
18 recursive = kwds.get("recursive", False) | |
19 exit = 0 | |
20 valid_tools = 0 | |
21 for path in paths: | |
22 for (tool_path, tool_xml) in yield_tool_xmls(ctx, path, recursive): | |
23 if handle_tool_load_error(tool_path, tool_xml): | |
24 exit = 1 | |
25 continue | |
26 info("Linting tool %s" % tool_path) | |
27 if not lint_xml(tool_xml, **lint_args): | |
28 error("Failed linting") | |
29 exit = 1 | |
30 else: | |
31 valid_tools += 1 | |
32 if exit == 0 and valid_tools == 0 and assert_tools: | |
33 exit = 2 | |
34 return exit | |
35 | |
36 | |
37 def handle_tool_load_error(tool_path, tool_xml): | |
38 """ Return True if tool_xml is tool load error (invalid XML), and | |
39 print a helpful error message. | |
40 """ | |
41 is_error = False | |
42 if is_tool_load_error(tool_xml): | |
43 info("Could not lint %s due to malformed xml." % tool_path) | |
44 is_error = True | |
45 return is_error | |
46 | |
47 | |
48 def yield_tool_xmls(ctx, path, recursive=False): | |
49 tools = load_tool_elements_from_path( | |
50 path, | |
51 recursive, | |
52 register_load_errors=True, | |
53 ) | |
54 for (tool_path, tool_xml) in tools: | |
55 if is_tool_load_error(tool_xml): | |
56 yield (tool_path, tool_xml) | |
57 continue | |
58 if not _is_tool_xml(ctx, tool_path, tool_xml): | |
59 continue | |
60 yield (tool_path, tool_xml) | |
61 | |
62 | |
63 def build_lint_args(ctx, **kwds): | |
64 report_level = kwds.get("report_level", "all") | |
65 fail_level = kwds.get("fail_level", "warn") | |
66 skip = kwds.get("skip", None) | |
67 if skip is None: | |
68 skip = ctx.global_config.get("lint_skip", "") | |
69 if isinstance(skip, list): | |
70 skip = ",".join(skip) | |
71 | |
72 skip_types = [s.strip() for s in skip.split(",")] | |
73 lint_args = dict( | |
74 level=report_level, | |
75 fail_level=fail_level, | |
76 extra_modules=_lint_extra_modules(**kwds), | |
77 skip_types=skip_types, | |
78 ) | |
79 return lint_args | |
80 | |
81 | |
82 def _lint_extra_modules(**kwds): | |
83 xsd = kwds.get("xsd", False) | |
84 if xsd: | |
85 return [planemo.linters.xsd] | |
86 else: | |
87 return [] | |
88 | |
89 | |
90 def _is_tool_xml(ctx, tool_path, tool_xml): | |
91 if os.path.basename(tool_path) in SHED_FILES: | |
92 return False | |
93 if tool_xml.getroot().tag != "tool": | |
94 if ctx.verbose: | |
95 info(SKIP_XML_MESSAGE % tool_path) | |
96 return False | |
97 return True |