From 27436f8b8a84fa70bba3a7b7d286fd3eba37fbfe Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Fri, 17 May 2013 15:30:52 +0200 Subject: bin/hostinfo: add support for --hosts and --short You could now use --hosts to get a listing of all available hosts with in the current basepath (set by HOSTINFO_PATH or --path or the default value /usr/local/share/hostinfo/). If you use --short you will remove the domain spline.inf.fu-berlin.de from the output and you will get only the hostnames. --- bin/hostinfo | 30 +++++++++++++++++++++++++++--- contrib/bash-completion | 5 ++--- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/bin/hostinfo b/bin/hostinfo index 8876c54..43fd5b4 100755 --- a/bin/hostinfo +++ b/bin/hostinfo @@ -44,6 +44,21 @@ def print_keys(path): data = _get_data(path) _print_keys(data) +def print_hosts(path, short): + metadata = os.path.join(path, 'metadata', 'hosts') + if os.path.exists(metadata): + hosts = yaml.load(file(metadata, 'r')) + if 'hosts' in hosts: + for host in hosts['hosts']: + if short: + print(host.replace('.spline.inf.fu-berlin.de','')) + else: + print(host) + return True + + sys.stderr.write("'%s' not found!\n" % metadata) + return False + def find_host(basepath, host): path = os.path.join(basepath, host) if os.path.exists(path): @@ -77,11 +92,13 @@ def main(): if 'HOSTINFO_PATH' in os.environ: basepath = os.environ['HOSTINFO_PATH'] - file = oneline = keys = verbose = nospaces = help = False + file = oneline = keys = verbose = nospaces = help = \ + hosts = short = False try: - optlist, args = getopt.gnu_getopt(sys.argv, 'ofkvnh?p:', + optlist, args = getopt.gnu_getopt(sys.argv, 'ofkvnh?p:ls', ['oneline', 'file', 'keys', 'verbose', - 'nospaces', 'help', 'path=']) + 'nospaces', 'help', 'path=', 'hosts', + 'short']) opts = {} for key, value in optlist: opts[key] = value @@ -92,6 +109,8 @@ def main(): verbose = any([o in opts for o in ['--verbose', '-v']]) nospaces = any([o in opts for o in ['--nospaces', '-n']]) help = any([o in opts for o in ['--help', '-h', '-?']]) + hosts = any([o in opts for o in ['--hosts', '-l']]) + short = any([o in opts for o in ['--short', '-s']]) if any([o in opts for o in ['--path', '-p']]): basepath = [opts[o] for o in ['--path', '-p'] if o in opts][0] @@ -100,6 +119,11 @@ def main(): print args = [] + if hosts: + if not print_hosts(basepath, short): + sys.exit(1) + sys.exit(0) + if help or len(args) < 1: print_help(self_name) if not help: diff --git a/contrib/bash-completion b/contrib/bash-completion index 1adad22..0994d39 100644 --- a/contrib/bash-completion +++ b/contrib/bash-completion @@ -1,7 +1,5 @@ # hostinfo completion -_hostinfo_hosts="$(for file in /usr/local/share/hostinfo/* ; do basename $file | sed 's/.spline.inf.fu-berlin.de//' ; done)" - _get_first_hostinfo_arg() { local i @@ -30,6 +28,7 @@ _get_hostinfo_path() done } + _hostinfo() { local cur prev @@ -43,7 +42,7 @@ _hostinfo() { _get_first_hostinfo_arg _get_hostinfo_path if [[ -z "$arg" ]]; then - _completion="$_hostinfo_hosts" + _completion="$(hostinfo $path --hosts --short 2>/dev/null)" else _completion="$(hostinfo $path --keys $arg 2>/dev/null)" fi -- cgit v1.2.3-1-g7c22