forked from upgrades-migrations/preupgrade-assistant
/
preupg-create-group-xml
executable file
·94 lines (83 loc) · 3.08 KB
/
preupg-create-group-xml
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/python2
from __future__ import print_function
import os
import sys
import optparse
import shutil
from xml.etree import ElementTree
from distutils import dir_util
from preuputils import variables
from preuputils.compose import ComposeXML
from preup import settings, utils
from preup.utils import get_file_content, write_to_file
from preup.exception import MissingFileInContentError, MissingHeaderCheckScriptError, MissingTagsIniFileError
try:
from xml.etree.ElementTree import ParseError
except ImportError:
from xml.parsers.expat import ExpatError as ParseError
def main():
parser = optparse.OptionParser(usage="%prog [options] dirname", description="Create XML files for OpenSCAP")
parser.add_option('-g', '--group',
help='Generate only group.xml file.',
action='store_true'
)
opts, args = parser.parse_args()
if len(args) > 1 or len(args) == 0:
print('Specify just one directory with INI file.')
parser.print_help()
sys.exit(1)
if not os.path.exists(args[0]):
print('Dir %s does not exists.' % args[0])
sys.exit(1)
if args[0].endswith('/'):
args[0] = args[0][:-1]
# License text will not be pregenerated
found = 0
for d in os.listdir(args[0]):
if d.endswith(".ini"):
found = 1
if not found:
print ('In directory %s was not found any INI file.' % args[0])
sys.exit(1)
dir_name = utils.get_valid_scenario(args[0])
if dir_name is None:
print ('Dir does not contain proper scenario.')
sys.exit(1)
index = 0
for i, d in enumerate(args[0].split(os.path.sep)):
if d == dir_name:
index = i
break
dir_name = '/'.join(args[0].split(os.path.sep)[:index+1])
result_dirname = dir_name + variables.result_prefix
if os.path.exists(result_dirname):
shutil.rmtree(result_dirname)
dir_util.copy_tree(dir_name, result_dirname)
dir_name = args[0].replace(dir_name, result_dirname)
filename = "all-xccdf.xml"
settings.autocomplete = False
template_file = ComposeXML.get_template_file()
target_tree = None
try:
target_tree = ElementTree.parse(template_file).getroot()
except IOError:
print ('Problem with reading template.xml file')
sys.exit(1)
try:
target_tree = ComposeXML.run_compose(target_tree, os.path.dirname(dir_name), content=os.path.basename(dir_name))
except MissingHeaderCheckScriptError:
sys.exit(1)
except MissingFileInContentError:
sys.exit(1)
except MissingTagsIniFileError:
sys.exit(1)
try:
# must be encoded by ElementTree!
data = ElementTree.tostring(target_tree, "utf-8")
write_to_file(os.path.join(dir_name, filename), "wb", data, False)
print ('Generated report file for preupgrade-assistant is: %s' % ''.join(os.path.join(dir_name, filename)))
except IOError:
print ("Problem with writing file %s." % filename)
#oscap_group.write_profile_xml(target_tree)
if __name__ == "__main__":
main()