Conditions | 19 |
Total Lines | 115 |
Code Lines | 94 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 380 |
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 profile_tool.main() 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 | #!/usr/bin/python3 |
||
177 | def main(): |
||
178 | args = parse_args() |
||
179 | |||
180 | if args.subcommand == "sub": |
||
181 | product_yaml = os.path.join(args.ssg_root, "products", args.product, "product.yml") |
||
182 | env_yaml = ssg.environment.open_environment(args.build_config_yaml, product_yaml) |
||
183 | try: |
||
184 | profile1 = ssg.build_yaml.Profile.from_yaml(args.profile1, env_yaml) |
||
185 | profile2 = ssg.build_yaml.Profile.from_yaml(args.profile2, env_yaml) |
||
186 | except jinja2.exceptions.TemplateNotFound as e: |
||
187 | print("Error: Profile {} could not be found.".format(str(e))) |
||
188 | exit(1) |
||
189 | |||
190 | subtracted_profile = profile1 - profile2 |
||
|
|||
191 | |||
192 | exclusive_rules = len(subtracted_profile.get_rule_selectors()) |
||
193 | exclusive_vars = len(subtracted_profile.get_variable_selectors()) |
||
194 | if exclusive_rules > 0: |
||
195 | print("{} rules were left after subtraction.".format(exclusive_rules)) |
||
196 | if exclusive_vars > 0: |
||
197 | print("{} variables were left after subtraction.".format(exclusive_vars)) |
||
198 | |||
199 | if exclusive_rules > 0 or exclusive_vars > 0: |
||
200 | profile1_basename = os.path.splitext( |
||
201 | os.path.basename(args.profile1))[0] |
||
202 | profile2_basename = os.path.splitext( |
||
203 | os.path.basename(args.profile2))[0] |
||
204 | |||
205 | subtracted_profile_filename = "{}_sub_{}.profile".format( |
||
206 | profile1_basename, profile2_basename) |
||
207 | print("Creating a new profile containing the exclusive selections: {}".format( |
||
208 | subtracted_profile_filename)) |
||
209 | |||
210 | subtracted_profile.title = profile1.title + " subtracted by " + profile2.title |
||
211 | subtracted_profile.dump_yaml(subtracted_profile_filename) |
||
212 | print("Profile {} was created successfully".format( |
||
213 | subtracted_profile_filename)) |
||
214 | else: |
||
215 | print("Subtraction would produce an empty profile. No new profile was generated") |
||
216 | exit(0) |
||
217 | |||
218 | benchmark = ssg.build_profile.XCCDFBenchmark(args.benchmark, args.product) |
||
219 | ret = [] |
||
220 | if args.profile: |
||
221 | ret.append(benchmark.show_profile_stats(args.profile, args)) |
||
222 | else: |
||
223 | ret.extend(benchmark.show_all_profile_stats(args)) |
||
224 | |||
225 | if args.format == "json": |
||
226 | print(json.dumps(ret, indent=4)) |
||
227 | if args.format == "html": |
||
228 | from json2html import json2html |
||
229 | filtered_output = [] |
||
230 | output_path = "./" |
||
231 | if args.output: |
||
232 | output_path = args.output |
||
233 | mkdir_p(output_path) |
||
234 | |||
235 | content_path = os.path.join(output_path, "content") |
||
236 | mkdir_p(content_path) |
||
237 | |||
238 | content_list = [ |
||
239 | 'rules', |
||
240 | 'missing_stig_ids', |
||
241 | 'missing_cis_refs', |
||
242 | 'missing_hipaa_refs', |
||
243 | 'missing_anssi_refs', |
||
244 | 'missing_ospp_refs', |
||
245 | 'missing_cui_refs', |
||
246 | 'missing_ovals', |
||
247 | 'missing_sces', |
||
248 | 'missing_bash_fixes', |
||
249 | 'missing_ansible_fixes', |
||
250 | 'missing_ignition_fixes', |
||
251 | 'missing_kubernetes_fixes', |
||
252 | 'missing_puppet_fixes', |
||
253 | 'missing_anaconda_fixes', |
||
254 | 'missing_cces', |
||
255 | 'ansible_parity', |
||
256 | 'implemented_checks', |
||
257 | 'implemented_fixes', |
||
258 | 'missing_checks', |
||
259 | 'missing_fixes' |
||
260 | ] |
||
261 | link = """<a href="{}"><div style="height:100%;width:100%">{}</div></a>""" |
||
262 | |||
263 | for profile in ret: |
||
264 | bash_fixes_count = profile['rules_count'] - profile['missing_bash_fixes_count'] |
||
265 | for content in content_list: |
||
266 | content_file = "{}_{}.txt".format(profile['profile_id'], content) |
||
267 | content_filepath = os.path.join("content", content_file) |
||
268 | count = len(profile[content]) |
||
269 | if count > 0: |
||
270 | if content == "ansible_parity": |
||
271 | #custom text link for ansible parity |
||
272 | count = link.format(content_filepath, "{} out of {} ({}%)".format(bash_fixes_count-count, bash_fixes_count, int(((bash_fixes_count-count)/bash_fixes_count)*100))) |
||
273 | count_href_element = link.format(content_filepath, count) |
||
274 | profile['{}_count'.format(content)] = count_href_element |
||
275 | with open(os.path.join(content_path, content_file), 'w+') as f: |
||
276 | f.write('\n'.join(profile[content])) |
||
277 | else: |
||
278 | profile['{}_count'.format(content)] = count |
||
279 | |||
280 | del profile[content] |
||
281 | filtered_output.append(profile) |
||
282 | |||
283 | with open(os.path.join(output_path, "statistics.html"), 'w+') as f: |
||
284 | f.write(json2html.convert(json=json.dumps(filtered_output), escape=False)) |
||
285 | |||
286 | elif args.format == "csv": |
||
287 | # we can assume ret has at least one element |
||
288 | # CSV header |
||
289 | print(",".join(ret[0].keys())) |
||
290 | for line in ret: |
||
291 | print(",".join([str(value) for value in line.values()])) |
||
292 | |||
296 |