Changeset ec9962b for fedd


Ignore:
Timestamp:
May 17, 2010 2:34:04 AM (15 years ago)
Author:
Ted Faber <faber@…>
Branches:
axis_example, compt_changes, info-ops, master, version-3.01, version-3.02
Children:
c7a982b
Parents:
b14b495
Message:

Make it simpler to specify multiple files on the command line.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • fedd/compose.py

    rb14b495 rec9962b  
    66import random
    77
    8 from optparse import OptionParser
     8from optparse import OptionParser, OptionValueError
    99from federation.remote_service import service_caller
    1010from federation import topdl
     
    231231    top.incorporate_elements()
    232232
     233def import_tcl_constraints(marks, provides, accepts, contents):
     234    """
     235    Contents is a list containing the lines of an annotated ns2 file.  This
     236    routine extracts the constraint comment lines and convertes them into
     237    constraints in the namespace of the tcl experiment, as well as inserting
     238    them in the accepts and provides indices.
     239    """
     240    for l in contents:
     241        m = const_re.search(l)
     242        if m:
     243            add_to_map(re.sub('\s', '', m.group(1)), marks, provides,
     244                    accepts)
     245
     246
     247def multi_callback(option, opt_str, value, parser):
     248    """
     249    Parse a --multifile command line option.  The parameter is of the form
     250    filename,count.  This splits the argument at the rightmost comma and
     251    inserts the filename, count tuple into the "files" option.  It handles a
     252    couple error cases, too.  This is an optparse.OptionParser callback.
     253    """
     254    idx = value.rfind(',')
     255    if idx != -1:
     256        try:
     257            parser.values.files.append((value[0:idx], int(value[idx+1:])))
     258        except ValueError, e:
     259            raise OptionValueError("Can't convert %s to int in multifile (%s)" \
     260                    % (value[idx+1:], value))
     261    else:
     262        raise OptionValueError("Bad format (need a comma) for multifile: %s" \
     263                % value)
     264
     265
    233266
    234267# Main line begins
     
    243276parser.add_option('--seed', dest='seed', type='int', default=None,
    244277        help='Random number seed')
     278parser.add_option('--multifile', dest='files', default=[], type='string',
     279        action='callback', callback=multi_callback,
     280        help="Include file multiple times")
    245281
    246282opts, args = parser.parse_args()
    247 
    248283
    249284if opts.cert:
     
    256291random.seed(opts.seed)
    257292
     293files = opts.files
     294files.extend([ (a, 1) for a in args])
     295
    258296comps = [ ]
    259297names = set()
     
    261299provides = { }
    262300accepts = { }
    263 for fn in args:
     301for fn, cnt in files:
    264302    marks = { }
    265     contents = ""
    266303    try:
    267304        f = open(fn, "r")
    268         for l in f:
    269             contents += l
    270             m = const_re.search(l)
    271             if m:
    272                 add_to_map(re.sub('\s', '', m.group(1)), marks, provides,
    273                         accepts)
     305        contents = [ l for l in f ]
    274306    except EnvironmentError, e:
    275307        print >>sys.stderr, "Error on %s: %s" % (fn, e)
    276308        continue
    277309
    278     top = remote_ns2topdl(opts.url, contents, cert)
     310    top = remote_ns2topdl(opts.url, "".join(contents), cert)
    279311    if not top:
    280312        sys.exit("Cannot create topology from: %s" % fn)
    281     add_interfaces(top, marks)
    282     localize_names(top, names, marks)
    283     top.incorporate_elements()
    284     constraints.extend(marks.values())
    285     comps.append(top)
     313
     314    for i in range(0, cnt):
     315        t = top.clone()
     316        import_tcl_constraints(marks, provides, accepts, contents)
     317        add_interfaces(t, marks)
     318        localize_names(t, names, marks)
     319        t.incorporate_elements()
     320        constraints.extend(marks.values())
     321        comps.append(t)
    286322
    287323# Let the user know if they messed up on specifying constraints.
Note: See TracChangeset for help on using the changeset viewer.