[TEST] Log for debug
[odoo/odoo.git] / openerpcommand / initialize.py
index 3393c9a..6261312 100644 (file)
@@ -1,8 +1,11 @@
 """
 Install OpenERP on a new (by default) database.
 """
+import contextlib
+import errno
 import os
 import sys
+import time
 
 import common
 
@@ -11,20 +14,42 @@ def install_openerp(database_name, create_database_flag, module_names, install_d
     config = openerp.tools.config
 
     if create_database_flag:
-        create_database(database_name)
+        with lock_file('/tmp/global_openerp_create_database.lock'):
+            create_database(database_name)
 
     config['init'] = dict.fromkeys(module_names, 1)
 
     # Install the import hook, to import openerp.addons.<module>.
     openerp.modules.module.initialize_sys_path()
-    if hasattr(openerp.modules.loading, 'open_openerp_namespace'):
-        openerp.modules.loading.open_openerp_namespace()
 
     registry = openerp.modules.registry.RegistryManager.get(
         database_name, update_module=True, force_demo=install_demo_data)
 
     return registry
 
+# From http://code.activestate.com/recipes/576572/
+@contextlib.contextmanager
+def lock_file(path, wait_delay=.1, max_try=600):
+    attempt = 0
+    while True:
+        attempt += 1
+        if attempt > max_try:
+            raise IOError("Could not lock file %s." % path)
+        try:
+            fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_RDWR)
+        except OSError, e:
+            if e.errno != errno.EEXIST:
+                raise
+            time.sleep(wait_delay)
+            continue
+        else:
+            break
+    try:
+        yield fd
+    finally:
+        os.close(fd)
+        os.unlink(path)
+
 # TODO turn template1 in a parameter
 # This should be exposed from openerp (currently in
 # openerp/service/web_services.py).
@@ -49,7 +74,7 @@ def run(args):
     config = openerp.tools.config
 
     if args.tests:
-        config['log_handler'] = [':TEST']
+        config['log_handler'] = [':INFO']
         config['test_enable'] = True
         config['without_demo'] = False
     else:
@@ -70,8 +95,19 @@ def run(args):
     else:
         module_names = ['base']
 
+    if args.coverage:
+        import coverage
+        # Without the `include` kwarg, coverage generates 'memory:0xXXXXX'
+        # filenames (which do not exist) and cause it to crash. No idea why.
+        cov = coverage.coverage(branch=True, include='*.py')
+        cov.start()
     openerp.netsvc.init_logger()
     registry = install_openerp(args.database, not args.no_create, module_names, not config['without_demo'])
+    if args.coverage:
+        cov.stop()
+        cov.html_report(directory='coverage')
+        # If we wanted the report on stdout:
+        # cov.report()
 
     # The `_assertion_report` attribute was added on the registry during the
     # OpenERP 7.0 development.
@@ -99,5 +135,9 @@ def add_parser(subparsers):
         ' (use the `run-tests` command to choose specific'
         ' tests to run against an existing database).'
         ' Demo data are installed.')
+    parser.add_argument('--coverage', action='store_true',
+        help='report code coverage (particularly useful with --tests).'
+        ' The report is generated in a coverage directory and you can'
+        ' then point your browser to coverage/index.html.')
 
     parser.set_defaults(run=run)