[IMP] use the openerp namespace.
[odoo/odoo.git] / openerp / addons / base / module / wizard / base_module_scan.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 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 import os
22 import glob
23 import imp
24
25 import tools
26
27 import zipfile
28 from osv import osv
29
30 class base_module_scan(osv.osv_memory):
31     """ scan module """
32
33     _name = "base.module.scan"
34     _description = "scan module"
35
36     def watch_dir(self, cr, uid, ids, context):
37         mod_obj = self.pool.get('ir.module.module')
38         all_mods = mod_obj.read(cr, uid, mod_obj.search(cr, uid, []), ['name', 'state'])
39         known_modules = [x['name'] for x in all_mods]
40         ls_ad = glob.glob(os.path.join(tools.config['addons_path'], '*', '__terp__.py'))
41         modules = [module_name_re.match(name).group(1) for name in ls_ad]
42         for fname in os.listdir(tools.config['addons_path']):
43             if zipfile.is_zipfile(fname):
44                 modules.append( fname.split('.')[0])
45         for module in modules:
46             if module in known_modules:
47                 continue
48             terp = mod_obj.get_module_info(module)
49             if not terp.get('installable', True):
50                 continue
51
52             # XXX check if this code is correct...
53             fm = imp.find_module(module)
54             try:
55                 imp.load_module(module, *fm)
56             finally:
57                 if fm[0]:
58                     fm[0].close()
59
60             values = mod_obj.get_values_from_terp(terp)
61             mod_id = mod_obj.create(cr, uid, dict(name=module, state='uninstalled', **values))
62             dependencies = terp.get('depends', [])
63             for d in dependencies:
64                 cr.execute('insert into ir_module_module_dependency (module_id,name) values (%s, %s)', (mod_id, d))
65         for module in known_modules:
66             terp = mod_obj.get_module_info(module)
67             if terp.get('installable', True):
68                 for mod in all_mods:
69                     if mod['name'] == module and mod['state'] == 'uninstallable':
70                         mod_obj.write(cr, uid, [mod['id']], {'state': 'uninstalled'})
71         return {}
72
73 base_module_scan()
74
75 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: