|
| 1 | +"""Logic for linting conda recipes.""" |
| 2 | + |
| 3 | +import os |
| 4 | + |
| 5 | +from functools import wraps |
| 6 | + |
| 7 | +from planemo.conda_verify.recipe import ( |
| 8 | + check_build_number, |
| 9 | + check_dir_content, |
| 10 | + check_license_family, |
| 11 | + check_name, |
| 12 | + check_requirements, |
| 13 | + check_source, |
| 14 | + check_url, |
| 15 | + check_version, |
| 16 | + FIELDS, |
| 17 | + get_field, |
| 18 | + RecipeError, |
| 19 | + render_jinja2, |
| 20 | + yamlize, |
| 21 | +) |
| 22 | +from planemo.exit_codes import ( |
| 23 | + EXIT_CODE_GENERIC_FAILURE, |
| 24 | + EXIT_CODE_OK, |
| 25 | +) |
| 26 | +from planemo.io import ( |
| 27 | + coalesce_return_codes, |
| 28 | + find_matching_directories, |
| 29 | + info, |
| 30 | +) |
| 31 | +from planemo.lint_util import handle_lint_complete, setup_lint |
| 32 | + |
| 33 | + |
| 34 | +def lint_recipes_on_paths(ctx, paths, **kwds): |
| 35 | + """Apply conda linting procedure to recipes on supplied paths.""" |
| 36 | + assert_tools = kwds.get("assert_recipes", True) |
| 37 | + recursive = kwds.get("recursive", False) |
| 38 | + exit_codes = [] |
| 39 | + for recipe_dir in yield_recipes_on_paths(ctx, paths, recursive): |
| 40 | + if lint_conda_recipe(ctx, recipe_dir, **kwds) != 0: |
| 41 | + exit_codes.append(EXIT_CODE_GENERIC_FAILURE) |
| 42 | + else: |
| 43 | + exit_codes.append(EXIT_CODE_OK) |
| 44 | + return coalesce_return_codes(exit_codes, assert_at_least_one=assert_tools) |
| 45 | + |
| 46 | + |
| 47 | +def lint_conda_recipe(ctx, recipe_dir, **kwds): |
| 48 | + info("Linting conda recipe %s" % recipe_dir) |
| 49 | + lint_args, lint_ctx = setup_lint(ctx, **kwds) |
| 50 | + |
| 51 | + def apply(f): |
| 52 | + lint_ctx.lint(f.__name__, f, recipe_dir) |
| 53 | + |
| 54 | + apply(lint_name) |
| 55 | + apply(lint_version) |
| 56 | + apply(lint_summary) |
| 57 | + apply(lint_build_number) |
| 58 | + apply(lint_directory_content) |
| 59 | + apply(lint_license_family) |
| 60 | + apply(lint_about_urls) |
| 61 | + apply(lint_source) |
| 62 | + apply(lint_fields) |
| 63 | + apply(lint_requirements) |
| 64 | + |
| 65 | + return handle_lint_complete(lint_ctx, lint_args) |
| 66 | + |
| 67 | + |
| 68 | +def wraps_recipe_error(is_error=True): |
| 69 | + |
| 70 | + def outer_wrapper(f): |
| 71 | + |
| 72 | + @wraps(f) |
| 73 | + def wrapper(recipe_dir, lint_ctx): |
| 74 | + try: |
| 75 | + f(recipe_dir, lint_ctx) |
| 76 | + except RecipeError as e: |
| 77 | + if is_error: |
| 78 | + lint_ctx.error(str(e)) |
| 79 | + else: |
| 80 | + lint_ctx.warn(str(e)) |
| 81 | + except TypeError as e: # Errors in recipe checking code from YAML. |
| 82 | + lint_ctx.error(str(e)) |
| 83 | + |
| 84 | + return wrapper |
| 85 | + |
| 86 | + return outer_wrapper |
| 87 | + |
| 88 | + |
| 89 | +def lints_metadata(f): |
| 90 | + |
| 91 | + @wraps(f) |
| 92 | + def wrapper(recipe_dir, lint_ctx): |
| 93 | + meta_path = os.path.join(recipe_dir, 'meta.yaml') |
| 94 | + with open(meta_path, 'rb') as fi: |
| 95 | + data = fi.read() |
| 96 | + if b'{{' in data: |
| 97 | + data = render_jinja2(recipe_dir) |
| 98 | + meta = dict(yamlize(data)) |
| 99 | + f(meta, lint_ctx) |
| 100 | + |
| 101 | + return wrapper |
| 102 | + |
| 103 | + |
| 104 | +@wraps_recipe_error(is_error=False) |
| 105 | +def lint_directory_content(recipe_dir, lint_ctx): |
| 106 | + check_dir_content(recipe_dir) |
| 107 | + lint_ctx.info("Directory content seems okay.") |
| 108 | + |
| 109 | + |
| 110 | +@lints_metadata |
| 111 | +@wraps_recipe_error(is_error=False) |
| 112 | +def lint_license_family(meta, lint_ctx): |
| 113 | + check_license_family(meta) |
| 114 | + lint_ctx.info("License from vaild license family.") |
| 115 | + |
| 116 | + |
| 117 | +@lints_metadata |
| 118 | +def lint_summary(meta, lint_ctx): |
| 119 | + summary = get_field(meta, 'about/summary') |
| 120 | + |
| 121 | + if not summary: |
| 122 | + lint_ctx.warn("No summary supplied in about metadata.") |
| 123 | + |
| 124 | + if summary and len(summary) > 80: |
| 125 | + msg = "summary exceeds 80 characters" |
| 126 | + lint_ctx.warn(msg) |
| 127 | + |
| 128 | + |
| 129 | +@lints_metadata |
| 130 | +@wraps_recipe_error(is_error=False) |
| 131 | +def lint_about_urls(meta, lint_ctx): |
| 132 | + for field in ('about/home', 'about/dev_url', 'about/doc_url', |
| 133 | + 'about/license_url'): |
| 134 | + url = get_field(meta, field) |
| 135 | + if url: |
| 136 | + check_url(url) |
| 137 | + lint_ctx.info("About urls (if present) are valid") |
| 138 | + |
| 139 | + |
| 140 | +@lints_metadata |
| 141 | +@wraps_recipe_error(is_error=True) |
| 142 | +def lint_source(meta, lint_ctx): |
| 143 | + check_source(meta) |
| 144 | + lint_ctx.info("Source (if present) is valid") |
| 145 | + |
| 146 | + |
| 147 | +@lints_metadata |
| 148 | +@wraps_recipe_error(is_error=True) |
| 149 | +def lint_build_number(meta, lint_ctx): |
| 150 | + build_number = get_field(meta, 'build/number', 0) |
| 151 | + check_build_number(build_number) |
| 152 | + lint_ctx.info("Valid build number [%s]" % build_number) |
| 153 | + |
| 154 | + |
| 155 | +@lints_metadata |
| 156 | +@wraps_recipe_error(is_error=True) |
| 157 | +def lint_version(meta, lint_ctx): |
| 158 | + version = get_field(meta, 'package/version') |
| 159 | + check_version(version) |
| 160 | + lint_ctx.info("Valid version number [%s]" % version) |
| 161 | + |
| 162 | + |
| 163 | +@lints_metadata |
| 164 | +@wraps_recipe_error(is_error=True) |
| 165 | +def lint_name(meta, lint_ctx): |
| 166 | + name = get_field(meta, 'package/name') |
| 167 | + check_name(name) |
| 168 | + lint_ctx.info("Valid recipe name [%s]" % name) |
| 169 | + |
| 170 | + |
| 171 | +@lints_metadata |
| 172 | +@wraps_recipe_error(is_error=False) |
| 173 | +def lint_fields(meta, lint_ctx): |
| 174 | + # Taken from validate_meta |
| 175 | + for section in meta: |
| 176 | + if section not in FIELDS: |
| 177 | + raise RecipeError("Unknown section: %s" % section) |
| 178 | + submeta = meta.get(section) |
| 179 | + if submeta is None: |
| 180 | + submeta = {} |
| 181 | + for key in submeta: |
| 182 | + # Next two lines added for planemo since we don't do the |
| 183 | + # select lines thing. |
| 184 | + if key == "skip": |
| 185 | + continue |
| 186 | + |
| 187 | + if key not in FIELDS[section]: |
| 188 | + raise RecipeError("in section %r: unknown key %r" % |
| 189 | + (section, key)) |
| 190 | + |
| 191 | + |
| 192 | +@lints_metadata |
| 193 | +@wraps_recipe_error(is_error=False) |
| 194 | +def lint_requirements(meta, lint_ctx): |
| 195 | + check_requirements(meta) |
| 196 | + lint_ctx.info("Reference recipe files appear valid") |
| 197 | + |
| 198 | + |
| 199 | +def yield_recipes_on_paths(ctx, paths, recursive): |
| 200 | + for path in paths: |
| 201 | + recipe_dirs = find_matching_directories( |
| 202 | + path, "meta.yaml", recursive=recursive |
| 203 | + ) |
| 204 | + for recipe_dir in recipe_dirs: |
| 205 | + yield recipe_dir |
| 206 | + |
| 207 | + |
| 208 | +__all__ = [ |
| 209 | + "lint_recipes_on_paths", |
| 210 | +] |
0 commit comments