1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2017 CEA/DEN, EDF R&D, OPEN CASCADE
4 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 from optparse import OptionParser
29 # Use to display newlines (\n) in epilog
30 class MyParser(OptionParser):
31 def format_epilog(self, formatter):
35 def __parse_args(args):
39 usage = "Usage: salome connect [-p port] [ -c command | script | - ]"
41 Connects a Python console to a local SALOME instance.\n
42 If port is given, try to connect to corresponding instance.
43 If port is not given, or does not correspond to a running instance,
44 ask user to select a port from list of available SALOME instances.\n
46 The -c option can be used to specify the command to execute in the interpreter.
47 A script can also be used.
49 salome connect -p 2810 -c 'print("Hello")'
50 salome connect -p 2810 hello.py
52 parser = MyParser(usage=usage, epilog=epilog)
53 parser.add_option("-p", metavar="<port>", default=0,
54 action="store", type="string", dest="port",
55 help="The port to connect to."
57 parser.add_option('-c', dest="command", default=None,
58 help="The command to execute in the interpreter."
61 (options, args) = parser.parse_args(args)
62 except Exception as e:
69 def __show_running_instances(list_of_instances):
71 print("Running instances:")
72 for i in range(len(list_of_instances)):
73 host, port, _ = list_of_instances[i]
74 print(" [%d] %s:%s"%(i+1, host, port))
78 def __choose_in(choices):
79 __show_running_instances(choices)
80 rep = input("Please enter the number of instance to use (0 to cancel): ")
82 return None, None, None
83 elif rep in [str(i) for i in range(1, len(choices)+1)]:
84 return choices[int(rep)-1]
86 print("*** Invalid number! ***")
87 return __choose_in(choices)
90 def __get_running_session(requested_port=None, lastInstanceByDefault=False):
93 from ORBConfigFile import readORBConfigFile
95 omniorbUserPath = os.getenv("OMNIORB_USER_PATH")
96 files = glob.glob(os.path.join(omniorbUserPath,".omniORB_"+salome_utils.getUserName()+"_*[!last].cfg"))
97 available_connexions = []
98 for filename in files:
99 host, port = readORBConfigFile(filename)
100 available_connexions.append((host, port, filename))
102 host, port, filename = None, None, None
104 print("Search for running instance on port %s..."%requested_port)
105 found = [(h,p,f) for h,p,f in available_connexions if int(p) == int(requested_port)]
107 print(" ...no running instance found")
108 elif len(found) == 1:
109 host, port, filename = found[0]
110 print(" ...found unique instance: %s:%s"%(host,port))
112 print(" ...multiple instances found ; please choose one in the following:")
113 host, port, filename = __choose_in(found)
114 else: # no requested port
115 if not available_connexions:
116 print("No running instance found")
117 elif len(available_connexions) == 1:
118 host, port, filename = available_connexions[0]
119 print("Found unique instance: %s:%s"%(host,port))
121 print("Multiple instances found ; please choose one in the following:")
122 host, port, filename = __choose_in(available_connexions)
126 print("Selected instance: %s:%s"%(host, port))
130 return host, port, filename
137 class client(orbmodule.client):
138 def initNS(self,args):
139 # Obtain a reference to the root naming context
140 obj = self.orb.resolve_initial_references("NameService")
142 self.rootContext = obj._narrow(CosNaming.NamingContext)
144 except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
145 print("It's not a valid naming service")
146 self.rootContext = None
156 traceback.print_exc()
160 session_server = clt.Resolve('/Kernel/Session')
162 session = clt.waitNS("/Kernel/Session")
163 catalog = clt.waitNS("/Kernel/ModulCatalog")
164 study = clt.waitNS("/Study")
168 from salome import lcc
169 print("--> now connected to SALOME")
172 def _prompt(environment=None, commands=None, message="Connecting to SALOME"):
173 if environment is None:
174 environment = globals().copy()
175 environment.update(locals())
182 readline.set_completer(rlcompleter.Completer(environment).complete)
183 readline.parse_and_bind("tab: complete")
184 # calling this with globals ensures we can see the environment
186 shell = code.InteractiveConsole(environment)
188 print("Execute command:", cmd)
194 def connect(args=None, env=None):
197 options, args = __parse_args(args)
198 host, port, filename = __get_running_session(options.port)
203 "os.environ['OMNIORB_CONFIG'] = '%s'"%filename,
207 cmd.append(options.command)
208 if args: # unprocessed: may be scripts
210 filename = os.path.abspath(os.path.expanduser(arg))
211 pythonLine = "exec(compile(open(%s, \"rb\").read(), %s, 'exec'))"%(filename, filename)
212 cmd.append(pythonLine)
216 absoluteAppliPath = os.getenv('ABSOLUTE_APPLI_PATH','')
217 env_copy = os.environ.copy()
218 cmdDump=pickle.dumps(cmd,protocol=0)
219 cmdString=cmdDump.decode()
220 proc = subprocess.Popen(['python', os.path.join(absoluteAppliPath,"bin","salome","runConsole.py"), cmdString], shell=False, close_fds=True, env=env_copy)
222 return proc.returncode
225 if __name__ == "__main__":
226 if len(sys.argv) == 2:
227 cmdBytes = sys.argv[1].encode()
228 cmd = pickle.loads(cmdBytes)
230 _prompt(commands=cmd)
232 print("runConsole.py: incorrect usage!")