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()
21 registry = openerp.modules.registry.RegistryManager.get(
22 database_name, update_module=True, force_demo=install_demo_data)
26 # TODO turn template1 in a parameter
27 # This should be exposed from openerp (currently in
28 # openerp/service/web_services.py).
29 def create_database(database_name):
31 db = openerp.sql_db.db_connect('template1')
32 cr = db.cursor() # TODO `with db as cr:`
35 cr.execute("""CREATE DATABASE "%s"
36 ENCODING 'unicode' TEMPLATE "template1" """ \
43 assert not (args.module and args.all_modules)
47 config = openerp.tools.config
50 config['log_handler'] = [':INFO']
51 config['test_enable'] = True
52 config['without_demo'] = False
54 config['log_handler'] = [':CRITICAL']
55 config['test_enable'] = False
56 config['without_demo'] = True
59 args.addons = args.addons.split(':')
62 config['addons_path'] = ','.join(args.addons)
65 module_names = common.get_addons_from_paths(args.addons, args.exclude)
67 module_names = args.module
69 module_names = ['base']
73 # Without the `include` kwarg, coverage generates 'memory:0xXXXXX'
74 # filenames (which do not exist) and cause it to crash. No idea why.
75 cov = coverage.coverage(branch=True, include='*.py')
77 openerp.netsvc.init_logger()
78 registry = install_openerp(args.database, not args.no_create, module_names, not config['without_demo'])
81 cov.html_report(directory='coverage')
82 # If we wanted the report on stdout:
85 # The `_assertion_report` attribute was added on the registry during the
86 # OpenERP 7.0 development.
87 if hasattr(registry, '_assertion_report'):
88 sys.exit(1 if registry._assertion_report.failures else 0)
90 def add_parser(subparsers):
91 parser = subparsers.add_parser('initialize',
92 description='Create and initialize a new OpenERP database.')
93 parser.add_argument('-d', '--database', metavar='DATABASE',
94 **common.required_or_default('DATABASE', 'the database to create'))
95 common.add_addons_argument(parser)
96 parser.add_argument('--module', metavar='MODULE', action='append',
97 help='specify a module to install'
98 ' (this option can be repeated)')
99 parser.add_argument('--all-modules', action='store_true',
100 help='install all visible modules (not compatible with --module)')
101 parser.add_argument('--no-create', action='store_true',
102 help='do not create the database, only initialize it')
103 parser.add_argument('--exclude', metavar='MODULE', action='append',
104 help='exclude a module from installation'
105 ' (this option can be repeated)')
106 parser.add_argument('--tests', action='store_true',
107 help='run the tests as modules are installed'
108 ' (use the `run-tests` command to choose specific'
109 ' tests to run against an existing database).'
110 ' Demo data are installed.')
111 parser.add_argument('--coverage', action='store_true',
112 help='report code coverage (particularly useful with --tests).'
113 ' The report is generated in a coverage directory and you can'
114 ' then point your browser to coverage/index.html.')
116 parser.set_defaults(run=run)