Revision 3ac0dd70

View differences:

bin/mn
118 118
    print "%s" % VERSION
119 119
    exit()
120 120

  
121
def custom( option, opt_str, value, parser, *args, **kwargs ):
122
    """Parse custom file and add params.
123
       option: option e.g. --custom
124
       opt_str: option string e.g. --custom
125
       value: the value the follows the option
126
       parser: option parser instance
127
       args: empty tuple
128
       kwargs: dict that contains { self : MininetRunner reference }"""
129

  
130
    self = kwargs['self']
131
    files = []
132
    if os.path.isfile( value ):
133
        # accept any single file (including those with commas)
134
        files.append( value )
135
    else:
136
        # accept a comma-separated list of filenames
137
        files += value.split(',')
138

  
139
    for fileName in files:
140
        customs = {}
141
        if os.path.isfile( fileName ):
142
            execfile( fileName, customs, customs )
143
            for name, val in customs.iteritems():
144
                self.setCustom( name, val )
145
        else:
146
            raise Exception( 'could not find custom file: %s' % fileName )
147

  
121 148
class MininetRunner( object ):
122 149
    "Build, setup, and run Mininet."
123 150

  
......
144 171
            # Add or modify global variable or class
145 172
            globals()[ name ] = value
146 173

  
147
    def parseCustomFile( self, fileName ):
148
        "Parse custom file and add params before parsing cmd-line options."
149
        customs = {}
150
        if os.path.isfile( fileName ):
151
            execfile( fileName, customs, customs )
152
            for name, val in customs.iteritems():
153
                self.setCustom( name, val )
154
        else:
155
            raise Exception( 'could not find custom file: %s' % fileName )
156

  
157 174
    def parseArgs( self ):
158 175
        """Parse command-line args and return options object.
159 176
           returns: opts parse options dict"""
160
        if '--custom' in sys.argv:
161
            index = sys.argv.index( '--custom' )
162
            if len( sys.argv ) > index + 1:
163
                filename = sys.argv[ index + 1 ]
164
                self.parseCustomFile( filename )
165
            else:
166
                raise Exception( 'Custom file name not found' )
167 177

  
168 178
        desc = ( "The %prog utility creates Mininet network from the\n"
169 179
                 "command line. It can create parametrized topologies,\n"
......
181 191

  
182 192
        opts.add_option( '--clean', '-c', action='store_true',
183 193
                         default=False, help='clean and exit' )
184
        opts.add_option( '--custom', type='string', default=None,
185
                         help='read custom topo and node params from .py' +
186
                         'file' )
194
        opts.add_option( '--custom', action='callback', callback=custom,
195
                         type='string', callback_kwargs={ 'self' : self },
196
                         help='read custom classes or params from .py file' )
187 197
        opts.add_option( '--test', type='choice', choices=TESTS,
188 198
                         default=TESTS[ 0 ],
189 199
                         help='|'.join( TESTS ) )
......
215 225
        opts.add_option( '--nat', action='store_true',
216 226
                         default=False, help="adds a NAT to the topology "
217 227
                         "that connects Mininet to the physical network" )
218
        opts.add_option( '--version', action='callback', callback=version )
228
        opts.add_option( '--version', action='callback', callback=version,
229
                         help='prints the version and exits' )
219 230
        opts.add_option( '--cluster', type='string', default=None,
220 231
                         metavar='server1,server2...',
221 232
                         help=( 'run on multiple servers (experimental!)' ) )

Also available in: Unified diff