[MERGE] forward port of branch 7.0 up to revid 4067 chs@openerp.com-20131114142639...
[odoo/odoo.git] / openerpcommand / initialize.py
1 """
2 Install OpenERP on a new (by default) database.
3 """
4 import os
5 import sys
6
7 import common
8
9 def install_openerp(database_name, create_database_flag, module_names, install_demo_data):
10     import openerp
11     config = openerp.tools.config
12
13     if create_database_flag:
14         create_database(database_name)
15
16     config['init'] = dict.fromkeys(module_names, 1)
17
18     # Install the import hook, to import openerp.addons.<module>.
19     openerp.modules.module.initialize_sys_path()
20
21     registry = openerp.modules.registry.RegistryManager.get(
22         database_name, update_module=True, force_demo=install_demo_data)
23
24     return registry
25
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):
30     import openerp
31     db = openerp.sql_db.db_connect('template1')
32     cr = db.cursor() # TODO `with db as cr:`
33     try:
34         cr.autocommit(True)
35         cr.execute("""CREATE DATABASE "%s"
36             ENCODING 'unicode' TEMPLATE "template1" """ \
37             % (database_name,))
38     finally:
39         cr.close()
40
41 def run(args):
42     assert args.database
43     assert not (args.module and args.all_modules)
44
45     import openerp
46
47     config = openerp.tools.config
48
49     if args.tests:
50         config['log_handler'] = [':INFO']
51         config['test_enable'] = True
52         config['without_demo'] = False
53     else:
54         config['log_handler'] = [':CRITICAL']
55         config['test_enable'] = False
56         config['without_demo'] = True
57
58     if args.addons:
59         args.addons = args.addons.split(':')
60     else:
61         args.addons = []
62     config['addons_path'] = ','.join(args.addons)
63
64     if args.all_modules:
65         module_names = common.get_addons_from_paths(args.addons, args.exclude)
66     elif args.module:
67         module_names = args.module
68     else:
69         module_names = ['base']
70
71     if args.coverage:
72         import coverage
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')
76         cov.start()
77     openerp.netsvc.init_logger()
78     registry = install_openerp(args.database, not args.no_create, module_names, not config['without_demo'])
79     if args.coverage:
80         cov.stop()
81         cov.html_report(directory='coverage')
82         # If we wanted the report on stdout:
83         # cov.report()
84
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)
89
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.')
115
116     parser.set_defaults(run=run)