Skip to content

Commit

Permalink
Make shell filter use cwd=$ASTRALITY_CONFIG_HOME
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobGM committed Feb 12, 2018
1 parent 3176128 commit d5c66e7
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 10 deletions.
18 changes: 15 additions & 3 deletions astrality/compiler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Module for compilation of templates."""

from functools import partial
import logging
from pathlib import Path
from typing import Any, Dict, Union
Expand Down Expand Up @@ -53,7 +54,10 @@ def context(config: ApplicationConfig) -> Context:
return contents


def jinja_environment(templates_folder: Path) -> Environment:
def jinja_environment(
templates_folder: Path,
shell_command_working_directory: Path,
) -> Environment:
"""Return a jinja Environment instance for templates in a folder."""
logger = logging.getLogger(__name__)
LoggingUndefined = make_logging_undefined(
Expand All @@ -74,7 +78,11 @@ def jinja_environment(templates_folder: Path) -> Environment:
)

# Add run shell command filter
env.filters['shell'] = run_shell
run_shell_from_working_directory = partial(
run_shell,
working_directory=shell_command_working_directory,
)
env.filters['shell'] = run_shell_from_working_directory

return env

Expand All @@ -91,11 +99,15 @@ def compile_template(
template: Path,
target: Path,
context: Context,
shell_command_working_directory: Path,
) -> None:
"""Compile template to target destination with specific context."""
logger.info(f'[Compiling] Template: "{template}" -> Target: "{target}"')

env = jinja_environment(templates_folder=template.parent)
env = jinja_environment(
templates_folder=template.parent,
shell_command_working_directory=shell_command_working_directory,
)
jinja_template = env.get_template(name=template.name)
result = jinja_template.render(context)

Expand Down
3 changes: 2 additions & 1 deletion astrality/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,11 @@ def compile_templates(self, trigger: str) -> None:
# No module has been specified, use the module itself
module_name = module.name

compiler.compile_template(
compiler.compile_template( # type: ignore
template=self.modules[module_name].templates[template_name]['source'],
target=self.modules[module_name].templates[template_name]['target'],
context=self.application_context,
shell_command_working_directory=self.application_config['_runtime']['config_directory'],
)

def startup(self):
Expand Down
6 changes: 3 additions & 3 deletions astrality/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
def conf_path():
"""Return str path to configuration directory."""
this_test_file = os.path.abspath(__file__)
conf_path = Path(this_test_file).parents[2]
return str(conf_path)
conf_path = Path(this_test_file).parents[1] / 'config'
return conf_path


@pytest.fixture
def conf_file_path(conf_path):
"""Return path to example configuration."""
return os.path.join(conf_path, 'astrality.yaml.example')
return conf_path / 'astrality.yaml'


@pytest.fixture(scope='session', autouse=True)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ 'pwd' | shell }}
23 changes: 20 additions & 3 deletions astrality/tests/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ def test_templates_folder():

@pytest.fixture
def jinja_test_env(test_templates_folder):
return jinja_environment(test_templates_folder)
return jinja_environment(
test_templates_folder,
shell_command_working_directory=Path('~').resolve(),
)


def test_rendering_environment_variables(jinja_test_env, expanded_env_dict):
Expand All @@ -42,12 +45,11 @@ def test_integer_indexed_templates(jinja_test_env):
assert template.render(context) == 'one\ntwo\ntwo'


# @pytest.mark.skip
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)
compile_template(template, target, context, Path('/'))

with open(target) as target:
assert target.read() == 'test_value\nfallback_value\n'
Expand All @@ -72,7 +74,22 @@ def test_run_shell_template_filter(test_templates_folder):
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'

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() == '/'
26 changes: 26 additions & 0 deletions astrality/tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,3 +761,29 @@ def test_detection_of_new_period_involving_several_modules(

# Now both timers should be considered period changed
assert module_manager.has_unfinished_tasks() == True

def test_that_shell_filter_is_run_from_config_directory(conf_path):
shell_filter_template = Path(__file__).parent / 'templates' / 'shell_filter_working_directory.template'
shell_filter_template_target = Path('/tmp/astrality/shell_filter_working_directory.template')
config = {
'module/A': {
'templates': {
'shell_filter_template': {
'source': str(shell_filter_template),
'target': str(shell_filter_template_target),
},
},
'on_startup': {
'compile': ['shell_filter_template'],
},
},
'_runtime': {
'config_directory': conf_path,
'temp_directory': Path('/tmp/astrality'),
},
}
module_manager = ModuleManager(config)
module_manager.compile_templates('on_startup')

with open(shell_filter_template_target) as compiled:
assert compiled.read() == str(conf_path)

0 comments on commit d5c66e7

Please sign in to comment.