2014-10-12 17:31:54 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
from __future__ import unicode_literals
|
2014-11-21 07:15:50 +00:00
|
|
|
from __future__ import print_function
|
2014-10-12 22:07:34 +00:00
|
|
|
|
2014-12-20 07:12:43 +00:00
|
|
|
import os
|
2015-01-06 21:52:01 +00:00
|
|
|
import traceback
|
2015-01-04 08:31:17 +00:00
|
|
|
import logging
|
2014-10-12 22:07:34 +00:00
|
|
|
import click
|
2014-10-12 17:31:54 +00:00
|
|
|
|
2014-10-12 17:45:35 +00:00
|
|
|
from prompt_toolkit import CommandLineInterface, AbortAction, Exit
|
2014-10-12 17:31:54 +00:00
|
|
|
from prompt_toolkit.layout import Layout
|
|
|
|
from prompt_toolkit.layout.prompt import DefaultPrompt
|
2014-11-21 07:15:50 +00:00
|
|
|
from prompt_toolkit.layout.menus import CompletionsMenu
|
2014-11-23 23:31:34 +00:00
|
|
|
from prompt_toolkit.history import FileHistory
|
2014-12-11 08:26:32 +00:00
|
|
|
from prompt_toolkit.key_bindings.emacs import emacs_bindings
|
2014-11-24 07:30:17 +00:00
|
|
|
from pygments.lexers.sql import SqlLexer
|
2014-12-05 16:56:59 +00:00
|
|
|
|
2014-12-08 08:43:21 +00:00
|
|
|
from .packages.tabulate import tabulate
|
2015-01-07 10:21:40 +00:00
|
|
|
from .packages.expanded import expanded_table
|
2014-12-12 15:10:55 +00:00
|
|
|
from .packages.pgspecial import (CASE_SENSITIVE_COMMANDS,
|
2015-01-07 10:21:40 +00:00
|
|
|
NON_CASE_SENSITIVE_COMMANDS, is_expanded_output)
|
2014-12-05 16:56:59 +00:00
|
|
|
from .pgcompleter import PGCompleter
|
2014-12-11 08:26:32 +00:00
|
|
|
from .pgtoolbar import PGToolbar
|
2014-12-05 16:56:59 +00:00
|
|
|
from .pgstyle import PGStyle
|
|
|
|
from .pgexecute import PGExecute
|
|
|
|
from .pgline import PGLine
|
|
|
|
from .config import write_default_config, load_config
|
2014-12-11 08:26:32 +00:00
|
|
|
from .key_bindings import pgcli_bindings
|
2014-10-12 17:31:54 +00:00
|
|
|
|
2015-01-07 21:50:48 +00:00
|
|
|
from urlparse import urlparse
|
|
|
|
from getpass import getuser
|
|
|
|
from psycopg2 import OperationalError
|
|
|
|
|
2015-01-04 08:31:17 +00:00
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
2014-10-12 22:07:34 +00:00
|
|
|
@click.command()
|
2015-01-08 12:58:44 +00:00
|
|
|
|
|
|
|
# Default host is '' so psycopg2 can default to either localhost or unix socket
|
|
|
|
@click.option('-h', '--host', default='', envvar='PGHOST',
|
2015-01-06 20:06:27 +00:00
|
|
|
help='Host address of the postgres database.')
|
2014-12-11 18:10:26 +00:00
|
|
|
@click.option('-p', '--port', default=5432, help='Port number at which the '
|
2015-01-06 20:06:27 +00:00
|
|
|
'postgres instance is listening.', envvar='PGPORT')
|
2015-01-07 21:50:48 +00:00
|
|
|
@click.option('-U', '--user', envvar='PGUSER', help='User name to '
|
2014-12-11 18:10:26 +00:00
|
|
|
'connect to the postgres database.')
|
2015-01-07 21:50:48 +00:00
|
|
|
@click.option('-W', '--password', 'prompt_passwd', is_flag=True, default=False,
|
|
|
|
help='Force password prompt.')
|
|
|
|
@click.option('-w', '--no-password', 'never_prompt', is_flag=True,
|
|
|
|
default=False, help='Never issue a password prompt')
|
|
|
|
@click.argument('database', default='', envvar='PGDATABASE')
|
|
|
|
def cli(database, user, host, port, prompt_passwd, never_prompt):
|
|
|
|
|
|
|
|
passwd = ''
|
|
|
|
if not database:
|
|
|
|
#default to current OS username just like psql
|
|
|
|
database = user = getuser()
|
|
|
|
elif '://' in database:
|
|
|
|
#a URI connection string
|
|
|
|
parsed = urlparse(database)
|
|
|
|
database = parsed.path[1:] # ignore the leading fwd slash
|
|
|
|
user = parsed.username
|
|
|
|
passwd = parsed.password
|
|
|
|
port = parsed.port
|
|
|
|
host = parsed.hostname
|
|
|
|
|
2015-01-08 12:58:44 +00:00
|
|
|
# Prompt for a password immediately if requested via the -W flag. This
|
|
|
|
# avoids wasting time trying to connect to the database and catching a
|
|
|
|
# no-password exception.
|
|
|
|
# If we successfully parsed a password from a URI, there's no need to prompt
|
|
|
|
# for it, even with the -W flag
|
2015-01-07 21:50:48 +00:00
|
|
|
if prompt_passwd and not passwd:
|
2014-12-24 23:11:47 +00:00
|
|
|
passwd = click.prompt('Password', hide_input=True, show_default=False,
|
|
|
|
type=str)
|
2015-01-07 21:50:48 +00:00
|
|
|
|
2015-01-08 12:58:44 +00:00
|
|
|
# Prompt for a password after 1st attempt to connect without a password
|
|
|
|
# fails. Don't prompt if the -w flag is supplied
|
2015-01-07 21:50:48 +00:00
|
|
|
auto_passwd_prompt = not passwd and not never_prompt
|
2014-12-24 23:11:47 +00:00
|
|
|
|
2014-11-27 23:02:54 +00:00
|
|
|
from pgcli import __file__ as package_root
|
|
|
|
package_root = os.path.dirname(package_root)
|
|
|
|
|
|
|
|
default_config = os.path.join(package_root, 'pgclirc')
|
|
|
|
write_default_config(default_config, '~/.pgclirc')
|
|
|
|
|
|
|
|
# Load config.
|
2014-12-30 22:38:57 +00:00
|
|
|
config = load_config('~/.pgclirc', default_config)
|
2014-12-11 08:26:32 +00:00
|
|
|
smart_completion = config.getboolean('main', 'smart_completion')
|
2014-12-29 22:15:23 +00:00
|
|
|
multi_line = config.getboolean('main', 'multi_line')
|
2015-01-04 08:31:17 +00:00
|
|
|
log_file = config.get('main', 'log_file')
|
|
|
|
log_level = config.get('main', 'log_level')
|
2014-11-27 23:02:54 +00:00
|
|
|
|
2015-01-04 08:31:17 +00:00
|
|
|
initialize_logging(log_file, log_level)
|
2014-12-20 07:12:43 +00:00
|
|
|
|
2015-01-04 22:52:04 +00:00
|
|
|
original_less_opts = adjust_less_opts()
|
|
|
|
|
|
|
|
_logger.debug('Launch Params: \n'
|
|
|
|
'\tdatabase: %r'
|
|
|
|
'\tuser: %r'
|
|
|
|
'\tpassword: %r'
|
|
|
|
'\thost: %r'
|
|
|
|
'\tport: %r', database, user, passwd, host, port)
|
2014-12-20 07:12:43 +00:00
|
|
|
|
2015-01-08 12:58:44 +00:00
|
|
|
# Attempt to connect to the database.
|
|
|
|
# Note that passwd may be empty on the first attempt. If connection fails
|
|
|
|
# because of a missing password, but we're allowed to prompt for a password,
|
|
|
|
# (no -w flag), prompt for a passwd and try again.
|
2014-11-23 08:09:00 +00:00
|
|
|
try:
|
2015-01-07 21:50:48 +00:00
|
|
|
try:
|
|
|
|
pgexecute = PGExecute(database, user, passwd, host, port)
|
|
|
|
except OperationalError as e:
|
|
|
|
if 'no password supplied' in e.message and auto_passwd_prompt:
|
|
|
|
passwd = click.prompt('Password', hide_input=True,
|
|
|
|
show_default=False, type=str)
|
|
|
|
pgexecute = PGExecute(database, user, passwd, host, port)
|
|
|
|
else:
|
|
|
|
raise e
|
|
|
|
|
2014-12-11 08:26:32 +00:00
|
|
|
except Exception as e: # Connecting to a database could fail.
|
2015-01-07 05:51:09 +00:00
|
|
|
_logger.debug('Database connection failed: %r.', e)
|
|
|
|
click.secho(str(e), err=True, fg='red')
|
2014-11-23 08:09:00 +00:00
|
|
|
exit(1)
|
2014-12-08 06:30:24 +00:00
|
|
|
layout = Layout(before_input=DefaultPrompt('%s> ' % pgexecute.dbname),
|
2014-12-18 19:06:47 +00:00
|
|
|
menus=[CompletionsMenu(max_height=10)],
|
2014-12-11 08:26:32 +00:00
|
|
|
lexer=SqlLexer,
|
|
|
|
bottom_toolbars=[
|
|
|
|
PGToolbar()])
|
|
|
|
completer = PGCompleter(smart_completion)
|
2014-12-12 15:10:55 +00:00
|
|
|
completer.extend_special_commands(CASE_SENSITIVE_COMMANDS.keys())
|
|
|
|
completer.extend_special_commands(NON_CASE_SENSITIVE_COMMANDS.keys())
|
2015-01-04 22:52:04 +00:00
|
|
|
refresh_completions(pgexecute, completer)
|
2014-12-29 22:15:23 +00:00
|
|
|
line = PGLine(always_multiline=multi_line, completer=completer,
|
2014-11-27 23:02:54 +00:00
|
|
|
history=FileHistory(os.path.expanduser('~/.pgcli-history')))
|
2014-12-11 08:26:32 +00:00
|
|
|
cli = CommandLineInterface(style=PGStyle, layout=layout, line=line,
|
|
|
|
key_binding_factories=[emacs_bindings, pgcli_bindings])
|
2014-10-12 17:31:54 +00:00
|
|
|
|
2014-10-12 17:45:35 +00:00
|
|
|
try:
|
|
|
|
while True:
|
2014-12-08 06:30:24 +00:00
|
|
|
cli.layout.before_input = DefaultPrompt('%s> ' % pgexecute.dbname)
|
2014-10-12 17:45:35 +00:00
|
|
|
document = cli.read_input(on_exit=AbortAction.RAISE_EXCEPTION)
|
2014-12-06 06:04:39 +00:00
|
|
|
|
|
|
|
# The reason we check here instead of inside the pgexecute is
|
|
|
|
# because we want to raise the Exit exception which will be caught
|
|
|
|
# by the try/except block that wraps the pgexecute.run() statement.
|
2015-01-04 22:52:04 +00:00
|
|
|
if quit_command(document.text):
|
2014-12-06 06:04:39 +00:00
|
|
|
raise Exit
|
2014-11-23 08:09:00 +00:00
|
|
|
try:
|
2015-01-04 22:52:04 +00:00
|
|
|
_logger.debug('sql: %r', document.text)
|
2014-12-09 06:59:25 +00:00
|
|
|
res = pgexecute.run(document.text)
|
2014-12-20 04:57:36 +00:00
|
|
|
output = []
|
2014-12-09 06:59:25 +00:00
|
|
|
for rows, headers, status in res:
|
2015-01-04 22:52:04 +00:00
|
|
|
_logger.debug("headers: %r", headers)
|
|
|
|
_logger.debug("rows: %r", rows)
|
2014-12-09 06:59:25 +00:00
|
|
|
if rows:
|
2015-01-07 10:21:40 +00:00
|
|
|
if is_expanded_output():
|
|
|
|
output.append(expanded_table(rows, headers))
|
|
|
|
else:
|
|
|
|
output.append(tabulate(rows, headers, tablefmt='psql'))
|
2014-12-13 21:44:46 +00:00
|
|
|
if status: # Only print the status if it's not None.
|
2014-12-20 04:57:36 +00:00
|
|
|
output.append(status)
|
2015-01-04 22:52:04 +00:00
|
|
|
_logger.debug("status: %r", status)
|
2015-01-08 07:47:45 +00:00
|
|
|
click.echo_via_pager('\n'.join(output))
|
2014-11-23 08:09:00 +00:00
|
|
|
except Exception as e:
|
2015-01-07 06:34:19 +00:00
|
|
|
_logger.error("sql: %r, error: %r", document.text, e)
|
2015-01-06 21:52:01 +00:00
|
|
|
_logger.error("traceback: %r", traceback.format_exc())
|
|
|
|
click.secho(str(e), err=True, fg='red')
|
2014-12-05 16:56:59 +00:00
|
|
|
|
|
|
|
# Refresh the table names and column names if necessary.
|
2015-01-04 22:52:04 +00:00
|
|
|
if need_completion_refresh(document.text):
|
2014-12-08 08:43:21 +00:00
|
|
|
completer.reset_completions()
|
2015-01-04 22:52:04 +00:00
|
|
|
refresh_completions(pgexecute, completer)
|
2014-10-12 17:45:35 +00:00
|
|
|
except Exit:
|
2014-11-21 07:15:50 +00:00
|
|
|
print ('GoodBye!')
|
2015-01-04 08:31:17 +00:00
|
|
|
finally: # Reset the less opts back to original.
|
2015-01-04 22:52:04 +00:00
|
|
|
_logger.debug('Restoring env var LESS to %r.', original_less_opts)
|
2015-01-04 08:31:17 +00:00
|
|
|
os.environ['LESS'] = original_less_opts
|
2014-12-05 16:56:59 +00:00
|
|
|
|
|
|
|
def need_completion_refresh(sql):
|
2014-12-13 21:44:46 +00:00
|
|
|
try:
|
|
|
|
first_token = sql.split()[0]
|
2015-01-08 15:56:07 +00:00
|
|
|
return first_token.lower() in ('alter', 'create', 'use', '\c', 'drop')
|
2014-12-13 21:44:46 +00:00
|
|
|
except Exception:
|
|
|
|
return False
|
2015-01-04 08:31:17 +00:00
|
|
|
|
|
|
|
def initialize_logging(log_file, log_level):
|
|
|
|
level_map = {'CRITICAL': logging.CRITICAL,
|
|
|
|
'ERROR': logging.ERROR,
|
|
|
|
'WARNING': logging.WARNING,
|
|
|
|
'INFO': logging.INFO,
|
|
|
|
'DEBUG': logging.DEBUG
|
|
|
|
}
|
|
|
|
|
|
|
|
handler = logging.FileHandler(os.path.expanduser(log_file))
|
|
|
|
|
|
|
|
formatter = logging.Formatter('%(asctime)s (%(process)d/%(threadName)s) '
|
|
|
|
'%(name)s %(levelname)s - %(message)s')
|
|
|
|
handler.setFormatter(formatter)
|
|
|
|
|
|
|
|
_logger.addHandler(handler)
|
|
|
|
_logger.setLevel(level_map[log_level.upper()])
|
|
|
|
|
|
|
|
_logger.debug('Initializing pgcli logging.')
|
|
|
|
_logger.debug('Log file "%s".' % log_file)
|
|
|
|
|
2015-01-04 22:52:04 +00:00
|
|
|
def adjust_less_opts():
|
2015-01-04 08:31:17 +00:00
|
|
|
less_opts = os.environ.get('LESS', '')
|
2015-01-04 22:52:04 +00:00
|
|
|
_logger.debug('Original value for LESS env var: %r', less_opts)
|
2015-01-04 08:31:17 +00:00
|
|
|
if not less_opts:
|
|
|
|
os.environ['LESS'] = '-RXF'
|
|
|
|
|
|
|
|
if 'X' not in less_opts:
|
|
|
|
os.environ['LESS'] += 'X'
|
|
|
|
if 'F' not in less_opts:
|
|
|
|
os.environ['LESS'] += 'F'
|
|
|
|
|
|
|
|
return less_opts
|
2015-01-04 22:52:04 +00:00
|
|
|
|
|
|
|
def quit_command(sql):
|
|
|
|
return (sql.strip().lower() == 'exit'
|
|
|
|
or sql.strip().lower() == 'quit'
|
|
|
|
or sql.strip() == '\q'
|
|
|
|
or sql.strip() == ':q')
|
|
|
|
|
|
|
|
def refresh_completions(pgexecute, completer):
|
|
|
|
tables = pgexecute.tables()
|
|
|
|
completer.extend_table_names(tables)
|
|
|
|
for table in tables:
|
2015-01-07 20:22:19 +00:00
|
|
|
table = table[1:-1] if table[0] == '"' and table[-1] == '"' else table
|
2015-01-04 22:52:04 +00:00
|
|
|
completer.extend_column_names(table, pgexecute.columns(table))
|
|
|
|
completer.extend_database_names(pgexecute.databases())
|
2015-01-06 19:49:29 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
cli()
|