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