Blame view

pyros.py 15.2 KB
efbcac9e   Etienne Pallier   1st version of NE...
1
2
#!/usr/bin/env python3

ddb06a2a   Etienne Pallier   new pyros2 launch...
3
4
5

import argparse
import fileinput
efbcac9e   Etienne Pallier   1st version of NE...
6
import os
efbcac9e   Etienne Pallier   1st version of NE...
7
import platform
efbcac9e   Etienne Pallier   1st version of NE...
8
import signal
ddb06a2a   Etienne Pallier   new pyros2 launch...
9
10
11
import subprocess
import sys
import time
efbcac9e   Etienne Pallier   1st version of NE...
12

1f6376dd   Etienne Pallier   pyros2 refactor &...
13

1f6376dd   Etienne Pallier   pyros2 refactor &...
14
15
16
17
18
19
20

"""
*****************************************************************
******************** GENERAL CONSTANTS **************************
*****************************************************************
"""

a3015e31   Etienne Pallier   table des commandes
21
22
23
DEBUG = True

INIT_FIXTURE = "initial_fixture.json"
01348735   Etienne Pallier   Bugfix pyros.py s...
24

eac3e2ab   Etienne Pallier   New AgentX
25
AGENTS = {
112f32f4   Etienne Pallier   start_agent_agent...
26
27
    #"agentX" : "majordome", 
    "agentX" : "agent", 
eac3e2ab   Etienne Pallier   New AgentX
28
29
30
31
32
33
34
    "webserver" : "webserver", 
    "monitoring" : "monitoring", 
    "majordome" : "majordome", 
    "scheduler" : "scheduler", 
    "alert_manager" : "alert_manager"
}
#AGENTS = ["agentX", "webserver", "monitoring", "majordome", "scheduler", "alert_manager"]
1f6376dd   Etienne Pallier   pyros2 refactor &...
35
36
37
38
39
40
41
42
#AGENTS = ["all", "webserver", "monitoring", "majordome", "scheduler", "alert"]

#COMMANDS = {"install": [], "start": AGENTS, "stop": AGENTS}

IS_WINDOWS = platform.system() == "Windows"

my_abs_path = os.path.dirname(os.path.realpath(__file__))
if IS_WINDOWS:
a3015e31   Etienne Pallier   table des commandes
43
    b_in_dir = "Scripts"
1f6376dd   Etienne Pallier   pyros2 refactor &...
44
45
46
47
    PYTHON = "python.exe"
    # should also be ok:
    #PYTHON = "python"
else:
a3015e31   Etienne Pallier   table des commandes
48
    b_in_dir = "bin"
1f6376dd   Etienne Pallier   pyros2 refactor &...
49
50
51
52
53
54
55
    PYTHON = "python3"
    # ok only from venv:
    #PYTHON = "python"
VENV_BIN = (
    my_abs_path
    + os.sep + "private"
    + os.sep + "venv_py3_pyros"
a3015e31   Etienne Pallier   table des commandes
56
    + os.sep + b_in_dir
1f6376dd   Etienne Pallier   pyros2 refactor &...
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
    + os.sep + PYTHON
)

class Colors:
    HEADER = "\033[95m"
    BLUE = "\033[94m"
    GREEN = "\033[92m"
    WARNING = "\033[93m"
    FAIL = "\033[91m"
    ENDC = "\033[0m"
    BOLD = "\033[1m"
    UNDERLINE = "\033[4m"




ddb06a2a   Etienne Pallier   new pyros2 launch...
73
74
75
76
77
# First, install the click package !!!
#import fire
try:
    import click # https://click.palletsprojects.com
except:
1f6376dd   Etienne Pallier   pyros2 refactor &...
78
79
    #pip = "pip" if platform.system() == "Windows" else "pip3"
    pip = "pip" if IS_WINDOWS else "pip3"
ddb06a2a   Etienne Pallier   new pyros2 launch...
80
81
82
83
84
85
86
87
88
89
90
    process = subprocess.Popen(pip + " install --upgrade click", shell=True)
    process.wait()
    if process.returncode == 0:
        print("click package installation successfull")
        # self.addExecuted(self.current_command, command)
        import click
    else:
        print("click package installation failed")
        # self.addError(self.current_command, command)
        
    
efbcac9e   Etienne Pallier   1st version of NE...
91

efbcac9e   Etienne Pallier   1st version of NE...
92
93


efbcac9e   Etienne Pallier   1st version of NE...
94

1f6376dd   Etienne Pallier   pyros2 refactor &...
95
96
97
98
99
"""
**************************************************************************
******************** GENERAL AND UTIL FUNCTIONS **************************
**************************************************************************
"""
efbcac9e   Etienne Pallier   1st version of NE...
100

a3015e31   Etienne Pallier   table des commandes
101
def _in_dir(dirname: str = ""):
b2e9866d   Etienne Pallier   bugfix start_agen...
102
    return os.path.basename(os.getcwd()) == dirname
efbcac9e   Etienne Pallier   1st version of NE...
103

efbcac9e   Etienne Pallier   1st version of NE...
104

a3015e31   Etienne Pallier   table des commandes
105
def _in_abs_dir(dirname: str = ""):
b2e9866d   Etienne Pallier   bugfix start_agen...
106
    return os.getcwd() == dirname
efbcac9e   Etienne Pallier   1st version of NE...
107
108


a3015e31   Etienne Pallier   table des commandes
109
110
111
def die(msg: str = ""):
    print()
    print("...ERROR...")
b2e9866d   Etienne Pallier   bugfix start_agen...
112
    print(msg)
a3015e31   Etienne Pallier   table des commandes
113
    print()
b2e9866d   Etienne Pallier   bugfix start_agen...
114
    exit(1)
a3015e31   Etienne Pallier   table des commandes
115
    
efbcac9e   Etienne Pallier   1st version of NE...
116

1f6376dd   Etienne Pallier   pyros2 refactor &...
117
118
#TODO: implement is_async
def execProcess(command, from_venv=False, is_async=False):
67f74b9e   Etienne Pallier   cleanup
119
120
    from_venv_str = " from venv ("+VENV_BIN+")" if from_venv else ""
    printFullTerm(Colors.BLUE, "Executing command" + " [" + command + "]" + from_venv_str)
1f6376dd   Etienne Pallier   pyros2 refactor &...
121
    if from_venv: command = VENV_BIN+' ' + command
b2e9866d   Etienne Pallier   bugfix start_agen...
122
123
124
125
126
127
128
129
    process = subprocess.Popen(command, shell=True)
    process.wait()
    if process.returncode == 0:
        printFullTerm(Colors.GREEN, "Process executed successfully")
        # self.addExecuted(self.current_command, command)
    else:
        printFullTerm(Colors.WARNING, "Process execution failed")
        # self.addError(self.current_command, command)
f4fd20f9   Etienne Pallier   pyros.py script i...
130
131
    #return process.returncode
    return True if process.returncode==0 else False
efbcac9e   Etienne Pallier   1st version of NE...
132

1f6376dd   Etienne Pallier   pyros2 refactor &...
133
def execProcessFromVenv(command:str):
f4fd20f9   Etienne Pallier   pyros.py script i...
134
    return execProcess(command, from_venv=True)
efbcac9e   Etienne Pallier   1st version of NE...
135

1f6376dd   Etienne Pallier   pyros2 refactor &...
136
137
#TODO: fusionner dans execProcess avec param is_async
def execProcessFromVenvAsync(command:str):
b2e9866d   Etienne Pallier   bugfix start_agen...
138
139
140
141
142
143
144
145
146
147
    args = command.split()
    printFullTerm(
        Colors.BLUE, "Executing command from venv [" + str(" ".join(args[1:])) + "]"
    )
    p = subprocess.Popen(args)
    subproc.append((p, " ".join(args[1:])))
    printFullTerm(Colors.GREEN, "Process launched successfully")
    # self.addExecuted(self.current_command, str(' '.join(args[1:])))
    # p.wait()
    return p
