Salome HOME
PR: merge from branch BR_UT_V310a2 tag BR_UT_V310a2_20051115
[modules/kernel.git] / src / LifeCycleCORBA / Launchers.py
1 import os
2 import Utils_Identity
3 import ConfigParser
4
5 class Config:
6    defaults={'user':'','opt':''}
7    def __init__(self):
8        self.config=ConfigParser.ConfigParser(self.defaults)
9        self.config.read(os.path.join(os.environ['HOME'],Utils_Identity.getapplipath(),'launchers.cfg'))
10    def computers(self):
11        return self.config.sections()
12    def options(self,computer):
13        if not self.config.has_section(computer):
14           return self.defaults
15        else:
16           d={}
17           for opt in self.config.options(computer):
18              d[opt]=self.config.get(computer,opt)
19           return d
20
21 config=None
22 def setConfig(conf):
23     global config
24     config=conf
25
26 setConfig(Config())
27        
28 class Launcher:
29    def Slaunch(self,computer,CMD):
30        self.launch(computer,CMD.split())
31
32 class SSH(Launcher):
33    def launch(self,computer,CMD):
34        print computer,CMD
35        conf=config.options(computer)
36        print conf
37        command=["ssh",computer]
38        if conf["user"]:
39           command=command+['-l',conf["user"]]
40        if conf["opt"]:
41           command=command+string.split(conf["opt"])
42        command=command+[os.path.join(Utils_Identity.getapplipath(),'runSession')]+CMD
43        command=command+['>', "/tmp/" + CMD[1] + "_" + computer + ".log 2>&1"]
44
45        print command
46        pid = os.spawnvp(os.P_NOWAIT, command[0], command)
47
48 launcher=None
49 _register={}
50
51 def register(name,klass):
52     _register[name]=klass
53
54 register("ssh",SSH())
55
56 def setLauncher(name):
57     global launcher
58     launcher=_register[name]
59
60 def getLauncher(computer):
61     """
62       Return a launcher for the computer
63       If can be specified in config
64       If not return the default one : launcher
65     """
66     conf=config.options(computer)
67     name=conf.get("launcher","")
68     return _register.get(name,launcher)
69
70 setLauncher("ssh")
71
72 try:
73    # Try to import a plugin module (user defined) that
74    # can define other launchers and register them
75    # 1- define a class
76    # 2- register it with : Launchers.register(name,klass)
77    # 3- eventually set it as default one : Launchers.setLauncher(name)
78    import pluginLauncher
79 except:
80    #import traceback
81    #traceback.print_exc()
82    print "----------------------------------------------------------"
83    print " no user defined launcher, ssh used. (see Launchers.py)   "
84    print "----------------------------------------------------------"
85    pass