diff --git a/splunk_add_on_ucc_framework/__init__.py b/splunk_add_on_ucc_framework/__init__.py index 9d8a09e96..367ada04e 100644 --- a/splunk_add_on_ucc_framework/__init__.py +++ b/splunk_add_on_ucc_framework/__init__.py @@ -572,24 +572,26 @@ def make_modular_alerts(ta_name, ta_namespace, schema_content, outputdir): ) -def get_ignore_list(ta_name, path): +def get_ignore_list(ta_name: str, uccignore_path: str, output_path: str): """ Return path of files/folders to be removed. Args: - ta_name (str): Name of TA. - path (str): Path of '.uccignore'. + ta_name: Name of TA. + uccignore_path: Path of '.uccignore'. + output_path: Path to output directory. Returns: list: List of paths to be removed from output directory. """ - if not os.path.exists(path): + if not os.path.exists(uccignore_path): + logger.info("No .uccignore specified - nothing to ignore") return [] else: - with open(path) as ignore_file: + with open(uccignore_path) as ignore_file: ignore_list = ignore_file.readlines() ignore_list = [ - (os.path.join("output", ta_name, get_os_path(path))).strip() + (os.path.join(output_path, ta_name, get_os_path(path))).strip() for path in ignore_list ] return ignore_list @@ -606,8 +608,10 @@ def remove_listed_files(ignore_list): for path in ignore_list: if os.path.exists(path): if os.path.isfile(path): + logger.info(f"Removing file {path} because of .uccignore") os.remove(path) elif os.path.isdir(path): + logger.info(f"Removing directory {path} because of .uccignore") shutil.rmtree(path, ignore_errors=True) else: logger.info( @@ -800,13 +804,13 @@ def _generate(source, config, ta_version, outputdir=None): logger.info(f"Install add-on requirements into {ucc_lib_target} from {source}") install_libs(source, ucc_lib_target=ucc_lib_target) - ignore_list = get_ignore_list( - ta_name, os.path.abspath(os.path.join(source, PARENT_DIR, ".uccignore")) - ) - remove_listed_files(ignore_list) logger.info("Copy package directory") recursive_overwrite(source, os.path.join(outputdir, ta_name)) + uccignore_path = os.path.abspath(os.path.join(source, PARENT_DIR, ".uccignore")) + ignore_list = get_ignore_list(ta_name, uccignore_path, outputdir) + remove_listed_files(ignore_list) + default_meta_conf_path = os.path.join( outputdir, ta_name, "metadata", "default.meta" ) diff --git a/tests/data/package_with_dot_uccignore/.uccignore b/tests/data/package_with_dot_uccignore/.uccignore new file mode 100644 index 000000000..6f5a9e1cf --- /dev/null +++ b/tests/data/package_with_dot_uccignore/.uccignore @@ -0,0 +1 @@ +default/pytest-splunk-addon-data.conf \ No newline at end of file diff --git a/tests/data/package_with_dot_uccignore/package/README.txt b/tests/data/package_with_dot_uccignore/package/README.txt new file mode 100644 index 000000000..530a9e548 --- /dev/null +++ b/tests/data/package_with_dot_uccignore/package/README.txt @@ -0,0 +1 @@ +Just a readme \ No newline at end of file diff --git a/tests/data/package_with_dot_uccignore/package/app.manifest b/tests/data/package_with_dot_uccignore/package/app.manifest new file mode 100644 index 000000000..6bcf830b8 --- /dev/null +++ b/tests/data/package_with_dot_uccignore/package/app.manifest @@ -0,0 +1,51 @@ +{ + "schemaVersion": "2.0.0", + "info": { + "title": "Splunk Add-on for UCC Example", + "id": { + "group": null, + "name": "Splunk_TA_UCCExample", + "version": "7.0.1" + }, + "author": [ + { + "name": "Splunk", + "email": null, + "company": null + } + ], + "releaseDate": null, + "description": "Splunk Add-on for UCC Example", + "classification": { + "intendedAudience": null, + "categories": [], + "developmentStatus": null + }, + "commonInformationModels": null, + "license": { + "name": null, + "text": "LICENSES/Apache-2.0.txt", + "uri": null + }, + "privacyPolicy": { + "name": null, + "text": null, + "uri": null + }, + "releaseNotes": { + "name": null, + "text": "./README.txt", + "uri": null + } + }, + "dependencies": null, + "tasks": null, + "inputGroups": null, + "incompatibleApps": null, + "platformRequirements": null, + "supportedDeployments": [ + "_standalone", + "_distributed" + ], + "targetWorkloads": null +} diff --git a/tests/data/package_with_dot_uccignore/package/default/app.conf b/tests/data/package_with_dot_uccignore/package/default/app.conf new file mode 100644 index 000000000..af80bbf04 --- /dev/null +++ b/tests/data/package_with_dot_uccignore/package/default/app.conf @@ -0,0 +1,23 @@ +###################################################### +# +# ${package.name} +# +# ${copyright} +# +###################################################### + +[install] +build = 0 + +[launcher] +author = Splunk Inc. +version = 0.1.0 +description = Splunk_TA_UCCExample + +[ui] +is_visible = true +label = Splunk_TA_UCCExample +docs_section_override=AddOns:released + +[package] +id = Splunk_TA_UCCExample diff --git a/tests/data/package_with_dot_uccignore/package/default/eventtypes.conf b/tests/data/package_with_dot_uccignore/package/default/eventtypes.conf new file mode 100644 index 000000000..bd34af462 --- /dev/null +++ b/tests/data/package_with_dot_uccignore/package/default/eventtypes.conf @@ -0,0 +1,2 @@ +[new_eventtype] +search = sourcetype=new_sourcetype \ No newline at end of file diff --git a/tests/data/package_with_dot_uccignore/package/default/props.conf b/tests/data/package_with_dot_uccignore/package/default/props.conf new file mode 100644 index 000000000..9527f0dc3 --- /dev/null +++ b/tests/data/package_with_dot_uccignore/package/default/props.conf @@ -0,0 +1,4 @@ +# props.conf + +[new_sourcetype] +EVAL-new_field = if(field2 = "hello", "world") \ No newline at end of file diff --git a/tests/data/package_with_dot_uccignore/package/default/pytest-splunk-addon-data.conf b/tests/data/package_with_dot_uccignore/package/default/pytest-splunk-addon-data.conf new file mode 100644 index 000000000..5ce423074 --- /dev/null +++ b/tests/data/package_with_dot_uccignore/package/default/pytest-splunk-addon-data.conf @@ -0,0 +1,2 @@ +[stanza] +key = value diff --git a/tests/data/package_with_dot_uccignore/package/default/tags.conf b/tests/data/package_with_dot_uccignore/package/default/tags.conf new file mode 100644 index 000000000..2dfd39e0e --- /dev/null +++ b/tests/data/package_with_dot_uccignore/package/default/tags.conf @@ -0,0 +1,2 @@ +[eventtype=new_eventtype] +new_tag = enabled \ No newline at end of file diff --git a/tests/data/test_ucc_generate.py b/tests/data/test_ucc_generate.py index 7afbc0992..9d10c27b3 100644 --- a/tests/data/test_ucc_generate.py +++ b/tests/data/test_ucc_generate.py @@ -182,6 +182,27 @@ def test_ucc_generate_with_configuration_files_only(self): msg=f"Expected file {expected_file_path} is different from {actual_file_path}", ) + def test_ucc_generate_uccignore(self): + with tempfile.TemporaryDirectory() as temp_dir: + package_folder = path.join( + path.dirname(path.realpath(__file__)), + "package_with_dot_uccignore", + "package", + ) + ucc.generate(source=package_folder, outputdir=temp_dir) + + ignored_file_exists = path.exists( + path.join( + temp_dir, + "Splunk_TA_UCCExample", + "default", + "pytest-splunk-addon-data.conf", + ) + ) + self.assertFalse( + ignored_file_exists, "Ignored file still exists after ucc-gen" + ) + if __name__ == "__main__": unittest.main()