summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason Gerard DeRose <jderose@redhat.com>2009-01-27 16:28:50 -0700
committerRob Crittenden <rcritten@redhat.com>2009-02-03 15:29:03 -0500
commitdb0168f7afdac55eb7e0488cdc05e7a77a25672d (patch)
tree5413f862a79ae8c8589f1bb1d00feb565309760d
parent9efda29d60ae83d17bce793e0a23e3ec43c67f80 (diff)
downloadfreeipa-db0168f7afdac55eb7e0488cdc05e7a77a25672d.tar.gz
freeipa-db0168f7afdac55eb7e0488cdc05e7a77a25672d.tar.xz
freeipa-db0168f7afdac55eb7e0488cdc05e7a77a25672d.zip
Started reworking CLI class into cli plugin
-rwxr-xr-xipa6
-rw-r--r--ipalib/backend.py7
-rw-r--r--ipalib/cli.py212
-rw-r--r--ipalib/crud.py2
-rw-r--r--ipalib/plugable.py38
-rw-r--r--ipalib/util.py1
-rw-r--r--ipaserver/plugins/b_ldap.py6
7 files changed, 231 insertions, 41 deletions
diff --git a/ipa b/ipa
index d06abe37a..9260391ef 100755
--- a/ipa
+++ b/ipa
@@ -26,9 +26,7 @@ The CLI functionality is implemented in ipalib/cli.py
"""
import sys
-from ipalib import api
-from ipalib.cli import CLI
+from ipalib import api, cli
if __name__ == '__main__':
- cli = CLI(api, sys.argv[1:])
- sys.exit(cli.run())
+ cli.run(api)
diff --git a/ipalib/backend.py b/ipalib/backend.py
index f2741abec..d484c22eb 100644
--- a/ipalib/backend.py
+++ b/ipalib/backend.py
@@ -95,6 +95,13 @@ class Connectible(Backend):
class Executioner(Backend):
+
+ def create_context(self, ccache=None, client_ip=None):
+ if self.env.in_server:
+ self.Backend.ldap.connect(ccache=ccache)
+ else:
+ self.Backend.xmlclient.connect(ccache=ccache)
+
def execute(self, name, *args, **options):
error = None
try:
diff --git a/ipalib/cli.py b/ipalib/cli.py
index 2c9a80b62..4377a0488 100644
--- a/ipalib/cli.py
+++ b/ipalib/cli.py
@@ -392,40 +392,43 @@ class textui(backend.Backend):
self.print_error(_('Cancelled.'))
-class help(frontend.Application):
- '''Display help on a command.'''
+class help(frontend.Command):
+ """
+ Display help for a command or topic.
+ """
- takes_args = ['command?']
+ takes_args = [Bytes('command?')]
- def run(self, command):
+ def finalize(self):
+ self.__topics = dict(
+ (to_cli(c.name), c) for c in self.Command()
+ )
+ super(help, self).finalize()
+
+ def run(self, key):
textui = self.Backend.textui
- if command is None:
+ if key is None:
self.print_commands()
return
- key = str(command)
- if key not in self.application:
+ name = from_cli(key)
+ if name not in self.Command:
raise HelpError(topic=key)
cmd = self.application[key]
print 'Purpose: %s' % cmd.doc
self.application.build_parser(cmd).print_help()
def print_commands(self):
- std = set(self.Command) - set(self.Application)
- print '\nStandard IPA commands:'
- for key in sorted(std):
- cmd = self.api.Command[key]
- self.print_cmd(cmd)
- print '\nSpecial CLI commands:'
- for cmd in self.api.Application():
- self.print_cmd(cmd)
+ mcl = self.get_mcl()
+ for cmd in self.api.Command():
+ print ' %s %s' % (
+ to_cli(cmd.name).ljust(mcl),
+ cmd.doc,
+ )
print '\nUse the --help option to see all the global options'
print ''
- def print_cmd(self, cmd):
- print ' %s %s' % (
- to_cli(cmd.name).ljust(self.application.mcl),
- cmd.doc,
- )
+ def get_mcl(self):
+ return max(len(k) for k in self.Command)
class console(frontend.Application):
@@ -499,22 +502,26 @@ cli_application_commands = (
)
-class KWCollector(object):
- def __init__(self):
- object.__setattr__(self, '_KWCollector__d', {})
+class Collector(object):
+ def __init__(self, **extra):
+ object.__setattr__(self, '_Collector__options', {})
+ object.__setattr__(self, '_Collector__extra', frozenset(extra))
+ for (key, value) in extra.iteritems():
+ object.__setattr__(self, key, value)
def __setattr__(self, name, value):
- if name in self.__d:
- v = self.__d[name]
- if type(v) is tuple:
- value = v + (value,)
- else:
- value = (v, value)
- self.__d[name] = value
+ if name not in self.__extra:
+ if name in self.__options:
+ v = self.__options[name]
+ if type(v) is tuple:
+ value = v + (value,)
+ else:
+ value = (v, value)
+ self.__options[name] = value
object.__setattr__(self, name, value)
def __todict__(self):
- return dict(self.__d)
+ return dict(self.__options)
class CLI(object):
@@ -819,3 +826,146 @@ class CLI(object):
def __getitem__(self, key):
assert self.__d is not None, 'you must call finalize() first'
return self.__d[key]
+
+
+class cli(backend.Executioner):
+ """
+ Backend plugin for executing from command line interface.
+ """
+
+ def run(self, argv):
+ if len(argv) == 0:
+ self.Command.help()
+ return
+ (key, argv) = (argv[0], argv[1:])
+ cmd = self.get_command(key)
+ (kw, collector) = self.parse(cmd, argv)
+ if collector._interactive:
+ self.prompt_interactively(cmd, kw, collector)
+ self.create_context()
+
+ def prompt_interactively(self, cmd, kw, collector):
+ """
+ Interactively prompt for missing or invalid values.
+
+ By default this method will only prompt for *required* Param that
+ have a missing or invalid value. However, if
+ ``CLI.options.prompt_all`` is True, this method will prompt for any
+ params that have a missing or required values, even if the param is
+ optional.
+ """
+ for param in cmd.params():
+ if param.password or param.autofill:
+ continue
+ elif param.name not in kw:
+ if not param.required and not collector._prompt_all:
+ continue
+ default = param.get_default(**kw)
+ error = None
+ while True:
+ if error is not None:
+ print '>>> %s: %s' % (param.cli_name, error)
+ raw = self.Backend.textui.prompt(param.cli_name, default)
+ try:
+ value = param(raw, **kw)
+ if value is not None:
+ kw[param.name] = value
+ break
+ except errors.ValidationError, e:
+ error = e.error
+
+ def get_command(self, key):
+ name = from_cli(key)
+ if name not in self.Command:
+ raise CommandError(name=key)
+ return self.Command[name]
+
+ def parse(self, cmd, argv):
+ parser = self.build_parser(cmd)
+ (collector, args) = parser.parse_args(argv,
+ Collector(_prompt_all=False, interactive=True)
+ )
+ options = collector.__todict__()
+ kw = cmd.args_options_2_params(*args, **options)
+ return (dict(self.parse_iter(cmd, kw)), collector)
+
+ # FIXME: Move decoding to Command, use same regardless of request source
+ def parse_iter(self, cmd, kw):
+ """
+ Decode param values if appropriate.
+ """
+ for (key, value) in kw.iteritems():
+ param = cmd.params[key]
+ if isinstance(param, Bytes):
+ yield (key, value)
+ else:
+ yield (key, self.Backend.textui.decode(value))
+
+ def build_parser(self, cmd):
+ parser = optparse.OptionParser(
+ usage=' '.join(self.usage_iter(cmd))
+ )
+ if len(cmd.params) > 0:
+ parser.add_option('-a', dest='_prompt_all', action='store_true',
+ help='Prompt for all values interactively')
+ parser.add_option('-n', dest='_interactive', action='store_false',
+ help="Don\'t prompt for any values interactively")
+ for option in cmd.options():
+ kw = dict(
+ dest=option.name,
+ help=option.doc,
+ )
+ if option.password:
+ kw['action'] = 'store_true'
+ elif option.type is bool:
+ if option.default is True:
+ kw['action'] = 'store_false'
+ else:
+ kw['action'] = 'store_true'
+ else:
+ kw['metavar'] = metavar=option.__class__.__name__.upper()
+ o = optparse.make_option('--%s' % to_cli(option.cli_name), **kw)
+ parser.add_option(o)
+ return parser
+
+ def usage_iter(self, cmd):
+ yield 'Usage: %%prog [global-options] %s' % to_cli(cmd.name)
+ for arg in cmd.args():
+ if arg.password:
+ continue
+ name = to_cli(arg.cli_name).upper()
+ if arg.multivalue:
+ name = '%s...' % name
+ if arg.required:
+ yield name
+ else:
+ yield '[%s]' % name
+
+
+cli_plugins = (
+ cli,
+ textui,
+ help,
+)
+
+
+def run(api):
+ try:
+ argv = api.bootstrap_with_global_options(context='cli')
+ for klass in cli_plugins:
+ api.register(klass)
+ api.load_plugins()
+ api.finalize()
+ api.Backend.cli.run(sys.argv[1:])
+ sys.exit()
+ except KeyboardInterrupt:
+ print ''
+ api.log.info('operation aborted')
+ sys.exit()
+ except PublicError, e:
+ error = e
+ except Exception, e:
+ api.log.exception('%s: %s', e.__class__.__name__, str(e))
+ error = InternalError()
+ api.log.error(error.strerror)
+ sys.exit(error.errno)
diff --git a/ipalib/crud.py b/ipalib/crud.py
index 5bae4fbc3..5faa69ef3 100644
--- a/ipalib/crud.py
+++ b/ipalib/crud.py
@@ -122,7 +122,7 @@ class Search(frontend.Method):
"""
-class CrudBackend(backend.Backend):
+class CrudBackend(backend.Connectible):
"""
Base class defining generic CRUD backend API.
"""
diff --git a/ipalib/plugable.py b/ipalib/plugable.py
index bc55c8082..a99cd5473 100644
--- a/ipalib/plugable.py
+++ b/ipalib/plugable.py
@@ -33,6 +33,7 @@ import logging
import os
from os import path
import subprocess
+import optparse
import errors2
from config import Env
import util
@@ -575,12 +576,37 @@ class API(DictProxy):
handler.setLevel(logging.INFO)
log.addHandler(handler)
- def bootstrap_with_global_options(self, options=None, context=None):
- if options is None:
- parser = util.add_global_options()
- (options, args) = parser.parse_args(
- list(s.decode('utf-8') for s in sys.argv[1:])
+ def add_global_options(self, parser=None, context=None):
+ """
+ Add global options to an optparse.OptionParser instance.
+ """
+ if parser is None:
+ parser = optparse.OptionParser()
+ parser.disable_interspersed_args()
+ parser.add_option('-e', dest='env', metavar='KEY=VAL', action='append',
+ help='Set environment variable KEY to VAL',
+ )
+ parser.add_option('-c', dest='conf', metavar='FILE',
+ help='Load configuration from FILE',
+ )
+ parser.add_option('-d', '--debug', action='store_true',
+ help='Produce full debuging output',
+ )
+ parser.add_option('-v', '--verbose', action='store_true',
+ help='Produce more verbose output',
+ )
+ if context == 'cli':
+ parser.add_option('-a', '--prompt-all', action='store_true',
+ help='Prompt for all values interactively'
)
+ parser.add_option('-n', '--no-prompt', action='store_false',
+ help="Don\'t prompt for values interactively"
+ )
+ return parser
+
+ def bootstrap_with_global_options(self, parser=None, context=None):
+ parser = self.add_global_options(parser, context)
+ (options, args) = parser.parse_args()
overrides = {}
if options.env is not None:
assert type(options.env) is list
@@ -600,6 +626,7 @@ class API(DictProxy):
if context is not None:
overrides['context'] = context
self.bootstrap(**overrides)
+ return args
def load_plugins(self):
"""
@@ -686,6 +713,7 @@ class API(DictProxy):
for p in plugins.itervalues():
p.instance.finalize()
+ assert islocked(p.instance) is True
object.__setattr__(self, '_API__finalized', True)
tuple(PluginInfo(p) for p in plugins.itervalues())
object.__setattr__(self, 'plugins',
diff --git a/ipalib/util.py b/ipalib/util.py
index 4a58d7fbc..d9221605d 100644
--- a/ipalib/util.py
+++ b/ipalib/util.py
@@ -120,6 +120,7 @@ def add_global_options(parser=None):
"""
if parser is None:
parser = optparse.OptionParser()
+ parser.disable_interspersed_args()
parser.add_option('-e', dest='env', metavar='KEY=VAL', action='append',
help='Set environment variable KEY to VAL',
)
diff --git a/ipaserver/plugins/b_ldap.py b/ipaserver/plugins/b_ldap.py
index 54766bbb7..350870392 100644
--- a/ipaserver/plugins/b_ldap.py
+++ b/ipaserver/plugins/b_ldap.py
@@ -41,6 +41,12 @@ class ldap(CrudBackend):
self.dn = _ldap.dn
super(ldap, self).__init__()
+ def create_connection(self, ccache=None):
+ return 'The LDAP connection.'
+
+ def destroy_connection(self):
+ pass
+
def make_user_dn(self, uid):
"""
Construct user dn from uid.