From eafaae8f73850d9d5385c906cc9fa5493cfce31e Mon Sep 17 00:00:00 2001 From: Timur Osmanov Date: Mon, 8 Apr 2024 17:03:32 +0300 Subject: [PATCH 1/2] add: strict check option --- README.md | 9 +++++++-- changelog.md | 4 ++++ foliant/preprocessors/checksources.py | 19 +++++++++++++++---- setup.py | 2 +- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index dd5511a..4c9a449 100644 --- a/README.md +++ b/README.md @@ -19,15 +19,20 @@ preprocessors: - checksources ``` -You can add a list of unmentioned files that wouldn't throw warnings by `not_in_chapters` option: +You can add a list of unmentioned files that wouldn't throw warnings by `not_in_chapters` option. +To perform a strict check, use the `strict_check` option: ```yaml preprocessors: - checksources: not_in_chapters: - tags.md + strict_check: true ``` -It is useful if you don't need to add some files to the table of contents. +The `not_in_chapters` option is useful if you don't need to add some files to the table of contents. + +If the `strict_check` option is enabled, then if a critical error is detected, the build will be aborted after applying the preprocessor. + diff --git a/changelog.md b/changelog.md index 113e802..480244b 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,7 @@ +# 1.0.1 + +- Added the `strict_check` option + # 1.0.0 - Initial release diff --git a/foliant/preprocessors/checksources.py b/foliant/preprocessors/checksources.py index 2168c6c..97d54ec 100644 --- a/foliant/preprocessors/checksources.py +++ b/foliant/preprocessors/checksources.py @@ -6,11 +6,12 @@ import os from foliant.preprocessors.utils.preprocessor_ext import BasePreprocessorExt - +from foliant.utils import output class Preprocessor(BasePreprocessorExt): defaults = { 'not_in_chapters': [], + 'strict_check': True, } def __init__(self, *args, **kwargs): @@ -20,6 +21,7 @@ def __init__(self, *args, **kwargs): self.logger.debug(f'Preprocessor inited: {self.__dict__}') self.src_dir = self.project_path / self.config['src_dir'] + self.critical_error = False def apply(self): self.logger.info('Applying preprocessor') @@ -43,7 +45,13 @@ def _recursive_process_chapters(chapters_subset): self.logger.debug(f'Adding file to the list of mentioned in chapters: {chapter_file_path}') else: self.logger.debug('Not exist, throw warning') - self._warning(f'{os.path.relpath(chapter_file_path)} does not exist') + msg = f'{os.path.relpath(chapter_file_path)} does not exist' + if self.options['strict_check']: + self.logger.error(msg) + self.critical_error = True + output(f'ERROR: {msg}') + else: + self._warning(msg) chapters_files_paths.append(chapter_file_path) @@ -82,5 +90,8 @@ def _fill_not_in_chapters(): else: self.logger.debug('Not mentioned, throw warning') self._warning(f'{os.path.relpath(markdown_file_path)} does not mentioned in chapters') - - self.logger.info('Preprocessor applied') + if self.critical_error: + self.logger.info('Critical errors have occurred') + os._exit(2) + else: + self.logger.info('Preprocessor applied') diff --git a/setup.py b/setup.py index 0700f4a..a45561c 100644 --- a/setup.py +++ b/setup.py @@ -16,7 +16,7 @@ description=SHORT_DESCRIPTION, long_description=LONG_DESCRIPTION, long_description_content_type='text/markdown', - version='1.0.0', + version='1.0.1', url='https://github.com/foliant-docs/foliantcontrib.checksources', packages=['foliant.preprocessors'], author='foliant-docs', From 50cb1a1e67d57d11d1ee8e3416f4fee0bb30056d Mon Sep 17 00:00:00 2001 From: Timur Osmanov Date: Mon, 8 Apr 2024 18:13:02 +0300 Subject: [PATCH 2/2] update --- foliant/preprocessors/checksources.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/foliant/preprocessors/checksources.py b/foliant/preprocessors/checksources.py index 97d54ec..0509916 100644 --- a/foliant/preprocessors/checksources.py +++ b/foliant/preprocessors/checksources.py @@ -8,6 +8,7 @@ from foliant.preprocessors.utils.preprocessor_ext import BasePreprocessorExt from foliant.utils import output + class Preprocessor(BasePreprocessorExt): defaults = { 'not_in_chapters': [], @@ -21,7 +22,7 @@ def __init__(self, *args, **kwargs): self.logger.debug(f'Preprocessor inited: {self.__dict__}') self.src_dir = self.project_path / self.config['src_dir'] - self.critical_error = False + self.critical_error = [] def apply(self): self.logger.info('Applying preprocessor') @@ -48,7 +49,7 @@ def _recursive_process_chapters(chapters_subset): msg = f'{os.path.relpath(chapter_file_path)} does not exist' if self.options['strict_check']: self.logger.error(msg) - self.critical_error = True + self.critical_error.append(msg) output(f'ERROR: {msg}') else: self._warning(msg) @@ -90,8 +91,10 @@ def _fill_not_in_chapters(): else: self.logger.debug('Not mentioned, throw warning') self._warning(f'{os.path.relpath(markdown_file_path)} does not mentioned in chapters') - if self.critical_error: + if len(self.critical_error) > 0: self.logger.info('Critical errors have occurred') + errors = '\n'.join(self.critical_error) + output(f'\nBuild failed: checksources preprocessor errors: \n{errors}\n') os._exit(2) else: self.logger.info('Preprocessor applied')