def execute(self) -> None: """Execute the action.""" if not self._sub_path.exists(): raise FileNotFoundError(f'Path "{self._sub_path}" does not exist') trash.store(self._sub_path, True) self._mark_executed()
def _run(self, args: argparse.Namespace) -> int: """Split an OSCAL file into elements.""" logger.debug('Entering trestle split.') log.set_log_level_from_args(args) # get the Model args_raw = args.__dict__ if args_raw[const.ARG_FILE] is None: logger.error(f'Argument "-{const.ARG_FILE_SHORT}" is required') return 1 file_path = pathlib.Path(args_raw[const.ARG_FILE]) if not file_path.exists(): logger.error(f'File {file_path} does not exist.') return 1 content_type = FileContentType.to_content_type(file_path.suffix) # find the base directory of the file file_absolute_path = pathlib.Path(file_path.absolute()) base_dir = file_absolute_path.parent model_type, _ = fs.get_stripped_contextual_model(file_absolute_path) # FIXME: Handle list/dicts model: OscalBaseModel = model_type.oscal_read(file_path) element_paths: List[ElementPath] = cmd_utils.parse_element_args( args_raw[const.ARG_ELEMENT].split(',')) split_plan = self.split_model(model, element_paths, base_dir, content_type, root_file_name=args_raw[const.ARG_FILE]) # Simulate the plan # if it fails, it would throw errors and get out of this command split_plan.simulate() # If we are here then simulation passed # so move the original file to the trash trash.store(file_path, True) # execute the plan split_plan.execute() return 0
def test_trash_recover(tmp_path) -> None: """Test recover trashed file or directory.""" test_utils.ensure_trestle_config_dir(tmp_path) data_dir: pathlib.Path = tmp_path / 'data' data_dir.mkdir(exist_ok=True, parents=True) readme_file: pathlib.Path = data_dir / 'readme.md' readme_file.touch() trash.store(readme_file, True) assert data_dir.exists() assert readme_file.exists() is False trash.recover(readme_file) assert data_dir.exists() assert readme_file.exists() trash.store(data_dir, True) assert data_dir.exists() is False assert readme_file.exists() is False trash.recover(data_dir) assert data_dir.exists() assert readme_file.exists()
def test_trash_store(tmp_path: pathlib.Path) -> None: """Test trash store function.""" test_utils.ensure_trestle_config_dir(tmp_path) data_dir: pathlib.Path = tmp_path / 'data' data_dir.mkdir(exist_ok=True, parents=True) readme_file: pathlib.Path = data_dir / 'readme.md' readme_file.touch() # trash using common trash method trash.store(readme_file, True) assert readme_file.exists() is False assert data_dir.exists() assert trash.to_trash_dir_path(data_dir).exists() assert trash.to_trash_file_path(readme_file).exists() # trash whole directory data_dir.mkdir(exist_ok=True, parents=True) readme_file.touch() trash.store(data_dir, True) assert data_dir.exists() is False assert readme_file.exists() is False assert trash.to_trash_dir_path(data_dir).exists() assert trash.to_trash_file_path(readme_file).exists()
def test_trash_recover(tmp_dir): """Test recover trashed file or directory.""" test_utils.ensure_trestle_config_dir(tmp_dir) data_dir: pathlib.Path = tmp_dir / 'data' fs.ensure_directory(data_dir) readme_file: pathlib.Path = data_dir / 'readme.md' readme_file.touch() trash.store(readme_file, True) assert data_dir.exists() assert readme_file.exists() is False trash.recover(readme_file) assert data_dir.exists() assert readme_file.exists() trash.store(data_dir, True) assert data_dir.exists() is False assert readme_file.exists() is False trash.recover(data_dir) assert data_dir.exists() assert readme_file.exists()