[FIX] orm/fields.function: function/related fields with type=m2o must properly return...
[odoo/odoo.git] / bin / pooler.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
19 #
20 ##############################################################################
21
22 pool_dic = {}
23
24 def get_db_and_pool(db_name, force_demo=False, status=None, update_module=False, pooljobs=True):
25     if not status:
26         status={}
27
28     db = get_db_only(db_name)
29
30     if db_name in pool_dic:
31         pool = pool_dic[db_name]
32     else:
33         import addons
34         import osv.osv
35         pool = osv.osv.osv_pool()
36         pool_dic[db_name] = pool
37         
38         try:
39             addons.load_modules(db, force_demo, status, update_module)
40         except Exception, e:
41             del pool_dic[db_name]
42             raise
43
44         cr = db.cursor()
45         try:
46             pool.init_set(cr, False)
47             cr.commit()
48         finally:
49             cr.close()
50
51         import report
52         report.interface.register_all(db)
53         if pooljobs:
54             pool.get('ir.cron')._poolJobs(db.dbname)
55     return db, pool
56
57
58 def restart_pool(db_name, force_demo=False, status=None, update_module=False):
59     if db_name in pool_dic:
60         pool_dic[db_name].get('ir.cron').cancel(db_name)
61         del pool_dic[db_name]
62     return get_db_and_pool(db_name, force_demo, status, update_module=update_module)
63
64
65 def get_db_only(db_name):
66     # ATTENTION:
67     # do not put this import outside this function
68     # sql_db must not be loaded before the logger is initialized.
69     # sql_db import psycopg2.tool which create a default logger if there is not.
70     # this resulting of having the logs outputed twice...
71     import sql_db
72     db = sql_db.db_connect(db_name)
73     return db
74
75
76 def get_db(db_name):
77     return get_db_and_pool(db_name)[0]
78
79
80 def get_pool(db_name, force_demo=False, status=None, update_module=False):
81     pool = get_db_and_pool(db_name, force_demo, status, update_module)[1]
82     return pool
83
84 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: