__main__.py 3.06 KB
Newer Older
1
2
3
4
5
6
7
8
import subprocess
import time
import atexit
import argparse
from importlib import import_module
import sys
import os

9

10
# init argument parser
11
12
parser = argparse.ArgumentParser(description="Run a hpp-rbprm scenario. \n"
                                             "Take care of starting and closing the viewer and the hpp-rbprm-server.")
13
14
15
16
parser.add_argument('scenario_name',
                    type=str,
                    help="The name of the scenario script to run. "
                    "If a relative path is given, hpp.corbaserver.rbprm.scenarios is prepended")
17
18
parser.add_argument("-n", "--no_viewer", help="Run rbprm without visualization.",action="store_true")

19
20
21
22
args = parser.parse_args()
# retrieve argument
scenario_name = args.scenario_name
scenario_name = scenario_name.rstrip(".py")  # remove extension if specified
23
scenario_name.replace("/", ".")  # if the path to the file is given, transform it to a python path
24
25
26
27
# try to import the given module
try:
    module_scenario = import_module(scenario_name)
except ImportError:
28
    print("Cannot import " + scenario_name + " try to prepend path")
29
30
31
32
33
34
    scenario_name = "hpp.corbaserver.rbprm.scenarios." + scenario_name
    try:
        module_scenario = import_module(scenario_name)
    except ImportError:
        print("Cannot import " + scenario_name + ". Check if the path is correct")
        sys.exit(1)
35

36
# kill already existing instance of the server
37
subprocess.run(["killall", "hpp-rbprm-server"])
38
# run the server in background :
39
# stdout and stderr outputs of the child process are redirected to devnull (hidden).
40
# preexec_fn is used to ignore ctrl-c signal send to the main script (otherwise they are forwarded to the child process)
41
42
43
44
process_server = subprocess.Popen("hpp-rbprm-server",
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.DEVNULL,
                                  preexec_fn=os.setpgrp)
45
46
47
48
# register cleanup methods to kill server when exiting python interpreter
atexit.register(process_server.kill)

# do the same for the viewer, exept if --no-viewer flag is set
49
if not args.no_viewer:
50
51
52
53
54
55
56
57
    subprocess.run(["killall", "gepetto-gui"])
    process_viewer = subprocess.Popen("gepetto-gui",
                                      stdout=subprocess.PIPE,
                                      stderr=subprocess.DEVNULL,
                                      preexec_fn=os.setpgrp)
    atexit.register(process_viewer.kill)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# wait a little for the initialization of the server/viewer
time.sleep(3)

# Get ContactGenerator or PathPlanner class from the imported module and run it
if hasattr(module_scenario, 'ContactGenerator'):
    print("# Run contact generation script ...")
    ContactGenerator = getattr(module_scenario, 'ContactGenerator')
    cg = ContactGenerator()
    cg.run()
elif hasattr(module_scenario, 'PathPlanner'):
    print("# Run guide planning script ...")
    PathPlanner = getattr(module_scenario, 'PathPlanner')
    planner = PathPlanner()
    planner.run()
else:
    print("Given script doesn't contain ContactGenerator neither PathPlanner class.")
    sys.exit(1)

76