diff --git a/splunk_add_on_ucc_framework/__init__.py b/splunk_add_on_ucc_framework/__init__.py index b1e4b6ffb..aa6d2e18e 100644 --- a/splunk_add_on_ucc_framework/__init__.py +++ b/splunk_add_on_ucc_framework/__init__.py @@ -6,6 +6,7 @@ import logging import os, time +import re import glob from os import system import shutil @@ -687,7 +688,30 @@ def main(): with open(os.path.abspath(os.path.join(outputdir, ta_name, "app.manifest")), "w") as manifest_file: manifest_file.write(json.dumps(manifest, indent=4, sort_keys=True)) - + + config_file = os.path.join(outputdir, ta_name,'default', "app.conf") + def save_comments(config_file): + """Save index and content of comments in config file and return dictionary thereof""" + comment_map = {} + with open(config_file, 'r') as file: + i = 0 + lines = file.readlines() + for line in lines: + if re.match( r'^\s*#.*?$', line): + comment_map[i] = line + i += 1 + return comment_map + + def restore_comments(config_file, comment_map): + """Write comments to config file at their original indices""" + with open(config_file, 'r') as file: + lines = file.readlines() + for (index, comment) in sorted(comment_map.items()): + lines.insert(index, comment) + with open(config_file, 'w') as file: + file.write(''.join(lines)) + + comment_map = save_comments(config_file) app_config = configparser.ConfigParser() app_config.read_file(open(os.path.join(outputdir, ta_name,'default', "app.conf"))) if not 'launcher' in app_config: @@ -713,7 +737,8 @@ def main(): with open(os.path.join(outputdir, ta_name,'default', "app.conf"), 'w') as configfile: app_config.write(configfile) - + restore_comments(config_file, comment_map) + #Copy Licenses license_dir = os.path.abspath(os.path.join(args.source, PARENT_DIR, "LICENSES"))