Merge branch 'master' of git://git.engineering.redhat.com/users/mnagy/freeipa2

This commit is contained in:
Jason Gerard DeRose committed 2008-10-07 16:48:07 -06:00
commit 439d9c305b
4 files changed
+185 -15

No files matched your search

+51 -7
View File
@@ -205,6 +205,9 @@ class CLI(object):
def __init__(self, api):
self.__api = api
self.__all_interactive = False
self.__not_interactive = False
self.__config = None
def __get_api(self):
return self.__api
@@ -219,6 +222,7 @@ class CLI(object):
print '\nSpecial CLI commands:'
for cmd in self.api.Application():
self.print_cmd(cmd)
print '\nUse the --help option to see all the global options'
print ''
def print_cmd(self, cmd):
@@ -252,19 +256,21 @@ class CLI(object):
def run(self):
self.finalize()
if len(sys.argv) < 2:
(args, env_dict) = self.parse_globals()
env_dict.update(config.read_config(self.__config))
self.api.env.update(config.generate_env(env_dict))
if len(args) < 1:
self.print_commands()
print 'Usage: ipa COMMAND'
print 'Usage: ipa [global-options] COMMAND'
sys.exit(2)
self.api.env.update(config.generate_env())
key = sys.argv[1]
key = args[0]
if key not in self:
self.print_commands()
print 'ipa: ERROR: unknown command %r' % key
sys.exit(2)
self.run_cmd(
self[key],
list(s.decode('utf-8') for s in sys.argv[2:])
list(s.decode('utf-8') for s in args[1:])
)
def run_cmd(self, cmd, argv):
@@ -275,7 +281,10 @@ class CLI(object):
for param in cmd.params():
if param.name not in kw:
if not param.required:
continue
if not self.__all_interactive:
continue
elif self.__not_interactive:
exit_error('Not enough arguments given')
default = param.get_default(**kw)
if default is None:
prompt = '%s: ' % param.name
@@ -318,11 +327,46 @@ class CLI(object):
)
return parser
def parse_globals(self, argv=sys.argv[1:]):
env_dict = {}
parser = optparse.OptionParser()
parser.disable_interspersed_args()
parser.add_option('-a', dest='interactive', action='store_true',
help='Prompt for all missing options interactively')
parser.add_option('-n', dest='interactive', action='store_false',
help='Don\'t prompt for any options interactively')
parser.add_option('-c', dest='config_file',
help='Specify different configuration file')
parser.add_option('-e', dest='environment',
help='Specify or override environment variables')
parser.add_option('-v', dest='verbose', action='store_true',
help='Verbose output')
(options, args) = parser.parse_args(argv)
if options.interactive == True:
self.__all_interactive = True
elif options.interactive == False:
self.__not_interactive = True
if options.config_file:
self.__config = options.config_file
if options.environment:
for a in options.environment.split(','):
a = a.split('=', 1)
if len(a) < 2:
parser.error('badly specified environment string,'\
'use var1=val1[,var2=val2]..')
env_dict[a[0].strip()] = a[1].strip()
if options.verbose != None:
env_dict.update(verbose=True)
return (args, env_dict)
def get_usage(self, cmd):
return ' '.join(self.get_usage_iter(cmd))
def get_usage_iter(self, cmd):
yield 'Usage: %%prog %s' % to_cli(cmd.name)
yield 'Usage: %%prog [global-options] %s' % to_cli(cmd.name)
for arg in cmd.args():
name = to_cli(arg.name).upper()
if arg.multivalue:
+31 -6
View File
@@ -17,7 +17,9 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
from ConfigParser import SafeConfigParser, ParsingError
import types
import os
DEFAULT_CONF='/etc/ipa/ipa.conf'
@@ -26,7 +28,8 @@ def generate_env(d={}):
server_context = False,
query_dns = True,
verbose = False,
servers = LazyIter(get_servers),
interactive = True,
server = LazyIter(get_servers),
realm = LazyProp(get_realm),
domain = LazyProp(get_domain),
)
@@ -68,11 +71,33 @@ class LazyIter(LazyProp):
yield item
def read_config(file=DEFAULT_CONF):
assert isinstance(file, basestring)
# open the file and read configuration, return a dict
# for now, these are here just for testing purposes
return dict(servers="server.ipatest.com", realm="IPATEST.COM")
def read_config(config_file=None):
assert config_file == None or isinstance(config_file, (basestring, file))
parser = SafeConfigParser()
if config_file == None:
files = [DEFAULT_CONF, os.path.expanduser('~/.ipa.conf')]
else:
files = [config_file]
for f in files:
try:
if isinstance(f, file):
parser.readfp(f)
else:
parser.read(f)
except ParsingError:
print "Can't read %s" % f
ret = {}
if parser.has_section('defaults'):
for name, value in parser.items('defaults'):
value = tuple(elem.strip() for elem in value.split(','))
if len(value) == 1:
value = value[0]
ret[name] = value
return ret
# these functions are here just to "emulate" dns resolving for now
+2 -2
View File
@@ -35,10 +35,10 @@ class envtest(frontend.Command):
print "Environment variables:"
for var in api.env:
val = api.env[var]
if var is 'servers':
if var is 'server':
print ""
print " Servers:"
for item in api.env.servers:
for item in api.env.server:
print " %s" % item
print ""
else:
+101
View File
@@ -0,0 +1,101 @@
# Authors:
# Martin Nagy <mnagy@redhat.com>
#
# Copyright (C) 2008 Red Hat
# see file 'COPYING' for use and warranty information
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 2 only
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
Unit tests for `ipalib.config` module.
"""
import types
from tstutil import raises
from ipalib import config
def test_generate_env():
"""
Test the `config.generate_env` function
"""
# Make sure we don't overwrite any properties
env = dict(
query_dns = False,
server = ('first', 'second'),
realm = 'myrealm',
)
d = config.generate_env(env)
assert d['query_dns'] == False
# Make sure the servers is overwrote properly (that it is still LazyProp)
iter = d['server'].get_value()
assert iter.next() == 'first'
assert iter.next() == 'second'
def test_LazyProp():
"""
Test the `config.LazyProp` class
"""
def dummy():
return 1
# Basic sanity testing with no initial value
prop = config.LazyProp(dummy)
assert prop.get_value() == 1
prop.set_value(2)
assert prop.get_value() == 2
# Basic sanity testing with initial value
prop = config.LazyProp(dummy, 3)
assert prop.get_value() == 3
prop.set_value(4)
assert prop.get_value() == 4
def test_LazyIter():
"""
Test the `config.LazyIter` class
"""
def dummy():
yield 1
yield 2
# Basic sanity testing with no initial value
prop = config.LazyIter(dummy)
iter = prop.get_value()
assert iter.next() == 1
assert iter.next() == 2
raises(StopIteration, iter.next)
# Basic sanity testing with initial value
prop = config.LazyIter(dummy, 0)
iter = prop.get_value()
assert iter.next() == 0
assert iter.next() == 1
assert iter.next() == 2
raises(StopIteration, iter.next)
def test_read_config():
"""
Test the `config.read_config` class
"""
raises(AssertionError, config.read_config, 1)