2 Install OpenERP on a new (by default) database.
12 # From http://code.activestate.com/recipes/576572/
13 @contextlib.contextmanager
14 def lock_file(path, wait_delay=.1, max_try=600):
19 raise IOError("Could not lock file %s." % path)
21 fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_RDWR)
23 if e.errno != errno.EEXIST:
25 time.sleep(wait_delay)
37 assert not (args.module and args.all_modules)
41 config = openerp.tools.config
42 config['db_name'] = args.database
45 config['log_handler'] = [':INFO']
46 config['test_enable'] = True
47 config['without_demo'] = False
49 config['xmlrpc_port'] = int(args.port)
51 config['log_handler'] = [':CRITICAL']
52 config['test_enable'] = False
53 config['without_demo'] = True
56 args.addons = args.addons.split(':')
59 config['addons_path'] = ','.join(args.addons)
62 module_names = common.get_addons_from_paths(args.addons, args.exclude)
64 module_names = args.module
66 module_names = ['base']
67 config['init'] = dict.fromkeys(module_names, 1)
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')
75 openerp.netsvc.init_logger()
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)
81 config['workers'] = False
83 rc = openerp.service.server.start(preload=[args.database], stop=True)
87 cov.html_report(directory='coverage')
88 # If we wanted the report on stdout:
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.')
121 parser.set_defaults(run=run)