2 # -*- encoding: utf-8 -*-
3 ##############################################################################
5 # Copyright (c) 2004-2008 Tiny SPRL (http://tiny.be) All Rights Reserved.
9 # WARNING: This program as such is intended to be used by professional
10 # programmers who take the whole responsability of assessing all potential
11 # consequences resulting from its eventual inadequacies and bugs
12 # End users who are looking for a ready-to-use solution with commercial
13 # garantees and support are strongly adviced to contract a Free Software
16 # This program is Free Software; you can redistribute it and/or
17 # modify it under the terms of the GNU General Public License
18 # as published by the Free Software Foundation; either version 2
19 # of the License, or (at your option) any later version.
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 ###############################################################################
33 Tiny ERP is an ERP+CRM program for small and medium businesses.
35 The whole source code is distributed under the terms of the
38 (c) 2003-TODAY, Fabien Pinckaers - Tiny sprl
41 __author__ = release.author
42 __version__ = release.version
45 __builtin__.__dict__['tinyerp_version'] = __version__
46 __builtin__.__dict__['tinyerp_version_string'] = "Tiny ERP Server " + __version__
50 #----------------------------------------------------------
52 #----------------------------------------------------------
53 import sys, os, signal
54 #----------------------------------------------------------
55 # ubuntu 8.04 has obsoleted `pyxml` package and installs here.
56 # the path needs to be updated before any `import xml`
57 # TODO: remove PyXML dependencies, use lxml instead.
58 #----------------------------------------------------------
59 _oldxml = '/usr/lib/python%s/site-packages/oldxml' % sys.version[:3]
60 if os.path.exists(_oldxml):
61 sys.path.append(_oldxml)
63 #----------------------------------------------------------
65 #----------------------------------------------------------
70 logger = netsvc.Logger()
72 #-----------------------------------------------------------------------
73 # import the tools module so that the commandline parameters are parsed
74 #-----------------------------------------------------------------------
78 if sys.platform == 'win32':
80 mx.DateTime.strptime = lambda x, y: mx.DateTime.mktime(time.strptime(x, y))
82 #os.chdir(tools.file_path_root)
84 #----------------------------------------------------------
86 #----------------------------------------------------------
87 logger.notifyChannel("objects", netsvc.LOG_INFO, 'initialising distributed objects services')
89 dispatcher = netsvc.Dispatcher()
90 dispatcher.monitor(signal.SIGINT)
92 #---------------------------------------------------------------
93 # connect to the database and initialize it with base if needed
94 #---------------------------------------------------------------
95 logger.notifyChannel("init", netsvc.LOG_INFO, 'connecting to database')
100 # try to connect to the database
104 except psycopg.OperationalError, err:
105 logger.notifyChannel("init", netsvc.LOG_ERROR, "could not connect to database '%s'!" % (tools.config["db_name"],))
107 msg = str(err).replace("FATAL:", "").strip()
108 db_msg = "database \"%s\" does not exist" % (tools.config["db_name"],)
110 # Note: this is ugly but since psycopg only uses one exception for all errors
111 # I don't think it's possible to do differently
114 this database does not exist
116 You need to create it using the command:
118 createdb --encoding=UNICODE '%s'
120 When you run tinyerp-server for the first time it will initialise the
121 database. You may force this behaviour at a later time by using the command:
123 ./tinyerp-server --init=all
125 Two accounts will be created by default:
126 1. login: admin password : admin
127 2. login: demo password : demo
129 """ % (tools.config["db_name"])
134 db_name = tools.config["db_name"]
136 # test whether it is needed to initialize the db (the db is empty)
138 cr = pooler.get_db_only(db_name).cursor()
139 except psycopg.OperationalError:
140 logger.notifyChannel("init", netsvc.LOG_INFO, "could not connect to database '%s'!" % db_name,)
143 cr.execute("SELECT relname FROM pg_class WHERE relkind='r' AND relname='ir_ui_menu'")
144 if len(cr.fetchall())==0:
146 logger.notifyChannel("init", netsvc.LOG_INFO, "init db")
148 # in that case, force --init=all
149 tools.config["init"]["all"] = 1
150 tools.config['update']['all'] = 1
151 if not tools.config['without_demo']:
152 tools.config["demo"]['all'] = 1
155 #----------------------------------------------------------
156 # launch modules install/upgrade/removes if needed
157 #----------------------------------------------------------
158 if tools.config['upgrade']:
159 print 'Upgrading new modules...'
161 (toinit, toupdate) = tools.upgrade.upgrade()
163 tools.config['init'][m] = 1
165 tools.config['update'][m] = 1
167 #----------------------------------------------------------
168 # import basic modules
169 #----------------------------------------------------------
170 import osv, workflow, report, service
172 #----------------------------------------------------------
174 #----------------------------------------------------------
177 addons.register_classes()
178 if tools.config['init'] or tools.config['update']:
179 pooler.get_db_and_pool(tools.config['db_name'], update_module=True)
181 #----------------------------------------------------------
183 #----------------------------------------------------------
184 if tools.config["translate_out"]:
187 if tools.config["language"]:
188 msg = "language %s" % (tools.config["language"],)
191 logger.notifyChannel("init", netsvc.LOG_INFO, 'writing translation file for %s to %s' % (msg, tools.config["translate_out"]))
193 fileformat = os.path.splitext(tools.config["translate_out"])[-1][1:].lower()
194 buf = file(tools.config["translate_out"], "w")
195 tools.trans_export(tools.config["language"], tools.config["translate_modules"], buf, fileformat)
198 logger.notifyChannel("init", netsvc.LOG_INFO, 'translation file written succesfully')
201 if tools.config["translate_in"]:
202 tools.trans_load(tools.config["db_name"], tools.config["translate_in"], tools.config["language"])
205 #----------------------------------------------------------------------------------
206 # if we don't want the server to continue to run after initialization, we quit here
207 #----------------------------------------------------------------------------------
208 if tools.config["stop_after_init"]:
212 #----------------------------------------------------------
214 #----------------------------------------------------------
216 if tools.config['xmlrpc']:
218 port = int(tools.config["port"])
220 logger.notifyChannel("init", netsvc.LOG_ERROR, "invalid port '%s'!" % (tools.config["port"],))
222 interface = tools.config["interface"]
223 secure = tools.config["secure"]
225 httpd = netsvc.HttpDaemon(interface, port, secure)
227 if tools.config["xmlrpc"]:
228 xml_gw = netsvc.xmlrpc.RpcGateway('web-services')
229 httpd.attach("/xmlrpc", xml_gw)
230 logger.notifyChannel("web-services", netsvc.LOG_INFO,
231 "starting XML-RPC" + \
232 (tools.config['secure'] and ' Secure' or '') + \
233 " services, port " + str(port))
236 #if tools.config["soap"]:
237 # soap_gw = netsvc.xmlrpc.RpcGateway('web-services')
238 # httpd.attach("/soap", soap_gw )
239 # logger.notifyChannel("web-services", netsvc.LOG_INFO, 'starting SOAP services, port '+str(port))
242 if tools.config['netrpc']:
244 netport = int(tools.config["netport"])
246 logger.notifyChannel("init", netsvc.LOG_ERROR, "invalid port '%s'!" % (tools.config["netport"],))
248 netinterface = tools.config["netinterface"]
250 tinySocket = netsvc.TinySocketServerThread(netinterface, netport, False)
251 logger.notifyChannel("web-services", netsvc.LOG_INFO, "starting netrpc service, port "+str(netport))
254 def handler(signum, frame):
255 from tools import config
256 if tools.config['netrpc']:
258 if tools.config['xmlrpc']:
261 if config['pidfile']:
262 os.unlink(config['pidfile'])
265 from tools import config
266 if config['pidfile']:
267 fd = open(config['pidfile'], 'w')
268 pidtext = "%d" % (os.getpid())
272 signal.signal(signal.SIGINT, handler)
273 signal.signal(signal.SIGTERM, handler)
275 logger.notifyChannel("web-services", netsvc.LOG_INFO, 'the server is running, waiting for connections...')
276 if tools.config['netrpc']:
278 if tools.config['xmlrpc']:
285 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: