-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_sequence_program.py
More file actions
70 lines (63 loc) · 2.35 KB
/
command_sequence_program.py
File metadata and controls
70 lines (63 loc) · 2.35 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""Library for scripts that execute (Bash) shell commands.
See commands_example for an example demonstrating typical usage.
"""
from argparse import ArgumentParser
from commands.base_command_sequence_step import BaseCommandSequenceStep
class CommandSequenceProgram():
"""A sequence of steps that each run (Bash) shell commands."""
def __init__(self, description: str, steps: list[BaseCommandSequenceStep]):
"""
Args:
description: the description of the sequence of commands
steps: a sequence of BaseCommandSequenceSteps
"""
self._arg_parser = ArgumentParser(description=description)
self._arg_parser.add_argument(
'--continue',
help=(
'continue from the specified step'
' (default: start at the beginning)'),
dest='continue_step',
metavar='STEP')
self._arg_parser.add_argument(
'--num-steps',
type=int,
help=(
'specify the number of steps to perform'
' (default: perform all steps)'))
self._arg_parser.add_argument(
'--dry-run',
action='store_true',
help='only print commands, don\'t actually execute them')
# Ensure that each has a unique step_id. Then call _add_args.
step_ids = set()
for step in steps:
if step.step_id is not None:
if step.step_id in step_ids:
raise ValueError(f'Duplicate step found: {step.step_id}')
step_ids.add(step.step_id)
step._add_args(self._arg_parser)
self._steps = steps
self._args = self._arg_parser.parse_args()
if self._args.num_steps is not None and self._args.num_steps < 1:
raise ValueError('NUM_STEPS must be greater than or equal to 1')
def run(self) -> None:
"""Runs the CommandSequenceProgram."""
print(
'All commands are run with bash using these bash options:'
f' {BaseCommandSequenceStep._BASH_OPTIONS_STRING}')
start_step = self._args.continue_step
steps_run = 0
for step in self._steps:
if start_step is not None:
if start_step == step.step_id:
start_step = None
else:
continue
step.run(self._args)
steps_run += 1
if self._args.num_steps is not None and steps_run >= self._args.num_steps:
break
if start_step is not None:
raise ValueError(f'No step with step id "{start_step}"')
print('\nDone.')