コード例 #1
0
def test_compilation_of_jinja_template(test_templates_folder):
    template = test_templates_folder / 'env_vars'
    target = Path('/tmp/astrality') / template.name
    compile_template(template, target, {}, Path('/'))

    with open(target) as target:
        assert target.read() == 'test_value\nfallback_value\n'
コード例 #2
0
 def compile_template(self, template: Path, target: Path) -> None:
     compiler.compile_template(
         template=template,
         target=target,
         context=self.context_store,
         shell_command_working_directory=self.directory,
         permissions=self.option(key='permissions'),
     )
コード例 #3
0
    def execute(self, dry_run: bool = False) -> Dict[Path, Path]:
        """
        Compile template source to target destination.

        :param dry_run: If True, skip and log compilation(s).
        :return: Dictionary with template content keys and target path values.
        """
        if self.null_object:
            # Null objects do nothing
            return {}
        elif 'target' not in self._options:
            # If no target is specified, we create a deterministic target.
            template = self.option(key='content', path=True)
            target = self.create_compilation_target(template=template)
            self._options['target'] = str(target)

        # These might either be file paths or directory paths
        template_source = self.option(key='content', path=True)
        target_source = self.option(key='target', path=True)
        if not template_source.exists():
            logger = logging.getLogger(__name__)
            logger.error(
                f'Could not compile template "{template_source}" '
                f'to target "{target_source}". No such path!', )
            return {}

        compile_pairs = utils.resolve_targets(
            content=template_source,
            target=target_source,
            include=self.option(key='include', default=r'(.+)'),
        )
        permissions = self.option(key='permissions')

        for content_file, target_file in compile_pairs.items():
            if dry_run:
                logger = logging.getLogger(__name__)
                logger.info(
                    f'SKIPPED: '
                    f'[Compiling] Template: "{content_file}" '
                    f'-> Target: "{target_file}"', )
            else:
                self.creation_store.backup(path=target_file)
                compiler.compile_template(
                    template=content_file,
                    target=target_file,
                    context=self.context_store,
                    shell_command_working_directory=self.directory,
                    permissions=permissions,
                )
                self.creation_store.insert_creation(
                    content=content_file,
                    target=target_file,
                    method=persistence.CreationMethod.COMPILE,
                )

            self._performed_compilations[content_file].add(target_file)

        return compile_pairs
コード例 #4
0
def test_compilation_of_jinja_template(test_templates_folder,
                                       expanded_env_dict):
    template = test_templates_folder / 'env_vars'
    target = Path('/tmp/astrality') / template.name
    context = {'env': expanded_env_dict}
    compile_template(template, target, context, Path('/'))

    with open(target) as target:
        assert target.read() == 'test_value\nfallback_value\n'
コード例 #5
0
def test_working_directory_of_shell_command_filter(test_templates_folder):
    shell_template_path = test_templates_folder / 'shell_filter_working_directory.template'
    compiled_shell_template_path = Path(
        '/tmp/astrality') / shell_template_path.name
    context = {}
    compile_template(
        template=shell_template_path,
        target=compiled_shell_template_path,
        context=context,
        shell_command_working_directory=Path('/'),
    )

    with open(compiled_shell_template_path) as target:
        assert target.read() == '/'
コード例 #6
0
def test_writing_template_file_with_specific_octal_permissions(tmpdir):
    tmpdir = Path(tmpdir)
    template = tmpdir / 'template'
    template.write_text('content')
    target = tmpdir / 'target'

    permissions = '514'
    compile_template(
        template=template,
        target=target,
        context={},
        shell_command_working_directory=tmpdir,
        permissions=permissions,
    )
    assert (target.stat().st_mode & 0o777) == 0o514
コード例 #7
0
def test_writing_template_file_with_default_permissions(tmpdir):
    tmpdir = Path(tmpdir)
    template = tmpdir / 'template'
    template.write_text('content')
    permission = 0o751
    template.chmod(permission)
    target = tmpdir / 'target'

    compile_template(
        template=template,
        target=target,
        context={},
        shell_command_working_directory=tmpdir,
    )
    assert (target.stat().st_mode & 0o777) == permission
コード例 #8
0
def test_run_shell_template_filter(test_templates_folder):
    shell_template_path = test_templates_folder / 'shell_filter.template'
    compiled_shell_template_path = Path(
        '/tmp/astrality') / shell_template_path.name
    compiled_shell_template_path.touch()

    context = {}
    compile_template(
        template=shell_template_path,
        target=compiled_shell_template_path,
        context=context,
        shell_command_working_directory=Path('/'),
    )

    with open(compiled_shell_template_path) as target:
        assert target.read(
        ) == 'quick\nanother_quick\nslow_but_allowed\n\nfallback'

    if compiled_shell_template_path.is_file():
        os.remove(compiled_shell_template_path)