File: hook.py

package info (click to toggle)
pyinfra 0.2.2%2Bgit20161227.ec708ef-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 11,804 kB
  • ctags: 677
  • sloc: python: 5,944; sh: 71; makefile: 11
file content (41 lines) | stat: -rw-r--r-- 881 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# pyinfra
# File: pyinfra/hook.py
# Desc: Python hooks for CLI deploys

from functools import wraps

from . import pseudo_state

HOOKS = {
    'before_connect': [],
    'before_facts': [],
    'before_deploy': [],
    'after_deploy': [],
}


class Error(Exception):
    '''
    Exception raised when encounting errors in deploy hooks.
    '''


def _make_hook_wrapper(hook_name):
    def hook_func(func):
        # Only add hooks when the state isn't active
        if pseudo_state.active:
            return

        HOOKS[hook_name].append(func)

        @wraps(func)
        def decorated(*args, **kwargs):
            return func(*args, **kwargs)

    return hook_func


before_connect = _make_hook_wrapper('before_connect')
before_facts = _make_hook_wrapper('before_facts')
before_deploy = _make_hook_wrapper('before_deploy')
after_deploy = _make_hook_wrapper('after_deploy')