dynagen-0.11.0/0000755000076500007650000000000011002651157011660 5ustar greggregdynagen-0.11.0/confConsole.py0000600000076500007650000025767611002651157014521 0ustar greggreg#!/usr/bin/python # -*- coding: utf-8 -*- """ confConsole.py Copyright (C) 2007 Pavel Skovajsa This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. """ import os import cmd import re from dynamips_lib import DynamipsError, DynamipsWarning, GENERIC_7200_PAS, GENERIC_3600_NMS, GENERIC_1700_NMS, GENERIC_2600_NMS, GENERIC_3700_NMS # Regex matching slot definitions. Used in pre_cmd() function in confRouterConsole SLOT_RE = re.compile('^slot[0-6]', re.IGNORECASE) # determine if we are in the debugger try: DBGPHideChildren except NameError: DEBUGGER = False else: DEBUGGER = True ###some functions from console.py################## def error(msg): """Print out an error message""" print '*** Error:', str(msg) def debug(string): """ Print string if debugging is true""" import __main__ # Debug level 2, console debugs if __main__.dynagen.debuglevel >= 2: print ' DEBUG: ' + str(string) ##############end of some functions from console.py################## class AbstractConsole(cmd.Cmd): """abstract console class, all other console and confconsole classes inherit behavior from it""" def __init__(self): cmd.Cmd.__init__(self) # Import the main namespace for use in this module # Yes, normally this is bad mojo, but I'm doing this to provide the console # access to the entire namespace in case the user wants to futz with stuff import __main__ self.namespace = __main__ self.debuglevel = self.namespace.dynagen.debuglevel ## 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 self._hist = [] ## No history yet self._locals = {} ## Initialize execution namespace for user self._globals = {} # Give the console access to the namespace self._globals['namespace'] = self.namespace 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 modifdy the input line before execution (for example, variable substitution) do it here. """ self._hist += [line.strip()] tokens = line.split() try: if tokens[0].lower() == 'con': tokens[0] = 'console' line = (' ').join(tokens) except IndexError: pass 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 do_py(self, line): """py \tExecute python statements""" if line == '?': print self.do_py.__doc__ return try: exec line in self._locals, self._globals except Exception, e: print e.__class__, ':', e 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. """ error('unknown command') def do_hist(self, args): """Print a list of commands that have been entered""" print self._hist def do_exit(self, args): """Exits from the console""" return -1 def do_end(self, args): """Exits from the console""" return -1 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 ' or '? ' gives help on """ ## The only reason to define this method is for the help text in the doc string cmd.Cmd.do_help(self, args) class confDefaultsConsole(AbstractConsole): """console class for managing the device Defaults console""" def __init__(self, prompt, dynagen, dynamips_server): AbstractConsole.__init__(self) self.prompt = prompt[:-1] + '-)' self.dynagen = dynagen self.dynamips_server = dynamips_server self.d = self.dynamips_server.host + ':' + str(self.dynamips_server.port) self.config = self.dynagen.defaults_config[self.d] self.adaptertuple = () self.max_slots = 6 self.min_slot = 0 self.chassis = 'None' self.default_image = 'None' self.default_ghostios = 'False' self.default_cnfg = 'None' self.default_conf = 'None' self.default_confreg = '0x2102' self.default_aux = 'None' self.default_image = 'None' self.default_idlepc = 'None' self.default_exec_area = 'None' self.default_mmap = True self.default_sparsemem = 'False' self.default_ghostsize = None def do_exit(self, args): """Exits from the console""" #if there are no scalars in self.config delete it if self.config.keys() == []: del self.dynagen.defaults_config[self.d][self.chassis] self.dynamips_server.configchange = True return -1 def precmd(self, line): ''' This method is called after the line has been input but before it has been interpreted. If you want to modifdy the input line before execution (for example, variable substitution) do it here. This is the tricky part how we use this method over here in confDefaultsRouterCOnsole. By entering "slot1 = NM-16ESW" the method do_slot will not be called just because there is that number 1 over there. So we will do a trick - change the line internally to "slot 1 = NM-16ESW". ''' self._hist += [line.strip()] if len(line) <= 4: return line first_four_letters = line[:4] if first_four_letters == 'slot': return 'slot ' + line[4:] else: return line def clean_args(self, args): """clean the arguments from spaces and stuff""" #get rid of that '=' argument = args.strip('=') #get rid of starting space argument = argument.strip() return argument def set_int_option(self, option, args): """set integer type option in config""" if '?' in args or args.strip() == '': print getattr(self, 'do_' + option).__doc__ return argument = self.clean_args(args) try: option_value = int(argument) if getattr(self, 'default_' + option) == option_value: if self.config.has_key(option): del self.config[option] else: self.config[option] = option_value except (TypeError, ValueError): error('enter number, not: ' + argument) self.dynamips_server.configchange = True def set_string_option(self, option, args): """set string type option in config""" if '?' in args or args.strip() == '': print getattr(self, 'do_' + option).__doc__ return option_value = self.clean_args(args) if getattr(self, 'default_' + option) == option_value: if self.config.has_key(option): del self.config[option] else: self.config[option] = option_value self.dynamips_server.configchange = True def do_no(self, args): """no