2014-03-19 08:53:43 +00:00
|
|
|
import logging
|
2014-03-20 14:33:46 +00:00
|
|
|
import os
|
|
|
|
import sys
|
2014-05-22 10:21:51 +00:00
|
|
|
import zipfile
|
2014-03-19 08:53:43 +00:00
|
|
|
from os.path import join as opj
|
|
|
|
|
|
|
|
import openerp
|
2014-03-20 14:33:46 +00:00
|
|
|
from openerp.osv import osv
|
2014-03-19 08:53:43 +00:00
|
|
|
from openerp.tools import convert_file
|
2014-05-22 10:21:51 +00:00
|
|
|
from openerp.tools.translate import _
|
2014-12-16 14:56:41 +00:00
|
|
|
from openerp import tools
|
2014-03-19 08:53:43 +00:00
|
|
|
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
2014-05-22 10:21:51 +00:00
|
|
|
MAX_FILE_SIZE = 100 * 1024 * 1024 # in megabytes
|
|
|
|
|
2014-03-19 08:53:43 +00:00
|
|
|
class view(osv.osv):
|
|
|
|
_inherit = "ir.module.module"
|
|
|
|
|
2014-06-11 07:55:17 +00:00
|
|
|
def import_module(self, cr, uid, module, path, force=False, context=None):
|
2014-03-19 08:53:43 +00:00
|
|
|
known_mods = self.browse(cr, uid, self.search(cr, uid, []))
|
|
|
|
known_mods_names = dict([(m.name, m) for m in known_mods])
|
2014-08-05 12:13:01 +00:00
|
|
|
installed_mods = [m.name for m in known_mods if m.state == 'installed']
|
2014-03-19 08:53:43 +00:00
|
|
|
|
|
|
|
terp = openerp.modules.load_information_from_description_file(module, mod_path=path)
|
|
|
|
values = self.get_values_from_terp(terp)
|
|
|
|
|
2014-08-05 12:13:01 +00:00
|
|
|
unmet_dependencies = set(terp['depends']).difference(installed_mods)
|
2014-03-19 08:53:43 +00:00
|
|
|
if unmet_dependencies:
|
2014-08-05 12:13:01 +00:00
|
|
|
msg = _("Unmet module dependencies: %s")
|
|
|
|
raise osv.except_osv(_('Error !'), msg % ', '.join(unmet_dependencies))
|
2014-03-19 08:53:43 +00:00
|
|
|
|
2014-08-05 12:13:01 +00:00
|
|
|
mod = known_mods_names.get(module)
|
2014-03-19 08:53:43 +00:00
|
|
|
if mod:
|
2014-08-05 12:13:01 +00:00
|
|
|
self.write(cr, uid, mod.id, dict(state='installed', **values))
|
2014-06-11 07:55:17 +00:00
|
|
|
mode = 'update' if not force else 'init'
|
2014-03-19 08:53:43 +00:00
|
|
|
else:
|
|
|
|
assert terp.get('installable', True), "Module not installable"
|
2014-08-05 12:13:01 +00:00
|
|
|
self.create(cr, uid, dict(name=module, state='installed', **values))
|
2014-03-19 08:53:43 +00:00
|
|
|
mode = 'init'
|
|
|
|
|
|
|
|
for kind in ['data', 'init_xml', 'update_xml']:
|
|
|
|
for filename in terp[kind]:
|
|
|
|
_logger.info("module %s: loading %s", module, filename)
|
|
|
|
noupdate = False
|
|
|
|
if filename.endswith('.csv') and kind in ('init', 'init_xml'):
|
|
|
|
noupdate = True
|
|
|
|
pathname = opj(path, filename)
|
|
|
|
idref = {}
|
|
|
|
convert_file(cr, module, filename, idref, mode=mode, noupdate=noupdate, kind=kind, pathname=pathname)
|
|
|
|
|
2014-03-20 14:33:46 +00:00
|
|
|
path_static = opj(path, 'static')
|
2014-03-24 15:17:11 +00:00
|
|
|
ir_attach = self.pool['ir.attachment']
|
2014-03-20 14:33:46 +00:00
|
|
|
if os.path.isdir(path_static):
|
2014-08-05 12:13:01 +00:00
|
|
|
for root, dirs, files in os.walk(path_static):
|
2014-03-20 14:33:46 +00:00
|
|
|
for static_file in files:
|
|
|
|
full_path = opj(root, static_file)
|
|
|
|
with open(full_path, 'r') as fp:
|
|
|
|
data = fp.read().encode('base64')
|
2014-03-24 15:17:11 +00:00
|
|
|
url_path = '/%s%s' % (module, full_path.split(path)[1].replace(os.path.sep, '/'))
|
2014-03-20 14:33:46 +00:00
|
|
|
url_path = url_path.decode(sys.getfilesystemencoding())
|
|
|
|
filename = os.path.split(url_path)[1]
|
|
|
|
values = dict(
|
|
|
|
name=filename,
|
|
|
|
datas_fname=filename,
|
|
|
|
url=url_path,
|
|
|
|
res_model='ir.ui.view',
|
|
|
|
type='binary',
|
|
|
|
datas=data,
|
|
|
|
)
|
2014-03-24 15:17:11 +00:00
|
|
|
att_id = ir_attach.search(cr, uid, [('url', '=', url_path), ('type', '=', 'binary'), ('res_model', '=', 'ir.ui.view')], context=context)
|
|
|
|
if att_id:
|
|
|
|
ir_attach.write(cr, uid, att_id, values, context=context)
|
|
|
|
else:
|
|
|
|
ir_attach.create(cr, uid, values, context=context)
|
2014-03-20 14:33:46 +00:00
|
|
|
|
2014-03-19 08:53:43 +00:00
|
|
|
return True
|
2014-03-20 14:33:46 +00:00
|
|
|
|
2014-06-11 07:55:17 +00:00
|
|
|
def import_zipfile(self, cr, uid, module_file, force=False, context=None):
|
2014-05-22 10:21:51 +00:00
|
|
|
if not module_file:
|
|
|
|
raise Exception("No file sent.")
|
|
|
|
if not zipfile.is_zipfile(module_file):
|
|
|
|
raise osv.except_osv(_('Error !'), _('File is not a zip file!'))
|
|
|
|
|
|
|
|
success = []
|
|
|
|
errors = dict()
|
2014-05-22 11:39:52 +00:00
|
|
|
module_names = []
|
2014-05-22 10:21:51 +00:00
|
|
|
with zipfile.ZipFile(module_file, "r") as z:
|
|
|
|
for zf in z.filelist:
|
|
|
|
if zf.file_size > MAX_FILE_SIZE:
|
2014-08-05 12:13:01 +00:00
|
|
|
msg = _("File '%s' exceed maximum allowed file size")
|
|
|
|
raise osv.except_osv(_('Error !'), msg % zf.filename)
|
2014-05-22 10:21:51 +00:00
|
|
|
|
|
|
|
with openerp.tools.osutil.tempdir() as module_dir:
|
|
|
|
z.extractall(module_dir)
|
|
|
|
dirs = [d for d in os.listdir(module_dir) if os.path.isdir(opj(module_dir, d))]
|
|
|
|
for mod_name in dirs:
|
2014-05-22 11:39:52 +00:00
|
|
|
module_names.append(mod_name)
|
2014-05-22 10:21:51 +00:00
|
|
|
try:
|
|
|
|
# assert mod_name.startswith('theme_')
|
|
|
|
path = opj(module_dir, mod_name)
|
2014-06-11 07:55:17 +00:00
|
|
|
self.import_module(cr, uid, mod_name, path, force=force, context=context)
|
2014-05-22 10:21:51 +00:00
|
|
|
success.append(mod_name)
|
|
|
|
except Exception, e:
|
2015-11-06 11:03:35 +00:00
|
|
|
_logger.exception('Error while importing module')
|
2014-12-16 14:56:41 +00:00
|
|
|
errors[mod_name] = tools.ustr(e)
|
2014-05-22 10:21:51 +00:00
|
|
|
r = ["Successfully imported module '%s'" % mod for mod in success]
|
|
|
|
for mod, error in errors.items():
|
|
|
|
r.append("Error while importing module '%s': %r" % (mod, error))
|
2014-05-22 11:39:52 +00:00
|
|
|
return '\n'.join(r), module_names
|