Changeset 798

Show
Ignore:
Timestamp:
09/16/07 16:48:24 (1 year ago)
Author:
robin
Message:

eggtool updated for use with pyrun

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • pyrun/trunk/pyrun.py

    r797 r798  
    317317        else: 
    318318            yield ia, a 
     319 
     320 
     321def get_inferior_argv( 
     322        opts, default_opts, flag_opts, argv, ia, short_opts=None): 
     323    if short_opts is None: 
     324        short_opts = {} 
     325 
     326    assert ia <= len(argv), ( 
     327            'ia=%s, argv="%s"' 
     328            ) % (ia, str(argv) 
     329                    ) 
     330    if ia == len(argv): 
     331        return [] 
     332 
     333    # Unconsumed arguments exist and start at index `ia` in `argv` As a 
     334    # convenience we allow any *one* of the caller options to be used as to 
     335    # delimit the end of its non option arguments. This means that we need to 
     336    # examine the first remaining option and determine whether its intended for 
     337    # the caller and if so whether it means we have more to do. But, and this 
     338    # is *IMPORTANT*, if any option value was taken before calling this routine 
     339    # it is assumed that the instance remaining in argv is for the inferior 
     340    # target and and hence is left in place. For example ``pyrun -m bar ~/foo 
     341    # -m 12`` means search under `~/foo` for package paths, then run module 
     342    # `bar` with an argv of: ``['bar', '-m', '12']`` 
     343    # 
     344 
     345    if argv[ia] == '--': 
     346        del argv[ia] 
     347    else: 
     348 
     349        # Short or long ? 
     350        if argv[ia].startswith('--'): 
     351            nextopt = argv[ia][2:] 
     352        else: 
     353            nextopt = argv[ia][1:] 
     354            if nextopt not in default_opts: 
     355                nextopt = short_opts[nextopt] 
     356 
     357        # If its one of the callers options and the callers option values does 
     358        # not have the corresponding value set then take nextopt; This alows 
     359        # for a degree of overlap in options between the caller and the 
     360        # inferior target - only the first instance of an option will be taken 
     361        # by the caller. 
     362 
     363        if nextopt in default_opts and ( 
     364                getattr(opts, nextopt) == default_opts[nextopt]): 
     365 
     366            # It's possibly a caller option. 
     367 
     368            # Distinguish between those arguments that require values and those 
     369            # that don't 
     370            if nextopt in flag_opts: 
     371 
     372                setattr(opts, nextopt, not default_opts[nextopt]) 
     373                del argv[ia] 
     374 
     375            else: 
     376                # if its not a flag and it is a caller option then it takes a 
     377                # single argument. 
     378                if len(argv) >= ia+1: 
     379                    setattr(opts, nextopt, argv[ia+1]) 
     380                    del argv[ia:ia+2] 
     381                else: 
     382                    log.warning( 
     383                            'The `-%s` option requires an argument', nextopt) 
     384 
     385 
     386        # else: its definitely *not* a caller option, leave it for the inferior 
     387    return argv[ia:] 
    319388 
    320389 
     
    583652    return '\n'.join(map(''.__class__.strip, s.split('\n'))) 
    584653 
     654 
     655 
    585656interactive_BANNER_BOILERPLATE = """\ 
    586657handy locals() are: 
     
    682753        # If there are no unconsumed arguments: We have already determined 
    683754        # the user does not want to run a module. So we are done. 
    684  
    685         assert ia <= len(argv), ( 
    686                 'ia=%s, argv="%s"' 
    687                 ) % (ia, str(argv) 
    688                         ) 
    689         if ia == len(argv): 
    690             argv.append('--') 
    691  
    692         # Unconsumed arguments exist and start at index `ia` in `argv` As a 
    693         # convenience we allow any *one* of the pyrun options to be used as to 
    694         # delimit the end of the discovery path. This means that we need to 
    695         # examine the first remaining option and determine whether its intended 
    696         # for pyrun and if so whether it means we have more to do. But, and 
    697         # this is *IMPORTANT*, if any option was set before the discovery path 
    698         # it is assumed to be for the target module and hence is ignored. For 
    699         # example ``pyrun -m bar ~/foo -m 12`` means search under `~/foo` for 
    700         # package paths, then run module `bar` with an argv of: 
    701         #   ``['bar', '-m', '12']`` 
    702         # 
    703  
    704         if argv[ia] == '--': 
    705             del argv[ia] 
    706         else: 
    707             # All pyrun options are short options. 
    708             nextopt = argv[ia][1:] 
    709             if nextopt in default_opts and ( 
    710                     getattr(opts, nextopt) == default_opts[nextopt]): 
    711  
    712                 # It's possibly a pyrun option. 
    713  
    714                 # Distinguish between those arguments that require values and 
    715                 # those that don't 
    716                 if nextopt in flagOPTIONS_runex: 
    717  
    718                     setattr(opts, nextopt, not default_opts[nextopt]) 
    719                     del argv[ia] 
    720  
    721                 else: 
    722                     # if its not a flag and it is a pyrun option then it 
    723                     # takes a single argument. 
    724                     if len(argv) < ia+1: 
    725                         print ('The `-%s` pyrun option requires an argument' 
    726                                 ) % nextopt 
    727                         return -1 
    728  
    729                     setattr(opts, nextopt, argv[ia+1]) 
    730                     del argv[ia:ia+2] 
    731  
    732             # else: its definitely *not* a pyrun option 
     755        inferior_argv = get_inferior_argv( 
     756                opts, default_opts, flagOPTIONS_runex, argv, ia) 
    733757 
    734758        if opts.c and opts.C and not opts.q: 
     
    768792        if modname or source: 
    769793            target_argv.append(modname or source) 
    770             target_argv.extend(argv[ia:]
     794            target_argv.extend(inferior_argv
    771795 
    772796        def run(): 
     
    943967    ] 
    944968 
    945 flagOPTIONS_runex=tuple([o[1:] for o, kw in OPTIONS_runex 
     969def _get_flag_opts(): 
     970    return tuple([o[1:] for o, kw in OPTIONS_runex 
    946971    if kw.get('action', None) in ('store_true', 'store_false')] 
    947972    ) 
    948973 
     974flagOPTIONS_runex=_get_flag_opts() 
     975 
    949976 
    950977if __name__=='__main__':