[db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 25 Mar 2011 15:45:38 +0100
branchstable
changeset 7112 bb27cc300040
parent 7111 6c8e8747268d
child 7113 2572aa6dd7b4
[db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
devtools/__init__.py
server/serverctl.py
--- a/devtools/__init__.py	Fri Mar 25 12:51:20 2011 +0100
+++ b/devtools/__init__.py	Fri Mar 25 15:45:38 2011 +0100
@@ -497,7 +497,8 @@
     @cached
     def dbcnx(self):
         from cubicweb.server.serverctl import _db_sys_cnx
-        return  _db_sys_cnx(self.system_source, 'CREATE DATABASE and / or USER', verbose=0)
+        return  _db_sys_cnx(self.system_source, 'CREATE DATABASE and / or USER',
+                            interactive=False)
 
     @property
     @cached
@@ -514,7 +515,8 @@
 
             createdb(self.helper, self.system_source, self.dbcnx, self.cursor)
             self.dbcnx.commit()
-            cnx = system_source_cnx(self.system_source, special_privs='LANGUAGE C', verbose=0)
+            cnx = system_source_cnx(self.system_source, special_privs='LANGUAGE C',
+                                    interactive=False)
             templcursor = cnx.cursor()
             try:
                 # XXX factorize with db-create code
--- a/server/serverctl.py	Fri Mar 25 12:51:20 2011 +0100
+++ b/server/serverctl.py	Fri Mar 25 15:45:38 2011 +0100
@@ -39,7 +39,7 @@
 
 # utility functions ###########################################################
 
-def source_cnx(source, dbname=None, special_privs=False, verbose=True):
+def source_cnx(source, dbname=None, special_privs=False, interactive=True):
     """open and return a connection to the system database defined in the
     given server.serverconfig
     """
@@ -50,21 +50,20 @@
         dbname = source['db-name']
     driver = source['db-driver']
     dbhelper = get_db_helper(driver)
-    if verbose:
+    if interactive:
         print '-> connecting to %s database' % driver,
         if dbhost:
             print '%s@%s' % (dbname, dbhost),
         else:
             print dbname,
     if dbhelper.users_support:
-        if not special_privs and source.get('db-user'):
-            user = source['db-user']
-            if verbose:
+        if not interactive or (not special_privs and source.get('db-user')):
+            user = source.get('db-user', os.environ.get('USER', ''))
+            if interactive:
                 print 'as', user
             password = source.get('db-password')
         else:
-            if verbose:
-                print
+            print
             if special_privs:
                 print 'WARNING'
                 print ('the user will need the following special access rights '
@@ -95,7 +94,7 @@
     return cnx
 
 def system_source_cnx(source, dbms_system_base=False,
-                      special_privs='CREATE/DROP DATABASE', verbose=True):
+                      special_privs='CREATE/DROP DATABASE', interactive=True):
     """shortcut to get a connextion to the instance system database
     defined in the given config. If <dbms_system_base> is True,
     connect to the dbms system database instead (for task such as
@@ -104,10 +103,12 @@
     if dbms_system_base:
         from logilab.database import get_db_helper
         system_db = get_db_helper(source['db-driver']).system_database()
-        return source_cnx(source, system_db, special_privs=special_privs, verbose=verbose)
-    return source_cnx(source, special_privs=special_privs, verbose=verbose)
+        return source_cnx(source, system_db, special_privs=special_privs,
+                          interactive=interactive)
+    return source_cnx(source, special_privs=special_privs,
+                      interactive=interactive)
 
-def _db_sys_cnx(source, special_privs, verbose=True):
+def _db_sys_cnx(source, special_privs, interactive=True):
     """return a connection on the RDMS system table (to create/drop a user or a
     database)
     """
@@ -118,7 +119,7 @@
     helper = get_db_helper(driver)
     # connect on the dbms system base to create our base
     cnx = system_source_cnx(source, True, special_privs=special_privs,
-                            verbose=verbose)
+                            interactive=interactive)
     # disable autocommit (isolation_level(1)) because DROP and
     # CREATE DATABASE can't be executed in a transaction
     try:
@@ -296,12 +297,12 @@
          {'short': 'c', 'type': 'yn', 'metavar': '<y or n>',
           'default': True,
           'help': 'create the database (yes by default)'}),
-        ('verbose',
-         {'short': 'v', 'type' : 'yn', 'metavar': '<verbose>',
-          'default': 'n',
-          'help': 'verbose mode: will ask all possible configuration questions',
-          }
-         ),
+        ('quiet',
+         {'short': 'q', 'action' : 'store_true',
+          'default': False,
+          'help': 'be quiet. Suppose database user in the sources file is a '
+          'super user and don\'t ask for alternate login.',
+          }),
         ('automatic',
          {'short': 'a', 'type' : 'yn', 'metavar': '<auto>',
           'default': 'n',
@@ -312,7 +313,7 @@
     def run(self, args):
         """run the command with its specific arguments"""
         from logilab.database import get_db_helper
-        verbose = self.get('verbose')
+        quiet = self.get('quiet')
         automatic = self.get('automatic')
         appid = args.pop()
         config = ServerConfiguration.config_for(appid)
@@ -329,7 +330,7 @@
             print '\n'+underline_title('Creating the system database')
             # connect on the dbms system base to create our base
             dbcnx = _db_sys_cnx(source, 'CREATE/DROP DATABASE and / or USER',
-                                verbose=verbose)
+                                interactive=not quiet)
             cursor = dbcnx.cursor()
             try:
                 if helper.users_support:
@@ -350,7 +351,7 @@
                 dbcnx.rollback()
                 raise
         cnx = system_source_cnx(source, special_privs='CREATE LANGUAGE',
-                                verbose=verbose)
+                                interactive=not quiet)
         cursor = cnx.cursor()
         helper.init_fti_extensions(cursor)
         # postgres specific stuff