[FIX] website_forum: fixed controller creating a new forum tha was crashing.
[odoo/odoo.git] / openerpcommand / initialize.py
1 """
2 Install OpenERP on a new (by default) database.
3 """
4 import contextlib
5 import errno
6 import os
7 import sys
8 import time
9
10 import common
11
12 # From http://code.activestate.com/recipes/576572/
13 @contextlib.contextmanager
14 def lock_file(path, wait_delay=.1, max_try=600):
15     attempt = 0
16     while True:
17         attempt += 1
18         if attempt > max_try:
19             raise IOError("Could not lock file %s." % path)
20         try:
21             fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_RDWR)
22         except OSError, e:
23             if e.errno != errno.EEXIST:
24                 raise
25             time.sleep(wait_delay)
26             continue
27         else:
28             break
29     try:
30         yield fd
31     finally:
32         os.close(fd)
33         os.unlink(path)
34
35 def run(args):
36     assert args.database
37     assert not (args.module and args.all_modules)
38
39     import openerp
40
41     config = openerp.tools.config
42     config['db_name'] = args.database
43
44     if args.tests:
45         config['log_handler'] = [':INFO']
46         config['test_enable'] = True
47         config['without_demo'] = False
48         if args.port:
49             config['xmlrpc_port'] = int(args.port)
50     else:
51         config['log_handler'] = [':CRITICAL']
52         config['test_enable'] = False
53         config['without_demo'] = True
54
55     if args.addons:
56         args.addons = args.addons.split(':')
57     else:
58         args.addons = []
59     config['addons_path'] = ','.join(args.addons)
60
61     if args.all_modules:
62         module_names = common.get_addons_from_paths(args.addons, args.exclude)
63     elif args.module:
64         module_names = args.module
65     else:
66         module_names = ['base']
67     config['init'] = dict.fromkeys(module_names, 1)
68
69     if args.coverage:
70         import coverage
71         # Without the `include` kwarg, coverage generates 'memory:0xXXXXX'
72         # filenames (which do not exist) and cause it to crash. No idea why.
73         cov = coverage.coverage(branch=True, include='*.py')
74         cov.start()
75     openerp.netsvc.init_logger()
76
77     if not args.no_create:
78         with lock_file('/tmp/global_openerp_create_database.lock'):
79             openerp.service.db._create_empty_database(args.database)
80
81     config['workers'] = False
82
83     rc = openerp.service.server.start(preload=[args.database], stop=True)
84
85     if args.coverage:
86         cov.stop()
87         cov.html_report(directory='coverage')
88         # If we wanted the report on stdout:
89         # cov.report()
90
91     sys.exit(rc)
92
93 def add_parser(subparsers):
94     parser = subparsers.add_parser('initialize',
95         description='Create and initialize a new OpenERP database.')
96     parser.add_argument('-d', '--database', metavar='DATABASE',
97         **common.required_or_default('DATABASE', 'the database to create'))
98     common.add_addons_argument(parser)
99     parser.add_argument('-P', '--port', metavar='PORT',
100         **common.required_or_default('PORT', 'the server port'))
101     parser.add_argument('--module', metavar='MODULE', action='append',
102         help='specify a module to install'
103         ' (this option can be repeated)')
104     parser.add_argument('--all-modules', action='store_true',
105         help='install all visible modules (not compatible with --module)')
106     parser.add_argument('--no-create', action='store_true',
107         help='do not create the database, only initialize it')
108     parser.add_argument('--exclude', metavar='MODULE', action='append',
109         help='exclude a module from installation'
110         ' (this option can be repeated)')
111     parser.add_argument('--tests', action='store_true',
112         help='run the tests as modules are installed'
113         ' (use the `run-tests` command to choose specific'
114         ' tests to run against an existing database).'
115         ' Demo data are installed.')
116     parser.add_argument('--coverage', action='store_true',
117         help='report code coverage (particularly useful with --tests).'
118         ' The report is generated in a coverage directory and you can'
119         ' then point your browser to coverage/index.html.')
120
121     parser.set_defaults(run=run)