コード例 #1
0
            def __call__(self, serv, id, db_name, demo, lang, user_password='******'):
                cr = None
                try:
                    serv.actions[id]['progress'] = 0
                    cr = sql_db.db_connect(db_name).cursor()
                    tools.init_db(cr)
                    tools.config['lang'] = lang
                    cr.commit()
                    cr.close()
                    cr = None
                    pool = pooler.restart_pool(db_name, demo, serv.actions[id],
                            update_module=True)[1]

                    cr = sql_db.db_connect(db_name).cursor()

                    if lang:
                        modobj = pool.get('ir.module.module')
                        mids = modobj.search(cr, 1, [('state', '=', 'installed')])
                        modobj.update_translations(cr, 1, mids, lang)

                    cr.execute('UPDATE res_users SET password=%s, context_lang=%s, active=True WHERE login=%s', (
                        user_password, lang, 'admin'))
                    cr.execute('SELECT login, password, name ' \
                               '  FROM res_users ' \
                               ' ORDER BY login')
                    serv.actions[id]['users'] = cr.dictfetchall()
                    serv.actions[id]['clean'] = True
                    cr.commit()
                    cr.close()
                except Exception, e:
                    serv.actions[id]['clean'] = False
                    serv.actions[id]['exception'] = e
                    import traceback
                    e_str = StringIO()
                    traceback.print_exc(file=e_str)
                    traceback_str = e_str.getvalue()
                    e_str.close()
                    netsvc.Logger().notifyChannel('web-services', netsvc.LOG_ERROR, 'CREATE DATABASE\n%s' % (traceback_str))
                    serv.actions[id]['traceback'] = traceback_str
                    if cr:
                        cr.close()
コード例 #2
0
ファイル: web_services.py プロジェクト: hectord/unifield
            def __call__(self, serv, id, db_name, demo, lang, user_password='******'):
                cr = None
                try:
                    serv.actions[id]['progress'] = 0
                    cr = sql_db.db_connect(db_name).cursor()
                    tools.init_db(cr)
                    tools.config['lang'] = lang
                    cr.commit()
                    cr.close(True)
                    cr = None
                    pool = pooler.restart_pool(db_name, demo, serv.actions[id],
                            update_module=True)[1]

                    cr = sql_db.db_connect(db_name).cursor()

                    if lang:
                        modobj = pool.get('ir.module.module')
                        mids = modobj.search(cr, 1, [('state', '=', 'installed')])
                        modobj.update_translations(cr, 1, mids, lang)

                    cr.execute('UPDATE res_users SET password=%s, context_lang=%s, active=True WHERE login=%s', (
                        user_password, lang, 'admin'))
                    cr.execute('SELECT login, password, name ' \
                               '  FROM res_users ' \
                               ' ORDER BY login')
                    serv.actions[id]['users'] = cr.dictfetchall()
                    serv.actions[id]['clean'] = True
                    cr.commit()
                    cr.close(True)
                except Exception, e:
                    serv.actions[id]['clean'] = False
                    serv.actions[id]['exception'] = e
                    import traceback
                    e_str = StringIO()
                    traceback.print_exc(file=e_str)
                    traceback_str = e_str.getvalue()
                    e_str.close()
                    netsvc.Logger().notifyChannel('web-services', netsvc.LOG_ERROR, 'CREATE DATABASE\n%s' % (traceback_str))
                    serv.actions[id]['traceback'] = traceback_str
                    if cr:
                        cr.close(True)