efbcac9e   Etienne Pallier   1st version of NE...
148

efbcac9e   Etienne Pallier   1st version of NE...
149

b2e9866d   Etienne Pallier   bugfix start_agen...
150
def printColor(color: Colors, message, file=sys.stdout, eol=os.linesep, forced=False):
1f6376dd   Etienne Pallier   pyros2 refactor &...
151
    #system = platform.system()
b2e9866d   Etienne Pallier   bugfix start_agen...
152
153
    """
    if (self.disp == False and forced == False):
efbcac9e   Etienne Pallier   1st version of NE...
154
        return 0
b2e9866d   Etienne Pallier   bugfix start_agen...
155
    """
1f6376dd   Etienne Pallier   pyros2 refactor &...
156
157
    #if system == "Windows":
    if IS_WINDOWS:
b2e9866d   Etienne Pallier   bugfix start_agen...
158
159
160
161
        print(message, file=file, end=eol)
    else:
        print(color + message + Colors.ENDC, file=file, end=eol)
    return 0
efbcac9e   Etienne Pallier   1st version of NE...
162
163


b2e9866d   Etienne Pallier   bugfix start_agen...
164
def printFullTerm(color: Colors, string: str):
1f6376dd   Etienne Pallier   pyros2 refactor &...
165
    #system = platform.system()
b2e9866d   Etienne Pallier   bugfix start_agen...
166
167
168
169
170
171
172
173
174
    columns = 100
    row = 1000
    disp = True
    value = int(columns / 2 - len(string) / 2)
    printColor(color, "-" * value, eol="")
    printColor(color, string, eol="")
    value += len(string)
    printColor(color, "-" * (columns - value))
    return 0
efbcac9e   Etienne Pallier   1st version of NE...
175

efbcac9e   Etienne Pallier   1st version of NE...
176

f4fd20f9   Etienne Pallier   pyros.py script i...
177
178


ddb06a2a   Etienne Pallier   new pyros2 launch...
179
180

"""
1f6376dd   Etienne Pallier   pyros2 refactor &...
181
182
183
********************************************************************************
******************** CLI COMMANDS DEFINITION (click format) ********************
********************************************************************************
ddb06a2a   Etienne Pallier   new pyros2 launch...
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
"""

'''
_global_test_options = [
    click.option('--test', '-t', is_flag=True, help="don't do it for real, just show what it would do"),
    click.option('--verbose', '-v', 'verbosity', flag_value=2, default=1, help='Verbose output'),
    click.option('--quiet', '-q', 'verbosity', flag_value=0, help='Minimal output'),
    #click.option('--fail-fast', '--failfast', '-f', 'fail_fast', is_flag=True, default=False, help='Stop on failure'),
]
def global_test_options(func):
    for option in reversed(_global_test_options):
        func = option(func)
    return func
'''

GLOBAL_OPTIONS = {}
def verbose_mode(): return GLOBAL_OPTIONS["verbose"]
def test_mode(): return GLOBAL_OPTIONS["test"]

@click.group()
@click.option('--test', '-t', is_flag=True, help="don't do it for real, just show what it would do")
@click.option('--verbose', '-v', is_flag=True, help='Verbose output')
#@click.option('--verbose', '-v', 'verbosity', flag_value=2, default=1, help='Verbose output'),
#@click.option('--quiet', '-q', 'verbosity', flag_value=0, help='Minimal output'),
#@click.option('--fail-fast', '--failfast', '-f', 'fail_fast', is_flag=True, default=False, help='Stop on failure'),
def pyros_launcher(test, verbose):
    #pass
    if test: click.echo('Test mode')
    if verbose: click.echo('Verbose mode')
    GLOBAL_OPTIONS["test"] = test
    GLOBAL_OPTIONS["verbose"] = verbose



