Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: build commands produces detailed output of what happened #927

Merged
merged 17 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions splunk_add_on_ucc_framework/commands/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import sys
from typing import Optional, List
import subprocess
import colorama as c
import fnmatch
import filecmp

from openapi3 import OpenAPI

Expand Down Expand Up @@ -327,12 +330,126 @@ def _get_python_version_from_executable(python_binary_name: str) -> str:
)


def summary_report(
source: str,
ta_name: str,
config_path: Optional[str] = None,
output_directory: Optional[str] = None,
verbose_report: Optional[str] = None,
):
# initialising colorama to handle ASCI color in windows cmd
c.init()
color_pallete = {
"copied": c.Fore.GREEN,
"conflict": c.Fore.RED,
"modified": c.Fore.YELLOW,
}

# conflicting files from ucc-gen package folder
conflict_path = os.path.join(internal_root_dir, "package")
# conflict files generated through-out the process
conflict_static_list = [
"import_declare_test.py",
f"{ta_name}_rh_*.py",
"app.conf",
"inputs.conf*",
"restmap.conf",
"server.conf",
f"{ta_name}_*.conf*",
"web.conf",
"default.xml",
"configuration.xml",
"dashboard.xml",
"inputs.xml",
"openapi.json",
]

def line_print(print_path, mod_type):
if verbose_report:
# print(
# color_pallete.get(mod_type, "") + str(print_path).ljust(80),
# mod_type + c.Style.RESET_ALL,
# )
logger.info(
color_pallete.get(mod_type, "") + str(print_path).ljust(80) +
mod_type + c.Style.RESET_ALL,
)
summary[mod_type] += 1

def summary_print(summary):
logger.info("File creation summary:")
for key, value in summary.items():
logger.info((key + ": ").ljust(15) + str(value))

def check_for_conflict(file, relative_file_path):
conflict_path_file = os.path.join(conflict_path, relative_file_path)

if os.path.isfile(conflict_path_file):
return True

for pattern in conflict_static_list:
if fnmatch.fnmatch(file, pattern):
return True

if file:
pass

return False

def file_check(file, output_directory, relative_file_path, source):
source_path = os.path.join(source, relative_file_path)

if os.path.isfile(source_path):
# file is present in package
output_path = os.path.join(output_directory, relative_file_path)

is_conflict = check_for_conflict(file, relative_file_path)

if not is_conflict:
files_are_same = filecmp.cmp(source_path, output_path)
if not files_are_same:
# output file was modified
line_print(relative_file_path, "modified")
else:
# files are the same
line_print(relative_file_path, "copied")
else:
line_print(relative_file_path, "conflict")
else:
# file does not exist in package
line_print(relative_file_path, "created")

summary = {"created": 0, "copied": 0, "modified": 0, "conflict": 0}

path_len = len(output_directory) + 1

for path, dir, files in os.walk(output_directory):
relative_path = path[path_len:]
# skipping lib directory
if relative_path[:3] == "lib":
if relative_path == "lib":
line_print("lib", "created")
continue

files = sorted(files, key=str.casefold)

for file in files:
relative_file_path = os.path.join(relative_path, file)
file_check(file, output_directory, relative_file_path, source)

# print(summary)
summary_print(summary)

return


def generate(
source: str,
config_path: Optional[str] = None,
addon_version: Optional[str] = None,
output_directory: Optional[str] = None,
python_binary_name: str = "python3",
verbose_report: Optional[str] = None,
) -> None:
logger.info(f"ucc-gen version {__version__} is used")
logger.info(f"Python binary name to use: {python_binary_name}")
Expand Down Expand Up @@ -583,3 +700,26 @@ def generate(
logger.info(f"Creating {output_openapi_folder} folder")
with open(output_openapi_path, "w") as openapi_file:
json.dump(open_api.raw_element, openapi_file, indent=4)

summary_report(
source=source,
ta_name=ta_name,
config_path=config_path,
output_directory=os.path.join(output_directory, ta_name),
verbose_report=verbose_report,
)


# TODO remove after debugging
if __name__ == "__main__":
source = "/Users/mmacalik/Documents/Ucc-Gen_webinar/repos/addonfactory-ucc-generator/temp_add_on/test_addon/package"
output_directory = "/Users/mmacalik/Documents/Ucc-Gen_webinar/repos/addonfactory-ucc-generator/temp_add_on/test_addon/output/test_addon"

app_manifest = _get_app_manifest(source)
ta_name = app_manifest.get_addon_name()
summary_report(
source=source,
output_directory=output_directory,
ta_name=ta_name,
verbose_report=True,
)
8 changes: 8 additions & 0 deletions splunk_add_on_ucc_framework/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
help="Python binary name to use to install requirements",
default="python3",
)
build_parser.add_argument(
"-v",
"--verbose",
action="store_true",
default=False,
help="Show files install result",
)

package_parser = subparsers.add_parser("package", description="Package an add-on")
package_parser.add_argument(
Expand Down Expand Up @@ -178,6 +185,7 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
addon_version=args.ta_version,
output_directory=args.output,
python_binary_name=args.python_binary_name,
verbose_report=args.verbose,
)
if args.command == "package":
package.package(path_to_built_addon=args.path, output_directory=args.output)
Expand Down