Skip to content


trestle.core.commands.init ¤

Trestle Init Command.

logger ¤


InitCmd (CommandBase) ¤

Initialize a trestle working directory.

Source code in trestle/core/commands/
class InitCmd(CommandBase):
    """Initialize a trestle working directory."""

    name = 'init'

    def _init_arguments(self) -> None:

        self.add_argument(const.INIT_FULL_SHORT, const.INIT_FULL_LONG, help=const.INIT_FULL_HELP, action='store_true')
            const.INIT_LOCAL_SHORT, const.INIT_LOCAL_LONG, help=const.INIT_LOCAL_HELP, action='store_true'
            const.INIT_GOVDOCS_SHORT, const.INIT_GOVDOCS_LONG, help=const.INIT_GOVDOCS_HELP, action='store_true'

    def _run(self, args: argparse.Namespace) -> int:
        """Create a trestle project in the current directory."""
            dir_path: pathlib.Path = args.trestle_root
            if not dir_path.exists() or not dir_path.is_dir():
                raise TrestleRootError(
                    f'Initialization failed. Given directory {dir_path} does not exist or is not a directory.'

            if not (args.full or args.local or args.govdocs):
                # Running in full mode by default
                args.full = True

            init_dist_folder = True if args.full else False
            init_oscal_folders = True if args.local or args.full else False
            copy_config_file = True if args.full or args.local else False

            self._create_directories(dir_path, init_oscal_folders, init_dist_folder)

            if copy_config_file:

  'Initialized trestle project successfully in {dir_path}')

            return CmdReturnCodes.SUCCESS.value

        except Exception as e:  # pragma: no cover
            return handle_generic_command_exception(e, logger, 'Failed to initialize Trestle working directory.')

    def _create_directories(self, root: pathlib.Path, create_model_folders: bool, create_dist_folder: bool) -> None:
        """Create the directory tree if it does not exist."""
        # Prepare directory list to be created
            directory_list = [root / pathlib.Path(const.TRESTLE_CONFIG_DIR)]

            if create_model_folders:
                for model_dir in const.MODEL_DIR_LIST:
                    directory_list.append(root / pathlib.Path(model_dir))
                    if create_dist_folder:
                        directory_list.append(root / pathlib.Path(const.TRESTLE_DIST_DIR) / model_dir)

            # Create directories
            for directory in directory_list:
                directory.mkdir(parents=True, exist_ok=True)
                file_path = pathlib.Path(directory) / const.TRESTLE_KEEP_FILE
        except OSError as e:
            raise TrestleError(f'Error while creating directories: {e}')
        except Exception as e:
            raise TrestleError(f'Unexpected error while creating directories: {e}')

    def _copy_config_file(self, root: pathlib.Path) -> None:
        """Copy the initial config.ini file to .trestle directory."""
            source_path = pathlib.Path(resource_filename('trestle.resources', const.TRESTLE_CONFIG_FILE)).resolve()
            destination_path = (root / pathlib.Path(const.TRESTLE_CONFIG_DIR) / const.TRESTLE_CONFIG_FILE).resolve()
            copyfile(source_path, destination_path)

        except (shutil.SameFileError, OSError) as e:
            raise TrestleError(f'Error while copying config file: {e}')
        except Exception as e:
            raise TrestleError(f'Unexpected error while copying config file: {e}')
name ¤

handler: python