af5d8b11   Etienne Pallier   Nouveau shell pyr...
218
219
220
221
222
@pyros_launcher.command(help="Run a pyros shell (django included)")
#@global_test_options
def shell():
    print()
    print("Launching a pyros shell")
0d065f79   Etienne Pallier   new migration fil...
223
224
225
    print("From this shell, type 'from common.models import *' to import all the pyros objects")
    print("Then, you can create any pyros object just by typing its name")
    print("For example, to create a AgentsSurvey object, type 'agent_survey = AgentsSurvey()'")
af5d8b11   Etienne Pallier   Nouveau shell pyr...
226
227
228
    print("See documentation, chapter '9.6 - Play with the pyros objects' for more details")
    print("Type 'exit()' to quit")
    print()
12a0badc   Etienne Pallier   update
229
    os.chdir("src/")
af5d8b11   Etienne Pallier   Nouveau shell pyr...
230
    # execProcess("python install.py install")
12a0badc   Etienne Pallier   update
231
    if not test_mode(): execProcessFromVenv("manage.py shell")
af5d8b11   Etienne Pallier   Nouveau shell pyr...
232
    # Go back to the initial dir
12a0badc   Etienne Pallier   update
233
    os.chdir("../")
af5d8b11   Etienne Pallier   Nouveau shell pyr...
234
235
236
    return True


fcfc6200   Etienne Pallier   ajout nouvelle co...
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
@pyros_launcher.command(help="Run a database (Mysql) shell")
def dbshell():
    print()
    print("Launching a database (mysql) shell")
    print("From this shell, type 'use database pyros;' to select the pyros database")
    print("Then type 'show tables;' to see all the pyros tables")
    print("Then for example, type 'select * from config;' to see the content of the 'config' table")
    print("Type 'exit' to quit")
    print()
    # execProcess("python install.py install")
    if not test_mode(): execProcessFromVenv("src/manage.py dbshell")
    # Go back to the initial dir
    return True


af5d8b11   Etienne Pallier   Nouveau shell pyr...
252
@pyros_launcher.command(help="Install the pyros software")
ddb06a2a   Etienne Pallier   new pyros2 launch...
253
254
#@global_test_options
def install():
1f6376dd   Etienne Pallier   pyros2 refactor &...
255
    print("Running install command")
ddb06a2a   Etienne Pallier   new pyros2 launch...
256
257
258
    #if test_mode(): print("in test mode")
    # self.execProcess("python3 install/install.py install")
    # if (os.path.basename(os.getcwd()) != "private"):
ddb06a2a   Etienne Pallier   new pyros2 launch...
259
    start_dir = os.getcwd()
a3015e31   Etienne Pallier   table des commandes
260
    os.chdir("install/") ; _in_dir("install") or die("Bad dir")
1f6376dd   Etienne Pallier   pyros2 refactor &...
261
262
    # execProcess("python install.py install")
    if not test_mode(): execProcess(PYTHON + " install.py")
ddb06a2a   Etienne Pallier   new pyros2 launch...
263
264
    # cd -
    # os.chdir("-")
a3015e31   Etienne Pallier   table des commandes
265
    os.chdir(start_dir) ; _in_abs_dir(start_dir) or die("Bad dir")
ddb06a2a   Etienne Pallier   new pyros2 launch...
266
267
268
    # return 0
    return True

f4fd20f9   Etienne Pallier   pyros.py script i...
269
270
271


    
01348735   Etienne Pallier   Bugfix pyros.py s...
272
273
274
275
276
277
'''
TODO:
'''
@pyros_launcher.command(help="Update the pyros software (git pull + update DB if necessary)")
def update():
    print("Running update command")
f4fd20f9   Etienne Pallier   pyros.py script i...
278
279
280
281
    #res = _gitpull()
    _gitpull() or die()
    #if not res: return False
    _updatedb() or die()
