bolt-ta 0.4.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
bolt/__init__.py ADDED
@@ -0,0 +1,38 @@
1
+ """
2
+ This is the main module that exposes the required functions to use bolt in your applications.
3
+ """
4
+ import bolt._btapp as btapp
5
+ import bolt._btoptions as btoptions
6
+ import bolt.utils.log as btlog
7
+
8
+
9
+ def register_module_tasks(module):
10
+ """ """
11
+ app = btapp.get_application()
12
+ app.registry.register_module_tasks(module)
13
+
14
+
15
+ def register_task(name, task):
16
+ """ """
17
+ app = btapp.get_application()
18
+ app.registry.register_task(name, task)
19
+
20
+
21
+ def run_task(task_name, continue_on_error=None):
22
+ continue_on_error = continue_on_error or False
23
+ app = btapp.get_application()
24
+ app.run_task(task_name, continue_on_error)
25
+
26
+
27
+ def run():
28
+ """
29
+ Entry point for the `bolt` executable.
30
+ """
31
+ options = btoptions.Options()
32
+ btlog.initialize_logging(options.log_level, options.log_file)
33
+ app = btapp.get_application()
34
+ app.run()
35
+
36
+
37
+ if __name__ == "__main__":
38
+ run()
bolt/_btapp.py ADDED
@@ -0,0 +1,114 @@
1
+ """
2
+ """
3
+ import os.path
4
+
5
+ import bolt._btconfig as btconfig
6
+ import bolt._btoptions as btoptions
7
+ import bolt._btregistry as btregistry
8
+ import bolt._btrunner as btrunner
9
+ from bolt._btutils import load_script
10
+
11
+ # Standard task modules.
12
+ import bolt.tasks.bolt_conttest as bolt_conttest
13
+ import bolt.tasks.bolt_coverage as bolt_coverage
14
+ import bolt.tasks.bolt_delete_files as bolt_delete_files
15
+ import bolt.tasks.bolt_mkdir as bolt_mkdir
16
+ import bolt.tasks.bolt_nose as bolt_nose
17
+ import bolt.tasks.bolt_pip as bolt_pip
18
+ import bolt.tasks.bolt_set_vars as bolt_set_vars
19
+ import bolt.tasks.bolt_setup as bolt_setup
20
+ import bolt.tasks.bolt_shell as bolt_shell
21
+ import bolt.tasks.bolt_sleep as bolt_sleep
22
+
23
+
24
+ class BoltApplication:
25
+ """ """
26
+
27
+ def __init__(self, options=None, registry=None, config_manager=None):
28
+ self._options = options or self._get_default_options()
29
+ self._registry = registry or self._get_default_registry()
30
+ self._config_manager = config_manager
31
+ self._register_standard_tasks()
32
+
33
+ @property
34
+ def config_manager(self):
35
+ """ """
36
+ return self._config_manager
37
+
38
+ @property
39
+ def options(self):
40
+ """ """
41
+ return self._options
42
+
43
+ @property
44
+ def registry(self):
45
+ """ """
46
+ return self._registry
47
+
48
+ def run(self):
49
+ """ """
50
+ self.run_task(self.options.task)
51
+
52
+ def run_task(self, task_name, continue_on_error=None):
53
+ """ """
54
+ continue_on_error = continue_on_error or self.options.continue_on_error
55
+ if not self._config_manager:
56
+ self._config_manager = self._get_default_config_manager()
57
+ runner = btrunner.TaskRunner(
58
+ self.config_manager, self.registry, continue_on_error
59
+ )
60
+ try:
61
+ runner.run(task_name)
62
+ finally:
63
+ runner.tear_down()
64
+
65
+ def _get_default_options(self):
66
+ return btoptions.Options()
67
+
68
+ def _get_default_registry(self):
69
+ return btregistry.TaskRegistry()
70
+
71
+ def _get_default_config_manager(self):
72
+ bf = BoltFile(self.options.bolt_file)
73
+ return btconfig.ConfigurationManager(bf.config)
74
+
75
+ def _register_standard_tasks(self):
76
+ self.registry.register_module_tasks(bolt_conttest)
77
+ self.registry.register_module_tasks(bolt_coverage)
78
+ self.registry.register_module_tasks(bolt_delete_files)
79
+ self.registry.register_module_tasks(bolt_mkdir)
80
+ self.registry.register_module_tasks(bolt_nose)
81
+ self.registry.register_module_tasks(bolt_pip)
82
+ self.registry.register_module_tasks(bolt_set_vars)
83
+ self.registry.register_module_tasks(bolt_setup)
84
+ self.registry.register_module_tasks(bolt_shell)
85
+ self.registry.register_module_tasks(bolt_sleep)
86
+
87
+ def _register_module(self, module):
88
+ module.register_tasks(self.registry)
89
+
90
+
91
+ class BoltFile:
92
+ """ """
93
+
94
+ def __init__(self, bolt_file_name):
95
+ self.filename = os.path.abspath(bolt_file_name)
96
+ self._bolt_file_module = self._load()
97
+
98
+ def _load(self):
99
+ return load_script(self.filename)
100
+
101
+ @property
102
+ def config(self):
103
+ """ """
104
+ return self._bolt_file_module.config
105
+
106
+
107
+ _application_instance = None
108
+
109
+
110
+ def get_application(options=None):
111
+ global _application_instance
112
+ if not _application_instance:
113
+ _application_instance = BoltApplication(options=options)
114
+ return _application_instance
bolt/_btconfig.py ADDED
@@ -0,0 +1,28 @@
1
+ """
2
+
3
+ """
4
+
5
+
6
+ class ConfigurationManager:
7
+ def __init__(self, full_config):
8
+ self._full_config = full_config
9
+
10
+ def get(self, task_key):
11
+ self.result_config = {}
12
+ self.current_config = self._full_config.copy()
13
+ [self._add_task_options(task_option) for task_option in task_key.split(".")]
14
+ return self.result_config
15
+
16
+ def _add_task_options(self, task_option):
17
+ task_config = self.current_config.get(task_option)
18
+ if task_config:
19
+ self._merge_into_result(task_config)
20
+ self._remove_from_result(task_option)
21
+ self.current_config = task_config
22
+
23
+ def _merge_into_result(self, task_config):
24
+ self.result_config.update(task_config)
25
+
26
+ def _remove_from_result(self, task_option):
27
+ if task_option in self.result_config:
28
+ del self.result_config[task_option]
bolt/_btoptions.py ADDED
@@ -0,0 +1,166 @@
1
+ """
2
+ Exposes classes related to the supported options for Bolt.
3
+ """
4
+ import argparse
5
+ import logging
6
+
7
+
8
+ class Commands:
9
+ """
10
+ Defines the supported commands.
11
+ """
12
+
13
+ EXECUTE = "execute"
14
+ HELP = "help"
15
+ VERSION = "version"
16
+
17
+
18
+ class Default:
19
+ """
20
+ Defines default values for options.
21
+ """
22
+
23
+ COMMAND = Commands.EXECUTE
24
+ TASK = "default"
25
+ BOLTFILE = "boltfile.py"
26
+ LOG_LEVEL = logging.INFO
27
+ LOG_FILE = None
28
+ CONTINUE_ON_ERROR = False
29
+
30
+
31
+ class OptionSwitch:
32
+ """
33
+ Defines values for the command line switches.
34
+ """
35
+
36
+ TASK = "task"
37
+ BOLTFILE_SHORT = "-b"
38
+ BOLTFILE_LONG = "--bolt-file"
39
+ LOG_LEVEL_SHORT = "-v"
40
+ LOG_LEVEL_LONG = "--log-level"
41
+ LOG_FILE_SHORT = "-o"
42
+ LOG_FILE_LONG = "--log-file"
43
+ CONTINUE_ON_ERROR_LONG = "--continue-on-error"
44
+ VERSION_LONG = "--version"
45
+
46
+
47
+ class Options:
48
+ """
49
+ Exposes application options that are initialized from the command line or
50
+ by specifying a parameters list.
51
+ """
52
+
53
+ def __init__(self, opt_list=None):
54
+ self._command = Commands.EXECUTE
55
+ self._initialize_options(opt_list)
56
+
57
+ @property
58
+ def command(self):
59
+ """
60
+ Returns the command to be executed.
61
+ """
62
+ return self._command
63
+
64
+ @property
65
+ def task(self):
66
+ """
67
+ Returns the task to be executed or the default task if none specified.
68
+ """
69
+ return self._options.task
70
+
71
+ @property
72
+ def bolt_file(self):
73
+ """
74
+ Returns the bolt file to use or the default bolt file if none specified.
75
+ """
76
+ return self._options.bolt_file
77
+
78
+ @property
79
+ def log_level(self):
80
+ """
81
+ Returns the log level to use or the default log level if none specified.
82
+ """
83
+ return self._options.log_level
84
+
85
+ @property
86
+ def log_file(self):
87
+ """
88
+ Returns the log file to use or None if no file is specified.
89
+ """
90
+ return self._options.log_file
91
+
92
+ @property
93
+ def continue_on_error(self):
94
+ """
95
+ Returns whether to continue executing tasks when errors are found. The
96
+ default is false.
97
+ """
98
+ return self._options.continue_on_error
99
+
100
+ def _initialize_options(self, opt_list=None):
101
+ parser = self._build_parser()
102
+ self._options = parser.parse_args(opt_list)
103
+ self._marshall_options()
104
+
105
+ def _build_parser(self):
106
+ parser = argparse.ArgumentParser()
107
+ parser.add_argument(
108
+ OptionSwitch.TASK,
109
+ nargs="?",
110
+ default=Default.TASK,
111
+ help='Task to be executed. It uses "default" if not specified',
112
+ )
113
+ parser.add_argument(
114
+ OptionSwitch.BOLTFILE_SHORT,
115
+ OptionSwitch.BOLTFILE_LONG,
116
+ default=Default.BOLTFILE,
117
+ help='Bolt file containing the configuration and task. The default is "boltfile.py"',
118
+ )
119
+ parser.add_argument(
120
+ OptionSwitch.LOG_LEVEL_SHORT,
121
+ OptionSwitch.LOG_LEVEL_LONG,
122
+ default=Default.LOG_LEVEL,
123
+ help="Specifies logging level.",
124
+ )
125
+ parser.add_argument(
126
+ OptionSwitch.LOG_FILE_SHORT,
127
+ OptionSwitch.LOG_FILE_LONG,
128
+ default=None,
129
+ help="Log file to write the output.",
130
+ )
131
+ parser.add_argument(
132
+ OptionSwitch.CONTINUE_ON_ERROR_LONG,
133
+ action="store_true",
134
+ help="Continue executing even if errors are found",
135
+ )
136
+ parser.add_argument(
137
+ OptionSwitch.VERSION_LONG,
138
+ action="store_true",
139
+ help="Shows the the application version",
140
+ )
141
+ return parser
142
+
143
+ def _marshall_options(self):
144
+ self._marshall_command()
145
+ self._marshall_log_level()
146
+
147
+ def _marshall_command(self):
148
+ if self._options.version:
149
+ self._command = Commands.VERSION
150
+
151
+ def _marshall_log_level(self):
152
+ if self._options.log_level == Default.LOG_LEVEL:
153
+ return
154
+ lc_level = self._options.log_level.lower()
155
+ if lc_level in ["", "n", "notset"]:
156
+ self._options.log_level = logging.NOTSET
157
+ elif lc_level in ["debug", "d", "dbg"]:
158
+ self._options.log_level = logging.DEBUG
159
+ elif lc_level in ["info", "i", "information"]:
160
+ self._options.log_level = logging.INFO
161
+ elif lc_level in ["warning", "warn", "w"]:
162
+ self._options.log_level = logging.WARNING
163
+ elif lc_level in ["error", "err", "e"]:
164
+ self._options.log_level = logging.ERROR
165
+ elif lc_level in ["critical", "crit", "c"]:
166
+ self._options.log_level = logging.CRITICAL
bolt/_btregistry.py ADDED
@@ -0,0 +1,60 @@
1
+ """
2
+ """
3
+ import logging
4
+
5
+ import bolt.api as api
6
+
7
+
8
+ class TaskRegistry:
9
+ """ """
10
+
11
+ def __init__(self):
12
+ self._tasks = {}
13
+
14
+ def register_task(self, name, task):
15
+ """
16
+
17
+ :param name:
18
+ :param task:
19
+ :return:
20
+ """
21
+ if not self._is_valid_task(task):
22
+ raise InvalidTaskTypeError(type(task))
23
+ self._tasks[name] = task
24
+ msg = "{task_name} task is registered.".format(task_name=name)
25
+ logging.debug(msg)
26
+
27
+ def register_module_tasks(self, module):
28
+ """ """
29
+ module.register_tasks(self)
30
+
31
+ def get(self, name):
32
+ """
33
+
34
+ :param name:
35
+ :return:
36
+ """
37
+ task_name = self._extract_task_name(name)
38
+ return self._tasks[task_name]
39
+
40
+ def _is_valid_task(self, task):
41
+ is_list = isinstance(task, list)
42
+ if is_list:
43
+ for t in task:
44
+ if not isinstance(t, str):
45
+ return False
46
+ return callable(task) or is_list
47
+
48
+ def _extract_task_name(self, full_task):
49
+ return full_task.split(".")[0]
50
+
51
+
52
+ class InvalidTaskTypeError(api.BoltError):
53
+ """ """
54
+
55
+ def __init__(self, task_type):
56
+ super(InvalidTaskTypeError, self).__init__(100)
57
+ self.task_type = task_type
58
+
59
+ def __repr__(self):
60
+ return "InvalidTaskTypeError({t})".format(t=self.task_type)
bolt/_btrunner.py ADDED
@@ -0,0 +1,63 @@
1
+ """
2
+ """
3
+ import logging
4
+ import bolt.api as api
5
+
6
+
7
+ class TaskRunner:
8
+ """ """
9
+
10
+ def __init__(self, config, registry, continue_on_error):
11
+ self._config = config
12
+ self._registry = registry
13
+ self._continue_on_error = continue_on_error
14
+ self._context = {}
15
+ self._script = None
16
+ self._executed_operations = None
17
+
18
+ def run(self, task):
19
+ self._build(task)
20
+ for task in self._script:
21
+ self._try_run_task(task)
22
+
23
+ def tear_down(self):
24
+ for operation in reversed(self._executed_operations):
25
+ if hasattr(operation, "tear_down"):
26
+ operation.tear_down()
27
+
28
+ def _build(self, task_name):
29
+ self._script = []
30
+ self._executed_operations = []
31
+ self._build_task(task_name)
32
+
33
+ def _build_task(self, task_name):
34
+ task_operation = self._registry.get(task_name)
35
+ if callable(task_operation):
36
+ task_config = self._config.get(task_name)
37
+ requested_task = (task_operation, task_config)
38
+ self._script.append(requested_task)
39
+ else:
40
+ for subtask in task_operation:
41
+ self._build_task(subtask)
42
+
43
+ def _try_run_task(self, task):
44
+ if self._continue_on_error:
45
+ self._run_task_protected(task)
46
+ else:
47
+ self._run_task(task)
48
+
49
+ def _run_task_protected(self, task):
50
+ try:
51
+ self._run_task(task)
52
+ except Exception as ex:
53
+ logging.exception(ex)
54
+
55
+ def _run_task(self, task):
56
+ operation, config = task
57
+ result = operation(config=config, context=self._context)
58
+ self._raise_if_not_none_or_zero(result)
59
+ self._executed_operations.append(operation)
60
+
61
+ def _raise_if_not_none_or_zero(self, result):
62
+ if result:
63
+ raise api.TaskFailedError()
bolt/_btutils.py ADDED
@@ -0,0 +1,47 @@
1
+ """
2
+ """
3
+ import importlib
4
+ import os.path
5
+ import sys
6
+
7
+
8
+ def add_search_path(*path_tokens):
9
+ """
10
+ Adds a new search path from where modules can be loaded.
11
+
12
+ This function is provided for test applications to add locations to the search path, so any required functionality
13
+ can be loaded. It helps keeping the step implementation modules simple by placing the bulk of the implementation in
14
+ separate utility libraries. This function can also be used to add the application being tested to the path, so its
15
+ functionality can be made available for testing.
16
+
17
+ :param arglist path_tokens:
18
+ Variable list of path tokens that is joined to create the full, absolute path to be added.
19
+ """
20
+ full_path = os.path.join(*path_tokens)
21
+ if full_path not in sys.path:
22
+ sys.path.insert(0, os.path.abspath(full_path))
23
+
24
+
25
+ def load_script(filename):
26
+ """
27
+ Loads a python script as a module.
28
+
29
+ This function is provided to allow applications to load a Python module by its file name.
30
+
31
+ :param string filename:
32
+ Name of the python file to be loaded as a module.
33
+
34
+ :return:
35
+ A |Python|_ module loaded from the specified file.
36
+ """
37
+ path, module_name, ext = _extract_script_components(filename)
38
+ add_search_path(path)
39
+ return importlib.import_module(module_name)
40
+ # return _load_module(module_name)
41
+
42
+
43
+ def _extract_script_components(filename):
44
+ path = os.path.dirname(filename)
45
+ base_name = os.path.basename(filename)
46
+ module_name, ext = os.path.splitext(base_name)
47
+ return path, module_name, ext
bolt/api.py ADDED
@@ -0,0 +1,123 @@
1
+ """
2
+ This module contains classes that can be used for the implementation of
3
+ bolt tasks.
4
+ """
5
+ import logging
6
+
7
+
8
+ class Task:
9
+ """ """
10
+
11
+ def __call__(self, **kwargs):
12
+ self.config = kwargs.get("config")
13
+ self._configure()
14
+ self._execute()
15
+
16
+ def _configure(self):
17
+ """ """
18
+ logging.warning("Derived classes should implement _configure()")
19
+
20
+ def _execute(self):
21
+ """ """
22
+ logging.warning("Derived classes should implement _execute()")
23
+
24
+ def _require(self, key):
25
+ value = self.config.get(key)
26
+ if not value:
27
+ raise RequiredConfigurationError(key)
28
+ return value
29
+
30
+ def _optional(self, key, default=None):
31
+ return self.config.get(key, default)
32
+
33
+
34
+ class BoltError(Exception):
35
+ """
36
+ Base class for all exceptions in Bolt. The class is provided for inheritance
37
+ and it should not be used by itself.
38
+
39
+ Errors for the Bolt application itself should be derived from this class.
40
+ """
41
+
42
+ def __init__(self, code=1):
43
+ self.code = code
44
+
45
+ def __repr__(self):
46
+ return "BoltError({code})".format(code=self.code)
47
+
48
+ def __str__(self):
49
+ return repr(self)
50
+
51
+
52
+ class TaskError(BoltError):
53
+ """
54
+ Base class for exceptions raised by a bolt task implementation. This class
55
+ is provided for inheritance and it should not be used by itself.
56
+
57
+ Exceptions derived from this class should be implemented to indicate errors
58
+ during the execution of a class. More specific exception base classes are
59
+ provided for the distinct processes of executing a class.
60
+
61
+ .. seealso::
62
+ :class:`RequiredConfigurationError`
63
+ :class:`ConfigurationValueError`
64
+ :class:`TaskFailedError`
65
+ """
66
+
67
+ def __init__(self, code=999):
68
+ super(TaskError, self).__init__(code)
69
+
70
+ def __repr__(self):
71
+ return "TaskError({code})".format(code=self.code)
72
+
73
+
74
+ class RequiredConfigurationError(TaskError):
75
+ """
76
+ Exception raised by tasks when a required configuration parameter is missing.
77
+
78
+ :param str parameter:
79
+ Name of the parameter missing.
80
+ """
81
+
82
+ def __init__(self, parameter):
83
+ super(RequiredConfigurationError, self).__init__(2)
84
+ self.parameter = parameter
85
+
86
+ def __repr__(self):
87
+ return "RequiredConfigurationError({param})".format(param=self.parameter)
88
+
89
+
90
+ class ConfigurationValueError(TaskError):
91
+ """
92
+ Exception raised by tasks when the value of a configuration parameter is
93
+ invalid.
94
+
95
+ :param str parameter:
96
+ Name of the parameter that has an invalid value.
97
+ :param any value:
98
+ Invalid value specified.
99
+ """
100
+
101
+ def __init__(self, parameter, value):
102
+ super(ConfigurationValueError, self).__init__(3)
103
+ self.parameter = parameter
104
+ self.value = value
105
+
106
+ def __repr__(self):
107
+ return "ConfigurationValueError({param}, {value})".format(
108
+ param=self.parameter, value=self.value
109
+ )
110
+
111
+
112
+ class TaskFailedError(TaskError):
113
+ """
114
+ Exception to indicate that a task execution has failed. Tasks can choose
115
+ to raise this exception directly or derive from a more specific exception
116
+ to provide more information about the error.
117
+ """
118
+
119
+ def __init__(self, code=4):
120
+ super(TaskFailedError, self).__init__(code)
121
+
122
+ def __repr__(self):
123
+ return "TaskFailedError({code})".format(code=self.code)
bolt/errors.py ADDED
@@ -0,0 +1,61 @@
1
+ """
2
+ This module defines exception classes used in the implementation of Bolt to
3
+ report errors or as base classes to define other error conditions.
4
+ """
5
+ from bolt.api import BoltError
6
+
7
+
8
+ class InvalidConfigurationError(BoltError):
9
+ """
10
+ Base class for all task configuration errors.
11
+ """
12
+
13
+ def __repr__(self):
14
+ return "InvalidConfigurationError()"
15
+
16
+
17
+ class ConfigurationParameterError(InvalidConfigurationError):
18
+ """
19
+ Base class for configuration parameters errors.
20
+ """
21
+
22
+ def __init__(self, param_name):
23
+ self.parameter = param_name
24
+
25
+ def __repr__(self):
26
+ return "ConfigurationParameterError({pn})".format(pn=repr(self.parameter))
27
+
28
+
29
+ class RequiredParameterMissingError(ConfigurationParameterError):
30
+ """
31
+ Indicates a requiered configuration parameter is missing from the task
32
+ configuration
33
+ """
34
+
35
+ def __repr__(self):
36
+ return "RequiredParameterMissingError({pn})".format(pn=repr(self.parameter))
37
+
38
+
39
+ class ConfigurationValueError(ConfigurationParameterError):
40
+ """
41
+ Indicates the value for a configuration parameter is invalid.
42
+ """
43
+
44
+ def __init__(self, param_name, value):
45
+ super(ConfigurationValueError, self).__init__(param_name)
46
+ self.value = value
47
+
48
+ def __repr__(self):
49
+ fmt = "ConfigurationValueError({pn}, {v})"
50
+ return fmt.format(pn=repr(self.parameter), v=repr(self.value))
51
+
52
+
53
+ class TaskError(BoltError):
54
+ """
55
+ Base class for exceptions raised during task execution. Developers should
56
+ raise this exception or a derived type to indicate an error during the
57
+ execution of the task they are implementing.
58
+ """
59
+
60
+ def __repr__(self):
61
+ return "TaskError()"