summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexander Sulfrian <alexander@sulfrian.net>2013-05-17 13:47:00 +0200
committerAlexander Sulfrian <alexander@sulfrian.net>2013-05-17 15:39:43 +0200
commit45253db95f05baf810c117a555ddec60c0349177 (patch)
tree67354b5fa0a9e6b2632e32fe27abdf868c89a72c
parentd0d48d32b86ab5f5a5f75074b9b452426b30e7d5 (diff)
downloadtools-45253db95f05baf810c117a555ddec60c0349177.tar.gz
tools-45253db95f05baf810c117a555ddec60c0349177.tar.bz2
tools-45253db95f05baf810c117a555ddec60c0349177.zip
hostinfo: basepath for the hostinfo files is configurable now
The basepath for the hostinfo files could now customized via the HOSTINFO_FILES environment variable or the --path, -p command line option. If nothing exists the default path /usr/local/share/hostinfo is used like before.
-rwxr-xr-xbin/hostinfo24
-rw-r--r--contrib/bash-completion47
2 files changed, 56 insertions, 15 deletions
diff --git a/bin/hostinfo b/bin/hostinfo
index a25d719..a1dde4a 100755
--- a/bin/hostinfo
+++ b/bin/hostinfo
@@ -5,7 +5,7 @@ import sys
import socket
import getopt
import yaml
-import os.path
+import os
from dns import resolver,reversename
own_directory = os.path.dirname(os.path.abspath(__file__))
@@ -44,14 +44,14 @@ def print_keys(path):
data = _get_data(path)
_print_keys(data)
-def find_host(host):
- path = "/usr/local/share/hostinfo/%s" % host
+def find_host(basepath, host):
+ path = os.path.join(basepath, host)
if os.path.exists(path):
return path
# try to build the fqdn
- path = "/usr/local/share/hostinfo/%s.spline.inf.fu-berlin.de" % \
- host.replace('.spline.de', '')
+ path = os.path.join(basepath, "%s.spline.inf.fu-berlin.de" %
+ host.replace('.spline.de', ''))
if os.path.exists(path):
return path
@@ -59,7 +59,7 @@ def find_host(host):
# try to use reverse dns
addr=reversename.from_address(host)
hostname = str(resolver.query(addr,"PTR")[0])
- path = "/usr/local/share/hostinfo/%s" % hostname[0:-1]
+ path = os.path.join(basepath, hostname[0:-1])
except:
pass
@@ -73,12 +73,15 @@ def print_help(self_name):
def main():
self_name = sys.argv.pop(0)
+ basepath = '/usr/local/share/hostinfo'
+ if 'HOSTINFO_PATH' in os.environ:
+ basepath = os.environ['HOSTINFO_PATH']
file = oneline = keys = verbose = nospaces = help = False
try:
- optlist, args = getopt.gnu_getopt(sys.argv, 'ofkvnh?',
+ optlist, args = getopt.gnu_getopt(sys.argv, 'ofkvnh?p:',
['oneline', 'file', 'keys', 'verbose',
- 'nospaces', 'help'])
+ 'nospaces', 'help', 'path='])
opts = {}
for key, value in optlist:
opts[key] = value
@@ -89,6 +92,9 @@ 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', '-?']])
+
+ if any([o in opts for o in ['--path', '-p']]):
+ basepath = [opts[o] for o in ['--path', '-p'] if o in opts][0]
except getopt.GetoptError, e:
print("Error: %s" % e)
print
@@ -100,7 +106,7 @@ def main():
sys.exit(1)
sys.exit(0)
- path = find_host(args[0])
+ path = find_host(basepath, args[0])
if path is None:
print("Host '%s' could not be found!" % args[0])
sys.exit(1)
diff --git a/contrib/bash-completion b/contrib/bash-completion
index 0c8fb78..1adad22 100644
--- a/contrib/bash-completion
+++ b/contrib/bash-completion
@@ -2,19 +2,54 @@
_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
+
+ arg=
+ for (( i=1; i < COMP_CWORD; i++ )); do
+ if [[ "${COMP_WORDS[i]}" != -* ]]; then
+ arg=${COMP_WORDS[i]}
+ break
+ fi
+
+ [[ "${COMP_WORDS[i]}" == "--path" || "${COMP_WORDS[i]}" == "-p" ]] && i=$((i+1))
+ done
+}
+
+_get_hostinfo_path()
+{
+ local i
+
+ path=
+ for (( i=1; i < COMP_CWORD; i++ )); do
+ if [[ "${COMP_WORDS[i]}" == "--path" || "${COMP_WORDS[i]}" == "-p" ]]; then
+ path="--path ${COMP_WORDS[i+1]}"
+ break
+ fi
+ done
+}
+
_hostinfo() {
local cur prev
COMPREPLY=()
_get_comp_words_by_ref cur prev
- _get_first_arg
- if [[ -z "$arg" ]]; then
- _completion="$_hostinfo_hosts"
+ if [[ "$prev" == "-p" || "$prev" == "--path" ]]; then
+ COMPREPLY=( $( compgen -d -o dirnames -S "/" $cur ) )
+ compopt -o nospace
else
- _completion="$(hostinfo --keys $arg 2>/dev/null)"
- fi
+ _get_first_hostinfo_arg
+ _get_hostinfo_path
+ if [[ -z "$arg" ]]; then
+ _completion="$_hostinfo_hosts"
+ else
+ _completion="$(hostinfo $path --keys $arg 2>/dev/null)"
+ fi
- COMPREPLY=( $( compgen -W "-o --oneline -f --file -k --keys -h -? --help $_completion" -- $cur ) )
+ COMPREPLY=( $( compgen -W "-o --oneline -f --file -k --keys -h -? --help
+ -p --path $_completion" -- $cur ) )
+ fi
}
complete -F _hostinfo hostinfo