Changeset 788

Show
Ignore:
Timestamp:
08/04/07 15:10:40 (1 year ago)
Author:
robin
Message:

--

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • pyrun/trunk/ChangeLog

    r787 r788  
    33 
    440.1.1 
     5  * Added -c, its much like python -c 
     6  * The `run script` option changed from -S to -C 
    57  * By default, print a warning if any of the argv discovery path items dont 
    68    exist on the file system. -q suppreses the warnings 
  • pyrun/trunk/pkg-info.rst

    r785 r788  
    11:License: MIT 
    22:Name: pyrun 
    3 :Version: 0.1 
     3:Version: 0.1.1dev 
    44:Author: Robin Bryce 
    55:Author-email: robinbryce@gmail.com 
  • pyrun/trunk/pyrun.py

    r787 r788  
    444444 
    445445 
    446 def compile_and_run(filename, mod_name='__main__', 
     446def compile_and_run(source, mod_name='__main__', 
    447447    init_globals=None, mod_fname=None, mod_loader=None, alter_sys=True): 
    448448    """compile `filename` and run the code using runpy._run_module_code.""" 
     
    454454    if mod_fname is None: 
    455455        mod_fname = filename 
    456     code = compiler.compile(file(filename).read(), filename, 'exec') 
     456    code = compiler.compile(source, mod_fname or '<unknown-source>', 'exec') 
    457457    return runpy._run_module_code(code, 
    458458            mod_name=mod_name, 
     
    482482 
    483483    try: 
     484        source = False 
     485        sourcefile = None 
     486        if opts.c and opts.C and not opts.q: 
     487            print ( 
     488            'Warning: -c and -C can not be used together. Ignoring "-C %s"' 
     489            ) % opts.C 
     490 
     491        if opts.C: 
     492            source = file(opts.C).read() 
     493            sourcefile = opts.C 
     494        if opts.c: 
     495            sourcefile = "<command-line>" 
     496            source = opts.c 
     497 
     498 
    484499        pthextend, minfos, ia, doesnotexist = discover_path( 
    485500                1, *argv) 
     
    544559            # else: its definitely *not* a pyrun option 
    545560 
    546         modname = not opts.S and (opts.m or (minfos and minfos[0][1]) or '') 
     561        if opts.c and opts.C and not opts.q: 
     562            print ( 
     563            'Warning: -c and -C can not be used together. Ignoring "-C %s"' 
     564            ) % opts.C 
     565 
     566        if opts.C: 
     567            source = file(opts.C).read() 
     568            sourcefile = opts.C 
     569        if opts.c: 
     570            sourcefile = "<command-line>" 
     571            source = opts.c 
     572 
     573        modname = not source and (opts.m or (minfos and minfos[0][1]) or '') 
    547574 
    548575        # Allways update the sys path. pthextend is the record of what we have 
     
    555582 
    556583        target_argv = [] 
    557         if modname or opts.S
    558             target_argv.append(modname or opts.S
     584        if modname or source
     585            target_argv.append(modname or source
    559586            target_argv.extend(argv[ia:]) 
    560587 
    561         def run_module(): 
     588        def run(): 
    562589            if opts.n: 
    563590                print 'execution of module disabled by user options' 
     
    568595            sys.argv[:] = target_argv[:] 
    569596            if runpy is not False: 
    570                 if not opts.S
     597                if not source
    571598                    return runpy.run_module( 
    572599                            modname, run_name='__main__', alter_sys=True 
    573600                            ) 
    574601                else: 
    575                     return compile_and_run(opts.S
     602                    return compile_and_run(source, mod_fname=sourcefile
    576603 
    577604        if opts.d: 
     
    592619            banner += """\ 
    593620handy locals() are: 
    594     The function run_module (runs the discovered module if there was one
     621    The function run() (runs the discovered module or -c/-C/-S options
    595622    The variables target_argv, pthextend 
    596623 
    597 Update target_argv *in place* before calling run_module if you want to tweak 
     624Update target_argv *in place* before calling run() if you want to tweak 
    598625the sys.argv the module sees.""" 
    599626 
     
    612639            print os.pathsep.join(pthextend) 
    613640 
    614         if not (opts.n or opts.d or opts.i) and (modname or opts.S): 
    615             exitval = run_module() 
     641        if not (opts.n or opts.d or opts.i) and (modname or source): 
     642            exitval = run() 
    616643            if not isinstance(exitval, int): 
    617644                return 0 
     
    689716 the discovery path.""")), 
    690717 
    691 ('-S', dict(default=None, type='string', metavar='SCRIPT', help= 
     718('-C', dict(default=None, type='string', metavar='SCRIPT', help= 
    692719"""Identify a *python* SCRIPT to execute. The script need not have file 
    693720extension but it must contain leagal python code. This option trumps -m. This 
     
    713740 
    714741('-c', dict(default=False, metavar='STATEMENT', help= 
    715 """Update sys.argv and sys.path then execute statement in a new 
    716 python module. [NYI]""")), 
    717  
    718 ('-C', dict(default=False, metavar='STATEMENT', help= 
    719 """Update sys.argv and sys.path then execute statement in the context of the 
    720 selected module. [NYI]""")) 
     742"""Update sys.argv and sys.path then execute the statement in a new, clean, 
     743module context.""")) 
    721744 
    722745    ]