コード例 #3
0
ファイル: __init__.py プロジェクト: lcrdcastro/viaweb
def load_modules(db, force_demo=False, status=None, update_module=False):
    if not status:
        status = {}
    cr = db.cursor()
    if cr:
        cr.execute("SELECT relname FROM pg_class WHERE relkind='r' AND relname='ir_module_module'")
        if len(cr.fetchall())==0:
            logger.notifyChannel("init", netsvc.LOG_INFO, "init db")
            tools.init_db(cr)
            tools.config["init"]["all"] = 1
            tools.config['update']['all'] = 1
            if not tools.config['without_demo']:
                tools.config["demo"]['all'] = 1
    force = []
    if force_demo:
        force.append('demo')

    # This is a brand new pool, just created in pooler.get_db_and_pool()
    pool = pooler.get_pool(cr.dbname)

    try:
        processed_modules = []
        report = tools.assertion_report()
        # NOTE: Try to also load the modules that have been marked as uninstallable previously...
        STATES_TO_LOAD = ['installed', 'to upgrade', 'uninstallable']
        if 'base' in tools.config['update'] or 'all' in tools.config['update']:
            cr.execute("update ir_module_module set state=%s where name=%s and state=%s", ('to upgrade', 'base', 'installed'))

        # STEP 1: LOAD BASE (must be done before module dependencies can be computed for later steps) 
        graph = create_graph(cr, ['base'], force)
        if not graph:
            logger.notifyChannel('init', netsvc.LOG_CRITICAL, 'module base cannot be loaded! (hint: verify addons-path)')
            raise osv.osv.except_osv(_('Could not load base module'), _('module base cannot be loaded! (hint: verify addons-path)'))
        processed_modules.extend(load_module_graph(cr, graph, status, perform_checks=(not update_module), report=report))

        if tools.config['load_language']:
            for lang in tools.config['load_language'].split(','):
                tools.load_language(cr, lang)

        # STEP 2: Mark other modules to be loaded/updated
        if update_module:
            modobj = pool.get('ir.module.module')
            logger.notifyChannel('init', netsvc.LOG_INFO, 'updating modules list')
            if ('base' in tools.config['init']) or ('base' in tools.config['update']):
                modobj.update_list(cr, 1)

            _check_module_names(cr, itertools.chain(tools.config['init'].keys(), tools.config['update'].keys()))

            mods = [k for k in tools.config['init'] if tools.config['init'][k]]
            if mods:
                ids = modobj.search(cr, 1, ['&', ('state', '=', 'uninstalled'), ('name', 'in', mods)])
                if ids:
                    modobj.button_install(cr, 1, ids)

            mods = [k for k in tools.config['update'] if tools.config['update'][k]]
            if mods:
                ids = modobj.search(cr, 1, ['&', ('state', '=', 'installed'), ('name', 'in', mods)])
                if ids:
                    modobj.button_upgrade(cr, 1, ids)

            cr.execute("update ir_module_module set state=%s where name=%s", ('installed', 'base'))

            STATES_TO_LOAD += ['to install']


        # STEP 3: Load marked modules (skipping base which was done in STEP 1)
        loop_guardrail = 0
        while True:
            loop_guardrail += 1
            if loop_guardrail > 100:
                raise ValueError('Possible recursive module tree detected, aborting.')
            cr.execute("SELECT name from ir_module_module WHERE state IN %s" ,(tuple(STATES_TO_LOAD),))

            module_list = [name for (name,) in cr.fetchall() if name not in graph]
            if not module_list:
                break

            new_modules_in_graph = upgrade_graph(graph, cr, module_list, force)
            if new_modules_in_graph == 0:
                # nothing to load
                break

            logger.notifyChannel('init', netsvc.LOG_DEBUG, 'Updating graph with %d more modules' % (len(module_list)))
            processed_modules.extend(load_module_graph(cr, graph, status, report=report, skip_modules=processed_modules))

        # load custom models
        cr.execute('select model from ir_model where state=%s', ('manual',))
        for model in cr.dictfetchall():
            pool.get('ir.model').instanciate(cr, 1, model['model'], {})

        # STEP 4: Finish and cleanup
        if processed_modules:
            cr.execute("""select model,name from ir_model where id NOT IN (select distinct model_id from ir_model_access)""")
            for (model, name) in cr.fetchall():
                model_obj = pool.get(model)
                if model_obj and not isinstance(model_obj, osv.osv.osv_memory):
                    logger.notifyChannel('init', netsvc.LOG_WARNING, 'object %s (%s) has no access rules!' % (model, name))

            # Temporary warning while we remove access rights on osv_memory objects, as they have
            # been replaced by owner-only access rights
            cr.execute("""select distinct mod.model, mod.name from ir_model_access acc, ir_model mod where acc.model_id = mod.id""")
            for (model, name) in cr.fetchall():
                model_obj = pool.get(model)
                if isinstance(model_obj, osv.osv.osv_memory):
                    logger.notifyChannel('init', netsvc.LOG_WARNING, 'In-memory object %s (%s) should not have explicit access rules!' % (model, name))

            cr.execute("SELECT model from ir_model")
            for (model,) in cr.fetchall():
                obj = pool.get(model)
                if obj:
                    obj._check_removed_columns(cr, log=True)
                else:
                    logger.notifyChannel('init', netsvc.LOG_WARNING, "Model %s is referenced but not present in the orm pool!" % model)

            # Cleanup orphan records
            pool.get('ir.model.data')._process_end(cr, 1, processed_modules)

        if report.get_report():
            logger.notifyChannel('init', netsvc.LOG_INFO, report)

        for kind in ('init', 'demo', 'update'):
            tools.config[kind] = {}

        cr.commit()
        if update_module:
            cr.execute("select id,name from ir_module_module where state=%s", ('to remove',))
            for mod_id, mod_name in cr.fetchall():
                cr.execute('select model,res_id from ir_model_data where noupdate=%s and module=%s order by id desc', (False, mod_name,))
                for rmod, rid in cr.fetchall():
                    uid = 1
                    rmod_module= pool.get(rmod)
                    if rmod_module:
                        rmod_module.unlink(cr, uid, [rid])
                    else:
                        logger.notifyChannel('init', netsvc.LOG_ERROR, 'Could not locate %s to remove res=%d' % (rmod,rid))
                cr.execute('delete from ir_model_data where noupdate=%s and module=%s', (False, mod_name,))
                cr.commit()
            #
            # TODO: remove menu without actions of children
            #
            while True:
                cr.execute('''delete from
                        ir_ui_menu
                    where
                        (id not IN (select parent_id from ir_ui_menu where parent_id is not null))
                    and
                        (id not IN (select res_id from ir_values where model='ir.ui.menu'))
                    and
                        (id not IN (select res_id from ir_model_data where model='ir.ui.menu'))''')
                cr.commit()
                if not cr.rowcount:
                    break
                else:
                    logger.notifyChannel('init', netsvc.LOG_INFO, 'removed %d unused menus' % (cr.rowcount,))

            cr.execute("update ir_module_module set state=%s where state=%s", ('uninstalled', 'to remove',))
            cr.commit()
    finally:
        cr.close()
