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 4 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
18 changes: 18 additions & 0 deletions docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,24 @@ It takes the following parameters:
Accepts absolute paths as well.
* `--python-binary-name` - [optional] Python binary name to use when
installing Python libraries. Default: `python3`.
* `-v` / `--verbose` - [optional] show detailed information about
created/copied/modified/conflict files after build is complete.
Default: `False`.

#### Verbose mode

Available from `v5.33.0`.

Running `ucc-gen build -v` or `ucc-gen build --verbose` prints additional information about
what was exactly created / copied / modified / conflict after the build is complete. It does
not scan `lib` folder due to the nature of the folder.

Below is the explanation on what exactly each state means:

* `created` - file is not in the original package and was created during the build process
* `copied` - file is in the original package and was copied during the build process
* `modified` - file is in the original package and was modified during the build process
* `conflict` - file is in the original package and was copied during the build process but may be generated by UCC itself so incorrect usage can lead to not working add-on

### `ucc-gen init`

Expand Down
126 changes: 126 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,128 @@ def _get_python_version_from_executable(python_binary_name: str) -> str:
)


def summary_report(
source: str,
ta_name: str,
output_directory: str,
verbose_report: bool,
) -> None:
# initialising colorama to handle ASCII color in windows cmd
c.init()
color_palette = {
"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 = frozenset(
[
"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: str, mod_type: str) -> None:
if verbose_report:
logger.info(
color_palette.get(mod_type, "")
+ str(print_path).ljust(80)
+ mod_type
+ c.Style.RESET_ALL,
)
summary[mod_type] += 1

def check_for_conflict(file: str, relative_file_path: str) -> bool:
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: str, output_directory: str, relative_file_path: str, source: str
) -> None:
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

if verbose_report:
logger.info("Detailed information about created/copied/modified/conflict files")
logger.info(
"Read more about it here: "
"https://splunk.github.io/addonfactory-ucc-generator/quickstart/#verbose-mode"
)

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)

summary_combined = ", ".join(
[
f"{file_type}: {amount_of_files}"
for file_type, amount_of_files in summary.items()
]
)
logger.info(f"File creation summary: {summary_combined}")


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: bool = False,
) -> 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 +702,10 @@ 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,
ta_name,
os.path.join(output_directory, ta_name),
verbose_report,
)
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 detailed information about created/copied/modified/conflict files after build is complete",
)

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
Original file line number Diff line number Diff line change
Expand Up @@ -1291,7 +1291,7 @@
"meta": {
"name": "Splunk_TA_UCCExample",
"restRoot": "splunk_ta_uccexample",
"version": "5.31.1Rdd3a1ca0",
"version": "5.32.0R9aa5509f",
"displayName": "Splunk UCC test Add-on",
"schemaVersion": "0.0.3"
}
Expand Down