diff options
Diffstat (limited to 'machammer/decorators.py')
-rw-r--r-- | machammer/decorators.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/machammer/decorators.py b/machammer/decorators.py new file mode 100644 index 0000000..32ec511 --- /dev/null +++ b/machammer/decorators.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- + +import re +import os +import stat +import inspect + +import hooks + + +def login(func): + def func_wrapper(hook='login'): + path = '/var/root/Library/mh_%shook.py' % hook + + # skip the decorator and function def + s = inspect.getsource(func).split('\n')[2:] + + # determine indent level for re-indentation + indent = re.match(r'^(\s+)', s[0]).group(0) + f = open(path, 'w') + f.write('#!/usr/bin/env python\n') + + for l in s: + f.write(l.replace(indent, '') + '\n') + + f.close() + # only root should read and execute + os.chown(path, 0, 0) + os.chmod(path, stat.S_IXUSR | stat.S_IRUSR) + + if hook == 'login': + hooks.login(path) + else: + hooks.logout(path) + + return path + + return func_wrapper |