コード例 #4
0
def load_modules(db, force_demo=False, status=None, update_module=False):
    if not status:
        status = {}
    cr = db.cursor()
    if cr:
        cr.execute(
            "SELECT relname FROM pg_class WHERE relkind='r' AND relname='ir_module_module'"
        )
        if len(cr.fetchall()) == 0:
            logger.notifyChannel("init", netsvc.LOG_INFO, "init db")
            tools.init_db(cr)
            tools.config["init"]["all"] = 1
            tools.config['update']['all'] = 1
            if not tools.config['without_demo']:
                tools.config["demo"]['all'] = 1
    force = []
    if force_demo:
        force.append('demo')

    # This is a brand new pool, just created in pooler.get_db_and_pool()
    pool = pooler.get_pool(cr.dbname)

    try:
        processed_modules = []
        report = tools.assertion_report()
        # NOTE: Try to also load the modules that have been marked as uninstallable previously...
        STATES_TO_LOAD = ['installed', 'to upgrade', 'uninstallable']
        if 'base' in tools.config['update'] or 'all' in tools.config['update']:
            cr.execute(
                "update ir_module_module set state=%s where name=%s and state=%s",
                ('to upgrade', 'base', 'installed'))

        # STEP 1: LOAD BASE (must be done before module dependencies can be computed for later steps)
        graph = create_graph(cr, ['base'], force)
        if not graph:
            logger.notifyChannel(
                'init', netsvc.LOG_CRITICAL,
                'module base cannot be loaded! (hint: verify addons-path)')
            raise osv.osv.except_osv(
                _('Could not load base module'),
                _('module base cannot be loaded! (hint: verify addons-path)'))
        processed_modules.extend(
            load_module_graph(cr,
                              graph,
                              status,
                              perform_checks=(not update_module),
                              report=report))

        if tools.config['load_language']:
            for lang in tools.config['load_language'].split(','):
                tools.load_language(cr, lang)

        # STEP 2: Mark other modules to be loaded/updated
        if update_module:
            modobj = pool.get('ir.module.module')
            logger.notifyChannel('init', netsvc.LOG_INFO,
                                 'updating modules list')
            if ('base' in tools.config['init']) or ('base'
                                                    in tools.config['update']):
                modobj.update_list(cr, 1)

            _check_module_names(
                cr,
                itertools.chain(tools.config['init'].keys(),
                                tools.config['update'].keys()))

            mods = [k for k in tools.config['init'] if tools.config['init'][k]]
            if mods:
                ids = modobj.search(
                    cr, 1,
                    ['&', ('state', '=', 'uninstalled'), ('name', 'in', mods)])
                if ids:
                    modobj.button_install(cr, 1, ids)

            mods = [
                k for k in tools.config['update'] if tools.config['update'][k]
            ]
            if mods:
                ids = modobj.search(
                    cr, 1,
                    ['&', ('state', '=', 'installed'), ('name', 'in', mods)])
                if ids:
                    modobj.button_upgrade(cr, 1, ids)

            cr.execute("update ir_module_module set state=%s where name=%s",
                       ('installed', 'base'))

            STATES_TO_LOAD += ['to install']

        # STEP 3: Load marked modules (skipping base which was done in STEP 1)
        loop_guardrail = 0
        while True:
            loop_guardrail += 1
            if loop_guardrail > 100:
                raise ValueError(
                    'Possible recursive module tree detected, aborting.')
            cr.execute("SELECT name from ir_module_module WHERE state IN %s",
                       (tuple(STATES_TO_LOAD), ))

            module_list = [
                name for (name, ) in cr.fetchall() if name not in graph
            ]
            if not module_list:
                break

            new_modules_in_graph = upgrade_graph(graph, cr, module_list, force)
            if new_modules_in_graph == 0:
                # nothing to load
                break

            logger.notifyChannel(
                'init', netsvc.LOG_DEBUG,
                'Updating graph with %d more modules' % (len(module_list)))
            processed_modules.extend(
                load_module_graph(cr,
                                  graph,
                                  status,
                                  report=report,
                                  skip_modules=processed_modules))

        # load custom models
        cr.execute('select model from ir_model where state=%s', ('manual', ))
        for model in cr.dictfetchall():
            pool.get('ir.model').instanciate(cr, 1, model['model'], {})

        # STEP 4: Finish and cleanup
        if processed_modules:
            cr.execute(
                """select model,name from ir_model where id NOT IN (select distinct model_id from ir_model_access)"""
            )
            for (model, name) in cr.fetchall():
                model_obj = pool.get(model)
                if model_obj and not isinstance(model_obj, osv.osv.osv_memory):
                    logger.notifyChannel(
                        'init', netsvc.LOG_WARNING,
                        'object %s (%s) has no access rules!' % (model, name))

            # Temporary warning while we remove access rights on osv_memory objects, as they have
            # been replaced by owner-only access rights
            cr.execute(
                """select distinct mod.model, mod.name from ir_model_access acc, ir_model mod where acc.model_id = mod.id"""
            )
            for (model, name) in cr.fetchall():
                model_obj = pool.get(model)
                if isinstance(model_obj, osv.osv.osv_memory):
                    logger.notifyChannel(
                        'init', netsvc.LOG_WARNING,
                        'In-memory object %s (%s) should not have explicit access rules!'
                        % (model, name))

            cr.execute("SELECT model from ir_model")
            for (model, ) in cr.fetchall():
                obj = pool.get(model)
                if obj:
                    obj._check_removed_columns(cr, log=True)
                else:
                    logger.notifyChannel(
                        'init', netsvc.LOG_WARNING,
                        "Model %s is referenced but not present in the orm pool!"
                        % model)

            # Cleanup orphan records
            pool.get('ir.model.data')._process_end(cr, 1, processed_modules)

        if report.get_report():
            logger.notifyChannel('init', netsvc.LOG_INFO, report)

        for kind in ('init', 'demo', 'update'):
            tools.config[kind] = {}

        cr.commit()
        if update_module:
            cr.execute("select id,name from ir_module_module where state=%s",
                       ('to remove', ))
            for mod_id, mod_name in cr.fetchall():
                cr.execute(
                    'select model,res_id from ir_model_data where noupdate=%s and module=%s order by id desc',
                    (
                        False,
                        mod_name,
                    ))
                for rmod, rid in cr.fetchall():
                    uid = 1
                    rmod_module = pool.get(rmod)
                    if rmod_module:
                        rmod_module.unlink(cr, uid, [rid])
                    else:
                        logger.notifyChannel(
                            'init', netsvc.LOG_ERROR,
                            'Could not locate %s to remove res=%d' %
                            (rmod, rid))
                cr.execute(
                    'delete from ir_model_data where noupdate=%s and module=%s',
                    (
                        False,
                        mod_name,
                    ))
                cr.commit()
            #
            # TODO: remove menu without actions of children
            #
            while True:
                cr.execute('''delete from
                        ir_ui_menu
                    where
                        (id not IN (select parent_id from ir_ui_menu where parent_id is not null))
                    and
                        (id not IN (select res_id from ir_values where model='ir.ui.menu'))
                    and
                        (id not IN (select res_id from ir_model_data where model='ir.ui.menu'))'''
                           )
                cr.commit()
                if not cr.rowcount:
                    break
                else:
                    logger.notifyChannel(
                        'init', netsvc.LOG_INFO,
                        'removed %d unused menus' % (cr.rowcount, ))

            cr.execute("update ir_module_module set state=%s where state=%s", (
                'uninstalled',
                'to remove',
            ))
            cr.commit()
    finally:
        cr.close()
