| Conditions | 8 |
| Total Lines | 57 |
| Code Lines | 45 |
| 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 | import sys |
||
| 155 | def main(): |
||
| 156 | global definitions |
||
| 157 | global tests |
||
| 158 | global objects |
||
| 159 | global states |
||
| 160 | global variables |
||
| 161 | global silent_mode |
||
| 162 | |||
| 163 | args = parse_options() |
||
| 164 | silent_mode = args.silent_mode |
||
| 165 | oval_version = args.oval_version |
||
| 166 | |||
| 167 | testfile = args.xmlfile |
||
| 168 | header = oval_generated_header("testoval.py", oval_version, "0.0.1") |
||
| 169 | testfile = find_testfile(testfile) |
||
| 170 | body = read_ovaldefgroup_file(testfile) |
||
| 171 | defname = add_oval_elements(body, header) |
||
| 172 | ovaltree = ET.fromstring(header + footer) |
||
| 173 | |||
| 174 | # append each major element type, if it has subelements |
||
| 175 | for element in [definitions, tests, objects, states, variables]: |
||
| 176 | if element.getchildren(): |
||
| 177 | ovaltree.append(element) |
||
| 178 | # re-map all the element ids from meaningful names to meaningless |
||
| 179 | # numbers |
||
| 180 | testtranslator = idtranslate.IDTranslator("scap-security-guide.testing") |
||
| 181 | ovaltree = testtranslator.translate(ovaltree) |
||
| 182 | (ovalfile, fname) = tempfile.mkstemp(prefix=defname, suffix=".xml") |
||
| 183 | os.write(ovalfile, ET.tostring(ovaltree)) |
||
| 184 | os.close(ovalfile) |
||
| 185 | if not silent_mode: |
||
| 186 | print("Evaluating with OVAL tempfile: " + fname) |
||
| 187 | print("OVAL Schema Version: %s" % oval_version) |
||
| 188 | print("Writing results to: " + fname + "-results") |
||
| 189 | cmd = "oscap oval eval --results " + fname + "-results " + fname |
||
| 190 | oscap_child = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) |
||
| 191 | cmd_out = oscap_child.communicate()[0] |
||
| 192 | if not silent_mode: |
||
| 193 | print cmd_out |
||
| 194 | if oscap_child.returncode != 0: |
||
| 195 | if not silent_mode: |
||
| 196 | print("Error launching 'oscap' command: \n\t" + cmd) |
||
| 197 | sys.exit(2) |
||
| 198 | if 'false' in cmd_out: |
||
| 199 | # at least one from the evaluated OVAL definitions evaluated to |
||
| 200 | # 'false' result, exit with '1' to indicate OVAL scan FAIL result |
||
| 201 | sys.exit(1) |
||
| 202 | # perhaps delete tempfile? |
||
| 203 | definitions = ET.Element("definitions") |
||
| 204 | tests = ET.Element("tests") |
||
| 205 | objects = ET.Element("objects") |
||
| 206 | states = ET.Element("states") |
||
| 207 | variables = ET.Element("variables") |
||
| 208 | |||
| 209 | # 'false' keyword wasn't found in oscap's command output |
||
| 210 | # exit with '0' to indicate OVAL scan TRUE result |
||
| 211 | sys.exit(0) |
||
| 212 |