01348735   Etienne Pallier   Bugfix pyros.py s...
282
283
    return True

01348735   Etienne Pallier   Bugfix pyros.py s...
284

f4fd20f9   Etienne Pallier   pyros.py script i...
285
286
287
288
289
def _gitpull():
    print("-- running git pull")
    GIT = "git.exe" if IS_WINDOWS else "git"
    if not test_mode(): return execProcess(f"{GIT} pull")
    return True
01348735   Etienne Pallier   Bugfix pyros.py s...
290

f4fd20f9   Etienne Pallier   pyros.py script i...
291
292
293
294
295
296
297
#@pyros_launcher.command(help="Update the pyros database")
def _updatedb():
    print("-- update db (make migrations + migrate)")
    if not test_mode() :
        _makemigrations() or die()
        _migrate() or die()
    return True
ddb06a2a   Etienne Pallier   new pyros2 launch...
298
299


f4fd20f9   Etienne Pallier   pyros.py script i...
300
301
302
303
304
305
306
@pyros_launcher.command(help="Update the pyros database and fill it with initial fixture data")
def initdb():
    if not test_mode():
        #updatedb()
        _makemigrations()
        _migrate()
        _loaddata()
ddb06a2a   Etienne Pallier   new pyros2 launch...
307
308
309
    return True


f4fd20f9   Etienne Pallier   pyros.py script i...
310

ddb06a2a   Etienne Pallier   new pyros2 launch...
311
312
313
314
315
316
317
318
@pyros_launcher.command(help="Launch an agent")
#@global_test_options
@click.argument('agent')
@click.option('--configfile', '-c', help='the configuration file to be used')
#@click.option('--format', '-f', type=click.Choice(['html', 'xml', 'text']), default='html', show_default=True)
#@click.option('--port', default=8000)
#def start(agent:str, configfile:str, test, verbosity):
def start(agent:str, configfile:str):
1f6376dd   Etienne Pallier   pyros2 refactor &...
319
    print("Running start command")
112f32f4   Etienne Pallier   start_agent_agent...
320
321
322
323
    if configfile: 
        print("With config file", configfile)
    else: 
        configfile = '' 
ddb06a2a   Etienne Pallier   new pyros2 launch...
324
325
    #if test_mode(): print("in test mode")
    #if verbose_mode(): print("in verbose mode")
f4fd20f9   Etienne Pallier   pyros.py script i...
326
    if not _check_agent(agent): return
a3015e31   Etienne Pallier   table des commandes
327
    # VENV_BIN = 'private/venv_py3_pyros' +  os.sep + self.b_in_dir + os.sep + self.bin_name
b2e9866d   Etienne Pallier   bugfix start_agen...
328

b2e9866d   Etienne Pallier   bugfix start_agen...
329

b2e9866d   Etienne Pallier   bugfix start_agen...
330
331
332
333
334
335
336
337
338
    # Start Agents
    """            
    if agent=="majordome" or agent=="all":
        from majordome.tasks import Majordome
        Majordome().run()
    if agent=="alert_manager" or agent=="all":
        from alert_manager.tasks import AlertListener
        AlertListener().run()
    """
eac3e2ab   Etienne Pallier   New AgentX
339
    for agent_name,agent_folder in AGENTS.items():
112f32f4   Etienne Pallier   start_agent_agent...
340

ddb06a2a   Etienne Pallier   new pyros2 launch...
341
        if agent in ("all", agent_name) :
112f32f4   Etienne Pallier   start_agent_agent...
342
343
            
            # Default case, launch agentX
