2 Install OpenERP on a new (by default) database.
12 def install_openerp(database_name, create_database_flag, module_names, install_demo_data):
14 config = openerp.tools.config
16 if create_database_flag:
17 with lock_file('/tmp/global_openerp_create_database.lock'):
18 create_database(database_name)
20 config['init'] = dict.fromkeys(module_names, 1)
22 # Install the import hook, to import openerp.addons.<module>.
23 openerp.modules.module.initialize_sys_path()
27 registry = openerp.modules.registry.RegistryManager.get(
28 database_name, update_module=True, force_demo=install_demo_data)
32 # From http://code.activestate.com/recipes/576572/
33 @contextlib.contextmanager
34 def lock_file(path, wait_delay=.1, max_try=600):
39 raise IOError("Could not lock file %s." % path)
41 fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_RDWR)
43 if e.errno != errno.EEXIST:
45 time.sleep(wait_delay)
55 # TODO turn template1 in a parameter
56 # This should be exposed from openerp (currently in
57 # openerp/service/web_services.py).
58 def create_database(database_name):
60 db = openerp.sql_db.db_connect('template1')
61 cr = db.cursor() # TODO `with db as cr:`
64 cr.execute("""CREATE DATABASE "%s"
65 ENCODING 'unicode' TEMPLATE "template1" """ \
72 assert not (args.module and args.all_modules)
76 config = openerp.tools.config
79 config['log_handler'] = [':INFO']
80 config['test_enable'] = True
81 config['without_demo'] = False
83 config['xmlrpc_port'] = int(args.port)
85 config['log_handler'] = [':CRITICAL']
86 config['test_enable'] = False
87 config['without_demo'] = True
90 args.addons = args.addons.split(':')
93 config['addons_path'] = ','.join(args.addons)
95 print "MY addons path is", config['addons_path']
98 module_names = common.get_addons_from_paths(args.addons, args.exclude)
100 module_names = args.module
102 module_names = ['base']
106 # Without the `include` kwarg, coverage generates 'memory:0xXXXXX'
107 # filenames (which do not exist) and cause it to crash. No idea why.
108 cov = coverage.coverage(branch=True, include='*.py')
110 openerp.netsvc.init_logger()
111 registry = install_openerp(args.database, not args.no_create, module_names, not config['without_demo'])
114 cov.html_report(directory='coverage')
115 # If we wanted the report on stdout:
118 # The `_assertion_report` attribute was added on the registry during the
119 # OpenERP 7.0 development.
120 if hasattr(registry, '_assertion_report'):
121 sys.exit(1 if registry._assertion_report.failures else 0)
123 def add_parser(subparsers):
124 parser = subparsers.add_parser('initialize',
125 description='Create and initialize a new OpenERP database.')
126 parser.add_argument('-d', '--database', metavar='DATABASE',
127 **common.required_or_default('DATABASE', 'the database to create'))
128 common.add_addons_argument(parser)
129 parser.add_argument('-P', '--port', metavar='PORT',
130 **common.required_or_default('PORT', 'the server port'))
131 parser.add_argument('--module', metavar='MODULE', action='append',
132 help='specify a module to install'
133 ' (this option can be repeated)')
134 parser.add_argument('--all-modules', action='store_true',
135 help='install all visible modules (not compatible with --module)')
136 parser.add_argument('--no-create', action='store_true',
137 help='do not create the database, only initialize it')
138 parser.add_argument('--exclude', metavar='MODULE', action='append',
139 help='exclude a module from installation'
140 ' (this option can be repeated)')
141 parser.add_argument('--tests', action='store_true',
142 help='run the tests as modules are installed'
143 ' (use the `run-tests` command to choose specific'
144 ' tests to run against an existing database).'
145 ' Demo data are installed.')
146 parser.add_argument('--coverage', action='store_true',
147 help='report code coverage (particularly useful with --tests).'
148 ' The report is generated in a coverage directory and you can'
149 ' then point your browser to coverage/index.html.')
151 parser.set_defaults(run=run)