environment-maintenance 5.3.9__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.
- environment_maintenance/__init__.py +16 -0
- environment_maintenance/__main__.py +2 -0
- environment_maintenance/actions/__init__.py +29 -0
- environment_maintenance/actions/application.py +136 -0
- environment_maintenance/actions/chain.py +127 -0
- environment_maintenance/actions/domain.py +75 -0
- environment_maintenance/actions/interpreters.py +11 -0
- environment_maintenance/actions/process.py +16 -0
- environment_maintenance/actions/transport.py +80 -0
- environment_maintenance/application_resources.py +71 -0
- environment_maintenance/checking/__init__.py +18 -0
- environment_maintenance/checking/application.py +92 -0
- environment_maintenance/checking/cli.py +26 -0
- environment_maintenance/checking/domain.py +19 -0
- environment_maintenance/checking/presentation.py +17 -0
- environment_maintenance/cli.py +116 -0
- environment_maintenance/configuration/__init__.py +57 -0
- environment_maintenance/configuration/application.py +119 -0
- environment_maintenance/configuration/domain.py +73 -0
- environment_maintenance/configuration/execution.py +10 -0
- environment_maintenance/configuration/json_file.py +25 -0
- environment_maintenance/configuration/json_patch.py +6 -0
- environment_maintenance/invocation.py +61 -0
- environment_maintenance/network/__init__.py +10 -0
- environment_maintenance/network/application.py +64 -0
- environment_maintenance/network/domain.py +136 -0
- environment_maintenance/network/linux.py +84 -0
- environment_maintenance/output.py +14 -0
- environment_maintenance/preparation/__init__.py +24 -0
- environment_maintenance/preparation/application.py +58 -0
- environment_maintenance/preparation/cli.py +54 -0
- environment_maintenance/preparation/presentation.py +12 -0
- environment_maintenance/registry/__init__.py +1 -0
- environment_maintenance/registry/domain.py +31 -0
- environment_maintenance/registry/files.py +95 -0
- environment_maintenance/resources/commands/invoke.bash +11 -0
- environment_maintenance/resources/commands/read.bash +3 -0
- environment_maintenance/resources/commands/write.bash +20 -0
- environment_maintenance/running/__init__.py +1 -0
- environment_maintenance/running/application.py +102 -0
- environment_maintenance/running/cli.py +76 -0
- environment_maintenance/running/console.py +36 -0
- environment_maintenance/running/domain.py +24 -0
- environment_maintenance/running/presentation.py +10 -0
- environment_maintenance/running/process_groups.py +11 -0
- environment_maintenance-5.3.9.dist-info/METADATA +11 -0
- environment_maintenance-5.3.9.dist-info/RECORD +50 -0
- environment_maintenance-5.3.9.dist-info/WHEEL +5 -0
- environment_maintenance-5.3.9.dist-info/entry_points.txt +2 -0
- environment_maintenance-5.3.9.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""Independent environment requirement runner. No application-specific dependencies."""
|
|
2
|
+
__version__ = "5.3.9"
|
|
3
|
+
|
|
4
|
+
from .actions import execute_action, plan_action, execute_chain, plan_chain
|
|
5
|
+
|
|
6
|
+
from .configuration import (
|
|
7
|
+
add_requirement,
|
|
8
|
+
clear_requirements,
|
|
9
|
+
delete_requirement,
|
|
10
|
+
list_requirement_ids,
|
|
11
|
+
read_requirements,
|
|
12
|
+
update_requirement,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
from .preparation import prepare_resources
|
|
16
|
+
from .checking import check_resources
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""Public action operations composed with the external process adapter."""
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
from .application import ActionExecutor, ResultInterpreter, prepare_action
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def plan_action(
|
|
10
|
+
action: dict[str, Any], parameters: dict[str, Any], *,
|
|
11
|
+
base_dir: str | Path, interpreters: dict[str, ResultInterpreter],
|
|
12
|
+
) -> dict[str, Any]:
|
|
13
|
+
"""Prepare every declared command without running processes or interpreters."""
|
|
14
|
+
return prepare_action(action, parameters, base_dir=base_dir, interpreters=interpreters)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def execute_action(
|
|
18
|
+
action: dict[str, Any], parameters: dict[str, Any], *,
|
|
19
|
+
base_dir: str | Path, interpreters: dict[str, ResultInterpreter],
|
|
20
|
+
) -> dict[str, Any]:
|
|
21
|
+
"""Run the prepared chain and preserve each attempted command's full result."""
|
|
22
|
+
from .process import run_command
|
|
23
|
+
|
|
24
|
+
return ActionExecutor(run_command).execute(
|
|
25
|
+
action, parameters, base_dir=base_dir, interpreters=interpreters
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
from .chain import execute_chain, plan_chain
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"""Prepare actions once and execute their ordered commands through narrow ports."""
|
|
2
|
+
|
|
3
|
+
from collections.abc import Callable
|
|
4
|
+
from copy import deepcopy
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from command_execution import TemplateRenderer
|
|
7
|
+
from typing import Any, Protocol
|
|
8
|
+
|
|
9
|
+
from .domain import declared_commands, interpreted_result, validate_command
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class ResultInterpreter(Protocol):
|
|
13
|
+
def interpret(self, result: dict[str, Any], options: dict[str, Any]) -> dict[str, Any]:
|
|
14
|
+
...
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
ProcessRunner = Callable[[dict[str, Any]], dict[str, Any]]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _prepare_command(
|
|
21
|
+
command: dict[str, Any], parameters: dict[str, Any], base_dir: Path, index: int
|
|
22
|
+
) -> dict[str, Any]:
|
|
23
|
+
try:
|
|
24
|
+
argv = [TemplateRenderer().render(part, parameters, 'python-template') for part in command["argv"]]
|
|
25
|
+
cwd = TemplateRenderer().render(command['cwd'], parameters, 'python-template')
|
|
26
|
+
env = {
|
|
27
|
+
key: TemplateRenderer().render(value, parameters, 'python-template')
|
|
28
|
+
for key, value in command["env"].items()
|
|
29
|
+
}
|
|
30
|
+
except (KeyError, ValueError) as exc:
|
|
31
|
+
raise ValueError(f"commands[{index}] template: {exc}") from exc
|
|
32
|
+
prepared = dict(
|
|
33
|
+
argv=argv, cwd=cwd, env=env,
|
|
34
|
+
interpreter=deepcopy(command["interpreter"]),
|
|
35
|
+
)
|
|
36
|
+
validate_command(prepared, index)
|
|
37
|
+
directory = Path(cwd)
|
|
38
|
+
if not directory.is_absolute():
|
|
39
|
+
directory = base_dir / directory
|
|
40
|
+
prepared["cwd"] = str(directory)
|
|
41
|
+
return prepared
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def validate_execution_inputs(
|
|
45
|
+
parameters: dict[str, Any], base_dir: str | Path,
|
|
46
|
+
interpreters: dict[str, ResultInterpreter],
|
|
47
|
+
) -> None:
|
|
48
|
+
"""Validate shared invocation inputs independently of command selection."""
|
|
49
|
+
if not isinstance(parameters, dict):
|
|
50
|
+
raise ValueError("parameters must be a scalar map")
|
|
51
|
+
for name, value in parameters.items():
|
|
52
|
+
if not isinstance(name, str) or not isinstance(value, (str, int, float, bool)):
|
|
53
|
+
raise ValueError(f"parameters[{name}]: expected a named scalar")
|
|
54
|
+
if (
|
|
55
|
+
not isinstance(base_dir, (str, Path)) or not str(base_dir)
|
|
56
|
+
or not Path(base_dir).is_absolute()
|
|
57
|
+
):
|
|
58
|
+
raise ValueError("base_dir must be an absolute path")
|
|
59
|
+
if not isinstance(interpreters, dict):
|
|
60
|
+
raise ValueError("interpreters must be a map")
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def prepare_action(
|
|
64
|
+
action: dict[str, Any], parameters: dict[str, Any], *,
|
|
65
|
+
base_dir: str | Path, interpreters: dict[str, ResultInterpreter],
|
|
66
|
+
) -> dict[str, Any]:
|
|
67
|
+
commands = declared_commands(action)
|
|
68
|
+
validate_execution_inputs(parameters, base_dir, interpreters)
|
|
69
|
+
prepared = []
|
|
70
|
+
for index, command in enumerate(commands):
|
|
71
|
+
name = command["interpreter"]["name"]
|
|
72
|
+
if name not in interpreters or not callable(
|
|
73
|
+
getattr(interpreters[name], "interpret", None)
|
|
74
|
+
):
|
|
75
|
+
raise ValueError(f"commands[{index}].interpreter {name}: callable interpret is required")
|
|
76
|
+
prepared.append(_prepare_command(command, parameters, Path(base_dir), index))
|
|
77
|
+
return dict(commands=prepared)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
class ActionExecutor:
|
|
81
|
+
def __init__(self, run_command: ProcessRunner):
|
|
82
|
+
self._run_command = run_command
|
|
83
|
+
|
|
84
|
+
def execute(
|
|
85
|
+
self, action: dict[str, Any], parameters: dict[str, Any], *,
|
|
86
|
+
base_dir: str | Path, interpreters: dict[str, ResultInterpreter],
|
|
87
|
+
) -> dict[str, Any]:
|
|
88
|
+
plan = prepare_action(action, parameters, base_dir=base_dir, interpreters=interpreters)
|
|
89
|
+
from .chain import execute_prepared
|
|
90
|
+
return execute_prepared(plan, interpreters, self._run_command)
|
|
91
|
+
|
|
92
|
+
def execute_command(
|
|
93
|
+
self, command: dict[str, Any], interpreter: ResultInterpreter, index: int, *,
|
|
94
|
+
stdin: bytes | None = None, capture_exception: bool = False,
|
|
95
|
+
) -> dict[str, Any]:
|
|
96
|
+
invocation = {field: deepcopy(command[field]) for field in ("argv", "cwd", "env")}
|
|
97
|
+
if stdin is not None:
|
|
98
|
+
invocation["stdin"] = stdin
|
|
99
|
+
record = dict(
|
|
100
|
+
invocation=invocation, interpreter=deepcopy(command["interpreter"]),
|
|
101
|
+
exit_code=None, stdout=b"", stderr=b"", interpretation=None, error=None,
|
|
102
|
+
)
|
|
103
|
+
if capture_exception:
|
|
104
|
+
record["exception"] = None
|
|
105
|
+
try:
|
|
106
|
+
raw = self._run_command(deepcopy(invocation))
|
|
107
|
+
except Exception as exc:
|
|
108
|
+
if not capture_exception and not isinstance(exc, (OSError, ValueError)):
|
|
109
|
+
raise
|
|
110
|
+
if capture_exception:
|
|
111
|
+
record["exception"] = {"type": type(exc).__name__, "message": str(exc)}
|
|
112
|
+
record["error"] = f"commands[{index}] launch argv/cwd: {exc}"
|
|
113
|
+
return record
|
|
114
|
+
record.update(raw)
|
|
115
|
+
interpretation, error, exception = self._interpret(raw, command["interpreter"], interpreter)
|
|
116
|
+
record.update(interpretation=interpretation, error=error)
|
|
117
|
+
if capture_exception:
|
|
118
|
+
record["exception"] = exception
|
|
119
|
+
return record
|
|
120
|
+
|
|
121
|
+
def _interpret(
|
|
122
|
+
self, raw: dict[str, Any], configuration: dict[str, Any], interpreter: ResultInterpreter
|
|
123
|
+
) -> tuple[dict[str, Any] | None, str | None, dict[str, str] | None]:
|
|
124
|
+
name = configuration["name"]
|
|
125
|
+
try:
|
|
126
|
+
answer = interpreter.interpret(
|
|
127
|
+
deepcopy(raw), deepcopy(configuration["options"])
|
|
128
|
+
)
|
|
129
|
+
interpretation = interpreted_result(answer)
|
|
130
|
+
except Exception as exc:
|
|
131
|
+
return None, f"interpreter {name}: {exc}", {"type": type(exc).__name__, "message": str(exc)}
|
|
132
|
+
error = (
|
|
133
|
+
None if interpretation["success"]
|
|
134
|
+
else f"interpreter {name}: {interpretation['error']}"
|
|
135
|
+
)
|
|
136
|
+
return interpretation, error, None
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"""Legacy EM views backed by the shared planner, coordinator and result store."""
|
|
2
|
+
from copy import deepcopy
|
|
3
|
+
import json
|
|
4
|
+
from command_execution import CommandPlanner, ChainCoordinator, RawOutcome
|
|
5
|
+
from .application import ActionExecutor, prepare_action
|
|
6
|
+
from .domain import chain_options
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def _encode(data):
|
|
10
|
+
return json.dumps(data, ensure_ascii=False, allow_nan=False).encode('utf-8')
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def prepare_chain(action, parameters, *, input_data, base_dir, interpreters):
|
|
14
|
+
_encode(input_data)
|
|
15
|
+
plan = prepare_action(action, parameters, base_dir=base_dir, interpreters=interpreters)
|
|
16
|
+
for index, (prepared, declared) in enumerate(zip(plan['commands'], action['commands'], strict=True)):
|
|
17
|
+
prepared.update(chain_options(declared, index))
|
|
18
|
+
return plan
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def plan_chain(action, parameters, *, input_data, base_dir, interpreters):
|
|
22
|
+
return prepare_chain(action, parameters, input_data=input_data, base_dir=base_dir, interpreters=interpreters)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def execute_prepared(plan, interpreters, runner, input_data=None, on_result=None, chain=False):
|
|
26
|
+
prepared, rows, records = {}, {}, {}
|
|
27
|
+
commands = []
|
|
28
|
+
executor = ActionExecutor(runner)
|
|
29
|
+
aggregate = dict(status='succeeded', data=deepcopy(input_data), commands=[], error=None)
|
|
30
|
+
for index, command in enumerate(plan['commands']):
|
|
31
|
+
if not command.get('enabled', True):
|
|
32
|
+
continue
|
|
33
|
+
identifier = str(index)
|
|
34
|
+
prepared[identifier] = command
|
|
35
|
+
# Legacy background rows do not alter the caller's synchronous decision.
|
|
36
|
+
mode = command.get('mode', 'sync')
|
|
37
|
+
commands.append(dict(id=identifier, argv=command['argv'], order=index,
|
|
38
|
+
execution=mode, on_error=command.get('on_error', 'stop') if mode == 'sync' else 'continue'))
|
|
39
|
+
rows[identifier] = dict(index=index, input_data=None, state='running',
|
|
40
|
+
invocation={key: deepcopy(command[key]) for key in ('argv', 'cwd', 'env')},
|
|
41
|
+
interpreter=deepcopy(command['interpreter']), exit_code=None, stdout=None,
|
|
42
|
+
stderr=None, interpretation=None, error=None, exception=None)
|
|
43
|
+
core_plan, issues = CommandPlanner().compile({'commands': commands})
|
|
44
|
+
if issues:
|
|
45
|
+
raise ValueError(str(issues))
|
|
46
|
+
|
|
47
|
+
class ConsumerCoordinator(ChainCoordinator):
|
|
48
|
+
def _run_process(self, spec, stdin, on_chunk, coordinator):
|
|
49
|
+
command = prepared[spec.id]
|
|
50
|
+
invocation = {key: deepcopy(command[key]) for key in ('argv', 'cwd', 'env')}
|
|
51
|
+
if chain:
|
|
52
|
+
invocation['stdin'] = stdin
|
|
53
|
+
try:
|
|
54
|
+
allowed = coordinator.start(lambda: True)
|
|
55
|
+
if allowed is None:
|
|
56
|
+
raise RuntimeError('Command dispatch stopped')
|
|
57
|
+
raw = runner(deepcopy(invocation))
|
|
58
|
+
records[spec.id] = dict(invocation=invocation, **{
|
|
59
|
+
key: value for key, value in raw.items() if key != 'invocation'})
|
|
60
|
+
return RawOutcome(spec.argv, raw['exit_code'], raw['stdout'], raw['stderr'])
|
|
61
|
+
except Exception as error:
|
|
62
|
+
if not chain and not isinstance(error, (OSError, ValueError)):
|
|
63
|
+
raise
|
|
64
|
+
detail = {'type': type(error).__name__, 'message': str(error)}
|
|
65
|
+
records[spec.id] = dict(invocation=invocation, exit_code=None, stdout=b'', stderr=b'',
|
|
66
|
+
exception=detail, error=f'commands[{spec.id}] launch argv/cwd: {error}')
|
|
67
|
+
return RawOutcome(spec.argv, None, launch_error=detail)
|
|
68
|
+
|
|
69
|
+
def interpret(raw, spec):
|
|
70
|
+
record, command = records[spec.id], prepared[spec.id]
|
|
71
|
+
if raw.launch_error:
|
|
72
|
+
record['interpretation'] = None
|
|
73
|
+
return dict(success=False, value=None, error=record['error'])
|
|
74
|
+
config = command['interpreter']
|
|
75
|
+
answer, error, exception = executor._interpret(record, config, interpreters[config['name']])
|
|
76
|
+
record.update(interpretation=answer, error=error, exception=exception)
|
|
77
|
+
if answer is None:
|
|
78
|
+
return dict(success=False, value=None, error=error)
|
|
79
|
+
forwarded = deepcopy(answer)
|
|
80
|
+
if chain and 'next_input' in answer:
|
|
81
|
+
try:
|
|
82
|
+
_encode(answer['next_input'])
|
|
83
|
+
except (ValueError, TypeError) as error:
|
|
84
|
+
record.update(error=f'commands[{spec.id}] next_input: {error}',
|
|
85
|
+
exception={'type': type(error).__name__, 'message': str(error)})
|
|
86
|
+
forwarded.pop('next_input', None)
|
|
87
|
+
forwarded.update(success=False, error=record['error'])
|
|
88
|
+
return forwarded
|
|
89
|
+
|
|
90
|
+
def completed(outcome):
|
|
91
|
+
row = rows[outcome.id]
|
|
92
|
+
if outcome.state == 'dispatch_failed':
|
|
93
|
+
detail = outcome.error
|
|
94
|
+
row.update(error=f'commands[{outcome.id}] dispatch: {detail["message"]}',
|
|
95
|
+
exception={key: detail[key] for key in ('type', 'message')})
|
|
96
|
+
else:
|
|
97
|
+
row.update(records[outcome.id])
|
|
98
|
+
row.update(input_data=deepcopy(outcome.input), state='completed')
|
|
99
|
+
if prepared[outcome.id].get('mode', 'sync') == 'sync' and row['error'] is not None:
|
|
100
|
+
aggregate['status'] = 'failed'
|
|
101
|
+
if aggregate['error'] is None:
|
|
102
|
+
aggregate['error'] = row['error']
|
|
103
|
+
if on_result is not None:
|
|
104
|
+
on_result(deepcopy(row))
|
|
105
|
+
|
|
106
|
+
coordinator = ConsumerCoordinator(core_plan, interpreter=interpret)
|
|
107
|
+
run = coordinator.dispatch(input_data, on_result=completed)
|
|
108
|
+
view = run.dispatch_snapshot()
|
|
109
|
+
aggregate['data'] = view['data']
|
|
110
|
+
attempted = {str(row.source_index) for row in coordinator.store.outcomes() if row.state == 'not_attempted'}
|
|
111
|
+
aggregate['commands'] = [rows[key] for key in prepared if key not in attempted]
|
|
112
|
+
if not chain:
|
|
113
|
+
run.wait()
|
|
114
|
+
for row in aggregate['commands']:
|
|
115
|
+
for field in ('index', 'input_data', 'state', 'exception'):
|
|
116
|
+
row.pop(field, None)
|
|
117
|
+
aggregate.pop('data')
|
|
118
|
+
return aggregate
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def execute_chain(action, parameters, *, input_data, base_dir, interpreters, run_command=None, on_result=None):
|
|
122
|
+
if run_command is None:
|
|
123
|
+
from .process import run_command
|
|
124
|
+
if not callable(run_command) or (on_result is not None and not callable(on_result)):
|
|
125
|
+
raise ValueError('run_command and on_result must be callable')
|
|
126
|
+
plan = prepare_chain(action, parameters, input_data=input_data, base_dir=base_dir, interpreters=interpreters)
|
|
127
|
+
return execute_prepared(plan, interpreters, run_command, input_data, on_result, chain=True)
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"""Rules for declared command chains and interpreted execution outcomes."""
|
|
2
|
+
|
|
3
|
+
from copy import deepcopy
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def validate_command(command: dict[str, Any], index: int) -> None:
|
|
8
|
+
"""Apply the same required-field rules before and after substitution."""
|
|
9
|
+
context = f"commands[{index}]"
|
|
10
|
+
if not isinstance(command, dict):
|
|
11
|
+
raise ValueError(f"{context} must be an object")
|
|
12
|
+
argv = command.get("argv")
|
|
13
|
+
if (
|
|
14
|
+
not isinstance(argv, list) or not argv
|
|
15
|
+
or any(not isinstance(part, str) for part in argv) or not argv[0]
|
|
16
|
+
):
|
|
17
|
+
raise ValueError(f"{context}.argv must be a string array with a nonempty executable")
|
|
18
|
+
cwd = command.get("cwd")
|
|
19
|
+
if not isinstance(cwd, str) or not cwd:
|
|
20
|
+
raise ValueError(f"{context}.cwd must be a nonempty string")
|
|
21
|
+
env = command.get("env")
|
|
22
|
+
if not isinstance(env, dict) or any(
|
|
23
|
+
not isinstance(key, str) or not isinstance(value, str)
|
|
24
|
+
for key, value in env.items()
|
|
25
|
+
):
|
|
26
|
+
raise ValueError(f"{context}.env must be a string map")
|
|
27
|
+
interpreter = command.get("interpreter")
|
|
28
|
+
if not isinstance(interpreter, dict):
|
|
29
|
+
raise ValueError(f"{context}.interpreter must be an object")
|
|
30
|
+
name = interpreter.get("name")
|
|
31
|
+
if not isinstance(name, str) or not name:
|
|
32
|
+
raise ValueError(f"{context}.interpreter.name must be a nonempty string")
|
|
33
|
+
if not isinstance(interpreter.get("options"), dict):
|
|
34
|
+
raise ValueError(f"{context}.interpreter.options must be an object")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def declared_commands(action: dict[str, Any]) -> list[dict[str, Any]]:
|
|
38
|
+
if not isinstance(action, dict):
|
|
39
|
+
raise ValueError("action must be an object")
|
|
40
|
+
commands = action.get("commands")
|
|
41
|
+
if not isinstance(commands, list) or not commands:
|
|
42
|
+
raise ValueError("action.commands must be a nonempty array")
|
|
43
|
+
for index, command in enumerate(commands):
|
|
44
|
+
validate_command(command, index)
|
|
45
|
+
return deepcopy(commands)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def interpreted_result(answer: dict[str, Any]) -> dict[str, Any]:
|
|
49
|
+
if not isinstance(answer, dict):
|
|
50
|
+
raise ValueError("interpreter result must be an object")
|
|
51
|
+
for field in ("success", "value", "error"):
|
|
52
|
+
if field not in answer:
|
|
53
|
+
raise ValueError(f"interpreter result requires {field}")
|
|
54
|
+
if type(answer["success"]) is not bool:
|
|
55
|
+
raise ValueError("interpreter result.success must be bool")
|
|
56
|
+
if answer["success"]:
|
|
57
|
+
if answer["error"] is not None:
|
|
58
|
+
raise ValueError("successful interpreter result.error must be None")
|
|
59
|
+
elif not isinstance(answer["error"], str) or not answer["error"]:
|
|
60
|
+
raise ValueError("failed interpreter result.error must be a nonempty string")
|
|
61
|
+
return deepcopy(answer)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def chain_options(command: dict[str, Any], index: int) -> dict[str, Any]:
|
|
65
|
+
"""Validate only the additional policy of the new chain contract."""
|
|
66
|
+
mode = command.get("mode", "sync")
|
|
67
|
+
enabled = command.get("enabled", True)
|
|
68
|
+
policy = command.get("on_error", "stop")
|
|
69
|
+
if mode not in ("sync", "async"):
|
|
70
|
+
raise ValueError(f"commands[{index}].mode must be sync or async")
|
|
71
|
+
if type(enabled) is not bool:
|
|
72
|
+
raise ValueError(f"commands[{index}].enabled must be bool")
|
|
73
|
+
if policy not in ("continue", "stop"):
|
|
74
|
+
raise ValueError(f"commands[{index}].on_error must be continue or stop")
|
|
75
|
+
return dict(mode=mode, enabled=enabled, on_error=policy)
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"""Optional generic interpretations of declared command exit codes."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class ExitCodeInterpreter:
|
|
5
|
+
def interpret(self, result, options):
|
|
6
|
+
success = result["exit_code"] in options["accept"]
|
|
7
|
+
return dict(success=success, value=result["exit_code"],
|
|
8
|
+
error=None if success else "command exit code is not accepted")
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
exit_result = ExitCodeInterpreter()
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""EM supplies a normal Bash command for its cwd/environment policy."""
|
|
2
|
+
from copy import deepcopy
|
|
3
|
+
from importlib.resources import files
|
|
4
|
+
from command_execution import ProcessWorker
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def run_command(invocation):
|
|
8
|
+
script = str(files('environment_maintenance').joinpath('resources/commands/invoke.bash'))
|
|
9
|
+
argv = ['bash', script, invocation['cwd'], str(len(invocation['env']))]
|
|
10
|
+
argv.extend(key + '=' + value for key, value in invocation['env'].items())
|
|
11
|
+
argv.extend(invocation['argv'])
|
|
12
|
+
raw = ProcessWorker().run(argv, stdin=invocation.get('stdin'))
|
|
13
|
+
if raw.launch_error:
|
|
14
|
+
raise OSError(raw.launch_error['message'])
|
|
15
|
+
return dict(invocation=deepcopy(invocation), exit_code=raw.returncode,
|
|
16
|
+
stdout=raw.stdout, stderr=raw.stderr)
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"""Shared ordinary JSON transport for configured action consumers."""
|
|
2
|
+
import base64
|
|
3
|
+
from copy import deepcopy
|
|
4
|
+
import importlib
|
|
5
|
+
import json
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from .application import ResultInterpreter, prepare_action
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _require_json_data(value: Any) -> None:
|
|
12
|
+
if value is None or type(value) in (str, bool, int, float):
|
|
13
|
+
return
|
|
14
|
+
if type(value) is list:
|
|
15
|
+
for item in value:
|
|
16
|
+
_require_json_data(item)
|
|
17
|
+
return
|
|
18
|
+
if type(value) is dict:
|
|
19
|
+
for key, item in value.items():
|
|
20
|
+
if type(key) is not str:
|
|
21
|
+
raise ValueError("JSON objects require string keys")
|
|
22
|
+
_require_json_data(item)
|
|
23
|
+
return
|
|
24
|
+
raise ValueError(f"Unsupported JSON value type: {type(value).__name__}")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def validate_json_data(value: Any) -> None:
|
|
28
|
+
"""Validate transport data without silent coercion or numeric overflow."""
|
|
29
|
+
_require_json_data(value)
|
|
30
|
+
json.dumps(value, allow_nan=False)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class JsonResultInterpreter:
|
|
34
|
+
def __init__(self, interpreter: ResultInterpreter):
|
|
35
|
+
self._interpreter = interpreter
|
|
36
|
+
|
|
37
|
+
def interpret(self, raw: dict[str, Any], options: dict[str, Any]) -> dict[str, Any]:
|
|
38
|
+
answer = self._interpreter.interpret(raw, options)
|
|
39
|
+
validate_json_data(answer)
|
|
40
|
+
return answer
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def load_interpreters(document: dict[str, Any]) -> dict[str, ResultInterpreter]:
|
|
44
|
+
declarations = document.get('interpreters')
|
|
45
|
+
if not isinstance(declarations, dict):
|
|
46
|
+
raise ValueError("interpreters must be an object")
|
|
47
|
+
result = {}
|
|
48
|
+
for name, declaration in declarations.items():
|
|
49
|
+
if not isinstance(name, str) or not name:
|
|
50
|
+
raise ValueError("interpreters require nonempty names")
|
|
51
|
+
if not isinstance(declaration, dict):
|
|
52
|
+
raise ValueError(f"interpreter {name}: expected module/attribute object")
|
|
53
|
+
module, attribute = declaration.get('module'), declaration.get('attribute')
|
|
54
|
+
if not isinstance(module, str) or not module or not isinstance(attribute, str) or not attribute:
|
|
55
|
+
raise ValueError(f"interpreter {name}: module and attribute must be nonempty strings")
|
|
56
|
+
try:
|
|
57
|
+
interpreter = getattr(importlib.import_module(module), attribute)
|
|
58
|
+
except Exception as exc:
|
|
59
|
+
raise ValueError(f"interpreter {name} {module}:{attribute}: {exc}") from exc
|
|
60
|
+
if not callable(getattr(interpreter, 'interpret', None)):
|
|
61
|
+
raise ValueError(f"interpreter {name} {module}:{attribute}: callable interpret is required")
|
|
62
|
+
result[name] = JsonResultInterpreter(interpreter)
|
|
63
|
+
return result
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def plan_for_json_transport(action, parameters, *, base_dir, interpreters):
|
|
67
|
+
plan = prepare_action(action, parameters, base_dir=base_dir, interpreters=interpreters)
|
|
68
|
+
validate_json_data(plan)
|
|
69
|
+
return plan
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def encode_action_result(result: dict[str, Any]) -> dict[str, Any]:
|
|
73
|
+
"""Encode raw streams only; interpreted JSON retains its original types."""
|
|
74
|
+
encoded = deepcopy(result)
|
|
75
|
+
for command in encoded['commands']:
|
|
76
|
+
for field in ('stdout', 'stderr'):
|
|
77
|
+
command[field] = dict(
|
|
78
|
+
encoding='base64', data=base64.b64encode(command[field]).decode('ascii')
|
|
79
|
+
)
|
|
80
|
+
return encoded
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"""Resolve declared application resource data without inspecting or acquiring it."""
|
|
2
|
+
import json
|
|
3
|
+
from string import Template
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def _render(value, parameters, field):
|
|
7
|
+
try:
|
|
8
|
+
return Template(value).substitute(parameters)
|
|
9
|
+
except (KeyError, ValueError) as exc:
|
|
10
|
+
raise ValueError(f'{field} template: {exc}') from exc
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _text(value, field):
|
|
14
|
+
if not isinstance(value, str) or not value.strip():
|
|
15
|
+
raise ValueError(f'{field} must be a nonempty string')
|
|
16
|
+
return value
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _port(value, parameters):
|
|
20
|
+
if isinstance(value, str):
|
|
21
|
+
value = _render(value, parameters, 'port')
|
|
22
|
+
if not value.isdecimal():
|
|
23
|
+
raise ValueError('port must be an integer from 1 to 65535')
|
|
24
|
+
value = int(value)
|
|
25
|
+
if type(value) is not int or not 1 <= value <= 65535:
|
|
26
|
+
raise ValueError('port must be an integer from 1 to 65535')
|
|
27
|
+
return value
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _metadata(value, parameters, field, required):
|
|
31
|
+
if not isinstance(value, dict) or not value:
|
|
32
|
+
raise ValueError(f'{field} must be a nonempty object')
|
|
33
|
+
result = {}
|
|
34
|
+
for key, item in value.items():
|
|
35
|
+
_text(key, f'{field} key')
|
|
36
|
+
if isinstance(item, str):
|
|
37
|
+
item = _render(item, parameters, f'{field}.{key}')
|
|
38
|
+
elif item is not None and type(item) not in (bool, int, float):
|
|
39
|
+
raise ValueError(f'{field}.{key} must be a JSON scalar')
|
|
40
|
+
result[key] = item
|
|
41
|
+
for key in required:
|
|
42
|
+
_text(result.get(key), f'{field}.{key}')
|
|
43
|
+
return result
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def resolve_resources(application, parameters):
|
|
47
|
+
"""Return a distinct resolved description; application PGID is owned by launch."""
|
|
48
|
+
if 'resources' not in application:
|
|
49
|
+
return {}
|
|
50
|
+
try:
|
|
51
|
+
declared = application['resources']
|
|
52
|
+
if not isinstance(declared, dict):
|
|
53
|
+
raise ValueError('resources must be an object')
|
|
54
|
+
unknown = set(declared) - {'host', 'port', 'queue', 'scheduler'}
|
|
55
|
+
if unknown:
|
|
56
|
+
raise ValueError(f'unknown resources fields: {sorted(unknown)}')
|
|
57
|
+
if ('host' in declared) != ('port' in declared):
|
|
58
|
+
raise ValueError('host and port must be declared together')
|
|
59
|
+
result = {}
|
|
60
|
+
if 'host' in declared:
|
|
61
|
+
host = _text(declared['host'], 'host')
|
|
62
|
+
result['host'] = _text(_render(host, parameters, 'host'), 'host')
|
|
63
|
+
result['port'] = _port(declared['port'], parameters)
|
|
64
|
+
for field, required in [('queue', ('driver', 'name')),
|
|
65
|
+
('scheduler', ('application', 'command'))]:
|
|
66
|
+
if field in declared:
|
|
67
|
+
result[field] = _metadata(declared[field], parameters, field, required)
|
|
68
|
+
json.dumps(result, allow_nan=False)
|
|
69
|
+
return result
|
|
70
|
+
except ValueError as exc:
|
|
71
|
+
raise ValueError(f"Application {application['id']} resources: {exc}") from exc
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""Public resource checking composed with the existing configuration and action owners."""
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from ..actions import execute_action, plan_action
|
|
6
|
+
from ..actions.application import ResultInterpreter
|
|
7
|
+
from ..configuration import read_requirements
|
|
8
|
+
from .application import check_resources as _check_resources
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def check_resources(
|
|
12
|
+
config_path: str | Path, parameters: dict[str, Any], *,
|
|
13
|
+
base_dir: str | Path, interpreters: dict[str, ResultInterpreter],
|
|
14
|
+
) -> dict[str, Any]:
|
|
15
|
+
"""Check declared conditions without installing or adjusting resources."""
|
|
16
|
+
return _check_resources(config_path, parameters, base_dir=base_dir,
|
|
17
|
+
interpreters=interpreters, reader=read_requirements,
|
|
18
|
+
planner=plan_action, executor=execute_action)
|