2 Install OpenERP on a new (by default) database.
9 def install_openerp(database_name, create_database_flag, module_names, install_demo_data):
11 config = openerp.tools.config
13 if create_database_flag:
14 create_database(database_name)
16 config['init'] = dict.fromkeys(module_names, 1)
18 # Install the import hook, to import openerp.addons.<module>.
19 openerp.modules.module.initialize_sys_path()
20 if hasattr(openerp.modules.loading, 'open_openerp_namespace'):
21 openerp.modules.loading.open_openerp_namespace()
23 registry = openerp.modules.registry.RegistryManager.get(
24 database_name, update_module=True, force_demo=install_demo_data)
28 # TODO turn template1 in a parameter
29 # This should be exposed from openerp (currently in
30 # openerp/service/web_services.py).
31 def create_database(database_name):
33 db = openerp.sql_db.db_connect('template1')
34 cr = db.cursor() # TODO `with db as cr:`
37 cr.execute("""CREATE DATABASE "%s"
38 ENCODING 'unicode' TEMPLATE "template1" """ \
45 assert not (args.module and args.all_modules)
49 config = openerp.tools.config
52 config['log_handler'] = [':TEST']
53 config['test_enable'] = True
54 config['without_demo'] = False
56 config['log_handler'] = [':CRITICAL']
57 config['test_enable'] = False
58 config['without_demo'] = True
61 args.addons = args.addons.split(':')
64 config['addons_path'] = ','.join(args.addons)
67 module_names = common.get_addons_from_paths(args.addons, args.exclude)
69 module_names = args.module
71 module_names = ['base']
75 # Without the `include` kwarg, coverage generates 'memory:0xXXXXX'
76 # filenames (which do not exist) and cause it to crash. No idea why.
77 cov = coverage.coverage(branch=True, include='*.py')
79 openerp.netsvc.init_logger()
80 registry = install_openerp(args.database, not args.no_create, module_names, not config['without_demo'])
83 cov.html_report(directory='coverage')
84 # If we wanted the report on stdout:
87 # The `_assertion_report` attribute was added on the registry during the
88 # OpenERP 7.0 development.
89 if hasattr(registry, '_assertion_report'):
90 sys.exit(1 if registry._assertion_report.failures else 0)
92 def add_parser(subparsers):
93 parser = subparsers.add_parser('initialize',
94 description='Create and initialize a new OpenERP database.')
95 parser.add_argument('-d', '--database', metavar='DATABASE',
96 **common.required_or_default('DATABASE', 'the database to create'))
97 common.add_addons_argument(parser)
98 parser.add_argument('--module', metavar='MODULE', action='append',
99 help='specify a module to install'
100 ' (this option can be repeated)')
101 parser.add_argument('--all-modules', action='store_true',
102 help='install all visible modules (not compatible with --module)')
103 parser.add_argument('--no-create', action='store_true',
104 help='do not create the database, only initialize it')
105 parser.add_argument('--exclude', metavar='MODULE', action='append',
106 help='exclude a module from installation'
107 ' (this option can be repeated)')
108 parser.add_argument('--tests', action='store_true',
109 help='run the tests as modules are installed'
110 ' (use the `run-tests` command to choose specific'
111 ' tests to run against an existing database).'
112 ' Demo data are installed.')
113 parser.add_argument('--coverage', action='store_true',
114 help='report code coverage (particularly useful with --tests).'
115 ' The report is generated in a coverage directory and you can'
116 ' then point your browser to coverage/index.html.')
118 parser.set_defaults(run=run)