Conditions | 1 |
Total Lines | 171 |
Code Lines | 117 |
Lines | 0 |
Ratio | 0 % |
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:
1 | #!/usr/bin/env python2 |
||
28 | def parse_args(): |
||
29 | parser = argparse.ArgumentParser() |
||
30 | |||
31 | common_parser = argparse.ArgumentParser(add_help=False) |
||
32 | common_parser.set_defaults(test_env=None) |
||
33 | |||
34 | backends = common_parser.add_mutually_exclusive_group(required=True) |
||
35 | |||
36 | backends.add_argument( |
||
37 | "--docker", dest="docker", metavar="BASE_IMAGE", |
||
38 | help="Use Docker test environment with this base image.") |
||
39 | backends.add_argument( |
||
40 | "--container", dest="container", metavar="BASE_IMAGE", |
||
41 | help="Use container test environment with this base image.") |
||
42 | |||
43 | backends.add_argument( |
||
44 | "--libvirt", dest="libvirt", metavar=("HYPERVISOR", "DOMAIN"), nargs=2, |
||
45 | help="libvirt hypervisor and domain name. When the leading URI driver protocol " |
||
46 | "is omitted from the hypervisor, qemu:/// protocol is assumed. " |
||
47 | "Example of a hypervisor domain name tuple: system ssg-test-suite") |
||
48 | |||
49 | common_parser.add_argument("--datastream", |
||
50 | dest="datastream", |
||
51 | metavar="DATASTREAM", |
||
52 | help=("Path to the Source DataStream on this " |
||
53 | "machine which is going to be tested")) |
||
54 | benchmarks = common_parser.add_mutually_exclusive_group() |
||
55 | benchmarks.add_argument("--xccdf-id", |
||
56 | dest="xccdf_id", |
||
57 | metavar="REF-ID", |
||
58 | default=None, |
||
59 | help="Reference ID related to benchmark to " |
||
60 | "be used.") |
||
61 | benchmarks.add_argument("--xccdf-id-number", |
||
62 | dest="xccdf_id_number", |
||
63 | metavar="REF-ID-SELECT", |
||
64 | type=int, |
||
65 | default=0, |
||
66 | help="Selection number of reference ID related " |
||
67 | "to benchmark to be used.") |
||
68 | common_parser.add_argument( |
||
69 | "--add-platform", |
||
70 | metavar="<CPE REGEX>", |
||
71 | default=None, |
||
72 | help="Find all CPEs that are present in local OpenSCAP's CPE dictionary " |
||
73 | "that match the provided regex, " |
||
74 | "and add them as platforms to all datastream benchmarks. " |
||
75 | "If the regex doesn't match anything, it will be treated " |
||
76 | "as a literal CPE, and added as a platform. " |
||
77 | "For example, use 'cpe:/o:fedoraproject:fedora:30' or 'enterprise_linux'.") |
||
78 | common_parser.add_argument( |
||
79 | "--remove-machine-only", |
||
80 | default=False, |
||
81 | action="store_true", |
||
82 | help="Removes machine-only platform constraint from rules " |
||
83 | "to enable testing these rules on container backends.") |
||
84 | common_parser.add_argument("--loglevel", |
||
85 | dest="loglevel", |
||
86 | metavar="LOGLEVEL", |
||
87 | default="INFO", |
||
88 | help="Default level of console output") |
||
89 | common_parser.add_argument("--logdir", |
||
90 | dest="logdir", |
||
91 | metavar="LOGDIR", |
||
92 | default=None, |
||
93 | help="Directory to which all output is saved") |
||
94 | |||
95 | common_parser.add_argument( |
||
96 | "--mode", |
||
97 | dest="scanning_mode", |
||
98 | default="online", |
||
99 | choices=("online", "offline"), |
||
100 | help="What type of check to use - either " |
||
101 | "Online check done by running oscap inside the concerned system, or " |
||
102 | "offline check that examines the filesystem from the host " |
||
103 | "(either may require extended privileges).") |
||
104 | |||
105 | common_parser.add_argument( |
||
106 | "--remediate-using", |
||
107 | dest="remediate_using", |
||
108 | default="oscap", |
||
109 | choices=ssg_test_suite.oscap.REMEDIATION_RULE_RUNNERS.keys(), |
||
110 | help="What type of remediations to use - openscap online one, " |
||
111 | "or remediation done by using remediation roles " |
||
112 | "that are saved to disk beforehand.") |
||
113 | |||
114 | subparsers = parser.add_subparsers(dest="subparser_name", |
||
115 | help="Subcommands: profile, rule, combined") |
||
116 | subparsers.required = True |
||
117 | |||
118 | parser_profile = subparsers.add_parser("profile", |
||
119 | formatter_class=argparse.RawDescriptionHelpFormatter, |
||
120 | epilog=textwrap.dedent("""\ |
||
121 | In case that tested profile contains rules which might prevent root ssh access |
||
122 | to the testing VM consider unselecting these rules. To unselect certain rules |
||
123 | from a datastream use `ds_unselect_rules.sh` script. List of such rules already |
||
124 | exists, see `unselect_rules_list` file. |
||
125 | Example usage: |
||
126 | ./ds_unselect_rules.sh ../build/ssg-fedora-ds.xml unselect_rules_list |
||
127 | """), |
||
128 | help=("Testing profile-based " |
||
129 | "remediation applied on already " |
||
130 | "installed machine"), |
||
131 | parents=[common_parser]) |
||
132 | parser_profile.set_defaults(func=ssg_test_suite.profile.perform_profile_check) |
||
133 | parser_profile.add_argument("target", |
||
134 | nargs="+", |
||
135 | metavar="DSPROFILE", |
||
136 | help=("Profiles to be tested, 'ALL' means every " |
||
137 | "profile of particular benchmark will be " |
||
138 | "evaluated.")) |
||
139 | |||
140 | parser_rule = subparsers.add_parser("rule", |
||
141 | help=("Testing remediations of particular " |
||
142 | "rule for various situations - " |
||
143 | "currently not supported " |
||
144 | "by openscap!"), |
||
145 | parents=[common_parser]) |
||
146 | parser_rule.set_defaults(func=ssg_test_suite.rule.perform_rule_check) |
||
147 | parser_rule.add_argument( |
||
148 | "target", |
||
149 | nargs="+", |
||
150 | metavar="RULE", |
||
151 | help=( |
||
152 | "Rule or rules to be tested. Special value 'ALL' means every " |
||
153 | "rule-testing scenario will be evaluated. The SSG rule ID prefix " |
||
154 | "is appended automatically if not provided. Wildcards to match " |
||
155 | "multiple rules are accepted." |
||
156 | ) |
||
157 | ) |
||
158 | parser_rule.add_argument("--debug", |
||
159 | dest="manual_debug", |
||
160 | action="store_true", |
||
161 | help=("If an error is encountered, all execution " |
||
162 | "on the VM / container will pause to allow " |
||
163 | "debugging.")) |
||
164 | parser_rule.add_argument("--dontclean", |
||
165 | dest="dont_clean", |
||
166 | action="store_true", |
||
167 | help="Do not remove html reports of successful runs") |
||
168 | parser_rule.add_argument("--scenarios", |
||
169 | dest="scenarios_regex", |
||
170 | default=None, |
||
171 | help="Regular expression matching test scenarios to run") |
||
172 | parser_rule.add_argument("--profile", |
||
173 | dest="scenarios_profile", |
||
174 | default=None, |
||
175 | help="Override the profile used for test scenarios." |
||
176 | " Variable selections will be done according " |
||
177 | "to this profile.") |
||
178 | |||
179 | parser_combined = subparsers.add_parser("combined", |
||
180 | help=("Tests all rules in a profile evaluating them " |
||
181 | "against their test scenarios."), |
||
182 | parents=[common_parser]) |
||
183 | parser_combined.set_defaults(func=ssg_test_suite.combined.perform_combined_check) |
||
184 | parser_combined.add_argument("--dontclean", |
||
185 | dest="dont_clean", |
||
186 | action="store_true", |
||
187 | help="Do not remove html reports of successful runs") |
||
188 | parser_combined.add_argument("--scenarios", |
||
189 | dest="scenarios_regex", |
||
190 | default=None, |
||
191 | help="Regular expression matching test scenarios to run") |
||
192 | parser_combined.add_argument("target", |
||
193 | metavar="TARGET", |
||
194 | help=("Profile whose rules are to be tested. Each rule selected " |
||
195 | "in the profile will be evaluated against all its test " |
||
196 | "scenarios.")) |
||
197 | |||
198 | return parser.parse_args() |
||
199 | |||
370 |