1### *************************************************************************
2### *
3# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4#
5# Copyright 2000, 2010 Oracle and/or its affiliates.
6#
7# OpenOffice.org - a multi-platform office productivity suite
8#
9# This file is part of OpenOffice.org.
10#
11# OpenOffice.org is free software: you can redistribute it and/or modify
12# it under the terms of the GNU Lesser General Public License version 3
13# only, as published by the Free Software Foundation.
14#
15# OpenOffice.org is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU Lesser General Public License version 3 for more details
19# (a copy is included in the LICENSE file that accompanied this code).
20#
21# You should have received a copy of the GNU Lesser General Public License
22# version 3 along with OpenOffice.org.  If not, see
23# <http://www.openoffice.org/license.html>
24# for a copy of the LGPLv3 License.
25#
26### ************************************************************************/
27
28#
29# Translated to python from "Bootstrap.java" by Kim Kulak
30#
31
32import os
33import random
34from sys import platform
35from time import sleep
36
37import uno
38from com.sun.star.connection import NoConnectException
39from com.sun.star.uno import Exception as UnoException
40
41
42class BootstrapException(UnoException):
43    pass
44
45def bootstrap():
46    """Bootstrap OOo and PyUNO Runtime.
47    The soffice process is started opening a named pipe of random name, then the local context is used
48	to access the pipe. This function directly returns the remote component context, from whereon you can
49	get the ServiceManager by calling getServiceManager() on the returned object.
50	"""
51    try:
52	# soffice script used on *ix, Mac; soffice.exe used on Windoof
53        if "UNO_PATH" in os.environ:
54            sOffice = os.environ["UNO_PATH"]
55        else:
56            sOffice = "" # lets hope for the best
57        sOffice = os.path.join(sOffice, "soffice")
58        if platform.startswith("win"):
59            sOffice += ".exe"
60
61        # Generate a random pipe name.
62        random.seed()
63        sPipeName = "uno" + str(random.random())[2:]
64
65        # Start the office proces, don't check for exit status since an exception is caught anyway if the office terminates unexpectedly.
66        cmdArray = (sOffice, "-nologo", "-nodefault", "".join(["-accept=pipe,name=", sPipeName, ";urp;"]))
67        os.spawnv(os.P_NOWAIT, sOffice, cmdArray)
68
69        # ---------
70
71        xLocalContext = uno.getComponentContext()
72        resolver = xLocalContext.ServiceManager.createInstanceWithContext(
73			"com.sun.star.bridge.UnoUrlResolver", xLocalContext)
74        sConnect = "".join(["uno:pipe,name=", sPipeName, ";urp;StarOffice.ComponentContext"])
75
76        # Wait until an office is started, but loop only nLoop times (can we do this better???)
77        nLoop = 20
78        while True:
79            try:
80                xContext = resolver.resolve(sConnect)
81                break
82            except NoConnectException:
83                nLoop -= 1
84                if nLoop <= 0:
85                    raise BootstrapException("Cannot connect to soffice server.", None)
86                sleep(0.5)  # Sleep 1/2 second.
87
88    except BootstrapException:
89        raise
90    except Exception, e:  # Any other exception
91        raise BootstrapException("Caught exception " + str(e), None)
92
93    return xContext
94