Skip to content

Commit

Permalink
Add --reset-setup flag for module setup actions
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobGM committed May 14, 2018
1 parent 173d2ad commit e61811c
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Added
that will be executed.
- Modules can now depend on other modules with the ``module`` requires keyword.
- Modules can now place action in a ``setup`` block, only to be executed once.
- You can now execute ``astrality --reset-setup module_name`` in order to
clear executed module setup actions.

Changed
-------
Expand Down
26 changes: 25 additions & 1 deletion astrality/executed_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
from pathlib import Path
import logging

from astrality import actions
from astrality.config import expand_path
Expand Down Expand Up @@ -51,7 +52,7 @@ def is_new(
action_options,
) -> bool:
"""
Return True if action config of action type has been saved earlier.
Return True if the specified action has not been executed earlier.
:param action_type: Type of action, see ActionBlock.action_types.
:param action_options: Configuration of action to be performed.
Expand Down Expand Up @@ -95,6 +96,29 @@ def write(self) -> None:
data=file_data,
)

def reset(self) -> None:
"""Delete all executed module actions."""
file_data = utils.load_yaml(path=self.path)
reset_actions = file_data.pop(self.module, None)

logger = logging.getLogger(__name__)
if not reset_actions:
logger.error(
'No saved executed on_setup actions for module '
f'"{self.module}"!',
)
else:
logger.info(
f'Reset the following actions for module "{self.module}":\n' +
utils.yaml_str({self.module: reset_actions}),
)

utils.dump_yaml(
path=self.path,
data=file_data,
)
self.old_actions = {}

@property
def path(self) -> Path:
"""Return path to file which stores executed module setup actions."""
Expand Down
32 changes: 31 additions & 1 deletion astrality/tests/test_executed_actions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Tests for astrality.executed_actions.ExecutedActions."""

import logging

from astrality.executed_actions import ExecutedActions


Expand All @@ -13,7 +15,7 @@ def test_that_executed_action_path_is_monkeypatched_in_all_files():
assert 'test' in path.parents[3].name


def test_that_actions_are_saved_to_file():
def test_that_actions_are_saved_to_file(caplog):
"""Saved actions should be persisted properly."""
action_option = {'shell': 'echo setup'}
executed_actions = ExecutedActions(module_name='github::user/repo::module')
Expand All @@ -38,3 +40,31 @@ def test_that_actions_are_saved_to_file():
action_type='run',
action_options=action_option,
)

# But when we reset the module, it should be counted as new
del executed_actions
executed_actions = ExecutedActions(module_name='github::user/repo::module')

caplog.clear()
executed_actions.reset()
assert executed_actions.is_new(
action_type='run',
action_options=action_option,
)

# Check that info is logged on success
assert caplog.record_tuples[0][1] == logging.INFO

# It should also be reset after loading from disk again
executed_actions.reset()
del executed_actions
executed_actions = ExecutedActions(module_name='github::user/repo::module')
assert executed_actions.is_new(
action_type='run',
action_options=action_option,
)

# Check that error is logged on invalid module name
caplog.clear()
ExecutedActions(module_name='i_do_not_exist').reset()
assert caplog.record_tuples[0][1] == logging.ERROR
12 changes: 11 additions & 1 deletion astrality/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,16 @@ def dump_yaml(path: Path, data: Dict) -> None:
:param path: Path to file to be created.
:param data: Data to be dumped to file.
"""
str_data = dump(data, Dumper=Dumper)
str_data = yaml_str(data)
with open(path, 'w') as yaml_file:
yaml_file.write(str_data)


def yaml_str(data: Any) -> str:
"""
Return YAML string representation of data.
:param data: Data to be converted to YAML string format.
:return: YAML string representation of python data structure.
"""
return dump(data, Dumper=Dumper)
12 changes: 12 additions & 0 deletions bin/astrality
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ sys.path.append(str(PROJECT_DIR))

from astrality.astrality import main
from astrality.config import resolve_config_directory, create_config_directory
from astrality.executed_actions import ExecutedActions

coloredlogs.install(fmt='%(asctime)s %(name)8s %(levelname)s\n%(message)s\n')
config_dir = resolve_config_directory()
Expand Down Expand Up @@ -45,6 +46,12 @@ parser.add_argument(
help='Manually define enabled module.',
action='append',
)
parser.add_argument(
'--reset-setup',
help='Reset all setup actions of module.',
action='append',
default=[],
)
parser.add_argument(
'-l',
'--logging-level',
Expand All @@ -56,6 +63,11 @@ parser.add_argument(
)
args = parser.parse_args()

if args.reset_setup:
for module_name in args.reset_setup:
ExecutedActions(module_name=module_name).reset()
sys.exit(0)

if args.create_example_config:
create_config_directory(empty=False)
elif args.create_empty_config:
Expand Down
11 changes: 6 additions & 5 deletions docs/modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,19 @@ Action blocks

When you want to assign :ref:`tasks <actions>` for Astrality to perform, you
have to define *when* to perform them. This is done by defining those
``actions`` in one of four available ``action blocks``.
``actions`` in one of five available ``action blocks``.

.. _module_events_on_startup:

``setup``:
``on_setup``:
Tasks to be performed only once and never again. Can be used for
setting up dependencies.

Executed actions are written to
``$XDG_DATA_HOME/astrality/setup.yml``, by default
``$HOME/.local/share``. Modify this file if you want to re-execute
setup actions.
``$HOME/.local/share``. Execute ``astrality --reset-setup module_name``
if you want to re-execute a module's setup actions during the next
run.

``on_startup``:
Tasks to be performed when Astrality first starts up.
Expand Down Expand Up @@ -213,7 +214,7 @@ Demonstration of module action blocks:
module_name:
...startup actions (option 1)...
setup:
on_setup:
...setup actions...
on_startup:
Expand Down

0 comments on commit e61811c

Please sign in to comment.