[FIX]
[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-2008 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, os, signal
38 #----------------------------------------------------------
39 # ubuntu 8.04 has obsoleted `pyxml` package and installs here.
40 # the path needs to be updated before any `import xml`
41 # TODO: remove PyXML dependencies, use lxml instead.
42 #----------------------------------------------------------
43 _oldxml = '/usr/lib/python%s/site-packages/oldxml' % sys.version[:3]
44 if os.path.exists(_oldxml):
45     sys.path.append(_oldxml)
46
47
48 import release
49 __author__ = release.author
50 __version__ = release.version
51
52 #----------------------------------------------------------
53 # get logger
54 #----------------------------------------------------------
55 import netsvc
56 logger = netsvc.Logger()
57
58 #-----------------------------------------------------------------------
59 # import the tools module so that the commandline parameters are parsed
60 #-----------------------------------------------------------------------
61 import tools
62
63 logger.notifyChannel("server", netsvc.LOG_INFO, "version - %s" % release.version )
64 for name, value in [('addons_path', tools.config['addons_path']),
65                     ('database hostname', tools.config['db_host'] or 'localhost'),
66                     ('database port', tools.config['db_port'] or '5432'),
67                     ('database user', tools.config['db_user'])]:
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 dispatcher = netsvc.Dispatcher()
82 dispatcher.monitor(signal.SIGINT)
83
84 #---------------------------------------------------------------
85 # connect to the database and initialize it with base if needed
86 #---------------------------------------------------------------
87 import pooler
88
89 #----------------------------------------------------------
90 # launch modules install/upgrade/removes if needed
91 #----------------------------------------------------------
92 if tools.config['upgrade']:
93     logger.notifyChannel('init', netsvc.LOG_INFO, 'Upgrading new modules...')
94     import tools.upgrade
95     (toinit, toupdate) = tools.upgrade.upgrade()
96     for m in toinit:
97         tools.config['init'][m] = 1
98     for m in toupdate:
99         tools.config['update'][m] = 1
100
101 #----------------------------------------------------------
102 # import basic modules
103 #----------------------------------------------------------
104 import osv
105 import workflow
106 import report
107 import service
108
109 #----------------------------------------------------------
110 # import addons
111 #----------------------------------------------------------
112 import addons
113
114 if tools.config['init'] or tools.config['update']:
115     pooler.get_db_and_pool(tools.config['db_name'], update_module=True)
116
117 #----------------------------------------------------------
118 # translation stuff
119 #----------------------------------------------------------
120 if tools.config["translate_out"]:
121     import csv
122
123     if tools.config["language"]:
124         msg = "language %s" % (tools.config["language"],)
125     else:
126         msg = "new language"
127     logger.notifyChannel("init", netsvc.LOG_INFO, 'writing translation file for %s to %s' % (msg, tools.config["translate_out"]))
128
129     fileformat = os.path.splitext(tools.config["translate_out"])[-1][1:].lower()
130     buf = file(tools.config["translate_out"], "w")
131     tools.trans_export(tools.config["language"], tools.config["translate_modules"], buf, fileformat)
132     buf.close()
133
134     logger.notifyChannel("init", netsvc.LOG_INFO, 'translation file written succesfully')
135     sys.exit(0)
136
137 if tools.config["translate_in"]:
138     tools.trans_load(tools.config["db_name"], tools.config["translate_in"], 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     try:
154         port = int(tools.config["port"])
155     except Exception:
156         logger.notifyChannel("init", netsvc.LOG_ERROR, "invalid port '%s'!" % (tools.config["port"],))
157         sys.exit(1)
158     interface = tools.config["interface"]
159     secure = tools.config["secure"]
160
161     httpd = netsvc.HttpDaemon(interface, port, secure)
162
163     if tools.config["xmlrpc"]:
164         xml_gw = netsvc.xmlrpc.RpcGateway('web-services')
165         httpd.attach("/xmlrpc", xml_gw)
166         logger.notifyChannel("web-services", netsvc.LOG_INFO, "starting XML-RPC%s services, port %s" % ((tools.config['secure'] and ' Secure' or ''), port))
167
168 #
169 #if tools.config["soap"]:
170 #   soap_gw = netsvc.xmlrpc.RpcGateway('web-services')
171 #   httpd.attach("/soap", soap_gw )
172 #   logger.notifyChannel("web-services", netsvc.LOG_INFO, 'starting SOAP services, port '+str(port))
173 #
174
175 if tools.config['netrpc']:
176     try:
177         netport = int(tools.config["netport"])
178     except Exception:
179         logger.notifyChannel("init", netsvc.LOG_ERROR, "invalid port '%s'!" % (tools.config["netport"],))
180         sys.exit(1)
181     netinterface = tools.config["netinterface"]
182
183     tinySocket = netsvc.TinySocketServerThread(netinterface, netport, False)
184     logger.notifyChannel("web-services", netsvc.LOG_INFO, "starting NET-RPC service, port "+str(netport))
185
186
187 def handler(signum, frame):
188     from tools import config
189     if tools.config['netrpc']:
190         tinySocket.stop()
191     if tools.config['xmlrpc']:
192         httpd.stop()
193     netsvc.Agent.quit()
194     if config['pidfile']:
195         os.unlink(config['pidfile'])
196     sys.exit(0)
197
198 from tools import config
199 if config['pidfile']:
200     fd = open(config['pidfile'], 'w')
201     pidtext = "%d" % (os.getpid())
202     fd.write(pidtext)
203     fd.close()
204
205 signal.signal(signal.SIGINT, handler)
206 signal.signal(signal.SIGTERM, handler)
207
208 logger.notifyChannel("web-services", netsvc.LOG_INFO, 'the server is running, waiting for connections...')
209 if tools.config['netrpc']:
210     tinySocket.start()
211 if tools.config['xmlrpc']:
212     httpd.start()
213 #dispatcher.run()
214
215 while True:
216     time.sleep(1)
217
218 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
219