コード例 #5
0
ファイル: __init__.py プロジェクト: MarkNorgate/addons-EAD
def load_modules(db, force_demo=False, status=None, update_module=False):
    if not status:
        status = {}

    cr = db.cursor()
    if cr:
       cr.execute("SELECT relname FROM pg_class WHERE relkind='r' AND relname='ir_module_module'")
       if len(cr.fetchall())==0:    
           logger.notifyChannel("init", netsvc.LOG_INFO, "init db")
           tools.init_db(cr)
           # in that case, force --init=all
           tools.config["init"]["all"] = 1
           tools.config['update']['all'] = 1
           if not tools.config['without_demo']:
               tools.config["demo"]['all'] = 1 
    force = []
    if force_demo:
        force.append('demo')
    pool = pooler.get_pool(cr.dbname)
    try:
        report = tools.assertion_report()
        # NOTE: Try to also load the modules that have been marked as uninstallable previously...
        STATES_TO_LOAD = ['installed', 'to upgrade', 'uninstallable']
        graph = create_graph(cr, ['base'], force)

        has_updates = load_module_graph(cr, graph, status, perform_checks=(not update_module), report=report)

        if update_module:
            modobj = pool.get('ir.module.module')
            logger.notifyChannel('init', netsvc.LOG_INFO, 'updating modules list')
            if ('base' in tools.config['init']) or ('base' in tools.config['update']):
                modobj.update_list(cr, 1)

            mods = [k for k in tools.config['init'] if tools.config['init'][k]]
            if mods:
                ids = modobj.search(cr, 1, ['&', ('state', '=', 'uninstalled'), ('name', 'in', mods)])
                if ids:
                    modobj.button_install(cr, 1, ids)

            mods = [k for k in tools.config['update'] if tools.config['update'][k]]
            if mods:
                ids = modobj.search(cr, 1, ['&', ('state', '=', 'installed'), ('name', 'in', mods)])
                if ids:
                    modobj.button_upgrade(cr, 1, ids)

            cr.execute("update ir_module_module set state=%s where name=%s", ('installed', 'base'))

            STATES_TO_LOAD += ['to install']

        loop_guardrail = 0
        while True:
            loop_guardrail += 1
            if loop_guardrail > 100:
                raise ProgrammingError()
            cr.execute("SELECT name from ir_module_module WHERE state in %s",
                       (tuple(STATES_TO_LOAD),)
                      )

            module_list = [name for (name,) in cr.fetchall() if name not in graph]
            if not module_list:
                break

            new_modules_in_graph = upgrade_graph(graph, cr, module_list, force)
            if new_modules_in_graph == 0:
                # nothing to load
                break
            logger.notifyChannel('init', netsvc.LOG_DEBUG, 'Updating graph with %d more modules' % (len(module_list)))
            r = load_module_graph(cr, graph, status, report=report)
            has_updates = has_updates or r

        if has_updates:
            cr.execute("""select model,name from ir_model where id not in (select model_id from ir_model_access)""")
            for (model, name) in cr.fetchall():
                logger.notifyChannel('init', netsvc.LOG_WARNING, 'object %s (%s) has no access rules!' % (model, name))

            cr.execute("SELECT model from ir_model")
            for (model,) in cr.fetchall():
                obj = pool.get(model)
                if obj:
                    obj._check_removed_columns(cr, log=True)

        if report.get_report():
            logger.notifyChannel('init', netsvc.LOG_INFO, report)

        for kind in ('init', 'demo', 'update'):
            tools.config[kind] = {}

        cr.commit()
        if update_module:
            cr.execute("select id,name from ir_module_module where state=%s", ('to remove',))
            for mod_id, mod_name in cr.fetchall():
                cr.execute('select model,res_id from ir_model_data where noupdate=%s and module=%s order by id desc', (False, mod_name,))
                for rmod, rid in cr.fetchall():
                    uid = 1
                    pool.get(rmod).unlink(cr, uid, [rid])
                cr.execute('delete from ir_model_data where noupdate=%s and module=%s', (False, mod_name,))
                cr.commit()
            #
            # TODO: remove menu without actions of children
            #
            while True:
                cr.execute('''delete from
                        ir_ui_menu
                    where
                        (id not in (select parent_id from ir_ui_menu where parent_id is not null))
                    and
                        (id not in (select res_id from ir_values where model='ir.ui.menu'))
                    and
                        (id not in (select res_id from ir_model_data where model='ir.ui.menu'))''')
                cr.commit()
                if not cr.rowcount:
                    break
                else:
                    logger.notifyChannel('init', netsvc.LOG_INFO, 'removed %d unused menus' % (cr.rowcount,))

            cr.execute("update ir_module_module set state=%s where state=%s", ('uninstalled', 'to remove',))
            cr.commit()
    finally:
        cr.close()