630dd6e4   Etienne Pallier   Renommé script st...
344
345
346
347
348
349
350
351
352
353
354
355
356
            #if agent_name == "agentX":

            # execProcessFromVenvAsync(VENV_BIN + " manage.py runserver")
            print(VENV_BIN)
            print("Launching agent", agent_name, "...")
            #if not test_mode(): execProcess(VENV_BIN + " manage.py runserver")
            #if not test_mode(): execProcessFromVenv("start_agent_" + agent_name + ".py " + configfile)
            cmd = "start_agent.py " + agent_name + " " + configfile
            if agent_name == "webserver": 
                cmd = "manage.py runserver"
                os.chdir("src")
            #if not test_mode(): execProcessFromVenv("start_agent.py " + agent_name + " " + configfile)
            if not test_mode(): execProcessFromVenv(cmd)
f4fd20f9   Etienne Pallier   pyros.py script i...
357
            # self._change_dir("..")
ddb06a2a   Etienne Pallier   new pyros2 launch...
358

630dd6e4   Etienne Pallier   Renommé script st...
359
            '''
ddb06a2a   Etienne Pallier   new pyros2 launch...
360
361
            # Any other agent
            else:
112f32f4   Etienne Pallier   start_agent_agent...
362
                # Go into src/
f4fd20f9   Etienne Pallier   pyros.py script i...
363
                # self._change_dir("src")
112f32f4   Etienne Pallier   start_agent_agent...
364
365
366
                os.chdir("src")
                # print("Current directory : " + str(os.getcwd()))
                if agent_name != "webserver": os.chdir(agent_folder)
1f6376dd   Etienne Pallier   pyros2 refactor &...
367
                # self.execProcessFromVenvAsync(self.VENV_BIN + ' start_agent_'+agent+'.py')
112f32f4   Etienne Pallier   start_agent_agent...
368
                #print(VENV_BIN)
ddb06a2a   Etienne Pallier   new pyros2 launch...
369
                print("Launching agent", agent_name, "...")
1f6376dd   Etienne Pallier   pyros2 refactor &...
370
                #if not test_mode(): execProcess(VENV_BIN + " start_agent_" + agent_name + ".py")
3ac24fdf   Etienne Pallier   AgentX v0.3 & bug...
371
372
373
                #TODO:
                # - start_agent agent_name (1 script unique)
                # - start_agent -c configfile
112f32f4   Etienne Pallier   start_agent_agent...
374
375
376
377
                cmd = "start_agent_" + agent_name + ".py " + configfile
                # Django default dev web server
                if agent_name == "webserver": cmd = "manage.py runserver"
                if not test_mode(): execProcessFromVenv(cmd)
ddb06a2a   Etienne Pallier   new pyros2 launch...
378
                # Go back to src/
f4fd20f9   Etienne Pallier   pyros.py script i...
379
                # self._change_dir('..')
ddb06a2a   Etienne Pallier   new pyros2 launch...
380
                os.chdir("..")
630dd6e4   Etienne Pallier   Renommé script st...
381
            '''
ddb06a2a   Etienne Pallier   new pyros2 launch...
382
                
b2e9866d   Etienne Pallier   bugfix start_agen...
383
    # Go back to root folder (/)
f4fd20f9   Etienne Pallier   pyros.py script i...
384
    # self._change_dir('..')
b2e9866d   Etienne Pallier   bugfix start_agen...
385
    os.chdir("..")
112f32f4   Etienne Pallier   start_agent_agent...
386
    return True
b2e9866d   Etienne Pallier   bugfix start_agen...
387
388


b2e9866d   Etienne Pallier   bugfix start_agen...
389
390


ddb06a2a   Etienne Pallier   new pyros2 launch...
391
392
393
@pyros_launcher.command(help="Kill an agent")
@click.argument('agent')
def stop(agent):
1f6376dd   Etienne Pallier   pyros2 refactor &...
394
    print("Running stop command")
f4fd20f9   Etienne Pallier   pyros.py script i...
395
    if not _check_agent(agent): return
ddb06a2a   Etienne Pallier   new pyros2 launch...
396
397
398



f4fd20f9   Etienne Pallier   pyros.py script i...
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
"""
********************************************************************************
******************** PRIVATE FUNCTIONS DEFINITION ******************************
********************************************************************************
"""

