| Conditions | 14 |
| Total Lines | 89 |
| Code Lines | 58 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 210 |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like ssg.build_sce.checks() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | from __future__ import absolute_import |
||
| 66 | def checks(env_yaml, yaml_path, sce_dirs, output): |
||
| 67 | product = utils.required_key(env_yaml, "product") |
||
| 68 | included_checks_count = 0 |
||
| 69 | reversed_dirs = sce_dirs[::-1] |
||
| 70 | already_loaded = dict() |
||
| 71 | local_env_yaml = dict() |
||
| 72 | local_env_yaml.update(env_yaml) |
||
| 73 | |||
| 74 | product_dir = os.path.dirname(yaml_path) |
||
| 75 | relative_guide_dir = utils.required_key(env_yaml, "benchmark_root") |
||
| 76 | guide_dir = os.path.abspath(os.path.join(product_dir, relative_guide_dir)) |
||
| 77 | additional_content_directories = env_yaml.get("additional_content_directories", []) |
||
| 78 | add_content_dirs = [ |
||
| 79 | os.path.abspath(os.path.join(product_dir, rd)) |
||
| 80 | for rd in additional_content_directories |
||
| 81 | ] |
||
| 82 | |||
| 83 | for _dir_path in find_rule_dirs_in_paths([guide_dir] + add_content_dirs): |
||
| 84 | rule_id = get_rule_dir_id(_dir_path) |
||
| 85 | |||
| 86 | rule_path = os.path.join(_dir_path, "rule.yml") |
||
| 87 | try: |
||
| 88 | rule = Rule.from_yaml(rule_path, env_yaml) |
||
| 89 | except DocumentationNotComplete: |
||
| 90 | # Happens on non-debug builds when a rule isn't yet completed. We |
||
| 91 | # don't want to build the SCE check for this rule yet so skip it |
||
| 92 | # and move on. |
||
| 93 | continue |
||
| 94 | |||
| 95 | prodtypes = parse_prodtype(rule.prodtype) |
||
| 96 | if prodtypes and 'all' not in prodtypes and product not in prodtypes: |
||
| 97 | # The prodtype exists, isn't all and doesn't contain this current |
||
| 98 | # product, so we're best to skip this rule altogether. |
||
| 99 | continue |
||
| 100 | |||
| 101 | local_env_yaml['rule_id'] = rule.id_ |
||
| 102 | local_env_yaml['rule_title'] = rule.title |
||
| 103 | local_env_yaml['products'] = prodtypes # default is all |
||
| 104 | |||
| 105 | for _path in get_rule_dir_sces(_dir_path, product): |
||
| 106 | # To be compatible with later checks, use the rule_id (i.e., the |
||
| 107 | # value of _dir) to recreate the expected filename if this OVAL |
||
| 108 | # was in a rule directory. However, note that unlike |
||
| 109 | # build_oval.checks(...), we have to get this script's extension |
||
| 110 | # first. |
||
| 111 | _, ext = os.path.splitext(_path) |
||
| 112 | filename = "{0}{1}".format(rule_id, ext) |
||
| 113 | |||
| 114 | sce_content, metadata = load_sce_and_metadata(_path, local_env_yaml) |
||
| 115 | metadata['filename'] = filename |
||
| 116 | |||
| 117 | if not _check_is_applicable_for_product(metadata, product): |
||
| 118 | continue |
||
| 119 | if _check_is_loaded(already_loaded, filename): |
||
| 120 | continue |
||
| 121 | |||
| 122 | output_file = open(os.path.join(output, filename), 'w') |
||
| 123 | print(sce_content, file=output_file) |
||
| 124 | |||
| 125 | included_checks_count += 1 |
||
| 126 | already_loaded[rule_id] = metadata |
||
| 127 | |||
| 128 | for sce_dir in reversed_dirs: |
||
| 129 | if not os.path.isdir(sce_dir): |
||
| 130 | continue |
||
| 131 | |||
| 132 | for filename in sorted(os.listdir(sce_dir)): |
||
| 133 | rule_id, _ = os.path.splitext(filename) |
||
| 134 | |||
| 135 | sce_content, metadata = load_sce_and_metadata(filename, env_yaml) |
||
| 136 | metadata['filename'] = filename |
||
| 137 | |||
| 138 | if not _check_is_applicable_for_product(metadata, product): |
||
| 139 | continue |
||
| 140 | if _check_is_loaded(already_loaded, filename): |
||
| 141 | continue |
||
| 142 | |||
| 143 | output_file = open(os.path.join(output, filename), 'w') |
||
| 144 | print(sce_content, file=output_file) |
||
| 145 | |||
| 146 | included_checks_count += 1 |
||
| 147 | already_loaded[rule_id] = metadata |
||
| 148 | |||
| 149 | # Finally, write out our metadata to disk so that we can reference it in |
||
| 150 | # later build stages (such as during building shorthand content). |
||
| 151 | metadata_path = os.path.join(output, 'metadata.json') |
||
| 152 | json.dump(already_loaded, open(metadata_path, 'w')) |
||
| 153 | |||
| 154 | return already_loaded |
||
| 155 |