[IMP] account_payment: use the new signal_xxx methods instead of trg_validate.
[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     if hasattr(openerp.modules.loading, 'open_openerp_namespace'):
21         openerp.modules.loading.open_openerp_namespace()
22
23     registry = openerp.modules.registry.RegistryManager.get(
24         database_name, update_module=True, force_demo=install_demo_data)
25
26     return registry
27
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):
32     import openerp
33     db = openerp.sql_db.db_connect('template1')
34     cr = db.cursor() # TODO `with db as cr:`
35     try:
36         cr.autocommit(True)
37         cr.execute("""CREATE DATABASE "%s"
38             ENCODING 'unicode' TEMPLATE "template1" """ \
39             % (database_name,))
40     finally:
41         cr.close()
42
43 def run(args):
44     assert args.database
45     assert not (args.module and args.all_modules)
46
47     import openerp
48
49     config = openerp.tools.config
50
51     if args.tests:
52         config['log_handler'] = [':TEST']
53         config['test_enable'] = True
54         config['without_demo'] = False
55     else:
56         config['log_handler'] = [':CRITICAL']
57         config['test_enable'] = False
58         config['without_demo'] = True
59
60     if args.addons:
61         args.addons = args.addons.split(':')
62     else:
63         args.addons = []
64     config['addons_path'] = ','.join(args.addons)
65
66     if args.all_modules:
67         module_names = common.get_addons_from_paths(args.addons, args.exclude)
68     elif args.module:
69         module_names = args.module
70     else:
71         module_names = ['base']
72
73     if args.coverage:
74         import coverage
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')
78         cov.start()
79     openerp.netsvc.init_logger()
80     registry = install_openerp(args.database, not args.no_create, module_names, not config['without_demo'])
81     if args.coverage:
82         cov.stop()
83         cov.html_report(directory='coverage')
84         # If we wanted the report on stdout:
85         # cov.report()
86
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)
91
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.')
117
118     parser.set_defaults(run=run)