-
Notifications
You must be signed in to change notification settings - Fork 1
/
script_tester.py
executable file
·69 lines (57 loc) · 2.37 KB
/
script_tester.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
#
# Copyright 2011 Red Hat Inc., Durham, North Carolina.
# All Rights Reserved.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Authors:
# "Martin Preisler" <[email protected]>
#
import argparse
parser = argparse.ArgumentParser(description = "Performs checks by given scripts and returns the result")
parser.add_argument("globs", metavar = "F", type = str, nargs = "+",
help = "File or glob of the check script to process")
args = parser.parse_args()
import glob
files = []
for g in args.globs:
gfiles = glob.glob(g)
files.extend(gfiles)
if len(gfiles) == 0:
print("No file matches '%s'" % (g))
import os
xccdf_result_map = dict()
xccdf_result_map["XCCDF_RESULT_PASS"] = 101
xccdf_result_map["XCCDF_RESULT_FAIL"] = 102
xccdf_result_map["XCCDF_RESULT_ERROR"] = 103
xccdf_result_map["XCCDF_RESULT_UNKNOWN"] = 104
xccdf_result_map["XCCDF_RESULT_NOT_APPLICABLE"] = 105
xccdf_result_map["XCCDF_RESULT_NOT_CHECKED"] = 106
xccdf_result_map["XCCDF_RESULT_NOT_SELECTED"] = 107
xccdf_result_map["XCCDF_RESULT_INFORMATIONAL"] = 108
xccdf_result_map["XCCDF_RESULT_FIXED"] = 109
xccdf_reverse_result_map = dict()
for key, value in xccdf_result_map.iteritems():
xccdf_reverse_result_map[value] = key
# set all XCCDF result types as environment variables
# (this is especially useful for bash scripts)
for key, value in xccdf_result_map.iteritems():
os.environ[key] = str(value)
import subprocess
for file_path in files:
result = subprocess.call(["./%s" % (file_path)])
result_text = xccdf_reverse_result_map[result] if xccdf_reverse_result_map.has_key(result) else "Unknown exit code %i" % (result)
print("Result of '%s' is %s" % (file_path, result_text))