2 # -*- coding: utf-8 -*-
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
9 Manage a OOo server: start, stop and unlisten.
11 If you want a full-blown server, help enhancing this script! See
14 Some ideas taken from Dag Wieers unoconv
15 <http://dag.wieers.com/home-made/unoconv/>
20 # * Implement a full-blown deamon for Unix and a full-blown service
21 # for Windows. Should this restart OOo automatically if it crashed?
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?
30 from openoffice.officehelper import _build_cmd_args, _build_connect_string, \
32 from openoffice.interact import Desktop
37 def start(pipename=None, host=None, port=None):
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).
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
51 def unlisten(pipename=None, host=None, port=None):
53 Tell the OOo instance listening on pipename or host/port to stop
54 listening on this connection.
56 connectString = _build_connect_string(pipename, host, port)
57 cmdArray = _build_cmd_args(connectString, unaccept=True)
58 subprocess.Popen(cmdArray).pid
61 def stop(pipename=None, host=None, port=None):
63 Stop the OOo instance listening pipename or host/port.
65 Note: If there is no instance listening on pipename or host/port,
66 none will be stopped, since it can not be reached.
68 # Connect to the instance listening on host/port
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.'
75 if desktop.CurrentFrame:
76 ### If there is a GUI now attached to the instance, disable only listener
77 unlisten(pipename, host, port)
79 ### If there is no GUI attached to the instance, terminate instance
80 unlisten(pipename, host, port)
83 except DisposedException:
87 if __name__ == '__main__':
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()
99 parser.error('requiers exactly one argument')
101 cmd = args[0].lower()
103 start(host=opts.host, port=opts.port)
104 elif cmd == 'unlisten':
105 unlisten(host=opts.host, port=opts.port)
107 stop(host=opts.host, port=opts.port)
109 parser.error('unknown action %r' % cmd)