2008-07-19 22:32:22 -05:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
# Authors:
|
|
|
|
# Jason Gerard DeRose <jderose@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
|
|
|
|
|
|
|
|
"""
|
|
|
|
Command Line Interface to IPA.
|
2008-07-20 20:44:59 -05:00
|
|
|
|
|
|
|
Just proof of concept stuff in here right now.
|
2008-07-19 22:32:22 -05:00
|
|
|
"""
|
|
|
|
|
|
|
|
import sys
|
2008-08-25 18:35:29 -05:00
|
|
|
from ipalib import api
|
2008-08-11 14:35:57 -05:00
|
|
|
from ipalib.cli import CLI
|
2008-08-12 20:03:32 -05:00
|
|
|
import ipalib.load_plugins
|
2008-08-11 14:35:57 -05:00
|
|
|
|
|
|
|
cli = CLI(api)
|
|
|
|
cli.run()
|
|
|
|
|
|
|
|
sys.exit()
|
2008-07-19 22:32:22 -05:00
|
|
|
|
2008-08-05 21:00:18 -05:00
|
|
|
TAB_WIDTH = 2
|
|
|
|
|
2008-07-20 20:44:59 -05:00
|
|
|
def _(msg):
|
2008-08-08 12:11:29 -05:00
|
|
|
"""
|
|
|
|
Dummy gettext function for testing.
|
|
|
|
"""
|
|
|
|
return msg
|
2008-07-20 20:44:59 -05:00
|
|
|
|
2008-08-05 21:00:18 -05:00
|
|
|
class row(object):
|
2008-08-08 12:11:29 -05:00
|
|
|
def __init__(self, tab, c1, c2=None):
|
2008-08-08 16:40:03 -05:00
|
|
|
assert type(tab) is int
|
|
|
|
assert type(c1) in (str, int)
|
|
|
|
assert type(c2) is str or c2 is None
|
|
|
|
self.tab = tab
|
|
|
|
self.c1 = c1
|
|
|
|
self.c2 = c2
|
2008-08-08 12:11:29 -05:00
|
|
|
|
|
|
|
def __len__(self):
|
2008-08-08 16:40:03 -05:00
|
|
|
return len(str(self.c1))
|
2008-08-08 12:11:29 -05:00
|
|
|
|
|
|
|
def pretty_print(self, just):
|
2008-08-08 16:40:03 -05:00
|
|
|
tab = ' ' * (self.tab * TAB_WIDTH)
|
|
|
|
if self.c2 is None:
|
|
|
|
print '%s%s' % (tab, self.c1)
|
|
|
|
else:
|
|
|
|
if type(self.c1) is int:
|
|
|
|
c1 = str(self.c1).rjust(just)
|
|
|
|
else:
|
|
|
|
c1 = self.c1.ljust(just)
|
|
|
|
print '%s%s %s' % (tab, c1, self.c2)
|
2008-08-05 21:00:18 -05:00
|
|
|
|
|
|
|
def pretty_print(rows):
|
2008-08-08 12:11:29 -05:00
|
|
|
rows = tuple(rows)
|
|
|
|
def get_lengths():
|
2008-08-08 16:40:03 -05:00
|
|
|
yield 0
|
|
|
|
for r in rows:
|
|
|
|
if r.c2 is not None:
|
|
|
|
yield len(r)
|
2008-08-08 12:11:29 -05:00
|
|
|
max_len = max(get_lengths())
|
|
|
|
for r in rows:
|
2008-08-08 16:40:03 -05:00
|
|
|
r.pretty_print(max_len)
|
2008-08-05 21:00:18 -05:00
|
|
|
|
2008-07-20 20:44:59 -05:00
|
|
|
|
2008-07-19 22:32:22 -05:00
|
|
|
def print_commands():
|
2008-08-08 12:11:29 -05:00
|
|
|
print 'Commands:'
|
|
|
|
m = api.max_cmd_len
|
|
|
|
for cmd in api.cmd:
|
2008-08-08 16:40:03 -05:00
|
|
|
print ' %s %s' % (str(cmd).ljust(m), cmd.get_doc(_))
|
2008-07-19 22:32:22 -05:00
|
|
|
|
|
|
|
def print_help(cmd):
|
2008-08-08 12:11:29 -05:00
|
|
|
print 'Help on %s' % cmd
|
2008-07-19 22:32:22 -05:00
|
|
|
|
2008-08-05 21:00:18 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-08-05 22:27:00 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
2008-08-05 21:00:18 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
2008-07-20 13:36:02 -05:00
|
|
|
def print_api():
|
2008-08-08 12:11:29 -05:00
|
|
|
def iter_api(tab):
|
2008-08-08 16:40:03 -05:00
|
|
|
for name in api:
|
|
|
|
ns = getattr(api, name)
|
|
|
|
yield row(
|
|
|
|
tab,
|
|
|
|
name,
|
|
|
|
repr(ns),
|
|
|
|
)
|
|
|
|
for i in ns:
|
|
|
|
yield row(
|
|
|
|
tab + 1,
|
|
|
|
i.name,
|
|
|
|
repr(i)
|
|
|
|
)
|
2008-08-08 12:11:29 -05:00
|
|
|
|
|
|
|
def iter_obj(tab):
|
2008-08-08 16:40:03 -05:00
|
|
|
for obj in api.obj:
|
|
|
|
yield row(
|
|
|
|
tab,
|
|
|
|
obj.name,
|
|
|
|
repr(obj),
|
|
|
|
)
|
|
|
|
for (n, f) in [('mthd', '.%s()'), ('prop', '.%s')]:
|
|
|
|
ns = getattr(obj, n)
|
|
|
|
yield row(
|
|
|
|
tab + 1,
|
|
|
|
n,
|
|
|
|
repr(ns),
|
|
|
|
)
|
|
|
|
for attr in ns:
|
|
|
|
yield row(
|
|
|
|
tab + 2,
|
|
|
|
f % attr.name,
|
|
|
|
repr(attr),
|
|
|
|
)
|
2008-08-08 12:11:29 -05:00
|
|
|
|
|
|
|
def iter_summary(tab):
|
2008-08-08 16:40:03 -05:00
|
|
|
for name in api:
|
|
|
|
ns = getattr(api, name)
|
|
|
|
yield row(
|
|
|
|
tab,
|
|
|
|
len(ns),
|
|
|
|
name
|
|
|
|
)
|
2008-08-08 12:11:29 -05:00
|
|
|
|
|
|
|
def print_heading(h):
|
2008-08-08 16:40:03 -05:00
|
|
|
print '\n%s:' % h
|
|
|
|
print '-' * (len(h) + 1)
|
2008-08-08 12:11:29 -05:00
|
|
|
|
|
|
|
tab = 1
|
|
|
|
print_heading('API Overview')
|
|
|
|
pretty_print(iter_api(tab))
|
|
|
|
|
|
|
|
print_heading('Object Details')
|
|
|
|
pretty_print(iter_obj(tab))
|
|
|
|
|
|
|
|
print_heading('Summary')
|
|
|
|
pretty_print(iter_summary(tab))
|
2008-08-05 21:00:18 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
2008-07-20 13:36:02 -05:00
|
|
|
|
|
|
|
|
2008-07-19 22:32:22 -05:00
|
|
|
if len(sys.argv) < 2:
|
2008-08-08 12:11:29 -05:00
|
|
|
print_commands()
|
|
|
|
print 'Usage: ipa COMMAND [OPTIONS]'
|
|
|
|
sys.exit(2)
|
2008-08-05 17:21:57 -05:00
|
|
|
name= sys.argv[1]
|
|
|
|
if name == '_api_':
|
2008-08-08 12:11:29 -05:00
|
|
|
print_api()
|
|
|
|
sys.exit()
|
2008-08-05 17:21:57 -05:00
|
|
|
elif name not in api.cmd:
|
2008-08-08 12:11:29 -05:00
|
|
|
print_commands()
|
|
|
|
print 'ipa: ERROR: unknown command %r' % name
|
|
|
|
sys.exit(2)
|
2008-08-05 17:21:57 -05:00
|
|
|
api.cmd[name]()
|