コード例 #6
0
ファイル: __init__.py プロジェクト: Buyanbat/XacCRM
def load_modules(db, force_demo=False, status=None, update_module=False):

    def check_module_name(cr, mods, state):
        for mod in mods:
            id = modobj.search(cr, 1, ['&', ('state', '=', state), ('name', '=', mod)])
            if id:
                getattr(modobj, states[state])(cr, 1, id)
            elif mod != 'all':
                logger.notifyChannel('init', netsvc.LOG_WARNING, 'module %s: invalid module name!' % (mod))

    if not status:
        status = {}
    cr = db.cursor()
    if cr:
        cr.execute("SELECT relname FROM pg_class WHERE relkind='r' AND relname='ir_module_module'")
        if len(cr.fetchall())==0:
            logger.notifyChannel("init", netsvc.LOG_INFO, "init db")
            tools.init_db(cr)
            tools.config["init"]["all"] = 1
            tools.config['update']['all'] = 1
            if not tools.config['without_demo']:
                tools.config["demo"]['all'] = 1
    force = []
    if force_demo:
        force.append('demo')
    pool = pooler.get_pool(cr.dbname)
    try:
        report = tools.assertion_report()
        # NOTE: Try to also load the modules that have been marked as uninstallable previously...
        STATES_TO_LOAD = ['installed', 'to upgrade', 'uninstallable']
        graph = create_graph(cr, ['base'], force)
        if not graph:
            logger.notifyChannel('init', netsvc.LOG_CRITICAL, 'module base cannot be loaded! (hint: verify addons-path)')
            raise osv.osv.except_osv('Could not load base module', 'module base cannot be loaded! (hint: verify addons-path)')
        has_updates = load_module_graph(cr, graph, status, perform_checks=(not update_module), report=report)

        if update_module:
            modobj = pool.get('ir.module.module')
            states = {'installed': 'button_upgrade', 'uninstalled': 'button_install'}
            logger.notifyChannel('init', netsvc.LOG_INFO, 'updating modules list')
            if ('base' in tools.config['init']) or ('base' in tools.config['update']):
                modobj.update_list(cr, 1)

            mods = [k for k in tools.config['init'] if tools.config['init'][k]]
            check_module_name(cr, mods, 'uninstalled')

            mods = [k for k in tools.config['update'] if tools.config['update'][k]]
            check_module_name(cr, mods, 'installed')

            cr.execute("update ir_module_module set state=%s where name=%s", ('installed', 'base'))

            STATES_TO_LOAD += ['to install']

        loop_guardrail = 0
        while True:
            loop_guardrail += 1
            if loop_guardrail > 100:
                raise ValueError('Possible recursive module tree detected, aborting.')
            cr.execute("SELECT name from ir_module_module WHERE state IN %s" ,(tuple(STATES_TO_LOAD),))

            module_list = [name for (name,) in cr.fetchall() if name not in graph]
            if not module_list:
                break

            new_modules_in_graph = upgrade_graph(graph, cr, module_list, force)
            if new_modules_in_graph == 0:
                # nothing to load
                break

            logger.notifyChannel('init', netsvc.LOG_DEBUG, 'Updating graph with %d more modules' % (len(module_list)))
            r = load_module_graph(cr, graph, status, report=report)
            has_updates = has_updates or r

        if has_updates:
            cr.execute("""select model,name from ir_model where id NOT IN (select distinct model_id from ir_model_access)""")
            for (model, name) in cr.fetchall():
                model_obj = pool.get(model)
                if not isinstance(model_obj, osv.osv.osv_memory):
                    logger.notifyChannel('init', netsvc.LOG_WARNING, 'object %s (%s) has no access rules!' % (model, name))

            # Temporary warning while we remove access rights on osv_memory objects, as they have
            # been replaced by owner-only access rights
            cr.execute("""select distinct mod.model, mod.name from ir_model_access acc, ir_model mod where acc.model_id = mod.id""")
            for (model, name) in cr.fetchall():
                model_obj = pool.get(model)
                if isinstance(model_obj, osv.osv.osv_memory):
                    logger.notifyChannel('init', netsvc.LOG_WARNING, 'In-memory object %s (%s) should not have explicit access rules!' % (model, name))

            cr.execute("SELECT model from ir_model")
            for (model,) in cr.fetchall():
                obj = pool.get(model)
                if obj:
                    obj._check_removed_columns(cr, log=True)

        if report.get_report():
            logger.notifyChannel('init', netsvc.LOG_INFO, report)

        for kind in ('init', 'demo', 'update'):
            tools.config[kind] = {}

        cr.commit()
        if update_module:
            cr.execute("select id,name from ir_module_module where state=%s", ('to remove',))
            for mod_id, mod_name in cr.fetchall():
                cr.execute('select model,res_id from ir_model_data where noupdate=%s and module=%s order by id desc', (False, mod_name,))
                for rmod, rid in cr.fetchall():
                    uid = 1
                    rmod_module= pool.get(rmod)
                    if rmod_module:
                        rmod_module.unlink(cr, uid, [rid])
                    else:
                        logger.notifyChannel('init', netsvc.LOG_ERROR, 'Could not locate %s to remove res=%d' % (rmod,rid))
                cr.execute('delete from ir_model_data where noupdate=%s and module=%s', (False, mod_name,))
                cr.commit()
            #
            # TODO: remove menu without actions of children
            #
            while True:
                cr.execute('''delete from
                        ir_ui_menu
                    where
                        (id not IN (select parent_id from ir_ui_menu where parent_id is not null))
                    and
                        (id not IN (select res_id from ir_values where model='ir.ui.menu'))
                    and
                        (id not IN (select res_id from ir_model_data where model='ir.ui.menu'))''')
                cr.commit()
                if not cr.rowcount:
                    break
                else:
                    logger.notifyChannel('init', netsvc.LOG_INFO, 'removed %d unused menus' % (cr.rowcount,))

            cr.execute("update ir_module_module set state=%s where state=%s", ('uninstalled', 'to remove',))
            cr.commit()
    finally:
        cr.close()
