-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.py
177 lines (146 loc) · 6.79 KB
/
actions.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"""
actions.py
Custom actions to be called from Cli objects ('cli.py')
"""
import argparse
import config
import tools
import printer
class ModifyBuildersList(argparse.Action):
"""
ModifyBuildersList
This class ensures that 'builder_list' passed as arguments are
valid.
"""
def __init__(self, *args, **kwargs):
argparse.Action.__init__(self, *args, **kwargs)
self.builders_list = []
self.builders_index_dict = {}
self.lfs_builders = ["toolchain", "system", "configuration"]
def __call__(self, parser, namespace, values, option_string=None):
# Sanitize builders list by converting elements to lowercase
self.builders_list = [b.lower() for b in values]
# .- Check there is not any duplicated builder before
# doing anything else
self.check_builders_dupes()
# Set 'config.REMOVE_REBOOT_COMPONENT' if 'configuration'
# is not the latest builder
if self.builders_list[-1] != "configuration":
setattr(config, "REMOVE_REBOOT_COMPONENT", True)
# Modify builders list by adding 'provider' and
# 'collectors' builders
# .- Remove 'provider' if present and ensure it is placed
# at the beginning of the 'self.builders_list' but only iff
# 'self.builder_list' is not compound by 'collector' builder
# alone.
if self.builders_list != ["collector"]:
tools.remove_all_and_add_element(self.builders_list,
"provider",
index=0)
# .- Remove 'collector' if present and ensure it is placed
# at the end of the 'self.builders_list' but only iff
# 'self.builder_list' is not compound by 'provider' builder
# alone.
if self.builders_list != ["provider"]:
tools.remove_all_and_add_element(self.builders_list, "collector")
# Substitute 'lfs' name for its builders component
tools.substitute_in_list(self.builders_list,
"lfs",
self.lfs_builders)
# .- Check there is not any duplicated builder after substitutions.
# Raise error if so
self.check_builders_dupes()
# .- Check we are not trying to run 'blfs' before any 'lfs' component
self.check_builders_order()
# Set generated value. 'self.dest' is the argument name 'builders_list'
setattr(namespace, self.dest, self.builders_list)
def check_builders_dupes(self, bl=None):
"""
Check there is no a duplicate builder on 'self.builder_list'
"""
# 'bl' is not None if we are running this method from 'unittest'
if bl is not None:
self.builders_list = bl
for b in set(self.builders_list):
if self.builders_list.count(b) > 1:
msg = "Duplicated builder '{b}' is not allowed".format(b=b)
printer.error(msg)
def check_builders_order(self, bl=None):
"""
Check 'self.builder_list' has a correct order according to:
- 'blfs' should be last builder on the list
- 'lfs' builders ('toolchain', 'system', 'configuration') should
maintain that order if all are present
- ['toolchain', 'configuration'] order is not allowed
"""
# 'bl' is not None if we are running this method from 'unittest'
if bl is not None:
self.builders_list = bl
# Get the position number of every builder in the 'self.builders_list'
self.builders_index_dict = {
"toolchain": tools.get_element_index(self.builders_list,
"toolchain",
not_present=-2),
"system": tools.get_element_index(self.builders_list,
"system",
not_present=-2),
"configuration": tools.get_element_index(self.builders_list,
"configuration",
not_present=-2),
"blfs": tools.get_element_index(self.builders_list,
"blfs",
not_present=-1)
}
# .- Ensure 'blfs' is present and its 'index' is max
if self.builders_index_dict["blfs"] != -1 and \
self.builders_index_dict["blfs"] != max(self.builders_index_dict.values()):
msg = "You can not build 'blfs' before any of the 'lfs' book's builders"
printer.error(msg)
# .- Check present 'lfs' book's builders can be built
is_toolchain_present = tools.is_element_present(self.builders_list,
"toolchain")
is_system_present = tools.is_element_present(self.builders_list,
"system")
is_configuration_present = tools.is_element_present(self.builders_list,
"configuration")
# Discard index values for builders not present, because it will always be
# the minimun index instead.
present_indexes_list = list(
filter(
lambda x: x >= 0,
[self.builders_index_dict[key] for key in self.builders_index_dict.keys()]
)
)
# Only check for min if there is any builder present.
# Set 'true' in other case
if tools.is_empty_list(present_indexes_list) is True:
has_toolchain_min_index = True
else:
has_toolchain_min_index = (
self.builders_index_dict["toolchain"] == min(
present_indexes_list
)
)
# Would be equal only in case both are not present
is_system_before_configuration = (
self.builders_index_dict["system"] <= self.builders_index_dict["configuration"]
)
is_lfs_order_valid = tools.check_lfs_builders_order(
t=is_toolchain_present,
s=is_system_present,
c=is_configuration_present,
m=has_toolchain_min_index,
la=is_system_before_configuration
)
# .- Check provided 'lfs' book's builder order
if is_lfs_order_valid is False:
msg = "You are trying to build 'lfs' \
book's builders in a wrong order"
printer.error(msg)
class SetConfigOption(argparse.Action):
"""
SetConfigOption
Set 'config.py' constants from values passed to the CLI interface.
"""
def __call__(self, parser, namespace, value, option_string=None):
setattr(config, self.dest.upper(), value)