Beispiel #1
0
    def make(self,
             target,
             backend='',
             project_path=Path('.'),
             config_file_name='foliant.yml',
             quiet=False,
             keep_tmp=False,
             debug=False):
        '''Make TARGET with BACKEND.'''

        # pylint: disable=too-many-arguments

        self.logger.setLevel(DEBUG if debug else WARNING)

        self.logger.info('Build started.')

        available_backends = get_available_backends()

        try:
            if backend:
                self.validate_backend(backend, target)
            else:
                backend = self.get_matching_backend(target, available_backends)

            config = self.get_config(project_path, config_file_name, quiet,
                                     debug)

        except (BackendError, ConfigError) as exception:
            self.logger.critical(str(exception))
            exit(str(exception))

        context = {
            'project_path': project_path,
            'config': config,
            'target': target,
            'backend': backend
        }

        backend_module = import_module(f'foliant.backends.{backend}')
        self.logger.debug(f'Imported backend {backend_module}.')

        with tmp(project_path / config['tmp_dir'], keep_tmp):
            result = backend_module.Backend(context, self.logger, quiet,
                                            debug).preprocess_and_make(target)

        if result:
            self.logger.info(f'Result: {result}')

            if not quiet:
                print('─' * 20)
                print(f'Result: {result}')
            else:
                print(result)

        else:
            self.logger.critical('No result returned by backend')
            exit('No result returned by backend')
            return None

        return result
Beispiel #2
0
    def validate_backend(backend: str, target: str) -> bool:
        '''Check that the specified backend exists and can build the specified target.'''

        available_backends = get_available_backends()

        if backend not in available_backends:
            raise BackendError(
                f'Backend {backend} not found. ' +
                f'Available backends are {", ".join(available_backends.keys())}.'
            )

        if target not in available_backends[backend]:
            raise BackendError(f'Backend {backend} cannot make {target}.')

        return True
Beispiel #3
0
    def validate_backend(backend: str, target: str) -> bool:
        '''Check that the specified backend exists and can build the specified target.'''

        available_backends = get_available_backends()

        if backend not in available_backends:
            raise BackendError(
                f'Backend {backend} not found. '
                + f'Available backends are {", ".join(available_backends.keys())}.'
            )

        if target not in available_backends[backend]:
            raise BackendError(f'Backend {backend} cannot make {target}.')

        return True
Beispiel #4
0
def test_get_available_backends():
    '''Check that vanilla Foliant ships only with `pre` backend, which makes `pre` target.'''

    assert utils.get_available_backends() == {'pre': ('pre',)}
Beispiel #5
0
def test_get_available_backends():
    '''Check that vanilla Foliant ships only with `pre` backend, which makes `pre` target.'''

    assert utils.get_available_backends() == {'pre': ('pre', )}
Beispiel #6
0
    def make(
            self,
            target,
            backend='',
            project_path=Path('.'),
            config_file_name='foliant.yml',
            quiet=False,
            keep_tmp=False,
            debug=False
        ):
        '''Make TARGET with BACKEND.'''

        # pylint: disable=too-many-arguments

        self.logger.setLevel(DEBUG if debug else WARNING)

        self.logger.info('Build started.')

        available_backends = get_available_backends()

        try:
            if backend:
                self.validate_backend(backend, target)
            else:
                backend = self.get_matching_backend(target, available_backends)

            config = self.get_config(project_path, config_file_name, quiet, debug)

        except (BackendError, ConfigError) as exception:
            self.logger.critical(str(exception))
            exit(str(exception))

        context = {
            'project_path': project_path,
            'config': config,
            'target': target,
            'backend': backend
        }

        backend_module = import_module(f'foliant.backends.{backend}')
        self.logger.debug(f'Imported backend {backend_module}.')

        with tmp(project_path/config['tmp_dir'], keep_tmp):
            result = backend_module.Backend(
                context,
                self.logger,
                quiet,
                debug
            ).preprocess_and_make(target)

        if result:
            self.logger.info(f'Result: {result}')

            if not quiet:
                print('─' * 20)
                print(f'Result: {result}')
            else:
                print(result)

        else:
            self.logger.critical('No result returned by backend')
            exit('No result returned by backend')
            return None

        return result