From df2048e869fe47dc36536419f716b2af6de441af Mon Sep 17 00:00:00 2001 From: Jennifer Power Date: Thu, 31 Aug 2023 18:22:31 -0400 Subject: [PATCH] refactor: updates AuthoredTypes to use repository API from trestle Signed-off-by: Jennifer Power --- trestlebot/tasks/authored/catalog.py | 34 +++++++++---------- trestlebot/tasks/authored/compdef.py | 32 +++++++++--------- trestlebot/tasks/authored/profile.py | 46 ++++++++++++------------- trestlebot/tasks/authored/ssp.py | 50 ++++++++++++---------------- 4 files changed, 77 insertions(+), 85 deletions(-) diff --git a/trestlebot/tasks/authored/catalog.py b/trestlebot/tasks/authored/catalog.py index 28ecba7a..79ac5538 100644 --- a/trestlebot/tasks/authored/catalog.py +++ b/trestlebot/tasks/authored/catalog.py @@ -20,8 +20,7 @@ import pathlib from trestle.common.err import TrestleError -from trestle.core.commands.author.catalog import CatalogAssemble, CatalogGenerate -from trestle.core.commands.common.return_codes import CmdReturnCodes +from trestle.core.repository import AgileAuthoring from trestlebot.tasks.authored.base_authored import ( AuthoredObjectException, @@ -44,17 +43,17 @@ def assemble(self, markdown_path: str, version_tag: str = "") -> None: """Run assemble actions for catalog type at the provided path""" trestle_root = pathlib.Path(self.get_trestle_root()) catalog = os.path.basename(markdown_path) + authoring = AgileAuthoring(trestle_root) try: - exit_code = CatalogAssemble.assemble_catalog( - trestle_root=trestle_root, - md_name=markdown_path, - assem_cat_name=catalog, - parent_cat_name="", - set_parameters_flag=True, + success = authoring.assemble_catalog_markdown( + name=catalog, + output=catalog, + markdown_dir=markdown_path, + set_parameters=True, regenerate=False, version=version_tag, ) - if exit_code != CmdReturnCodes.SUCCESS.value: + if not success: raise AuthoredObjectException( f"Unknown error occurred while assembling {catalog}" ) @@ -63,20 +62,19 @@ def assemble(self, markdown_path: str, version_tag: str = "") -> None: def regenerate(self, model_path: str, markdown_path: str) -> None: """Run assemble actions for catalog type at the provided path""" - trestle_root = self.get_trestle_root() - trestle_path = pathlib.Path(trestle_root) - catalog_generate: CatalogGenerate = CatalogGenerate() + trestle_root = pathlib.Path(self.get_trestle_root()) + authoring = AgileAuthoring(trestle_root) catalog = os.path.basename(model_path) try: - exit_code = catalog_generate.generate_markdown( - trestle_root=trestle_path, - catalog_path=pathlib.Path(trestle_root, model_path, "catalog.json"), - markdown_path=pathlib.Path(trestle_root, markdown_path, catalog), - yaml_header={}, + success = authoring.generate_catalog_markdown( + name=catalog, + output=os.path.join(markdown_path, catalog), + force_overwrite=False, + yaml_header="", overwrite_header_values=False, ) - if exit_code != CmdReturnCodes.SUCCESS.value: + if not success: raise AuthoredObjectException( f"Unknown error occurred while regenerating {catalog}" ) diff --git a/trestlebot/tasks/authored/compdef.py b/trestlebot/tasks/authored/compdef.py index 28c04aaa..7ba4c86d 100644 --- a/trestlebot/tasks/authored/compdef.py +++ b/trestlebot/tasks/authored/compdef.py @@ -29,10 +29,9 @@ from trestle.common.load_validate import load_validate_model_name from trestle.common.model_utils import ModelUtils from trestle.core.catalog.catalog_interface import CatalogInterface -from trestle.core.commands.author.component import ComponentAssemble, ComponentGenerate -from trestle.core.commands.common.return_codes import CmdReturnCodes from trestle.core.models.file_content_type import FileContentType from trestle.core.profile_resolver import ProfileResolver +from trestle.core.repository import AgileAuthoring from trestlebot.tasks.authored.base_authored import ( AuthoredObjectException, @@ -55,16 +54,17 @@ def assemble(self, markdown_path: str, version_tag: str = "") -> None: """Run assemble actions for compdef type at the provided path""" trestle_root = pathlib.Path(self.get_trestle_root()) compdef = os.path.basename(markdown_path) + + authoring = AgileAuthoring(trestle_root) try: - exit_code = ComponentAssemble.assemble_component( - trestle_root=trestle_root, - md_name=markdown_path, - assem_comp_name=compdef, - parent_comp_name="", + success = authoring.assemble_component_definition_markdown( + name=compdef, + output=compdef, + markdown_dir=os.path.join(markdown_path, compdef), regenerate=False, version=version_tag, ) - if exit_code != CmdReturnCodes.SUCCESS.value: + if not success: raise AuthoredObjectException( f"Unknown error occurred while assembling {compdef}" ) @@ -73,18 +73,18 @@ def assemble(self, markdown_path: str, version_tag: str = "") -> None: def regenerate(self, model_path: str, markdown_path: str) -> None: """Run assemble actions for compdef type at the provided path""" - trestle_root = self.get_trestle_root() - trestle_path = pathlib.Path(trestle_root) - comp_generate: ComponentGenerate = ComponentGenerate() + trestle_root = pathlib.Path(self.get_trestle_root()) + authoring = AgileAuthoring(trestle_root) comp_name = os.path.basename(model_path) + try: - exit_code = comp_generate.component_generate_all( - trestle_root=trestle_path, - comp_def_name=comp_name, - markdown_dir_name=os.path.join(trestle_root, markdown_path, comp_name), + success = authoring.generate_component_definition_markdown( + name=comp_name, + output=markdown_path, + force_overwrite=False, ) - if exit_code != CmdReturnCodes.SUCCESS.value: + if not success: raise AuthoredObjectException( f"Unknown error occurred while regenerating {comp_name}" ) diff --git a/trestlebot/tasks/authored/profile.py b/trestlebot/tasks/authored/profile.py index 0bda8e64..f370f07e 100644 --- a/trestlebot/tasks/authored/profile.py +++ b/trestlebot/tasks/authored/profile.py @@ -27,9 +27,8 @@ from trestle.common.err import TrestleError, TrestleNotFoundError from trestle.common.load_validate import load_validate_model_name from trestle.common.model_utils import ModelUtils -from trestle.core.commands.author.profile import ProfileAssemble, ProfileGenerate -from trestle.core.commands.common.return_codes import CmdReturnCodes from trestle.core.models.file_content_type import FileContentType +from trestle.core.repository import AgileAuthoring from trestle.oscal.common import IncludeAll from trestlebot.tasks.authored.base_authored import ( @@ -52,21 +51,23 @@ def __init__(self, trestle_root: str) -> None: def assemble(self, markdown_path: str, version_tag: str = "") -> None: """Run assemble actions for profile type at the provided path""" trestle_root = pathlib.Path(self.get_trestle_root()) + authoring = AgileAuthoring(trestle_root) + profile = os.path.basename(markdown_path) + try: - exit_code = ProfileAssemble.assemble_profile( - trestle_root=trestle_root, - md_name=markdown_path, - assem_prof_name=profile, - parent_prof_name="", - set_parameters_flag=True, + success = authoring.assemble_profile_markdown( + name=profile, + output=profile, + markdown_dir=markdown_path, + set_parameters=True, regenerate=False, version=version_tag, - allowed_sections=None, - required_sections=[], - sections_dict={}, + sections="", + required_sections="", + allowed_sections="", ) - if exit_code != CmdReturnCodes.SUCCESS.value: + if not success: raise AuthoredObjectException( f"Unknown error occurred while assembling {profile}" ) @@ -75,22 +76,21 @@ def assemble(self, markdown_path: str, version_tag: str = "") -> None: def regenerate(self, model_path: str, markdown_path: str) -> None: """Run assemble actions for profile type at the provided path""" - trestle_root = self.get_trestle_root() - trestle_path = pathlib.Path(trestle_root) - profile_generate: ProfileGenerate = ProfileGenerate() + trestle_root = pathlib.Path(self.get_trestle_root()) + authoring = AgileAuthoring(trestle_root) profile = os.path.basename(model_path) try: - exit_code = profile_generate.generate_markdown( - trestle_root=trestle_path, - profile_path=pathlib.Path(trestle_root, model_path, "profile.json"), - markdown_path=pathlib.Path(trestle_root, markdown_path, profile), - yaml_header={}, + success = authoring.generate_profile_markdown( + name=profile, + output=os.path.join(markdown_path, profile), + force_overwrite=False, + yaml_header="", overwrite_header_values=False, - sections_dict={}, - required_sections=[], + sections="", + required_sections="", ) - if exit_code != CmdReturnCodes.SUCCESS.value: + if not success: raise AuthoredObjectException( f"Unknown error occurred while regenerating {profile}" ) diff --git a/trestlebot/tasks/authored/ssp.py b/trestlebot/tasks/authored/ssp.py index d86bae3d..c36de38b 100644 --- a/trestlebot/tasks/authored/ssp.py +++ b/trestlebot/tasks/authored/ssp.py @@ -16,7 +16,6 @@ """Trestle Bot functions for SSP authoring""" -import argparse import json import logging import os @@ -24,8 +23,9 @@ from typing import Any, Dict, List, Optional from trestle.common.err import TrestleError -from trestle.core.commands.author.ssp import SSPAssemble, SSPFilter, SSPGenerate +from trestle.core.commands.author.ssp import SSPFilter from trestle.core.commands.common.return_codes import CmdReturnCodes +from trestle.core.repository import AgileAuthoring from trestlebot.const import COMPDEF_KEY_NAME, LEVERAGED_SSP_KEY_NAME, PROFILE_KEY_NAME from trestlebot.tasks.authored.base_authored import ( @@ -137,11 +137,6 @@ def write_out(self) -> None: json.dump(data, file, indent=4) -# TODO: Move away from using private run to a public function. -# Done initially because a lot of required high level logic for SSP is private. -# See - https://github.com/IBM/compliance-trestle/pull/1432 - - class AuthoredSSP(AuthorObjectBase): """ Class for authoring OSCAL SSPs in automation @@ -156,26 +151,24 @@ def __init__(self, trestle_root: str, ssp_index: SSPIndex) -> None: def assemble(self, markdown_path: str, version_tag: str = "") -> None: """Run assemble actions for ssp type at the provided path""" - ssp_assemble: SSPAssemble = SSPAssemble() ssp = os.path.basename(markdown_path) comps = self.ssp_index.get_comps_by_ssp(ssp) component_str = ",".join(comps) + trestle_root = pathlib.Path(self.get_trestle_root()) + authoring = AgileAuthoring(trestle_root) + try: - args = argparse.Namespace( - trestle_root=self.get_trestle_root(), - markdown=markdown_path, + success = authoring.assemble_ssp_markdown( + name=ssp, output=ssp, - verbose=0, + markdown_dir=markdown_path, regenerate=False, version=version_tag, - name=None, compdefs=component_str, ) - - exit_code = ssp_assemble._run(args) - if exit_code != CmdReturnCodes.SUCCESS.value: + if not success: raise AuthoredObjectException( f"Unknown error occurred while assembling {ssp}" ) @@ -184,30 +177,31 @@ def assemble(self, markdown_path: str, version_tag: str = "") -> None: def regenerate(self, model_path: str, markdown_path: str) -> None: """Run regenerate actions for ssp type at the provided path""" - trestle_root = self.get_trestle_root() - trestle_path = pathlib.Path(trestle_root) - ssp_generate: SSPGenerate = SSPGenerate() ssp = os.path.basename(model_path) comps = self.ssp_index.get_comps_by_ssp(ssp) + component_str = ",".join(comps) + profile = self.ssp_index.get_profile_by_ssp(ssp) leveraged_ssp = self.ssp_index.get_leveraged_by_ssp(ssp) if leveraged_ssp is None: leveraged_ssp = "" + trestle_root = pathlib.Path(self.get_trestle_root()) + authoring = AgileAuthoring(trestle_root) + try: - exit_code = ssp_generate._generate_ssp_markdown( - trestle_root=trestle_path, - profile_name_or_href=profile, - compdef_name_list=comps, - md_path=pathlib.Path(trestle_root, markdown_path, ssp), - yaml_header={}, - overwrite_header_values=False, + success = authoring.generate_ssp_markdown( + output=os.path.join(markdown_path, ssp), force_overwrite=False, - leveraged_ssp_name_or_href=leveraged_ssp, + yaml_header="", + overwrite_header_values=False, + compdefs=component_str, + profile=profile, + leveraged_ssp=leveraged_ssp, ) - if exit_code != CmdReturnCodes.SUCCESS.value: + if not success: raise AuthoredObjectException( f"Unknown error occurred while regenerating {ssp}" )