Changeset 801

Show
Ignore:
Timestamp:
09/16/07 22:59:00 (1 year ago)
Author:
robin
Message:

--

Files:

Legend:

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

    r800 r801  
    99import logging 
    1010log = logging.getLogger(__name__) 
    11  
    12 log = logging.getLogger(__name__) 
    13 log.setLevel(logging.INFO) 
    1411 
    1512pyrun = None 
     
    216213 
    217214 
    218 def generate_egg_scripts(path, reqs, projects, scripts, bindir, 
    219         extrapath=(), 
    220         executable=None, initialization='', 
    221         arguments=None, template=None, **xtemplatekw): 
     215_default_generator_opts=dict( 
     216    dry_run=False, 
     217    extrapath=(), 
     218    executable=None, initialization='', 
     219    arguments=None, template=None, 
     220    xtemplatekw=None 
     221    ) 
     222 
     223def generate_egg_scripts(path, reqs, projects, scripts, bindir, **kw): 
    222224 
    223225    ensure_pkg_resources() 
    224  
    225     if not arguments: 
    226         arguments = '' 
     226    variables = _default_generator_opts.copy() 
     227    variables.update(kw) 
     228 
     229 
     230    arguments = variables['arguments'] 
    227231 
    228232    locations, parameters = resolve_egg_scripts( 
     
    231235    generated = [] 
    232236    for sname, name, module_name, attrs in parameters: 
    233         if name not in scripts: 
     237        # If we are simply given a path, generate *ALL* scripts, 
     238        # Otherwise only generate those which are explicitly requested. 
     239        if name not in scripts and (reqs or projects or scripts): 
    234240            continue 
    235241        # if scripts is a flat list it is simply saying 
     
    246252            invoke_program = '%s.%s(%s)' % (module_name, attrs, s[1]) 
    247253        else: 
    248             invoke_program = '%s.%s(%s)' % (module_name, attrs, arguments) 
     254            invoke_program = '%s.%s(%s)' % (module_name, attrs, 
     255                    arguments or '') 
    249256 
    250257        sname = join(bindir, sname) 
    251         generated.append( 
    252                 generate_script(module_name, locations, sname, extrapath, 
    253             executable, initialization, invoke_program, 
    254             template, **xtemplatekw)) 
     258        generated.extend( 
     259            generate_script(module_name, locations, sname, 
     260                invoke_program=invoke_program, 
     261                **variables 
     262                ) 
     263            ) 
    255264 
    256265    return generated 
    257266 
    258267 
    259 def generate_script(module_name, path, dest, extrapath, 
    260         executable, initialization, 
    261         invoke_program, template, **xtemplatekw): 
     268def generate_script(module_name, path, dest, **opts): 
    262269    """Generate script `dest` with bake in relative paths to items in `path` 
    263270 
    264271    """ 
    265272 
     273    ensure_pkg_resources() 
     274 
     275    variables = _default_generator_opts.copy() 
     276    variables.update(opts) 
     277 
     278    dry_run = variables['dry_run'] 
     279    extrapath = variables['extrapath'] 
     280    executable = variables['executable'] 
     281    initialization = variables['initialization'] 
     282    arguments = variables['arguments'] 
     283    template = variables['template'] 
     284    xtemplatekw = variables['xtemplatekw'] 
     285    invoke_program = variables.get('invoke_program', None) 
     286 
    266287    if invoke_program is None: 
    267         invoke_program = '%s.run()' % module_name 
     288        invoke_program = '%s.run(%s)' % (module_name, arguments or '') 
    268289    if not template: 
    269290        template = script_template 
     
    272293    if initialization is None: 
    273294        initialization = '' 
     295    if not xtemplatekw: 
     296        xtemplatekw = {} 
    274297 
    275298    generated = [] 
     
    309332            # generate exe file and give the script a magic name: 
    310333            exe = script+'.exe' 
    311             open(exe, 'wb').write( 
    312                 pkg_resources.resource_string('setuptools', 'cli.exe') 
    313                 ) 
     334            if not dry_run: 
     335                open(exe, 'wb').write( 
     336                    pkg_resources.resource_string('setuptools', 'cli.exe') 
     337                    ) 
    314338            generated.append(exe) 
     339 
    315340    if changed: 
    316         open(dest, 'w').write(contents) 
    317         log.info("Generated script %r.", script) 
    318  
     341        if not dry_run: 
     342            open(dest, 'w').write(contents) 
    319343        try: 
    320344            os.chmod(dest, 0755) 
     
    397421# command line tool 
    398422 
     423def reportline(head, tail, truncate=78, reportmethod=log.info): 
     424 
     425    if not truncate: 
     426        reportmethod(''.join([head, tail])) 
     427    else: 
     428        maxlen = truncate - len(tail) - 1 
     429        if len(head) > maxlen: 
     430            maxlen -= len(' ...') 
     431            reportmethod(head[:maxlen] + ' ... ' + tail) 
     432        else: 
     433            head += ' ' * (maxlen - len(head)) 
     434            reportmethod(''.join([head, tail])) 
     435 
     436 
    399437def get_options(parser=None): 
    400438    """Create (or update) an optparse.OptionParser. 
     
    407445 
    408446    parser.add_option('-q', '--quiet', default=False, action='store_true') 
     447    parser.add_option('-N', '--dry-run', default=False, action='store_true') 
    409448    parser.add_option('-R', dest='requirements', default=[], action='append') 
    410449    parser.add_option('-P', dest='projects', default=[], action='append') 
     
    416455def run(argv=None): 
    417456 
     457    logging.basicConfig( 
     458        level=logging.INFO, 
     459        format='%(message)s' 
     460    ) 
     461    log.setLevel(logging.INFO) 
     462 
     463 
    418464    ensure_pkg_resources() 
    419465    ensure_pyrun() 
     
    430476    parser.disable_interspersed_args() 
    431477    opts, args = parser.parse_args(args=argv[1:]) 
     478 
     479    dry_run = getattr(opts, 'dry_run', False) 
    432480    legal_postpath_opts={ 
    433481            '-R': opts.requirements, '-P' : opts.projects, '-s':opts.scripts 
     
    452500        log.warning('\t' + '\n\t'.join(doesnotexist)) 
    453501 
    454  
    455     curoplist = legal_postpath_opts[argv[ia]] 
    456     ia += 1 
    457     while ia < len(argv): 
    458         if argv[ia] in legal_postpath_opts: 
    459             curoplist = legal_postpath_opts[argv[ia]] 
    460         elif argv[ia].startswith('-'): 
    461             log.error(dedent('''\ 
    462             "%s" is ileagal after the discovery path. [%s] are the only options 
    463             which may follow the discovery path.''' % ( 
    464                 argv[ia], '|'.join(legal_postpath_opts.keys()))) 
    465             ) 
    466             sys.exit(-1) 
    467         else: 
    468             curoplist.append(argv[ia]) 
     502    if ia < len(argv): 
     503        curoplist = legal_postpath_opts[argv[ia]] 
    469504        ia += 1 
     505        while ia < len(argv): 
     506            if argv[ia] in legal_postpath_opts: 
     507                curoplist = legal_postpath_opts[argv[ia]] 
     508            elif argv[ia].startswith('-'): 
     509                log.error(dedent('''\ 
     510                "%s" is ileagal after the discovery path. [%s] are the only 
     511                options which may follow the discovery path.''' % ( argv[ia], 
     512                    '|'.join(legal_postpath_opts.keys()))) 
     513                ) 
     514                sys.exit(-1) 
     515            else: 
     516                curoplist.append(argv[ia]) 
     517            ia += 1 
    470518 
    471519    generated = generate_egg_scripts(pthextend, 
     
    473521            opts.projects, 
    474522            opts.scripts, 
    475             opts.bindir) 
    476     print 'Generated %d scripts' % len(generated) 
     523            opts.bindir, 
     524            dry_run=dry_run) 
     525 
     526    status='[ok]' if not dry_run else '[dry-run]' 
     527    for sn in generated: 
     528        reportline(sn, status) 
    477529 
    478530