|
| 1 | +import os |
| 2 | +import yaml |
| 3 | +from galaxy.tools.lint import LintContext |
| 4 | +from planemo.lint import lint_xsd |
| 5 | +from planemo.tool_lint import ( |
| 6 | + build_lint_args, |
| 7 | + yield_tool_xmls, |
| 8 | +) |
| 9 | +from planemo.xml import XSDS_PATH |
| 10 | + |
| 11 | + |
| 12 | +from planemo.io import info |
| 13 | +from planemo.io import error |
| 14 | + |
| 15 | +from galaxy.tools.lint import lint_xml_with |
| 16 | + |
| 17 | +TOOL_DEPENDENCIES_XSD = os.path.join(XSDS_PATH, "tool_dependencies.xsd") |
| 18 | +REPO_DEPENDENCIES_XSD = os.path.join(XSDS_PATH, "repository_dependencies.xsd") |
| 19 | + |
| 20 | + |
| 21 | +def lint_repository(ctx, path, **kwds): |
| 22 | + info("Linting repository %s" % path) |
| 23 | + lint_args = build_lint_args(**kwds) |
| 24 | + lint_ctx = LintContext(lint_args["level"]) |
| 25 | + lint_ctx.lint( |
| 26 | + "tool_dependencies", |
| 27 | + lint_tool_dependencies, |
| 28 | + path, |
| 29 | + ) |
| 30 | + lint_ctx.lint( |
| 31 | + "repository_dependencies", |
| 32 | + lint_repository_dependencies, |
| 33 | + path, |
| 34 | + ) |
| 35 | + lint_ctx.lint( |
| 36 | + "shed_yaml", |
| 37 | + lint_shed_yaml, |
| 38 | + path, |
| 39 | + ) |
| 40 | + if kwds["tools"]: |
| 41 | + for (tool_path, tool_xml) in yield_tool_xmls(ctx, path): |
| 42 | + info("+Linting tool %s" % tool_path) |
| 43 | + lint_xml_with( |
| 44 | + lint_ctx, |
| 45 | + tool_xml, |
| 46 | + extra_modules=lint_args["extra_modules"] |
| 47 | + ) |
| 48 | + failed = lint_ctx.failed(lint_args["fail_level"]) |
| 49 | + if failed: |
| 50 | + error("Failed linting") |
| 51 | + return 1 if failed else 0 |
| 52 | + |
| 53 | + |
| 54 | +def lint_tool_dependencies(path, lint_ctx): |
| 55 | + tool_dependencies = os.path.join(path, "tool_dependencies.xml") |
| 56 | + if not os.path.exists(tool_dependencies): |
| 57 | + lint_ctx.info("No tool_dependencies.xml, skipping.") |
| 58 | + return |
| 59 | + lint_xsd(lint_ctx, TOOL_DEPENDENCIES_XSD, tool_dependencies) |
| 60 | + |
| 61 | + |
| 62 | +def lint_repository_dependencies(path, lint_ctx): |
| 63 | + repo_dependencies = os.path.join(path, "repository_dependencies.xml") |
| 64 | + if not os.path.exists(repo_dependencies): |
| 65 | + lint_ctx.info("No repository_dependencies.xml, skipping.") |
| 66 | + return |
| 67 | + lint_xsd(lint_ctx, REPO_DEPENDENCIES_XSD, repo_dependencies) |
| 68 | + |
| 69 | + |
| 70 | +def lint_shed_yaml(path, lint_ctx): |
| 71 | + shed_yaml = os.path.join(path, ".shed.yml") |
| 72 | + if not os.path.exists(shed_yaml): |
| 73 | + lint_ctx.info("No .shed.yml file found, skipping.") |
| 74 | + return |
| 75 | + try: |
| 76 | + shed_contents = yaml.load(open(shed_yaml, "r")) |
| 77 | + except Exception as e: |
| 78 | + lint_ctx.warn("Failed to parse .shed.yml file [%s]" % str(e)) |
| 79 | + |
| 80 | + warned = False |
| 81 | + for required_key in ["owner", "name"]: |
| 82 | + if required_key not in shed_contents: |
| 83 | + lint_ctx.warn(".shed.yml did not contain key [%s]" % required_key) |
| 84 | + warned = True |
| 85 | + |
| 86 | + if not warned: |
| 87 | + lint_ctx.info(".shed.yml found and appears to be valid YAML.") |
0 commit comments