| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
# Copyright (c) 2022-2026 The Project Contributors. |
| 4 |
# |
| 5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
# you may not use this file except in compliance with the License. |
| 7 |
# You may obtain a copy of the License at |
| 8 |
# |
| 9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
# |
| 11 |
# Unless required by applicable law or agreed to in writing, software |
| 12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
# See the License for the specific language governing permissions and |
| 15 |
# limitations under the License. |
| 16 |
# |
| 17 |
# For a detailed list of copyright holders and contribution history, |
| 18 |
# please refer to the CONTRIBUTORS.md file in the project root. |
| 19 |
|
| 20 |
from __future__ import (absolute_import, division, print_function) |
| 21 |
__metaclass__ = type |
| 22 |
|
| 23 |
from ansible.module_utils.basic import missing_required_lib |
| 24 |
import traceback |
| 25 |
import sys |
| 26 |
import os |
| 27 |
|
| 28 |
|
| 29 |
BS4_LIBRARY_IMPORT_ERROR = None |
| 30 |
try: |
| 31 |
from bs4 import BeautifulSoup |
| 32 |
except ImportError: |
| 33 |
BS4_LIBRARY_IMPORT_ERROR = traceback.format_exc() |
| 34 |
HAS_BS4_LIBRARY = False |
| 35 |
else: |
| 36 |
HAS_BS4_LIBRARY = True |
| 37 |
|
| 38 |
LXML_LIBRARY_IMPORT_ERROR = None |
| 39 |
try: |
| 40 |
from lxml import etree |
| 41 |
except ImportError: |
| 42 |
LXML_LIBRARY_IMPORT_ERROR = traceback.format_exc() |
| 43 |
HAS_LXML_LIBRARY = False |
| 44 |
else: |
| 45 |
HAS_LXML_LIBRARY = True |
| 46 |
|
| 47 |
|
| 48 |
def debug_bs4(module): |
| 49 |
# Diagnose XML file parsing errors in Beautiful Soup |
| 50 |
# https://stackoverflow.com/questions/56942892/cannot-parse-iso-8859-15-encoded-xml-with-bs4/56947172#56947172 |
| 51 |
if not HAS_BS4_LIBRARY: |
| 52 |
module.fail_json(msg=missing_required_lib( |
| 53 |
"bs4"), exception=BS4_LIBRARY_IMPORT_ERROR) |
| 54 |
from bs4.diagnose import diagnose |
| 55 |
with open('control.xml', 'rb') as f: |
| 56 |
diagnose(f) |
| 57 |
|
| 58 |
|
| 59 |
# SWPM2 control.xml conversion to utf8 |
| 60 |
def control_xml_utf8(filepath, module): |
| 61 |
if not HAS_LXML_LIBRARY: |
| 62 |
module.fail_json(msg=missing_required_lib( |
| 63 |
"lxml"), exception=LXML_LIBRARY_IMPORT_ERROR) |
| 64 |
source = filepath + "/control.xml" |
| 65 |
|
| 66 |
# Convert control.xml from iso-8859-1 to UTF-8, so it can be used with Beautiful Soup lxml-xml parser |
| 67 |
# https://stackoverflow.com/questions/64629600/how-can-you-convert-a-xml-iso-8859-1-to-utf-8-using-python-3-7-7/64634454#64634454 |
| 68 |
with open(source, 'rb') as source: |
| 69 |
parser = etree.XMLParser(encoding="iso-8859-1", strip_cdata=False) |
| 70 |
root = etree.parse(source, parser) |
| 71 |
|
| 72 |
string = etree.tostring(root, xml_declaration=True, encoding="UTF-8", |
| 73 |
pretty_print=True).decode('utf8').encode('iso-8859-1') |
| 74 |
|
| 75 |
# string1 = etree.tostring(root, xml_declaration=True, encoding="UTF-8", |
| 76 |
# pretty_print=True).decode('utf8').encode('utf-8').strip() |
| 77 |
|
| 78 |
with open('control_utf8.xml', 'wb') as target: |
| 79 |
target.write(string) |
| 80 |
|
| 81 |
|
| 82 |
# SWPM2 Component and Parameters extract all as CSV |
| 83 |
def control_xml_to_csv(filepath, module): |
| 84 |
if not HAS_BS4_LIBRARY: |
| 85 |
module.fail_json(msg=missing_required_lib( |
| 86 |
"bs4"), exception=BS4_LIBRARY_IMPORT_ERROR) |
| 87 |
|
| 88 |
infile = open(filepath + "/control_utf8.xml", "r") |
| 89 |
contents = infile.read() |
| 90 |
|
| 91 |
soup = BeautifulSoup(markup=contents, features='lxml-xml') |
| 92 |
space = soup.find('components') |
| 93 |
|
| 94 |
component_list = space.findChildren("component", recursive=False) |
| 95 |
|
| 96 |
csv_output = open('control_output.csv', 'w') |
| 97 |
csv_header = '"' + 'Component Name' + '","' + 'Component Display Name' + '","' + 'Parameter Name' + '","' + 'Parameter Inifile Key' + \ |
| 98 |
'","' + 'Parameter Access' + '","' + 'Parameter Encode' + '","' + \ |
| 99 |
'Parameter Default Value' + '","' + 'Parameter Inifile description' + '"' |
| 100 |
csv_output.write("%s\n" % csv_header) |
| 101 |
|
| 102 |
for component in component_list: |
| 103 |
for parameter in component.findChildren("parameter"): |
| 104 |
component_key = parameter.findParent("component") |
| 105 |
component_key_name_text = component_key["name"] |
| 106 |
for child in component_key.findChildren("display-name"): |
| 107 |
component_key_display_name_text = child.get_text().replace('\n', '') |
| 108 |
component_parameter_key_name = parameter["name"] |
| 109 |
component_parameter_key_inifile_name = parameter.get( |
| 110 |
"defval-for-inifile-generation", "") |
| 111 |
component_parameter_key_access = parameter.get("access", "") |
| 112 |
component_parameter_key_encode = parameter.get("encode", "") |
| 113 |
component_parameter_key_defval = parameter.get("defval", "") |
| 114 |
component_parameter_contents_doclong_text = parameter.get_text().replace('\n', '') |
| 115 |
component_parameter_contents_doclong_text_quote_replacement = component_parameter_contents_doclong_text.replace( |
| 116 |
'"', '\'') |
| 117 |
csv_string = '"' + component_key_name_text + '","' + component_key_display_name_text + '","' + \ |
| 118 |
component_parameter_key_name + '","' + component_parameter_key_inifile_name + '","' + \ |
| 119 |
component_parameter_key_access + '","' + component_parameter_key_encode + '","' + \ |
| 120 |
component_parameter_key_defval + '","' + \ |
| 121 |
component_parameter_contents_doclong_text_quote_replacement + '"' |
| 122 |
csv_output.write("%s\n" % csv_string) |
| 123 |
|
| 124 |
csv_output.close() |
| 125 |
|
| 126 |
|
| 127 |
# SWPM2 Component and Parameters extract all and generate template inifile.params |
| 128 |
def control_xml_to_inifile_params(filepath, module): |
| 129 |
if not HAS_BS4_LIBRARY: |
| 130 |
module.fail_json(msg=missing_required_lib( |
| 131 |
"bs4"), exception=BS4_LIBRARY_IMPORT_ERROR) |
| 132 |
|
| 133 |
infile = open(filepath + "/control_utf8.xml", "r") |
| 134 |
contents = infile.read() |
| 135 |
|
| 136 |
soup = BeautifulSoup(markup=contents, features='lxml-xml') |
| 137 |
space = soup.find('components') |
| 138 |
|
| 139 |
component_list = space.findChildren("component", recursive=False) |
| 140 |
|
| 141 |
inifile_output = open('generated_inifile_params', 'w') |
| 142 |
|
| 143 |
inifile_params_header = """############ |
| 144 |
# SWPM Unattended Parameters inifile.params generated export |
| 145 |
# |
| 146 |
# |
| 147 |
# Export of all SWPM Component and the SWPM Unattended Parameters. Not all components have SWPM Unattended Parameters. |
| 148 |
# |
| 149 |
# All parameters are commented-out, each hash # before the parameter is removed to activate the parameter. |
| 150 |
# When running SWPM in Unattended Mode, the activated parameters will create a new SWPM file in the sapinst directory. |
| 151 |
# If any parameter is marked as 'encode', the plaintext value will be coverted to DES hash |
| 152 |
# for this parameter in the new SWPM file (in the sapinst directory). |
| 153 |
# |
| 154 |
# An inifile.params is otherwise obtained after running SWPM as GUI or Unattended install, |
| 155 |
# and will be generated for a specific Product ID (such as 'NW_ABAP_OneHost:S4HANA1809.CORE.HDB.CP'). |
| 156 |
############ |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
############ |
| 161 |
# MANUAL |
| 162 |
############ |
| 163 |
|
| 164 |
# The folder containing all archives that have been downloaded from http://support.sap.com/swdc and are supposed to be used in this procedure |
| 165 |
# archives.downloadBasket = |
| 166 |
""" |
| 167 |
|
| 168 |
inifile_output.write(inifile_params_header) |
| 169 |
|
| 170 |
for component in component_list: |
| 171 |
component_key_name_text = component["name"] |
| 172 |
component_key_display_name = component.find("display-name") |
| 173 |
if component_key_display_name is not None: |
| 174 |
component_key_display_name_text = component_key_display_name.get_text() |
| 175 |
inifile_output.write("\n\n\n\n############\n# Component: %s\n# Component Display Name: %s\n############\n" % ( |
| 176 |
component_key_name_text, component_key_display_name_text)) |
| 177 |
for parameter in component.findChildren("parameter"): |
| 178 |
# component_key=parameter.findParent("component") |
| 179 |
component_parameter_key_encode = parameter.get("encode", None) |
| 180 |
component_parameter_key_inifile_name = parameter.get( |
| 181 |
"defval-for-inifile-generation", None) |
| 182 |
component_parameter_key_defval = parameter.get("defval", "") |
| 183 |
component_parameter_contents_doclong_text = parameter.get_text().replace('\n', '') |
| 184 |
# component_parameter_contents_doclong_text_quote_replacement=component_parameter_contents_doclong_text.replace('"','\'') |
| 185 |
if component_parameter_key_inifile_name is not None: |
| 186 |
inifile_output.write("\n# %s" % ( |
| 187 |
component_parameter_contents_doclong_text)) |
| 188 |
if component_parameter_key_encode == "true": |
| 189 |
inifile_output.write( |
| 190 |
"\n# Encoded parameter. Plaintext values will be coverted to DES hash") |
| 191 |
inifile_output.write("\n# %s = %s\n" % ( |
| 192 |
component_parameter_key_inifile_name, component_parameter_key_defval)) |
| 193 |
|
| 194 |
inifile_output.close() |
| 195 |
|
| 196 |
# SWPM2 product.catalog conversion to utf8 |
| 197 |
|
| 198 |
|
| 199 |
def product_catalog_xml_utf8(filepath, module): |
| 200 |
if not HAS_LXML_LIBRARY: |
| 201 |
module.fail_json(msg=missing_required_lib( |
| 202 |
"lxml"), exception=LXML_LIBRARY_IMPORT_ERROR) |
| 203 |
|
| 204 |
source = filepath + "/product.catalog" |
| 205 |
|
| 206 |
# Convert control.xml from iso-8859-1 to UTF-8, so it can be used with Beautiful Soup lxml-xml parser |
| 207 |
# https://stackoverflow.com/questions/64629600/how-can-you-convert-a-xml-iso-8859-1-to-utf-8-using-python-3-7-7/64634454#64634454 |
| 208 |
with open(source, 'rb') as source: |
| 209 |
parser = etree.XMLParser(encoding="iso-8859-1", strip_cdata=False) |
| 210 |
root = etree.parse(source, parser) |
| 211 |
|
| 212 |
string = etree.tostring(root, xml_declaration=True, encoding="UTF-8", |
| 213 |
pretty_print=True).decode('utf8').encode('iso-8859-1') |
| 214 |
|
| 215 |
with open('product_catalog_utf8.xml', 'wb') as target: |
| 216 |
target.write(string) |
| 217 |
|
| 218 |
# SWPM2 Product Catalog entries to CSV |
| 219 |
# Each Product Catalog entry is part of a components group, which may have attributes: |
| 220 |
# output-dir, control-file, product-dir (link to SWPM directory of param file etc) |
| 221 |
# Attributes possible for each entry = control-file, db, id, name, os, os-type, output-dir, |
| 222 |
# ppms-component, ppms-component-release, product, product-dir, release, table |
| 223 |
|
| 224 |
|
| 225 |
def product_catalog_xml_to_csv(filepath, module): |
| 226 |
if not HAS_BS4_LIBRARY: |
| 227 |
module.fail_json(msg=missing_required_lib( |
| 228 |
"bs4"), exception=BS4_LIBRARY_IMPORT_ERROR) |
| 229 |
|
| 230 |
infile = open(filepath + "/product_catalog_utf8.xml", "r") |
| 231 |
contents = infile.read() |
| 232 |
|
| 233 |
soup = BeautifulSoup(markup=contents, features='lxml-xml') |
| 234 |
space = soup.find_all('component') |
| 235 |
|
| 236 |
csv_output = open('product_catalog_output.csv', 'w') |
| 237 |
csv_header = '"' + 'Product Catalog Component Name' + '","' + 'Product Catalog Component ID' + '","' + 'Product Catalog Component Table' + '","' + \ |
| 238 |
'Product Catalog Component Output Dir' + '","' + 'Product Catalog Component Display Name' + \ |
| 239 |
'","' + 'Product Catalog Component UserInfo' + '"' |
| 240 |
csv_output.write("%s\n" % csv_header) |
| 241 |
|
| 242 |
for component in space: |
| 243 |
component_name = component.get("name", "") |
| 244 |
component_id = component.get("id", "") |
| 245 |
component_table = component.get("table", "") |
| 246 |
component_output_dir = component.get("output-dir", "") |
| 247 |
for displayname in component.findChildren("display-name"): |
| 248 |
component_displayname = displayname.get_text().strip() |
| 249 |
for userinfo in component.findChildren("user-info"): |
| 250 |
html_raw = userinfo.get_text().strip() |
| 251 |
html_parsed = BeautifulSoup(html_raw, 'html.parser') |
| 252 |
component_userinfo = html_parsed.get_text().replace('"', '\'') |
| 253 |
csv_string = '"' + component_name + '","' + component_id + '","' + component_table + '","' + \ |
| 254 |
component_output_dir + '","' + component_displayname + \ |
| 255 |
'","' + component_userinfo + '"' |
| 256 |
csv_output.write("%s\n" % csv_string) |
| 257 |
|
| 258 |
csv_output.close() |
| 259 |
|
| 260 |
|
| 261 |
# Get arguments passed to Python script session |
| 262 |
# Define path to control.xml, else assume in /tmp directory |
| 263 |
|
| 264 |
if len(sys.argv) > 1: |
| 265 |
control_xml_path = sys.argv[1] |
| 266 |
else: |
| 267 |
control_xml_path = "/tmp" |
| 268 |
|
| 269 |
if control_xml_path == "": |
| 270 |
control_xml_path = os.getcwd() |
| 271 |
|
| 272 |
if os.path.exists(control_xml_path + '/control.xml'): |
| 273 |
control_xml_utf8(control_xml_path, '') |
| 274 |
control_xml_to_csv(control_xml_path, '') |
| 275 |
control_xml_to_inifile_params(control_xml_path, '') |