~stephane-openerp/openobject-server/python_lib

« back to all changes in this revision

Viewing changes to bin/addons/__init__.py

  • Committer: Stephane Wirtel
  • Date: 2010-01-31 21:49:48 UTC
  • Revision ID: stephane@openerp.com-20100131214948-a3p93d5ml7tir1f0
[REF] Extract the Logger class from the netsvc module

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
from zipfile import PyZipFile, ZIP_DEFLATED
42
42
from cStringIO import StringIO
43
43
 
44
 
 
45
 
logger = netsvc.Logger()
 
44
from logger import Logger, LOG_ERROR, LOG_WARNING, LOG_INFO, LOG_CRITICAL, LOG_DEBUG
 
45
 
 
46
 
 
47
logger = Logger()
46
48
 
47
49
_ad = os.path.abspath(opj(tools.config['root_path'], 'addons'))     # default addons path (base)
48
50
ad_paths= map(lambda m: os.path.abspath(m.strip()),tools.config['addons_path'].split(','))
167
169
 
168
170
    if downloaded:
169
171
        return opj(_ad, module)
170
 
    logger.notifyChannel('init', netsvc.LOG_WARNING, 'module %s: module not found' % (module,))
 
172
    logger.notifyChannel('init', LOG_WARNING, 'module %s: module not found' % (module,))
171
173
    return False
172
174
 
173
175
 
322
324
        if not mod_path or not terp_file:
323
325
            global not_loaded
324
326
            not_loaded.append(module)
325
 
            logger.notifyChannel('init', netsvc.LOG_WARNING, 'module %s: not installable' % (module))
 
327
            logger.notifyChannel('init', LOG_WARNING, 'module %s: not installable' % (module))
326
328
            raise osv.osv.except_osv('Error!',"Module '%s' was not found" % (module,))
327
329
 
328
330
        if os.path.isfile(terp_file) or zipfile.is_zipfile(mod_path+'.zip'):
329
331
            try:
330
332
                info = eval(tools.file_open(terp_file).read())
331
333
            except:
332
 
                logger.notifyChannel('init', netsvc.LOG_ERROR, 'module %s: eval file %s' % (module, terp_file))
 
334
                logger.notifyChannel('init', LOG_ERROR, 'module %s: eval file %s' % (module, terp_file))
333
335
                raise
334
336
            if info.get('installable', True):
335
337
                packages.append((module, info.get('depends', []), info))
363
365
 
364
366
    for package in later:
365
367
        unmet_deps = filter(lambda p: p not in graph, dependencies[package])
366
 
        logger.notifyChannel('init', netsvc.LOG_ERROR, 'module %s: Unmet dependencies: %s' % (package, ', '.join(unmet_deps)))
 
368
        logger.notifyChannel('init', LOG_ERROR, 'module %s: Unmet dependencies: %s' % (package, ', '.join(unmet_deps)))
367
369
 
368
370
    result = len(graph) - len_graph
369
371
    if result != len(module_list):
370
 
        logger.notifyChannel('init', netsvc.LOG_WARNING, 'Not all modules have loaded.')
 
372
        logger.notifyChannel('init', LOG_WARNING, 'Not all modules have loaded.')
371
373
    return result
372
374
 
373
375
 
374
376
def init_module_objects(cr, module_name, obj_list):
375
 
    logger.notifyChannel('init', netsvc.LOG_INFO, 'module %s: creating or updating database tables' % module_name)
 
377
    logger.notifyChannel('init', LOG_INFO, 'module %s: creating or updating database tables' % module_name)
376
378
    todo = []
377
379
    for obj in obj_list:
378
380
        try:
398
400
    def log(e):
399
401
        mt = isinstance(e, zipimport.ZipImportError) and 'zip ' or ''
400
402
        msg = "Couldn't load %smodule %s" % (mt, m)
401
 
        logger.notifyChannel('init', netsvc.LOG_CRITICAL, msg)
402
 
        logger.notifyChannel('init', netsvc.LOG_CRITICAL, e)
 
403
        logger.notifyChannel('init', LOG_CRITICAL, msg)
 
404
        logger.notifyChannel('init', LOG_CRITICAL, e)
403
405
 
404
406
    global loaded
405
407
    if m in loaded:
406
408
        return
407
 
    logger.notifyChannel('init', netsvc.LOG_INFO, 'module %s: registering objects' % m)
 
409
    logger.notifyChannel('init', LOG_INFO, 'module %s: registering objects' % m)
408
410
    mod_path = get_module_path(m)
409
411
 
410
412
    try:
554
556
                        fp2.seek(0)
555
557
                        try:
556
558
                            mod = imp.load_source(name, pyfile, fp2)
557
 
                            logger.notifyChannel('migration', netsvc.LOG_INFO, 'module %(addon)s: Running migration %(version)s %(name)s' % mergedict({'name': mod.__name__}, strfmt))
 
559
                            logger.notifyChannel('migration', LOG_INFO, 'module %(addon)s: Running migration %(version)s %(name)s' % mergedict({'name': mod.__name__}, strfmt))
558
560
                            mod.migrate(self.cr, pkg.installed_version)
559
561
                        except ImportError:
560
 
                            logger.notifyChannel('migration', netsvc.LOG_ERROR, 'module %(addon)s: Unable to load %(stage)s-migration file %(file)s' % mergedict({'file': pyfile}, strfmt))
 
562
                            logger.notifyChannel('migration', LOG_ERROR, 'module %(addon)s: Unable to load %(stage)s-migration file %(file)s' % mergedict({'file': pyfile}, strfmt))
561
563
                            raise
562
564
                        except AttributeError:
563
 
                            logger.notifyChannel('migration', netsvc.LOG_ERROR, 'module %(addon)s: Each %(stage)s-migration file must have a "migrate(cr, installed_version)" function' % strfmt)
 
