Setting tag plcapi-5.4-2
[plcapi.git] / plcsh
diff --git a/plcsh b/plcsh
index 7c4b69a..7bc3abe 100755 (executable)
--- a/plcsh
+++ b/plcsh
@@ -5,33 +5,47 @@
 # Mark Huang <mlhuang@cs.princeton.edu>
 # Copyright (C) 2005 The Trustees of Princeton University
 #
-# $Id: plcsh,v 1.2 2007/01/11 05:28:49 mlhuang Exp $
-#
 
 import os
 import sys
+from socket import gethostname
 from optparse import OptionParser
+from getpass import getpass
 from traceback import print_exc
 
 sys.path.append(os.path.dirname(os.path.realpath(sys.argv[0])))
 from PLC.Shell import Shell
 
-parser = OptionParser(add_help_option = False)
+usage="""Usage: %prog [options]
+   runs an interactive shell
+Usage: %prog [options] script script-arguments
+Usage: %prog script [plcsh-options --] script arguments
+   run a script"""
+
+parser = OptionParser(usage=usage,add_help_option = False)
 parser.add_option("-f", "--config", help = "PLC configuration file")
 parser.add_option("-h", "--url", help = "API URL")
 parser.add_option("-c", "--cacert", help = "API SSL certificate")
+parser.add_option("-k", "--insecure", help = "Do not check SSL certificate")
 parser.add_option("-m", "--method", help = "API authentication method")
+parser.add_option("-s", "--session", help = "API session key")
 parser.add_option("-u", "--user", help = "API user name")
 parser.add_option("-p", "--password", help = "API password")
 parser.add_option("-r", "--role", help = "API role")
 parser.add_option("-x", "--xmlrpc", action = "store_true", default = False, help = "Use XML-RPC interface")
-parser.add_option("--help", action = "help", help = "show this help message and exit")
+# pass this to the invoked shell if any
+parser.add_option("--help", action = "store_true", dest="help", default=False,
+                  help = "show this help message and exit")
 (options, args) = parser.parse_args()
 
+if not args and options.help:
+    parser.print_help()
+    sys.exit(1)    
+
 # If user is specified but password is not
 if options.user is not None and options.password is None:
     try:
-        options.password = getpass.getpass()
+        options.password = getpass()
     except (EOFError, KeyboardInterrupt):
         print
         sys.exit(0)
@@ -43,7 +57,8 @@ try:
                   config = options.config,
                   url = options.url, xmlrpc = options.xmlrpc, cacert = options.cacert,
                   method = options.method, role = options.role,
-                  user = options.user, password = options.password)
+                  user = options.user, password = options.password,
+                  session = options.session)
     # Register a few more globals for backward compatibility
     auth = shell.auth
     api = shell.api
@@ -54,11 +69,23 @@ except Exception, err:
     parser.print_help()
     sys.exit(1)
 
-# If called by a script
-if len(sys.argv) > 1 and os.path.exists(sys.argv[1]):
-    # Pop us off the argument stack
-    sys.argv.pop(0)
-    execfile(sys.argv[0])
+# If called by a script 
+if args:
+    if not os.path.exists(args[0]):
+        print 'File %s not found'%args[0]
+        parser.print_help()
+        sys.exit(1)
+    else:
+        # re-append --help if provided
+        if options.help:
+            args.append('--help')
+        # use args as sys.argv for the next shell, so our own options get removed for the next script
+        sys.argv = args
+        script = sys.argv[0]
+        # Add of script to sys.path 
+        path = os.path.dirname(os.path.abspath(script))
+        sys.path.append(path)
+        execfile(script)
 
 # Otherwise, run an interactive shell environment
 else:
@@ -68,6 +95,11 @@ else:
     elif shell.auth['AuthMethod'] == "anonymous":
         prompt = "[anonymous]"
         print "Connected anonymously"
+    elif shell.auth['AuthMethod'] == "session":
+        # XXX No way to tell node and user sessions apart from the
+        # client point of view.
+        prompt = "[%s]" % gethostname()
+        print "%s connected using session authentication" % gethostname()
     else:
         prompt = "[%s]" % shell.auth['Username']
         print "%s connected using %s authentication" % \