예제 #1
0
파일: gsh_console.py 프로젝트: tiradani/gsh
    def __init__(self, site):
        cmd.Cmd.__init__(self)
        self.suffix = "=>> "
        self.intro = "Welcome to gsh! "

        self.prompt = site + " " + self.suffix
        self.keywords = ["!", "hist", "exit", "help", "setsite", "getsite", "EOF", "vdt_location", "version"]
        self.home = os.environ["HOME"]
        self.history = "%s/.gsh_history" % self.home
        self.workfile = "%s/gsh_workfile" % self.home

        call(["/bin/touch", self.workfile])
        call(["/bin/touch", self.history])

        # site specific variables
        self.site_env = {}

        self.site = site
        self.site_name = getSiteNameFromFQDN(self.site)
        # register command handler
        self.commandHandler = CommandHandler(self)

        pwd = self.commandHandler.get_pwd(empty_cwd=True)
        self.cwd = pwd.split("/")
        self.old_cwd = None
예제 #2
0
파일: gsh_console.py 프로젝트: holzman/gsh
    def __init__(self, site, gsh_location):
        cmd.Cmd.__init__(self)
        self.suffix = "=>> "
        self.intro  = "Welcome to gsh! "

        self.prompt = site + " " + self.suffix
        self.keywords = ["!", "hist", "exit", "help", "setsite", "getsite", 
                         "EOF", "vdt_location", "version"]
        self.home = os.environ['HOME']
        self.history = "%s/.gsh_history" % self.home
        self.workfile = "%s/gsh_workfile" % self.home

        call(["/bin/touch", self.workfile])
        call(["/bin/touch", self.history])

        # site specific variables
        self.site_env = {}
        self.site_env["PWD"] = ""

        self.site = site
        self.site_name = getSiteNameFromFQDN(self.site)
        # register command handler
        self.commandHandler = CommandHandler(self)
예제 #3
0
파일: gsh_console.py 프로젝트: holzman/gsh
class Console(cmd.Cmd):
    def __init__(self, site, gsh_location):
        cmd.Cmd.__init__(self)
        self.suffix = "=>> "
        self.intro  = "Welcome to gsh! "

        self.prompt = site + " " + self.suffix
        self.keywords = ["!", "hist", "exit", "help", "setsite", "getsite", 
                         "EOF", "vdt_location", "version"]
        self.home = os.environ['HOME']
        self.history = "%s/.gsh_history" % self.home
        self.workfile = "%s/gsh_workfile" % self.home

        call(["/bin/touch", self.workfile])
        call(["/bin/touch", self.history])

        # site specific variables
        self.site_env = {}
        self.site_env["PWD"] = ""

        self.site = site
        self.site_name = getSiteNameFromFQDN(self.site)
        # register command handler
        self.commandHandler = CommandHandler(self)

    ## Command definitions to support Cmd object functionality ##
    def do_EOF(self, args):
        """Exit on system end of file character"""
        readline.write_history_file(self.history)
        call(["/bin/rm", "-f", self.workfile])

        return self.do_exit(args)

    def do_shell(self, args):
        """Pass command to a system shell when line begins with '!'"""
        if isinstance(args, types.StringType):
            args = shlex.split(args)
        call(args)

    def do_help(self, args):
        """Get help on commands
           'help' or '?' with no arguments prints a list of commands for which help is available
           'help <command>' or '? <command>' gives help on <command>
        """
        ## The only reason to define this method is for the help text in the doc string
        cmd.Cmd.do_help(self, args)

    ## Override methods in Cmd object ##
    def preloop(self):
        """Initialization before prompting user for commands.
           Despite the claims in the Cmd documentaion, Cmd.preloop() is not a stub.
        """
        cmd.Cmd.preloop(self)   ## sets up command completion
        # need to read history and set self._hist
        f = open(self.history)
        hist = f.readlines()
        f.close()
        self._hist = [a[:-1] for a in hist]
        readline.read_history_file(self.history)
        self._locals  = {}      ## Initialize execution namespace for user
        self._globals = {}

    def postloop(self):
        """Take care of any unfinished business.
           Despite the claims in the Cmd documentaion, Cmd.postloop() is not a stub.
        """
        cmd.Cmd.postloop(self)   ## Clean up command completion
        print "Exiting..."

    def precmd(self, line):
        """ This method is called after the line has been input but before
            it has been interpreted. If you want to modify the input line
            before execution (for example, variable substitution) do it here.
        """
        self._hist += [ line.strip() ]
        if len(line) > 1: # added so that gsh doesn't bomb when user presses enter without typing anything else
            if not (line.split()[0] in self.keywords):
                if len(self.site) < 1:
                    print "You did not specify a site.  Please execute setsite <fqdn> with a valid fqdn."
                    line = ""
                else:
                    line = self.commandHandler.customCommand(line, self.workfile)

        return line

    def postcmd(self, stop, line):
        """If you want to stop the console, return something that evaluates to true.
           If you want to do some post command processing, do it here.
        """
        return stop

    def emptyline(self):
        """Do nothing on empty input line"""
        pass

    def default(self, line):
        """Called on an input line when the command prefix is not recognized.
           In that case we execute the line as Python code.
        """
        try:
            exec(line) in self._locals, self._globals
        except Exception, e:
            print e.__class__, ":", e