-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce package command (#846)
* feat: introduce package command * test: fixing unit tests
- Loading branch information
Showing
8 changed files
with
202 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# | ||
# Copyright 2023 Splunk Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
import logging | ||
import os | ||
import tarfile | ||
from typing import Optional | ||
|
||
from splunk_add_on_ucc_framework import app_manifest as app_manifest_lib | ||
|
||
logger = logging.getLogger("ucc_gen") | ||
|
||
|
||
def _get_package_output_path(output_directory: Optional[str] = None) -> str: | ||
if output_directory is None: | ||
return os.path.join(os.getcwd()) | ||
else: | ||
if not os.path.isabs(output_directory): | ||
return os.path.join(os.getcwd(), output_directory) | ||
return output_directory | ||
|
||
|
||
def package(path_to_built_addon: str, output_directory: Optional[str] = None) -> None: | ||
""" | ||
Archives a built add-on to the current directory with a specific add-on name | ||
and version. | ||
""" | ||
logger.info(f"Packaging app at {path_to_built_addon}") | ||
app_manifest = app_manifest_lib.AppManifest() | ||
app_manifest_path = os.path.join( | ||
path_to_built_addon, app_manifest_lib.APP_MANIFEST_FILE_NAME | ||
) | ||
if not os.path.exists(app_manifest_path): | ||
logger.error( | ||
f"No {app_manifest_lib.APP_MANIFEST_FILE_NAME} found @ {app_manifest_path}. " | ||
f"Cannot package an add-on without a manifest file. " | ||
f"Please check the --path provided." | ||
) | ||
exit(1) | ||
with open(app_manifest_path) as _f: | ||
app_manifest.read(_f.read()) | ||
|
||
addon_name = app_manifest.get_addon_name() | ||
addon_version = app_manifest.get_addon_version() | ||
output_directory = _get_package_output_path(output_directory) | ||
archive_path = os.path.join( | ||
output_directory, f"{addon_name}-{addon_version}.tar.gz" | ||
) | ||
with tarfile.open(archive_path, mode="w", encoding="utf-8") as archive_file: | ||
logger.info(path_to_built_addon) | ||
archive_file.add(path_to_built_addon, arcname=addon_name) | ||
logger.info(f"Package exported to {archive_path}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import os | ||
import tempfile | ||
|
||
from splunk_add_on_ucc_framework.commands import build | ||
from splunk_add_on_ucc_framework.commands import init | ||
from splunk_add_on_ucc_framework.commands import package | ||
|
||
|
||
def test_ucc_package(): | ||
""" | ||
Checks the complete from initializing the add-on, building it and then packaging it. | ||
""" | ||
addon_name = "init_addon_for_ucc_package" | ||
generated_addon_path = init.init( | ||
addon_name, | ||
"Demo Add-on for Splunk", | ||
"demo_input", | ||
"1.0.0", | ||
overwrite=True, | ||
) | ||
with tempfile.TemporaryDirectory() as temp_dir_for_build: | ||
build.generate( | ||
os.path.join(generated_addon_path, "package"), | ||
os.path.join(generated_addon_path, "globalConfig.json"), | ||
"1.0.0", | ||
output_directory=temp_dir_for_build, | ||
) | ||
|
||
path_to_built_addon = os.path.join( | ||
temp_dir_for_build, | ||
addon_name, | ||
) | ||
with tempfile.TemporaryDirectory() as temp_dir_for_package: | ||
package.package(path_to_built_addon, output_directory=temp_dir_for_package) | ||
|
||
found_files = os.listdir(temp_dir_for_package) | ||
if "init_addon_for_ucc_package-1.0.0.tar.gz" not in found_files: | ||
assert False, "No archive found where it should be" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters