a3d14ddfae1d44b7d90b9531b7062c7feb2012c5
[odoo/odoo.git] / bin / openerp-server.py
1 #!/usr/bin/python
2 # -*- encoding: utf-8 -*-
3 ##############################################################################
4 #
5 #    OpenERP, Open Source Management Solution   
6 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
7 #    $Id$
8 #
9 #    This program is free software: you can redistribute it and/or modify
10 #    it under the terms of the GNU General Public License as published by
11 #    the Free Software Foundation, either version 3 of the License, or
12 #    (at your option) any later version.
13 #
14 #    This program is distributed in the hope that it will be useful,
15 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 #    GNU General Public License for more details.
18 #
19 #    You should have received a copy of the GNU General Public License
20 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 #
22 ##############################################################################
23
24 """
25 OpenERP - Server
26 OpenERP is an ERP+CRM program for small and medium businesses.
27
28 The whole source code is distributed under the terms of the
29 GNU Public Licence.
30
31 (c) 2003-TODAY, Fabien Pinckaers - Tiny sprl
32 """
33
34 #----------------------------------------------------------
35 # python imports
36 #----------------------------------------------------------
37 import sys
38 import os
39 import signal
40 #----------------------------------------------------------
41 # ubuntu 8.04 has obsoleted `pyxml` package and installs here.
42 # the path needs to be updated before any `import xml`
43 # TODO: remove PyXML dependencies, use lxml instead.
44 #----------------------------------------------------------
45 _oldxml = '/usr/lib/python%s/site-packages/oldxml' % sys.version[:3]
46 if os.path.exists(_oldxml):
47     sys.path.append(_oldxml)
48
49
50 import release
51 __author__ = release.author
52 __version__ = release.version
53
54 #----------------------------------------------------------
55 # get logger
56 #----------------------------------------------------------
57 import netsvc
58 logger = netsvc.Logger()
59
60 #-----------------------------------------------------------------------
61 # import the tools module so that the commandline parameters are parsed
62 #-----------------------------------------------------------------------
63 import tools
64
65 logger.notifyChannel("server", netsvc.LOG_INFO, "version - %s" % release.version )
66 for name, value in [('addons_path', tools.config['addons_path']),
67                     ('database hostname', tools.config['db_host'] or 'localhost'),
68                     ('database port', tools.config['db_port'] or '5432'),
69                     ('database user', tools.config['db_user'])]:
70     logger.notifyChannel("server", netsvc.LOG_INFO, "%s - %s" % ( name, value ))
71
72 import time
73
74 if sys.platform == 'win32':
75     import mx.DateTime
76     mx.DateTime.strptime = lambda x, y: mx.DateTime.mktime(time.strptime(x, y))
77
78 #----------------------------------------------------------
79 # init net service
80 #----------------------------------------------------------
81 logger.notifyChannel("objects", netsvc.LOG_INFO, 'initialising distributed objects services')
82
83 #---------------------------------------------------------------
84 # connect to the database and initialize it with base if needed
85 #---------------------------------------------------------------
86 import pooler
87
88 #----------------------------------------------------------
89 # import basic modules
90 #----------------------------------------------------------
91 import osv
92 import workflow
93 import report
94 import service
95
96 #----------------------------------------------------------
97 # import addons
98 #----------------------------------------------------------
99
100 import addons
101
102 #----------------------------------------------------------
103 # Load and update databases if requested
104 #----------------------------------------------------------
105
106 if tools.config['db_name']:
107     for db in tools.config['db_name'].split(','):
108         pooler.get_db_and_pool(db, update_module=tools.config['init'] or tools.config['update'])
109
110 #----------------------------------------------------------
111 # translation stuff
112 #----------------------------------------------------------
113 if tools.config["translate_out"]:
114     import csv
115
116     if tools.config["language"]:
117         msg = "language %s" % (tools.config["language"],)
118     else:
119         msg = "new language"
120     logger.notifyChannel("init", netsvc.LOG_INFO, 
121                          'writing translation file for %s to %s' % (msg, 
122                                                                     tools.config["translate_out"]))
123
124     fileformat = os.path.splitext(tools.config["translate_out"])[-1][1:].lower()
125     buf = file(tools.config["translate_out"], "w")
126     tools.trans_export(tools.config["language"], tools.config["translate_modules"], buf, fileformat)
127     buf.close()
128
129     logger.notifyChannel("init", netsvc.LOG_INFO, 'translation file written succesfully')
130     sys.exit(0)
131
132 if tools.config["translate_in"]:
133     tools.trans_load(tools.config["db_name"], 
134                      tools.config["translate_in"], 
135                      tools.config["language"])
136     sys.exit(0)
137
138 #----------------------------------------------------------------------------------
139 # if we don't want the server to continue to run after initialization, we quit here
140 #----------------------------------------------------------------------------------
141 if tools.config["stop_after_init"]:
142     sys.exit(0)
143
144
145 #----------------------------------------------------------
146 # Launch Server
147 #----------------------------------------------------------
148
149 if tools.config['xmlrpc']:
150     port = int(tools.config['port'])
151     interface = tools.config["interface"]
152     secure = tools.config["secure"]
153
154     httpd = netsvc.HttpDaemon(interface, port, secure)
155
156     xml_gw = netsvc.xmlrpc.RpcGateway('web-services')
157     httpd.attach("/xmlrpc", xml_gw)
158     logger.notifyChannel("web-services", netsvc.LOG_INFO, 
159                          "starting XML-RPC%s services, port %s" % 
160                          ((tools.config['secure'] and ' Secure' or ''), port))
161
162 #
163 #if tools.config["soap"]:
164 #   soap_gw = netsvc.xmlrpc.RpcGateway('web-services')
165 #   httpd.attach("/soap", soap_gw )
166 #   logger.notifyChannel("web-services", netsvc.LOG_INFO, 'starting SOAP services, port '+str(port))
167 #
168
169 if tools.config['netrpc']:
170     netport = int(tools.config['netport'])
171     netinterface = tools.config["netinterface"]
172     tinySocket = netsvc.TinySocketServerThread(netinterface, netport, False)
173     logger.notifyChannel("web-services", netsvc.LOG_INFO, 
174                          "starting NET-RPC service, port %d" % (netport,))
175
176 LST_SIGNALS = ['SIGINT', 'SIGTERM']
177 if os.name == 'posix':
178     LST_SIGNALS.extend(['SIGUSR1','SIGQUIT'])
179
180
181 SIGNALS = dict(
182     [(getattr(signal, sign), sign) for sign in LST_SIGNALS]
183 )
184
185 def handler(signum, _):
186     """
187     :param signum: the signal number
188     :param _: 
189     """
190     if tools.config['netrpc']:
191         tinySocket.stop()
192     if tools.config['xmlrpc']:
193         httpd.stop()
194     netsvc.Agent.quit()
195     if tools.config['pidfile']:
196         os.unlink(tools.config['pidfile'])
197     logger.notifyChannel('shutdown', netsvc.LOG_INFO, 
198                          "Shutdown Server! - %s" % ( SIGNALS[signum], ))
199     logger.shutdown()
200     sys.exit(0)
201
202 for signum in SIGNALS:
203     signal.signal(signum, handler)
204
205 if tools.config['pidfile']:
206     fd = open(tools.config['pidfile'], 'w')
207     pidtext = "%d" % (os.getpid())
208     fd.write(pidtext)
209     fd.close()
210
211 logger.notifyChannel("web-services", netsvc.LOG_INFO, 
212                      'the server is running, waiting for connections...')
213
214 if tools.config['netrpc']:
215     tinySocket.start()
216 if tools.config['xmlrpc']:
217     httpd.start()
218
219 while True:
220     time.sleep(1)
221
222 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
223