a18db85d2e43d211019a265aa68d13b450f6be99
[odoo/odoo.git] / bin / openerp-server.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 ##############################################################################
4 #    
5 #    OpenERP, Open Source Management Solution
6 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU Affero General Public License as
10 #    published by the Free Software Foundation, either version 3 of the
11 #    License, or (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU Affero General Public License for more details.
17 #
18 #    You should have received a copy of the GNU Affero General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
20 #
21 ##############################################################################
22
23 """
24 OpenERP - Server
25 OpenERP is an ERP+CRM program for small and medium businesses.
26
27 The whole source code is distributed under the terms of the
28 GNU Public Licence.
29
30 (c) 2003-TODAY, Fabien Pinckaers - Tiny sprl
31 """
32
33 #----------------------------------------------------------
34 # python imports
35 #----------------------------------------------------------
36 import sys
37 import os
38 import signal
39 import pwd
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 import release
53 __author__ = release.author
54 __version__ = release.version
55
56 # We DON't log this using the standard logger, because we might mess
57 # with the logfile's permissions. Just do a quick exit here.
58 if pwd.getpwuid(os.getuid())[0] == 'root' :
59         sys.stderr.write("Attempted to run OpenERP server as root. This is not good, aborting.\n")
60         sys.exit(1)
61
62 #----------------------------------------------------------
63 # get logger
64 #----------------------------------------------------------
65 import netsvc
66 logger = netsvc.Logger()
67
68 #-----------------------------------------------------------------------
69 # import the tools module so that the commandline parameters are parsed
70 #-----------------------------------------------------------------------
71 import tools
72
73 logger.notifyChannel("server", netsvc.LOG_INFO, "version - %s" % release.version )
74 for name, value in [('addons_path', tools.config['addons_path']),
75                     ('database hostname', tools.config['db_host'] or 'localhost'),
76                     ('database port', tools.config['db_port'] or '5432'),
77                     ('database user', tools.config['db_user'])]:
78     logger.notifyChannel("server", netsvc.LOG_INFO, "%s - %s" % ( name, value ))
79
80 import time
81
82 if sys.platform == 'win32':
83     import mx.DateTime
84     mx.DateTime.strptime = lambda x, y: mx.DateTime.mktime(time.strptime(x, y))
85
86 #----------------------------------------------------------
87 # init net service
88 #----------------------------------------------------------
89 logger.notifyChannel("objects", netsvc.LOG_INFO, 'initialising distributed objects services')
90
91 #---------------------------------------------------------------
92 # connect to the database and initialize it with base if needed
93 #---------------------------------------------------------------
94 import pooler
95
96 #----------------------------------------------------------
97 # import basic modules
98 #----------------------------------------------------------
99 import osv
100 import workflow
101 import report
102 import service
103
104 #----------------------------------------------------------
105 # import addons
106 #----------------------------------------------------------
107
108 import addons
109
110 #----------------------------------------------------------
111 # Load and update databases if requested
112 #----------------------------------------------------------
113
114 import service.http_server
115
116 if not ( tools.config["stop_after_init"] or \
117         tools.config["translate_in"] or \
118         tools.config["translate_out"] ):
119         service.http_server.init_servers()
120         service.http_server.init_xmlrpc()
121
122         import service.netrpc_server
123         service.netrpc_server.init_servers()
124
125 if tools.config['db_name']:
126     for db in tools.config['db_name'].split(','):
127         pooler.get_db_and_pool(db, update_module=tools.config['init'] or tools.config['update'])
128
129 #----------------------------------------------------------
130 # translation stuff
131 #----------------------------------------------------------
132 if tools.config["translate_out"]:
133     import csv
134
135     if tools.config["language"]:
136         msg = "language %s" % (tools.config["language"],)
137     else:
138         msg = "new language"
139     logger.notifyChannel("init", netsvc.LOG_INFO, 
140                          'writing translation file for %s to %s' % (msg, 
141                                                                     tools.config["translate_out"]))
142
143     fileformat = os.path.splitext(tools.config["translate_out"])[-1][1:].lower()
144     buf = file(tools.config["translate_out"], "w")
145     tools.trans_export(tools.config["language"], tools.config["translate_modules"], buf, fileformat)
146     buf.close()
147
148     logger.notifyChannel("init", netsvc.LOG_INFO, 'translation file written successfully')
149     sys.exit(0)
150
151 if tools.config["translate_in"]:
152     tools.trans_load(tools.config["db_name"], 
153                      tools.config["translate_in"], 
154                      tools.config["language"])
155     sys.exit(0)
156
157 #----------------------------------------------------------------------------------
158 # if we don't want the server to continue to run after initialization, we quit here
159 #----------------------------------------------------------------------------------
160 if tools.config["stop_after_init"]:
161     sys.exit(0)
162
163
164 #----------------------------------------------------------
165 # Launch Servers
166 #----------------------------------------------------------
167
168 LST_SIGNALS = ['SIGINT', 'SIGTERM']
169 if os.name == 'posix':
170     LST_SIGNALS.extend(['SIGUSR1','SIGQUIT'])
171
172
173 SIGNALS = dict(
174     [(getattr(signal, sign), sign) for sign in LST_SIGNALS]
175 )
176
177 def handler(signum, _):
178     """
179     :param signum: the signal number
180     :param _: 
181     """
182     netsvc.Agent.quit()
183     netsvc.Server.quitAll()
184     if tools.config['pidfile']:
185         os.unlink(tools.config['pidfile'])
186     logger.notifyChannel('shutdown', netsvc.LOG_INFO, 
187                          "Shutdown Server! - %s" % ( SIGNALS[signum], ))
188     logger.shutdown()
189     sys.exit(0)
190
191 for signum in SIGNALS:
192     signal.signal(signum, handler)
193
194 if tools.config['pidfile']:
195     fd = open(tools.config['pidfile'], 'w')
196     pidtext = "%d" % (os.getpid())
197     fd.write(pidtext)
198     fd.close()
199
200
201 netsvc.Server.startAll()
202
203 logger.notifyChannel("web-services", netsvc.LOG_INFO, 
204                      'the server is running, waiting for connections...')
205
206 while True:
207     time.sleep(60)
208
209 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
210