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