def _migrate():
    _change_dir("src")
    res = execProcessFromVenv("manage.py migrate")
    _change_dir("..")
    return res

def _makemigrations():
    _change_dir("src")
    #execProcessFromVenv(self.venv_bin + " manage.py makemigrations")
    res = execProcessFromVenv("manage.py makemigrations")
    print("res is", res)
    _change_dir("..")
    return res

#TODO: mettre la fixture en date naive (sans time zone)
def _loaddata():
    _change_dir("src")
a3015e31   Etienne Pallier   table des commandes
422
423
    #execProcessFromVenv(self.venv_bin + " manage.py loaddata misc" + os.sep + "fixtures" + os.sep + self.INIT_FIXTURE)
    res = execProcessFromVenv("manage.py loaddata misc" + os.sep + "fixtures" + os.sep + INIT_FIXTURE)
f4fd20f9   Etienne Pallier   pyros.py script i...
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
    _change_dir("..")
    return res

def _change_dir(path):
    if DEBUG: print("Moving to : " + path)
    os.chdir(path)
    if DEBUG: print("Current directory : " + str(os.getcwd()))



def _check_agent(agent):
    # Check that agent exists
    if agent not in AGENTS.keys() and agent != "all": 
        print("This agent does not exist")
        print("Here is the allowed list of agents:")
        print("- all => will launch ALL agents")
        for agent_name in AGENTS.keys(): print('-',agent_name)
        return False
    return True



"""
********************************************************************************
********************************* main() FUNCTION ******************************
********************************************************************************
"""
ddb06a2a   Etienne Pallier   new pyros2 launch...
451
452
453
454
def main():
    pyros_launcher()
    
    
1f6376dd   Etienne Pallier   pyros2 refactor &...
455
# AVIRER
ddb06a2a   Etienne Pallier   new pyros2 launch...
456
457
458
459
460
461
462
463
464
465
#@click.command()
#@click.argument('start')
def oldmain():
    '''
    cli()
    return
    '''
    
    #fire.Fire(Commands)
    #return
efbcac9e   Etienne Pallier   1st version of NE...
466

b2e9866d   Etienne Pallier   bugfix start_agen...
467
468
469
470
471
472
473
474
475
476
477
    # if len(sys.argv) == 3 and sys.argv[1].startswith("simulator"): SIMULATOR_CONFIG_FILE = sys.argv[2]
    # print(sys.argv)
    """
    system = platform.system()
    columns = 100
    row = 1000
    disp = True
    """

    # Read command
    if len(sys.argv) <= 1:
a3015e31   Etienne Pallier   table des commandes
478
        die("You must give a command name")
b2e9866d   Etienne Pallier   bugfix start_agen...
479
480
    command = sys.argv[1]
    if not command in COMMANDS.keys():
a3015e31   Etienne Pallier   table des commandes
481
        die("This command does not exist")
b2e9866d   Etienne Pallier   bugfix start_agen...
482
483
484
485
486
487
    command_all_args = COMMANDS[command]

    # Read command args if should be
    command_arg = None
    if command_all_args:
        if len(sys.argv) <= 2:
a3015e31   Etienne Pallier   table des commandes
488
            die("This command should be given an argument")
b2e9866d   Etienne Pallier   bugfix start_agen...
489
490
        command_arg = sys.argv[2]
        if not command_arg in command_all_args:
a3015e31   Etienne Pallier   table des commandes
491
            die("This argument does not exist for command " + command)
b2e9866d   Etienne Pallier   bugfix start_agen...
492
493
494
495
496
497
498
499
500
501
502

    print("Executing command", command)
    if command_arg:
        print("with arg", command_arg)

    # command(command_arg)
    if command_arg:
        globals()[command](command_arg)
    else:
        globals()[command]()
    # sys.exit(pyros.exec())
ddb06a2a   Etienne Pallier   new pyros2 launch...
503
504
505
506



if __name__ == "__main__": main()