Improved installation checks.
[openoffice-python:openoffice-python.git] / sample-scripts / ooo-server
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright (c) 2008 by Hartmut Goebel <h.goebel@goebel-consult.de>
5 # Licenced under the GNU General Public License v3 (GPLv3)
6 # see file LICENSE-gpl-3.0.txt
7 #
8 """
9 Manage a OOo server: start, stop and unlisten.
10
11 If you want a full-blown server, help enhancing this script! See
12 source for comments.
13
14 Some ideas taken from Dag Wieers unoconv
15 <http://dag.wieers.com/home-made/unoconv/>
16 """
17
18 # Todo:
19 #
20 # * Implement a full-blown deamon for Unix and a full-blown service
21 #   for Windows. Should this restart OOo automatically if it crashed?
22 #
23 # * Design issue: OOo behaves different depending whether there is
24 #   already an instance running: If one it running, the new process
25 #   returns. If there is no instance running yet, he new porcess will
26 #   _not_ return. But how determine whether the server started
27 #   successfully or there has been an error?
28 #
29
30 from openoffice.officehelper import _build_cmd_args, _build_connect_string, \
31      BootstrapException
32 from openoffice.interact import Desktop
33
34 import subprocess
35 import sys
36
37 def start(pipename=None, host=None, port=None):
38     """
39     Start an OOo instance which listens on pipename or host/port.
40     If there is already an running instance, it will be reused (done
41     by OOo automatically).
42     """
43     connectString = _build_connect_string(pipename, host, port)
44     cmdArray = _build_cmd_args(connectString)
45     # Todo: OOo starts a new instance, if noone is running. This is a
46     # drawback, since one can not tell, whether he has started the
47     # instancce or is reusing an already running one.
48     subprocess.Popen(cmdArray).pid
49
50
51 def unlisten(pipename=None, host=None, port=None):
52     """
53     Tell the OOo instance listening on pipename or host/port to stop
54     listening on this connection.
55     """
56     connectString = _build_connect_string(pipename, host, port)
57     cmdArray = _build_cmd_args(connectString, unaccept=True)
58     subprocess.Popen(cmdArray).pid
59
60
61 def stop(pipename=None, host=None, port=None):
62     '''
63     Stop the OOo instance listening pipename or host/port.
64
65     Note: If there is no instance listening on pipename or host/port,
66     none will be stopped, since it can not be reached.
67     '''
68     # Connect to the instance listening on host/port
69     try:
70         desktop = Desktop(host=host, port=port)._desktop # todo: use slots
71     except BootstrapException:
72         print >> sys.stderr, 'Could not contact OOo instance to stop it. Perhaps is was not running at all.'
73         raise SystemExit(1)
74
75     if desktop.CurrentFrame:
76         ### If there is a GUI now attached to the instance, disable only listener
77         unlisten(pipename, host, port)
78     else:
79         ### If there is no GUI attached to the instance, terminate instance
80         unlisten(pipename, host, port)
81         try:
82             desktop.terminate()
83         except DisposedException:
84             pass
85     
86
87 if __name__ == '__main__':
88     import optparse
89     parser = optparse.OptionParser('%prog [options] '
90                                    '[ start | unlisten | stop ]')
91     parser.add_option('--host',  default='localhost',
92                       help="server address to listen on (default: %default, "
93                            "use '0.0.0.0' for listening on all IPv4 addresses)")
94     parser.add_option('--port',  default=2002, type=int,
95                       help="port to listen on (default: %default)")
96     opts, args = parser.parse_args()
97
98     if len(args) != 1:
99         parser.error('requiers exactly one argument')
100
101     cmd = args[0].lower()
102     if cmd == 'start':
103         start(host=opts.host, port=opts.port)
104     elif  cmd == 'unlisten':
105         unlisten(host=opts.host, port=opts.port)
106     elif  cmd == 'stop':
107         stop(host=opts.host, port=opts.port)
108     else:
109         parser.error('unknown action %r' % cmd)