Salome HOME
Update for slurm 21.
[modules/kernel.git] / bin / runRemote.py
1 # Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
2 #
3 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 #
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
10 #
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # Lesser General Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 #
20 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #
22
23 import os
24 from optparse import OptionParser
25 import subprocess
26 from salome_utils import getUserName
27 from salomeContextUtils import getShortAndExtraArgs
28
29 # Use to display newlines (\n) in epilog
30 class MyParser(OptionParser):
31     def format_epilog(self, formatter):
32         return self.epilog
33 #
34 #
35 # --- set the OMNIORB_CONFIG file and environment relative to this run of SALOME
36 def _writeConfigFile(port, host):
37     path = os.environ['OMNIORB_USER_PATH']
38     kwargs = {'with_username' : getUserName()}
39
40     from ORBConfigFile import writeORBConfigFile
41     [ filename, msgSize ] = writeORBConfigFile(path, host, port, kwargs)
42
43     os.environ['OMNIORB_CONFIG'] = filename
44 #
45
46 def runRemote(args):
47     if args is None:
48       args = []
49     usage = "Usage: salome runremote [options] -- command"
50     epilog  = """\n
51 Execute command in SALOME environment from a remote call, ssh or rsh.
52 salome runremote is used notably to launch containers from a distant salome session.
53
54 For example:
55     salome remote -m xxx.cea.fr -p 2810 -- ls /tmp >/dev/null 2>&1  
56      -> execute the command ls /tmp >/dev/null 2>&1
57
58     salome remote -m xxx.cea.fr -p 2810 -- SALOME_Container Cont_S 
59                                         -ORBInitRef NameService=IOR:01...
60      -> starts a Salome container called Cont_S connected to port 2810 
61         of xxx.cea.fr
62 """
63     parser = MyParser(usage=usage, epilog=epilog)
64     parser.add_option("-p", "--port", metavar="<port>", default="2810",
65                       action="store", type="string", dest="port",
66                       help="The port to connect to."
67                      )
68     parser.add_option("-m", "--machine", metavar="<machine>",
69                       action="store", type="string", dest="host", 
70                       default="localhost",
71                       help="The machine where salome was launched."
72                      )
73     parser.add_option('-d', '--directory', dest="directory", 
74                       metavar="<directory>", default=None,
75                       help="The directory where to execute the command."
76                      )
77
78     # separate runRemote args from the command to run (given after --)
79     short_args, extra_args = getShortAndExtraArgs(args)
80     try:
81       (options, args) = parser.parse_args(short_args)
82     except Exception as e:
83       print(e)
84       print(usage)
85       print(epilog)
86       return
87
88     port = options.port
89     host = options.host
90     directory = options.directory
91     command=extra_args[1:]
92
93     _writeConfigFile(port, host)
94     os.environ['NSPORT'] = port
95     os.environ['NSHOST'] = host
96     print("[ Remote Command ] ", " ".join(command))
97     cmd = subprocess.Popen(command, cwd=directory)
98     cmd.wait()
99     return
100 #
101