565
                            logger.notifyChannel('migration', LOG_ERROR, 'module %(addon)s: Each %(stage)s-migration file must have a "migrate(cr, installed_version)" function' % strfmt)
564
566
                        except:
565
567
                            raise
566
568
                    finally:
587
589
    has_updates = False
588
590
    modobj = None
589
591
 
590
 
    logger.notifyChannel('init', netsvc.LOG_DEBUG, 'loading %d packages..' % len(graph))
 
592
    logger.notifyChannel('init', LOG_DEBUG, 'loading %d packages..' % len(graph))
591
593
    
592
594
    for package in graph:
593
 
        logger.notifyChannel('init', netsvc.LOG_INFO, 'module %s: loading objects' % package.name)
 
595
        logger.notifyChannel('init', LOG_INFO, 'module %s: loading objects' % package.name)
594
596
        migrations.migrate_module(package, 'pre')
595
597
        register_class(package.name)
596
598
        modules = pool.instanciate(package.name, cr)
630
632
                    'certificate': package.data.get('certificate') or None,
631
633
                    })
632
634
                for filename in package.data.get('%s_xml' % kind, []):
633
 
                    logger.notifyChannel('init', netsvc.LOG_INFO, 'module %s: loading %s' % (m, filename))
 
635
                    logger.notifyChannel('init', LOG_INFO, 'module %s: loading %s' % (m, filename))
634
636
                    name, ext = os.path.splitext(filename)
635
637
                    fp = tools.file_open(opj(m, filename))
636
638
                    if ext == '.csv':
648
650
                status['progress'] = (float(statusi)+0.75) / len(graph)
649
651
                for xml in package.data.get('demo_xml', []):
650
652
                    name, ext = os.path.splitext(xml)
651
 
                    logger.notifyChannel('init', netsvc.LOG_INFO, 'module %s: loading %s' % (m, xml))
 
653
                    logger.notifyChannel('init', LOG_INFO, 'module %s: loading %s' % (m, xml))
652
654
                    fp = tools.file_open(opj(m, xml))
653
655
                    if ext == '.csv':
654
656
                        tools.convert_csv_import(cr, m, os.path.basename(xml), fp.read(), idref, mode=mode, noupdate=True)
692
694
    if cr:
693
695
       cr.execute("SELECT relname FROM pg_class WHERE relkind='r' AND relname='ir_module_module'")
694
696
       if len(cr.fetchall())==0:    
695
 
           logger.notifyChannel("init", netsvc.LOG_INFO, "init db")
 
697
           logger.notifyChannel("init", LOG_INFO, "init db")
696
698
           tools.init_db(cr)
697
699
#           cr.execute("update res_users set password=%s where id=%s",('admin',1))
698
700
           # in that case, force --init=all
719
721
            return
720
722
        if update_module:
721
723
            modobj = pool.get('ir.module.module')
722
 
            logger.notifyChannel('init', netsvc.LOG_INFO, 'updating modules list')
 
724
            logger.notifyChannel('init', LOG_INFO, 'updating modules list')
723
725
            if ('base' in tools.config['init']) or ('base' in tools.config['update']):
724
726
                modobj.update_list(cr, 1)
725
727
 
754
756
            if new_modules_in_graph == 0:
755
757
                # nothing to load
756
758
                break
757
 
            logger.notifyChannel('init', netsvc.LOG_DEBUG, 'Updating graph with %d more modules' % (len(module_list)))
 
759
            logger.notifyChannel('init', LOG_DEBUG, 'Updating graph with %d more modules' % (len(module_list)))
758
760
            r = load_module_graph(cr, graph, status, report=report)
759
761
            has_updates = has_updates or r
760
762
 
761
763
        if has_updates:
762
764
            cr.execute("""select model,name from ir_model where id not in (select model_id from ir_model_access)""")
763
765
            for (model, name) in cr.fetchall():
764
 
                logger.notifyChannel('init', netsvc.LOG_WARNING, 'object %s (%s) has no access rules!' % (model, name))
 
766
                logger.notifyChannel('init', LOG_WARNING, 'object %s (%s) has no access rules!' % (model, name))
765
767
 
766
768
            cr.execute("SELECT model from ir_model")
767
769
            for (model,) in cr.fetchall():
770
772
                    obj._check_removed_columns(cr, log=True)
771
773
 
772
774
        if report.get_report():
773
 
            logger.notifyChannel('init', netsvc.LOG_INFO, report)
 
775
            logger.notifyChannel('init', LOG_INFO, report)
774
776
 
775
777
        for kind in ('init', 'demo', 'update'):
776
778
            tools.config[kind] = {}
786
788
                    if rmod_module:
787
789
                        rmod_module.unlink(cr, uid, [rid])
788
790
                    else:
789
 
                        logger.notifyChannel('init', netsvc.LOG_ERROR, 'Could not locate %s to remove res=%d' % (rmod,rid))
 
791
                        logger.notifyChannel('init', LOG_ERROR, 'Could not locate %s to remove res=%d' % (rmod,rid))
790
792
                cr.execute('delete from ir_model_data where noupdate=%s and module=%s', (False, mod_name,))
791
793
                cr.commit()
792
794
            #
805
807
                if not cr.rowcount:
806
808
                    break
807
809
                else:
808
 
                    logger.notifyChannel('init', netsvc.LOG_INFO, 'removed %d unused menus' % (cr.rowcount,))
 
810
                    logger.notifyChannel('init', LOG_INFO, 'removed %d unused menus' % (cr.rowcount,))
809
811
 
810
812
            cr.execute("update ir_module_module set state=%s where state=%s", ('uninstalled', 'to remove',))
811
813
            cr.commit()