Permalink
Browse files

Add -W/--password option to dbtoyaml/yamltodb. Fixes #18.

 * docs/dbtoyaml.rst, docs/yamltodb.rst: Document new option.
 * pyrseas/dbconn.py (DbConnection.__init__): Add pswd argument and
   prompt the uer if True.  (DbConnection.connect): Add password to
   connection string. If connect fails and it's a FATAL error, exit.
 * pyrseas/dbtoyaml.py, pyrseas/yamltodb.py: Add new option.
  • Loading branch information...
1 parent 7ff6fd0 commit 78aa96106ede0e213a0758d52a2ed85ec09e660f @jmafc jmafc committed Jan 13, 2012
Showing with 47 additions and 11 deletions.
  1. +10 −0 docs/dbtoyaml.rst
  2. +10 −0 docs/yamltodb.rst
  3. +19 −8 pyrseas/dbconn.py
  4. +4 −2 pyrseas/dbtoyaml.py
  5. +4 −1 pyrseas/yamltodb.py
View
10 docs/dbtoyaml.rst
@@ -121,6 +121,16 @@ dbname
User name to connect as. The default user name is provided by the
environment variable :envvar:`USER`.
+-W\, --password
+
+ Force dbtoyaml to prompt for a password before connecting to a
+ database. If this option is not specified and password
+ authentication is required, dbtoyaml will resort to libpq
+ defaults, i.e., `password file
+ <http://www.postgresql.org/docs/current/static/libpq-pgpass.html>`_
+ or `PGPASSWORD environment variable
+ <http://www.postgresql.org/docs/current/static/libpq-envars.html>`_.
+
Examples
--------
View
10 docs/yamltodb.rst
@@ -87,6 +87,16 @@ yamlspec
Execute the generated statements against the database mentioned in
``dbname``. This implies the --single-transaction option.
+-W\, --password
+
+ Force yamltodb to prompt for a password before connecting to a
+ database. If this option is not specified and password
+ authentication is required, yamltodb will resort to libpq
+ defaults, i.e., `password file
+ <http://www.postgresql.org/docs/current/static/libpq-pgpass.html>`_
+ or `PGPASSWORD environment variable
+ <http://www.postgresql.org/docs/current/static/libpq-envars.html>`_.
+
Examples
--------
View
27 pyrseas/dbconn.py
@@ -6,8 +6,9 @@
A `DbConnection` is a helper class representing a connection to a
PostgreSQL database.
"""
-
import os
+import sys
+import getpass
from psycopg2 import connect
from psycopg2.extras import DictConnection
@@ -16,16 +17,21 @@
class DbConnection(object):
"""A database connection, possibly disconnected"""
- def __init__(self, dbname, user=None, host=None, port=None):
+ def __init__(self, dbname, user=None, pswd=False, host=None, port=None):
"""Initialize the connection information
:param dbname: database name
:param user: user name
+ :param pswd: prompt user for password
:param host: host name
:param port: host port number
"""
self.dbname = dbname
self.user = user
+ if pswd:
+ self.pswd = " password=%s" % getpass.getpass()
+ else:
+ self.pswd = ''
if host is None or host == '127.0.0.1' or host == 'localhost':
self.host = ''
else:
@@ -41,13 +47,18 @@ def connect(self):
"""Connect to the database
If user is None, the USER environment variable is used
- instead. The password is either not required or supplied by
- other means, e.g., a $HOME/.pgpass file.
+ instead.
"""
- self.conn = connect("%s%sdbname=%s user=%s" % (
- self.host, self.port, self.dbname,
- self.user or os.getenv("USER")),
- connection_factory=DictConnection)
+ try:
+ self.conn = connect("%s%sdbname=%s user=%s%s" % (
+ self.host, self.port, self.dbname,
+ self.user or os.getenv("USER"), self.pswd),
+ connection_factory=DictConnection)
+ except Exception as exc:
+ if exc.message[:6] == 'FATAL:':
+ sys.exit("Database connection error: %s" % exc.message[8:])
+ else:
+ raise
try:
self._execute("set search_path to public, pg_catalog")
except:
View
6 pyrseas/dbtoyaml.py
@@ -23,6 +23,8 @@ def main(host='localhost', port=5432, schema=None):
help="database server port (default %default)")
parser.add_option('-U', '--username', dest='username',
help="database user name (default %default)")
+ parser.add_option('-W', '--password', action="store_true",
+ help="force password prompt")
parser.add_option('-n', '--schema', dest='schema',
help="only for named schema (default %default)")
parser.add_option('-t', '--table', dest='tablist', action='append',
@@ -39,8 +41,8 @@ def main(host='localhost', port=5432, schema=None):
parser.error("database name not specified")
dbname = args[0]
- db = Database(DbConnection(dbname, options.username, options.host,
- options.port))
+ db = Database(DbConnection(dbname, options.username, options.password,
+ options.host, options.port))
dbmap = db.to_map()
# trim the map of schemas/tables not selected
if options.schema:
View
5 pyrseas/yamltodb.py
@@ -24,6 +24,8 @@ def main(host='localhost', port=5432):
help="database server port (default %default)")
parser.add_option('-U', '--username', dest='username',
help="database user name (default %default)")
+ parser.add_option('-W', '--password', action="store_true",
+ help="force password prompt")
parser.add_option('-1', '--single-transaction', action='store_true',
dest='onetrans',
help="wrap commands in BEGIN/COMMIT")
@@ -43,7 +45,8 @@ def main(host='localhost', port=5432):
dbname = args[0]
yamlspec = args[1]
- dbconn = DbConnection(dbname, options.username, options.host, options.port)
+ dbconn = DbConnection(dbname, options.username, options.password,
+ options.host, options.port)
db = Database(dbconn)
inmap = yaml.load(open(yamlspec))
if options.schlist:

0 comments on commit 78aa961

Please sign in to comment.