Merged for financial improvements
[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 _oldxml1 = '/usr/lib/python%s/site-packages/oldxml' % sys.version[:3]
46 _oldxml2 = '/usr/lib/python%s/dist-packages/oldxml' % sys.version[:3]
47 if os.path.exists(_oldxml1):
48     sys.path.insert(0,_oldxml1)
49 elif os.path.exists(_oldxml2):
50     sys.path.insert(0,_oldxml2)    
51
52
53 import release
54 __author__ = release.author
55 __version__ = release.version
56
57 #----------------------------------------------------------
58 # get logger
59 #----------------------------------------------------------
60 import netsvc
61 logger = netsvc.Logger()
62
63 #-----------------------------------------------------------------------
64 # import the tools module so that the commandline parameters are parsed
65 #-----------------------------------------------------------------------
66 import tools
67
68 logger.notifyChannel("server", netsvc.LOG_INFO, "version - %s" % release.version )
69 for name, value in [('addons_path', tools.config['addons_path']),
70                     ('database hostname', tools.config['db_host'] or 'localhost'),
71                     ('database port', tools.config['db_port'] or '5432'),
72                     ('database user', tools.config['db_user'])]:
73     logger.notifyChannel("server", netsvc.LOG_INFO, "%s - %s" % ( name, value ))
74
75 import time
76
77 if sys.platform == 'win32':
78     import mx.DateTime
79     mx.DateTime.strptime = lambda x, y: mx.DateTime.mktime(time.strptime(x, y))
80
81 #----------------------------------------------------------
82 # init net service
83 #----------------------------------------------------------
84 logger.notifyChannel("objects", netsvc.LOG_INFO, 'initialising distributed objects services')
85
86 #---------------------------------------------------------------
87 # connect to the database and initialize it with base if needed
88 #---------------------------------------------------------------
89 import pooler
90
91 #----------------------------------------------------------
92 # import basic modules
93 #----------------------------------------------------------
94 import osv
95 import workflow
96 import report
97 import service
98
99 #----------------------------------------------------------
100 # import addons
101 #----------------------------------------------------------
102
103 import addons
104
105 #----------------------------------------------------------
106 # Load and update databases if requested
107 #----------------------------------------------------------
108
109 if tools.config['db_name']:
110     for db in tools.config['db_name'].split(','):
111         pooler.get_db_and_pool(db, update_module=tools.config['init'] or tools.config['update'])
112
113 #----------------------------------------------------------
114 # translation stuff
115 #----------------------------------------------------------
116 if tools.config["translate_out"]:
117     import csv
118
119     if tools.config["language"]:
120         msg = "language %s" % (tools.config["language"],)
121     else:
122         msg = "new language"
123     logger.notifyChannel("init", netsvc.LOG_INFO, 
124                          'writing translation file for %s to %s' % (msg, 
125                                                                     tools.config["translate_out"]))
126
127     fileformat = os.path.splitext(tools.config["translate_out"])[-1][1:].lower()
128     buf = file(tools.config["translate_out"], "w")
129     tools.trans_export(tools.config["language"], tools.config["translate_modules"], buf, fileformat)
130     buf.close()
131
132     logger.notifyChannel("init", netsvc.LOG_INFO, 'translation file written successfully')
133     sys.exit(0)
134
135 if tools.config["translate_in"]:
136     tools.trans_load(tools.config["db_name"], 
137                      tools.config["translate_in"], 
138                      tools.config["language"])
139     sys.exit(0)
140
141 #----------------------------------------------------------------------------------
142 # if we don't want the server to continue to run after initialization, we quit here
143 #----------------------------------------------------------------------------------
144 if tools.config["stop_after_init"]:
145     sys.exit(0)
146
147
148 #----------------------------------------------------------
149 # Launch Server
150 #----------------------------------------------------------
151
152 if tools.config['xmlrpc']:
153     port = int(tools.config['port'])
154     interface = tools.config["interface"]
155     secure = tools.config["secure"]
156
157     httpd = netsvc.HttpDaemon(interface, port, secure)
158
159     xml_gw = netsvc.xmlrpc.RpcGateway('web-services')
160     httpd.attach("/xmlrpc", xml_gw)
161     logger.notifyChannel("web-services", netsvc.LOG_INFO, 
162                          "starting XML-RPC%s services, port %s" % 
163                          ((tools.config['secure'] and ' Secure' or ''), port))
164
165 #
166 #if tools.config["soap"]:
167 #   soap_gw = netsvc.xmlrpc.RpcGateway('web-services')
168 #   httpd.attach("/soap", soap_gw )
169 #   logger.notifyChannel("web-services", netsvc.LOG_INFO, 'starting SOAP services, port '+str(port))
170 #
171
172 if tools.config['netrpc']:
173     netport = int(tools.config['netport'])
174     netinterface = tools.config["netinterface"]
175     tinySocket = netsvc.TinySocketServerThread(netinterface, netport, False)
176     logger.notifyChannel("web-services", netsvc.LOG_INFO, 
177                          "starting NET-RPC service, port %d" % (netport,))
178
179 LST_SIGNALS = ['SIGINT', 'SIGTERM']
180 if os.name == 'posix':
181     LST_SIGNALS.extend(['SIGUSR1','SIGQUIT'])
182
183
184 SIGNALS = dict(
185     [(getattr(signal, sign), sign) for sign in LST_SIGNALS]
186 )
187
188 def handler(signum, _):
189     """
190     :param signum: the signal number
191     :param _: 
192     """
193     if tools.config['netrpc']:
194         tinySocket.stop()
195     if tools.config['xmlrpc']:
196         httpd.stop()
197     netsvc.Agent.quit()
198     if tools.config['pidfile']:
199         os.unlink(tools.config['pidfile'])
200     logger.notifyChannel('shutdown', netsvc.LOG_INFO, 
201                          "Shutdown Server! - %s" % ( SIGNALS[signum], ))
202     logger.shutdown()
203     sys.exit(0)
204
205 for signum in SIGNALS:
206     signal.signal(signum, handler)
207
208 if tools.config['pidfile']:
209     fd = open(tools.config['pidfile'], 'w')
210     pidtext = "%d" % (os.getpid())
211     fd.write(pidtext)
212     fd.close()
213
214 logger.notifyChannel("web-services", netsvc.LOG_INFO, 
215                      'the server is running, waiting for connections...')
216
217 if tools.config['netrpc']:
218     tinySocket.start()
219 if tools.config['xmlrpc']:
220     httpd.start()
221
222 while True:
223     time.sleep(1)
224
225 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
226