1#************************************************************** 2# 3# Licensed to the Apache Software Foundation (ASF) under one 4# or more contributor license agreements. See the NOTICE file 5# distributed with this work for additional information 6# regarding copyright ownership. The ASF licenses this file 7# to you under the Apache License, Version 2.0 (the 8# "License"); you may not use this file except in compliance 9# with the License. You may obtain a copy of the License at 10# 11# http://www.apache.org/licenses/LICENSE-2.0 12# 13# Unless required by applicable law or agreed to in writing, 14# software distributed under the License is distributed on an 15# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16# KIND, either express or implied. See the License for the 17# specific language governing permissions and limitations 18# under the License. 19# 20#************************************************************** 21 22# 23# Translated to python from "Bootstrap.java" by Kim Kulak 24# 25 26import os 27import random 28from sys import platform 29from time import sleep 30 31import uno 32from com.sun.star.connection import NoConnectException 33from com.sun.star.uno import Exception as UnoException 34 35 36class BootstrapException(UnoException): 37 pass 38 39def bootstrap(): 40 """Bootstrap OOo and PyUNO Runtime. 41 The soffice process is started opening a named pipe of random name, then the local context is used 42 to access the pipe. This function directly returns the remote component context, from whereon you can 43 get the ServiceManager by calling getServiceManager() on the returned object. 44 """ 45 try: 46 # soffice script used on *ix, Mac; soffice.exe used on Windoof 47 if "UNO_PATH" in os.environ: 48 sOffice = os.environ["UNO_PATH"] 49 else: 50 sOffice = "" # lets hope for the best 51 sOffice = os.path.join(sOffice, "soffice") 52 if platform.startswith("win"): 53 sOffice += ".exe" 54 55 # Generate a random pipe name. 56 random.seed() 57 sPipeName = "uno" + str(random.random())[2:] 58 59 # Start the office proces, don't check for exit status since an exception is caught anyway if the office terminates unexpectedly. 60 cmdArray = (sOffice, "-nologo", "-nodefault", "".join(["-accept=pipe,name=", sPipeName, ";urp;"])) 61 os.spawnv(os.P_NOWAIT, sOffice, cmdArray) 62 63 # --------- 64 65 xLocalContext = uno.getComponentContext() 66 resolver = xLocalContext.ServiceManager.createInstanceWithContext( 67 "com.sun.star.bridge.UnoUrlResolver", xLocalContext) 68 sConnect = "".join(["uno:pipe,name=", sPipeName, ";urp;StarOffice.ComponentContext"]) 69 70 # Wait until an office is started, but loop only nLoop times (can we do this better???) 71 nLoop = 20 72 while True: 73 try: 74 xContext = resolver.resolve(sConnect) 75 break 76 except NoConnectException: 77 nLoop -= 1 78 if nLoop <= 0: 79 raise BootstrapException("Cannot connect to soffice server.", None) 80 sleep(0.5) # Sleep 1/2 second. 81 82 except BootstrapException: 83 raise 84 except Exception as e: # Any other exception 85 raise BootstrapException("Caught exception " + str(e), None) 86 87 return xContext 88