コード例 #7
0
def load_modules(db, force_demo=False, status=None, update_module=False):
    def check_module_name(cr, mods, state):
        for mod in mods:
            id = modobj.search(
                cr, 1, ['&', ('state', '=', state), ('name', '=', mod)])
            if id:
                getattr(modobj, states[state])(cr, 1, id)
            elif mod != 'all':
                logger.notifyChannel('init', netsvc.LOG_WARNING,
                                     'module %s: invalid module name!' % (mod))

    if not status:
        status = {}
    cr = db.cursor()
    if cr:
        cr.execute(
            "SELECT relname FROM pg_class WHERE relkind='r' AND relname='ir_module_module'"
        )
        if len(cr.fetchall()) == 0:
            logger.notifyChannel("init", netsvc.LOG_INFO, "init db")
            tools.init_db(cr)
            tools.config["init"]["all"] = 1
            tools.config['update']['all'] = 1
            if not tools.config['without_demo']:
                tools.config["demo"]['all'] = 1
    force = []
    if force_demo:
        force.append('demo')
    pool = pooler.get_pool(cr.dbname)
    try:
        report = tools.assertion_report()
        # NOTE: Try to also load the modules that have been marked as uninstallable previously...
        STATES_TO_LOAD = ['installed', 'to upgrade', 'uninstallable']
        graph = create_graph(cr, ['base'], force)
        if not graph:
            logger.notifyChannel(
                'init', netsvc.LOG_CRITICAL,
                'module base cannot be loaded! (hint: verify addons-path)')
            raise osv.osv.except_osv(
                'Could not load base module',
                'module base cannot be loaded! (hint: verify addons-path)')
        has_updates = load_module_graph(cr,
                                        graph,
                                        status,
                                        perform_checks=(not update_module),
                                        report=report)

        if update_module:
            modobj = pool.get('ir.module.module')
            states = {
                'installed': 'button_upgrade',
                'uninstalled': 'button_install'
            }
            logger.notifyChannel('init', netsvc.LOG_INFO,
                                 'updating modules list')
            if ('base' in tools.config['init']) or ('base'
                                                    in tools.config['update']):
                modobj.update_list(cr, 1)

            mods = [k for k in tools.config['init'] if tools.config['init'][k]]
            check_module_name(cr, mods, 'uninstalled')

            mods = [
                k for k in tools.config['update'] if tools.config['update'][k]
            ]
            check_module_name(cr, mods, 'installed')

            cr.execute("update ir_module_module set state=%s where name=%s",
                       ('installed', 'base'))

            STATES_TO_LOAD += ['to install']

        loop_guardrail = 0
        while True:
            loop_guardrail += 1
            if loop_guardrail > 100:
                raise ValueError(
                    'Possible recursive module tree detected, aborting.')
            cr.execute("SELECT name from ir_module_module WHERE state IN %s",
                       (tuple(STATES_TO_LOAD), ))

            module_list = [
                name for (name, ) in cr.fetchall() if name not in graph
            ]
            if not module_list:
                break

            new_modules_in_graph = upgrade_graph(graph, cr, module_list, force)
            if new_modules_in_graph == 0:
                # nothing to load
                break

            logger.notifyChannel(
                'init', netsvc.LOG_DEBUG,
                'Updating graph with %d more modules' % (len(module_list)))
            r = load_module_graph(cr, graph, status, report=report)
            has_updates = has_updates or r

        if has_updates:
            cr.execute(
                """select model,name from ir_model where id NOT IN (select distinct model_id from ir_model_access)"""
            )
            for (model, name) in cr.fetchall():
                model_obj = pool.get(model)
                if not isinstance(model_obj, osv.osv.osv_memory):
                    logger.notifyChannel(
                        'init', netsvc.LOG_WARNING,
                        'object %s (%s) has no access rules!' % (model, name))

            # Temporary warning while we remove access rights on osv_memory objects, as they have
            # been replaced by owner-only access rights
            cr.execute(
                """select distinct mod.model, mod.name from ir_model_access acc, ir_model mod where acc.model_id = mod.id"""
            )
            for (model, name) in cr.fetchall():
                model_obj = pool.get(model)
                if isinstance(model_obj, osv.osv.osv_memory):
                    logger.notifyChannel(
                        'init', netsvc.LOG_WARNING,
                        'In-memory object %s (%s) should not have explicit access rules!'
                        % (model, name))

            cr.execute("SELECT model from ir_model")
            for (model, ) in cr.fetchall():
                obj = pool.get(model)
                if obj:
                    obj._check_removed_columns(cr, log=True)

        if report.get_report():
            logger.notifyChannel('init', netsvc.LOG_INFO, report)

        for kind in ('init', 'demo', 'update'):
            tools.config[kind] = {}

        cr.commit()
        if update_module:
            cr.execute("select id,name from ir_module_module where state=%s",
                       ('to remove', ))
            for mod_id, mod_name in cr.fetchall():
                cr.execute(
                    'select model,res_id from ir_model_data where noupdate=%s and module=%s order by id desc',
                    (
                        False,
                        mod_name,
                    ))
                for rmod, rid in cr.fetchall():
                    uid = 1
                    rmod_module = pool.get(rmod)
                    if rmod_module:
                        rmod_module.unlink(cr, uid, [rid])
                    else:
                        logger.notifyChannel(
                            'init', netsvc.LOG_ERROR,
                            'Could not locate %s to remove res=%d' %
                            (rmod, rid))
                cr.execute(
                    'delete from ir_model_data where noupdate=%s and module=%s',
                    (
                        False,
                        mod_name,
                    ))
                cr.commit()
            #
            # TODO: remove menu without actions of children
            #
            while True:
                cr.execute('''delete from
                        ir_ui_menu
                    where
                        (id not IN (select parent_id from ir_ui_menu where parent_id is not null))
                    and
                        (id not IN (select res_id from ir_values where model='ir.ui.menu'))
                    and
                        (id not IN (select res_id from ir_model_data where model='ir.ui.menu'))'''
                           )
                cr.commit()
                if not cr.rowcount:
                    break
                else:
                    logger.notifyChannel(
                        'init', netsvc.LOG_INFO,
                        'removed %d unused menus' % (cr.rowcount, ))

            cr.execute("update ir_module_module set state=%s where state=%s", (
                'uninstalled',
                'to remove',
            ))
            cr.commit()
    finally:
        cr.close()