|
@@ -0,0 +1,162 @@
|
|
1
|
+import argparse
|
|
2
|
+import sys
|
|
3
|
+
|
|
4
|
+from lodel import logger
|
|
5
|
+from lodel.exceptions import *
|
|
6
|
+
|
|
7
|
+##@brief Stores registered scripts
|
|
8
|
+__registered_scripts = dict()
|
|
9
|
+
|
|
10
|
+##@brief LodelScript metaclass that allows to "catch" child class
|
|
11
|
+#declaration
|
|
12
|
+#
|
|
13
|
+#Automatic script registration on child class declaration
|
|
14
|
+class MetaLodelScript(type):
|
|
15
|
+
|
|
16
|
+ def __init__(self, name, bases, attrs):
|
|
17
|
+ #Here we can store all child classes of LodelScript
|
|
18
|
+ super().__init__(name, bases, attrs)
|
|
19
|
+ if len(bases) == 1 and bases[0] == object:
|
|
20
|
+ print("Dropped : ", name, bases)
|
|
21
|
+ return
|
|
22
|
+
|
|
23
|
+ self.__register_script(name)
|
|
24
|
+ #_action initialization
|
|
25
|
+ if self._action is None:
|
|
26
|
+ logger.warning("%s._action is None. Trying to use class name as \
|
|
27
|
+action identifier" % name)
|
|
28
|
+ self._action = name
|
|
29
|
+ self._action = self._action.lower()
|
|
30
|
+ if self._description is None:
|
|
31
|
+ self._description = self._default_description()
|
|
32
|
+ self._parser = argparse.ArgumentParser(
|
|
33
|
+ prog = self._prog_name(),
|
|
34
|
+ description = self._description)
|
|
35
|
+ self.argparser_config(self._parser)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+ ##@brief Handles script registration
|
|
39
|
+ #@note Script list is maitained in
|
|
40
|
+ #lodel.plugin.admin_script.__registered_scripts
|
|
41
|
+ def __register_script(self, name):
|
|
42
|
+ if self._action is None:
|
|
43
|
+ logger.warning("%s._action is None. Trying to use class name as \
|
|
44
|
+action identifier" % name)
|
|
45
|
+ self._action = name
|
|
46
|
+ self._action = self._action.lower()
|
|
47
|
+ script_registration(self._action, self)
|
|
48
|
+
|
|
49
|
+ def __str__(self):
|
|
50
|
+ return '%s : %s' % (self._action, self._description)
|
|
51
|
+
|
|
52
|
+class LodelScript(object, metaclass=MetaLodelScript):
|
|
53
|
+
|
|
54
|
+ ##@brief A string to identify the action
|
|
55
|
+ _action = None
|
|
56
|
+ ##@brief Script descripiton (argparse argument)
|
|
57
|
+ _description = None
|
|
58
|
+ ##@brief argparse.ArgumentParser instance
|
|
59
|
+ _parser = None
|
|
60
|
+
|
|
61
|
+ ##@brief No instanciation
|
|
62
|
+ def __init__(self):
|
|
63
|
+ raise NotImplementedError("Static class")
|
|
64
|
+
|
|
65
|
+ ##@brief Virtual method. Designed to initialize arguement parser.
|
|
66
|
+ #@param argparser ArgumentParser : Child class argument parser instance
|
|
67
|
+ #@return MUST return the argument parser (NOT SURE ABOUT THAT !! Maybe it \
|
|
68
|
+ #works by reference)
|
|
69
|
+ @classmethod
|
|
70
|
+ def argparser_config(cls, parser):
|
|
71
|
+ raise LodelScriptError("LodelScript.argparser_config() is a pure \
|
|
72
|
+virtual method! MUST be implemented by ALL child classes")
|
|
73
|
+
|
|
74
|
+ ##@brief Virtual method. Run the script
|
|
75
|
+ #@return None or an integer that will be the script return code
|
|
76
|
+ @classmethod
|
|
77
|
+ def run(cls, args):
|
|
78
|
+ raise LodelScriptError("LodelScript.run() is a pure virtual method. \
|
|
79
|
+MUST be implemented by ALL child classes")
|
|
80
|
+
|
|
81
|
+ ##@brief Called by main_run() to execute a script.
|
|
82
|
+ #
|
|
83
|
+ #Handles argument parsing and then call LodelScript.run()
|
|
84
|
+ @classmethod
|
|
85
|
+ def _run(cls):
|
|
86
|
+ args = cls._parser.parse_args()
|
|
87
|
+ return cls.run(args)
|
|
88
|
+
|
|
89
|
+ ##@brief Append action name to the prog name
|
|
90
|
+ #@note See argparse.ArgumentParser() prog argument
|
|
91
|
+ @classmethod
|
|
92
|
+ def _prog_name(cls):
|
|
93
|
+ return '%s %s' % (sys.argv[0], cls._action)
|
|
94
|
+
|
|
95
|
+ ##@brief Return the default description for an action
|
|
96
|
+ @classmethod
|
|
97
|
+ def _default_description(cls):
|
|
98
|
+ return "Lodel2 script : %s" % cls._action
|
|
99
|
+
|
|
100
|
+ @classmethod
|
|
101
|
+ def help_exit(cls,msg = None, return_code = 1, exit_after = True):
|
|
102
|
+ if not (msg is None):
|
|
103
|
+ print(msg, file=sys.stderr)
|
|
104
|
+ cls._parser.print_help()
|
|
105
|
+ if exit_after:
|
|
106
|
+ exit(1)
|
|
107
|
+
|
|
108
|
+def script_registration(action_name, cls):
|
|
109
|
+ __registered_scripts[action_name] = cls
|
|
110
|
+ logger.info("New script registered : %s" % action_name)
|
|
111
|
+
|
|
112
|
+##@brief Return a list containing all available actions
|
|
113
|
+def _available_actions():
|
|
114
|
+ return [ act for act in __registered_scripts ]
|
|
115
|
+
|
|
116
|
+##@brief Returns default runner argument parser
|
|
117
|
+def _default_parser():
|
|
118
|
+
|
|
119
|
+ action_list = _available_actions()
|
|
120
|
+ if len(action_list) > 0:
|
|
121
|
+ action_list = ', '.join(sorted(action_list))
|
|
122
|
+ else:
|
|
123
|
+ action_list = 'NO SCRIPT FOUND !'
|
|
124
|
+
|
|
125
|
+ parser = argparse.ArgumentParser(description = "Lodel2 script runner")
|
|
126
|
+ parser.add_argument('-L', '--list-actions', action='store_true',
|
|
127
|
+ default=False, help="List available actions")
|
|
128
|
+ parser.add_argument('action', metavar="ACTION", type=str,
|
|
129
|
+ help="One of the following actions : %s" % action_list, nargs='?')
|
|
130
|
+ parser.add_argument('option', metavar="OPTIONS", type=str, nargs='*',
|
|
131
|
+ help="Action options. Use %s ACTION -h to have help on a specific \
|
|
132
|
+action" % sys.argv[0])
|
|
133
|
+ return parser
|
|
134
|
+
|
|
135
|
+##@brief Main function of lodel_admin.py script
|
|
136
|
+#
|
|
137
|
+#This function take care to run the good plugins and clean sys.argv from
|
|
138
|
+#action name before running script
|
|
139
|
+#
|
|
140
|
+#@return DO NOT RETURN BUT exit() ONCE SCRIPT EXECUTED !!
|
|
141
|
+def main_run():
|
|
142
|
+ default_parser = _default_parser()
|
|
143
|
+ if len(sys.argv) == 1:
|
|
144
|
+ default_parser.print_help()
|
|
145
|
+ exit(1)
|
|
146
|
+ args = default_parser.parse_args()
|
|
147
|
+ if args.list_actions:
|
|
148
|
+ print("Available actions :")
|
|
149
|
+ for sname in sorted(__registered_scripts.keys()):
|
|
150
|
+ print("\t- %s" % __registered_scripts[sname])
|
|
151
|
+ exit(0)
|
|
152
|
+ #preparing sys.argv (deleting action)
|
|
153
|
+ action = sys.argv[1].lower()
|
|
154
|
+ del(sys.argv[1])
|
|
155
|
+ if action not in __registered_scripts:
|
|
156
|
+ print("Unknow action '%s'\n" % action, file=sys.stderr)
|
|
157
|
+ default_parser.print_help()
|
|
158
|
+ exit(1)
|
|
159
|
+ script = __registered_scripts[action]
|
|
160
|
+ ret = script._run()
|
|
161
|
+ ret = 0 if ret is None else ret
|
|
162
|
+ exit(ret)
|