|
@@ -7,29 +7,31 @@ LodelContext.expose_modules(globals(), {
|
7
|
7
|
'lodel.exceptions': ['LodelException', 'LodelExceptions',
|
8
|
8
|
'LodelFatalError', 'DataNoneValid', 'FieldValidationError']})
|
9
|
9
|
|
10
|
|
-##@defgroup lodel2_script Administration scripts
|
11
|
|
-#@ingroup lodel2_plugins
|
|
10
|
+## @defgroup lodel2_script Administration scripts
|
|
11
|
+# @ingroup lodel2_plugins
|
12
|
12
|
|
13
|
|
-##@package lodel.plugin.script
|
14
|
|
-#@brief Lodel2 utility for writting administration scripts
|
15
|
|
-#@ingroup lodel2_plugins
|
16
|
|
-#@ingroup lodel2_script
|
|
13
|
+## @package lodel.plugin.scripts Lodel2 utility for writting administration scripts
|
|
14
|
+# @ingroup lodel2_plugins
|
|
15
|
+# @ingroup lodel2_script
|
17
|
16
|
|
18
|
|
-##@brief Stores registered scripts
|
19
|
|
-#@todo store it in MetaLodelScript
|
|
17
|
+## @brief Stores registered scripts
|
|
18
|
+# @todo store it in MetaLodelScript
|
20
|
19
|
__registered_scripts = dict()
|
21
|
20
|
|
22
|
21
|
|
23
|
|
-##@brief LodelScript metaclass that allows to "catch" child class
|
24
|
|
-#declaration
|
25
|
|
-#@ingroup lodel2_script
|
26
|
|
-#@ingroup lodel2_plugins
|
|
22
|
+## @brief LodelScript metaclass that allows to "catch" child class declaration
|
|
23
|
+# @ingroup lodel2_script
|
|
24
|
+# @ingroup lodel2_plugins
|
27
|
25
|
#
|
28
|
|
-#Automatic action registration on child class declaration
|
|
26
|
+# Automatic action registration on child class declaration
|
29
|
27
|
class MetaLodelScript(type):
|
30
|
28
|
|
|
29
|
+ ##
|
|
30
|
+ # @param name str : action's name
|
|
31
|
+ # @param bases list
|
|
32
|
+ # @param attrs list
|
31
|
33
|
def __init__(self, name, bases, attrs):
|
32
|
|
- #Here we can store all child classes of LodelScript
|
|
34
|
+ # Here we can store all child classes of LodelScript
|
33
|
35
|
super().__init__(name, bases, attrs)
|
34
|
36
|
if len(bases) == 1 and bases[0] == object:
|
35
|
37
|
return
|
|
@@ -49,9 +51,9 @@ action identifier" % name)
|
49
|
51
|
self.argparser_config(self._parser)
|
50
|
52
|
|
51
|
53
|
|
52
|
|
- ##@brief Handles script registration
|
53
|
|
- #@note Script list is maitained in
|
54
|
|
- #lodel.plugin.admin_script.__registered_scripts
|
|
54
|
+ ## @brief Handles script registration
|
|
55
|
+ # @note Script list is maitained in lodel.plugin.admin_script.__registered_scripts
|
|
56
|
+ # @param name str
|
55
|
57
|
def __register_script(self, name):
|
56
|
58
|
if self._action is None:
|
57
|
59
|
logger.warning("%s._action is None. Trying to use class name as \
|
|
@@ -64,57 +66,67 @@ action identifier" % name)
|
64
|
66
|
return '%s : %s' % (self._action, self._description)
|
65
|
67
|
|
66
|
68
|
|
67
|
|
-##@brief Class designed to facilitate custom script writting
|
68
|
|
-#@ingroup lodel2_plugins
|
69
|
|
-#@ingroup lodel2_script
|
|
69
|
+## @brief Class designed to facilitate custom script writting
|
|
70
|
+# @ingroup lodel2_plugins
|
|
71
|
+# @ingroup lodel2_script
|
70
|
72
|
class LodelScript(object, metaclass=MetaLodelScript):
|
71
|
73
|
|
72
|
|
- ##@brief A string to identify the action
|
|
74
|
+ ## @brief A string to identify the action
|
73
|
75
|
_action = None
|
74
|
|
- ##@brief Script descripiton (argparse argument)
|
|
76
|
+ ## @brief Script descripiton (argparse argument)
|
75
|
77
|
_description = None
|
76
|
|
- ##@brief argparse.ArgumentParser instance
|
|
78
|
+ ## @brief argparse.ArgumentParser instance
|
77
|
79
|
_parser = None
|
78
|
80
|
|
79
|
|
- ##@brief No instanciation
|
|
81
|
+ ## @brief No instanciation
|
80
|
82
|
def __init__(self):
|
81
|
83
|
raise NotImplementedError("Static class")
|
82
|
84
|
|
83
|
|
- ##@brief Virtual method. Designed to initialize arguement parser.
|
84
|
|
- #@param parser ArgumentParser : Child class argument parser instance
|
85
|
|
- #@return MUST return the argument parser (NOT SURE ABOUT THAT !! Maybe it \
|
86
|
|
- #works by reference)
|
|
85
|
+ ## @brief Virtual method. Designed to initialize arguement parser.
|
|
86
|
+ # @param parser ArgumentParser : Child class argument parser instance
|
|
87
|
+ # @return MUST return the argument parser (NOT SURE ABOUT THAT !! Maybe it works by reference)
|
|
88
|
+ # @throw LodelScriptError in case it is not implemented in the child class
|
87
|
89
|
@classmethod
|
88
|
90
|
def argparser_config(cls, parser):
|
89
|
91
|
raise LodelScriptError("LodelScript.argparser_config() is a pure \
|
90
|
92
|
virtual method! MUST be implemented by ALL child classes")
|
91
|
93
|
|
92
|
|
- ##@brief Virtual method. Run the script
|
93
|
|
- #@return None or an integer that will be the script return code
|
|
94
|
+ ## @brief Virtual method. Runs the script
|
|
95
|
+ # @param args list
|
|
96
|
+ # @return None or an integer that will be the script return code
|
|
97
|
+ # @throw LodelScriptError in case it is not implemented in the child class
|
94
|
98
|
@classmethod
|
95
|
99
|
def run(cls, args):
|
96
|
100
|
raise LodelScriptError("LodelScript.run() is a pure virtual method. \
|
97
|
101
|
MUST be implemented by ALL child classes")
|
98
|
102
|
|
99
|
|
- ##@brief Called by main_run() to execute a script.
|
|
103
|
+ ## @brief Executes a script
|
|
104
|
+ #
|
|
105
|
+ # Called by main_run()
|
100
|
106
|
#
|
101
|
|
- #Handles argument parsing and then call LodelScript.run()
|
|
107
|
+ # Handles argument parsing and then call LodelScript.run()
|
102
|
108
|
@classmethod
|
103
|
109
|
def _run(cls):
|
104
|
110
|
args = cls._parser.parse_args()
|
105
|
111
|
return cls.run(args)
|
106
|
112
|
|
107
|
|
- ##@brief Append action name to the prog name
|
108
|
|
- #@note See argparse.ArgumentParser() prog argument
|
|
113
|
+ ## @brief Append action name to the prog name
|
|
114
|
+ # @note See argparse.ArgumentParser() prog argument
|
|
115
|
+ # @return str
|
109
|
116
|
@classmethod
|
110
|
117
|
def _prog_name(cls):
|
111
|
118
|
return '%s %s' % (sys.argv[0], cls._action)
|
112
|
119
|
|
113
|
|
- ##@brief Return the default description for an action
|
|
120
|
+ ## @brief Return the default description for an action
|
|
121
|
+ # @return str
|
114
|
122
|
@classmethod
|
115
|
123
|
def _default_description(cls):
|
116
|
124
|
return "Lodel2 script : %s" % cls._action
|
117
|
125
|
|
|
126
|
+ ## @brief handles the help message of an action
|
|
127
|
+ # @param msg str
|
|
128
|
+ # @param return_code int : the default return code is 1
|
|
129
|
+ # @param exit_after bool : default value is True, so that there is an exit after the message printing
|
118
|
130
|
@classmethod
|
119
|
131
|
def help_exit(cls,msg = None, return_code = 1, exit_after = True):
|
120
|
132
|
if not (msg is None):
|
|
@@ -123,15 +135,23 @@ MUST be implemented by ALL child classes")
|
123
|
135
|
if exit_after:
|
124
|
136
|
exit(1)
|
125
|
137
|
|
|
138
|
+
|
|
139
|
+## @brief Registers the script class for an action
|
|
140
|
+# @param action_name str
|
|
141
|
+# @param cls LodelScript
|
126
|
142
|
def script_registration(action_name, cls):
|
127
|
143
|
__registered_scripts[action_name] = cls
|
128
|
144
|
logger.info("New script registered : %s" % action_name)
|
129
|
145
|
|
130
|
|
-##@brief Return a list containing all available actions
|
|
146
|
+
|
|
147
|
+## @brief Returns a list containing all available actions
|
|
148
|
+# @return list
|
131
|
149
|
def _available_actions():
|
132
|
150
|
return [ act for act in __registered_scripts ]
|
133
|
151
|
|
134
|
|
-##@brief Returns default runner argument parser
|
|
152
|
+
|
|
153
|
+## @brief Returns default runner's argument parser
|
|
154
|
+# @param ArgumentParser
|
135
|
155
|
def _default_parser():
|
136
|
156
|
|
137
|
157
|
action_list = _available_actions()
|
|
@@ -150,12 +170,12 @@ def _default_parser():
|
150
|
170
|
action" % sys.argv[0])
|
151
|
171
|
return parser
|
152
|
172
|
|
153
|
|
-##@brief Main function of lodel_admin.py script
|
|
173
|
+## @brief Main function of lodel_admin.py script
|
154
|
174
|
#
|
155
|
|
-#This function take care to run the good plugins and clean sys.argv from
|
156
|
|
-#action name before running script
|
|
175
|
+# This function take care to run the good plugins and clean sys.argv from
|
|
176
|
+# action name before running script
|
157
|
177
|
#
|
158
|
|
-#@return DO NOT RETURN BUT exit() ONCE SCRIPT EXECUTED !!
|
|
178
|
+# @return DO NOT RETURN BUT exit() ONCE SCRIPT EXECUTED !!
|
159
|
179
|
def main_run():
|
160
|
180
|
default_parser = _default_parser()
|
161
|
181
|
if len(sys.argv) == 1:
|
|
@@ -182,43 +202,44 @@ def main_run():
|
182
|
202
|
ret = 0 if ret is None else ret
|
183
|
203
|
exit(ret)
|
184
|
204
|
|
185
|
|
-##@page lodel2_script_doc Lodel2 scripting
|
186
|
|
-#@ingroup lodel2_script
|
|
205
|
+## @page lodel2_script_doc Lodel2 scripting
|
|
206
|
+# @ingroup lodel2_script
|
187
|
207
|
#
|
188
|
|
-#@section lodel2_script_adm Lodel2 instance administration scripts
|
|
208
|
+# @section lodel2_script_adm Lodel2 instance administration scripts
|
189
|
209
|
#
|
190
|
|
-#Lodel2 provides instance administration operation using Makefiles or
|
191
|
|
-#lodel_admin.py script ( see @ref lodel2_instance_admin ).
|
|
210
|
+# In Lodel2, it is possible to administrate instances using either Makefiles
|
|
211
|
+# or lodel_admin.py script ( see @ref lodel2_instance_admin ).
|
192
|
212
|
#
|
193
|
|
-#The lodel_admin.py script take as first option an action. Each action
|
194
|
|
-#correspond to a sub-script with it's own options etc. To get a list
|
195
|
|
-#of all available action run <code>python3 lodel_admin.py -L</code>.
|
|
213
|
+# The lodel_admin.py script takes an action as first argument. Each action
|
|
214
|
+# corresponds to a sub-script with its own options etc. To get a list
|
|
215
|
+# of all available action run <code>python3 lodel_admin.py -L</code>.
|
196
|
216
|
#
|
197
|
|
-#@section lodel2_script_action lodel_admin.py actions
|
|
217
|
+# @section lodel2_script_action lodel_admin.py actions
|
198
|
218
|
#
|
199
|
|
-#Action implementation is done by class inheritance. To create a new action
|
200
|
|
-#write a @ref lodel.plugin.scripts.LodelScript "LodelScript" derived class (
|
201
|
|
-#see @ref lodel.plugin.core_scripts "core_scripts.py" file as example )
|
|
219
|
+# Action implementation is done by class inheritance. To create a new action,
|
|
220
|
+# one has to write a @ref lodel.plugin.scripts.LodelScript "LodelScript"
|
|
221
|
+# derived class ( see @ref lodel.plugin.core_scripts "core_scripts.py" file
|
|
222
|
+# as example )
|
202
|
223
|
#
|
203
|
|
-#@subsection lodel2_script_inheritance LodelScript inheritance
|
|
224
|
+# @subsection lodel2_script_inheritance LodelScript inheritance
|
204
|
225
|
#
|
205
|
|
-#In order to implement properly a new action you have to write a new
|
206
|
|
-#@ref lodel.plugin.scripts.LodelScript "LodelScript" derivated class.
|
207
|
|
-#Some methods and attributes are mandatory to write a fully functionnal
|
208
|
|
-#derivated class. Here is a list :
|
|
226
|
+# In order to implement properly a new action you have to write a new
|
|
227
|
+# @ref lodel.plugin.scripts.LodelScript "LodelScript" derivated class.
|
|
228
|
+# Some methods and attributes are mandatory to write a fully functionnal
|
|
229
|
+# derivated class. Here is a list :
|
209
|
230
|
#
|
210
|
|
-#- mandatory methods
|
211
|
|
-# - @ref plugin.scripts.LodelScript.argparser_config() "argparser_config()" :
|
|
231
|
+# - mandatory methods
|
|
232
|
+# - @ref plugin.scripts.LodelScript.argparser_config() "argparser_config()" :
|
212
|
233
|
#(classmethod) initialize argparse.Parser
|
213
|
|
-# - @ref plugin.scripts.LodelScript.run() "run()" : (classmethod) contains the
|
|
234
|
+# - @ref plugin.scripts.LodelScript.run() "run()" : (classmethod) contains the
|
214
|
235
|
#code that runs to perform the action
|
215
|
|
-#- mandatory attributes
|
216
|
|
-# - @ref plugin.scripts.LodelScript::_action "_action" : (class attribute)
|
|
236
|
+# - mandatory attributes
|
|
237
|
+# - @ref plugin.scripts.LodelScript::_action "_action" : (class attribute)
|
217
|
238
|
#stores action name
|
218
|
|
-# - @ref plugin.scripts.LodelScript::_description "_description" : (class
|
|
239
|
+# - @ref plugin.scripts.LodelScript::_description "_description" : (class
|
219
|
240
|
#attribute) sotres a short action description
|
220
|
241
|
#
|
221
|
|
-#@note On script's action registration : once child class is written you only
|
222
|
|
-#need to import it to trigger script's action registration (see
|
223
|
|
-#@ref plugin.scripts.MetaLodelScript )
|
|
242
|
+# @note On script's action registration : once child class is written you only
|
|
243
|
+# need to import it to trigger script's action registration (see
|
|
244
|
+# @ref plugin.scripts.MetaLodelScript )
|
224
|
245
|
#
|