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