[IMP]: convert language import wizard into osv memory wizard
[odoo/odoo.git] / bin / addons / base / module / wizard / add_new.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #    Copyright (C) 2010 OpenERP s.a. (<http://openerp.com>).
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 import os
24 import re
25 import glob
26 import time
27 import imp
28
29 import tools
30 import wizard
31 import pooler
32
33 import zipfile
34
35 module_name_re = re.compile('.*addons.(.*?).__terp__.py$')
36
37 _info_arch = '''<?xml version="1.0"?>
38 <form string="Scan for new modules">
39   <label string="This function will check if you installed new modules in the 'addons' path of your server installation." colspan="4" />
40 </form>
41 '''
42 _info_fields = {}
43
44 class wizard_install_module(wizard.interface):
45     def watch_dir(self, cr, uid, data, context):
46         mod_obj = pooler.get_pool(cr.dbname).get('ir.module.module')
47         all_mods = mod_obj.read(cr, uid, mod_obj.search(cr, uid, []), ['name', 'state'])
48         known_modules = [x['name'] for x in all_mods]
49         ls_ad = glob.glob(os.path.join(tools.config['addons_path'], '*', '__terp__.py'))
50         modules = [module_name_re.match(name).group(1) for name in ls_ad]
51         for fname in os.listdir(tools.config['addons_path']):
52             if zipfile.is_zipfile(fname):
53                 modules.append( fname.split('.')[0])
54         for module in modules:
55             if module in known_modules:
56                 continue
57             terp = mod_obj.get_module_info(module)
58             if not terp.get('installable', True):
59                 continue
60
61             # XXX check if this code is correct...
62             fm = imp.find_module(module)
63             try:
64                 imp.load_module(module, *fm)
65             finally:
66                 if fm[0]:
67                     fm[0].close()
68
69             values = mod_obj.get_values_from_terp(terp)
70             mod_id = mod_obj.create(cr, uid, dict(name=module, state='uninstalled', **values))
71             dependencies = terp.get('depends', [])
72             for d in dependencies:
73                 cr.execute('insert into ir_module_module_dependency (module_id,name) values (%s, %s)', (mod_id, d))
74         for module in known_modules:
75             terp = mod_obj.get_module_info(module)
76             if terp.get('installable', True):
77                 for mod in all_mods:
78                     if mod['name'] == module and mod['state'] == 'uninstallable':
79                         mod_obj.write(cr, uid, [mod['id']], {'state': 'uninstalled'})
80         return {}
81
82     states = {
83         'init': {
84             'actions': [], 
85             'result': {'type':'form', 'arch': _info_arch, 'fields': _info_fields, 'state':[('end','Cancel','gtk-cancel'),('addmod','Check new modules','gtk-ok')]}
86         },
87         'addmod': {
88             'actions': [watch_dir],
89             'result': {'type':'state', 'state':'end'}
90         },
91     }
92 wizard_install_module('module.module.scan')
93
94
95 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
96