Setting tag plcapi-7.0-0
[plcapi.git] / plcsh
diff --git a/plcsh b/plcsh
index fed505f..15a42a5 100755 (executable)
--- a/plcsh
+++ b/plcsh
@@ -1,12 +1,10 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
 #
 # Interactive shell for testing PLCAPI
 #
 # Mark Huang <mlhuang@cs.princeton.edu>
 # Copyright (C) 2005 The Trustees of Princeton University
 #
-# $Id: plcsh,v 1.5 2007/02/02 04:39:03 mlhuang Exp $
-#
 
 import os
 import sys
@@ -18,7 +16,13 @@ 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")
@@ -28,16 +32,23 @@ 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")
+parser.add_option("-x", "--xmlrpc", action = "store_true",
+                  default = False, help = "Use XML-RPC interface")
+# 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()
     except (EOFError, KeyboardInterrupt):
-        print
+        print()
         sys.exit(0)
 
 # Initialize a single global instance (scripts may re-initialize
@@ -53,46 +64,61 @@ try:
     auth = shell.auth
     api = shell.api
     config = shell.config
-except Exception, err:
-    print "Error:", err
-    print
+except Exception as err:
+    print("Error:", err)
+    print()
     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 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)
+        with open(script) as feed:
+            exec(feed.read())
 
 # Otherwise, run an interactive shell environment
 else:
     if shell.server is None:
-        print "PlanetLab Central Direct API Access"
+        print("PlanetLab Central Direct API Access")
         prompt = ""
     elif shell.auth['AuthMethod'] == "anonymous":
         prompt = "[anonymous]"
-        print "Connected anonymously"
+        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()
+        print("%s connected using session authentication" % gethostname())
     else:
         prompt = "[%s]" % shell.auth['Username']
-        print "%s connected using %s authentication" % \
-              (shell.auth['Username'], shell.auth['AuthMethod'])
+        print("%s connected using %s authentication" % \
+              (shell.auth['Username'], shell.auth['AuthMethod']))
 
     # Readline and tab completion support
     import atexit
     import readline
     import rlcompleter
 
-    print 'Type "system.listMethods()" or "help(method)" for more information.'
+    print('Type "system.listMethods()" or "help(method)" for more information.')
     # Load command history
     history_path = os.path.join(os.environ["HOME"], ".plcapi_history")
     try:
-        file(history_path, 'a').close()
+        # check directory: pretend to open
+        with open(history_path, 'a') as check:
+            pass
         readline.read_history_file(history_path)
         atexit.register(readline.write_history_file, history_path)
     except IOError:
@@ -111,11 +137,11 @@ else:
                         sep = ">>> "
                     else:
                         sep = "... "
-                    line = raw_input(prompt + sep)
+                    line = input(prompt + sep)
                 # Ctrl-C
                 except KeyboardInterrupt:
                     command = ""
-                    print
+                    print()
                     break
 
                 # Build up multi-line command
@@ -139,13 +165,13 @@ else:
                     # Try evaluating as an expression and printing the result
                     result = eval(command)
                     if result is not None:
-                        print result
+                        print(result)
                 except SyntaxError:
                     # Fall back to executing as a statement
-                    exec command
-            except Exception, err:
+                    exec(command)
+            except Exception as err:
                 print_exc()
 
     except EOFError:
-        print
+        print()
         pass