Commit 8f373fd5 authored by Gary Wong's avatar Gary Wong
Browse files

Use getopt instead of argparse in geni-get.

This provides better backward compatibility: it avoids the accidental
dependency on Python 2.7, which is not available under (e.g.) FreeBSD 8.2.
parent f87c3fd6
......@@ -28,7 +28,7 @@
# }}}
#
import argparse
import getopt
import re
import socket
import sys
......@@ -36,25 +36,52 @@ import sys
PORT=7777
VERSION=1
parser = argparse.ArgumentParser()
parser.add_argument( '-a', '--all', action='store_true', help='dump all information available' )
parser.add_argument( '-c', '--client-version', action='version', help='display client version', version=str( VERSION ) )
parser.add_argument( '-n', '--no-cache', action='store_true', help='disable reading cached results' ) # Ignored; we don't currently cache anyway...
parser.add_argument( '-p', '--port', type=int, help='specify server port', default=PORT )
parser.add_argument( '-v', '--version', action='store_true', help='display server version' )
parser.add_argument( 'command', nargs='?' )
args = parser.parse_args()
def usage():
print "Usage:"
print " " + sys.argv[ 0 ] + " [options] [command]"
print ""
print "Options:"
print " -a, --all dump all information available"
print " -c, --client-version display client version"
print " -h, --help show this message"
print " -n, --no-cache disable reading cached results"
print " -p, --port specify server port"
print " -v, --version display server version"
if args.all:
command = "all"
elif args.version:
command = "version"
elif args.command:
command = args.command
else:
print >> sys.stderr, sys.argv[ 0 ] + ": no command specified"
try:
opts, args = getopt.getopt( sys.argv[ 1: ], "achnp:v", [ "all", "client-version", "help", "no-cache", "port=", "version" ] )
except getopt.GetoptError as err:
print str( err )
usage()
sys.exit( 1 )
port = PORT
command = None
for opt, param in opts:
if opt in ( "-a", "--all" ):
command = "all"
elif opt in ( "-c", "--client-version" ):
print "1"
sys.exit( 0 )
elif opt in ( "-h", "--help" ):
usage()
sys.exit( 0 )
elif opt in ( "-n", "--no-cache" ):
# Ignored; we don't currently cache anyway...
pass
elif opt in ( "-p", "--port" ):
port = int( param )
elif opt in ( "-v", "--version" ):
command = "version"
if command == None:
if len( args ) > 0:
command = args[ 0 ]
else:
print >> sys.stderr, sys.argv[ 0 ] + ": no command specified"
sys.exit( 1 )
# Our tmcd server is also our DNS server, so that's how we find it.
resolv_conf = open( "/etc/resolv.conf", "r" )
for line in resolv_conf.readlines():
......@@ -69,7 +96,7 @@ else:
resolv_conf.close()
sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
sock.connect( (server, args.port) )
sock.connect( (server, port) )
sock.send( "geni_" + command )
while True:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment