Changeset 801
- Timestamp:
- 09/16/07 22:59:00 (1 year ago)
- Files:
-
- pyrun/trunk/bakescripts.py (moved) (moved from pyrun/trunk/pyscriptbake.py) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
pyrun/trunk/bakescripts.py
r800 r801 9 9 import logging 10 10 log = logging.getLogger(__name__) 11 12 log = logging.getLogger(__name__)13 log.setLevel(logging.INFO)14 11 15 12 pyrun = None … … 216 213 217 214 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 223 def generate_egg_scripts(path, reqs, projects, scripts, bindir, **kw): 222 224 223 225 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'] 227 231 228 232 locations, parameters = resolve_egg_scripts( … … 231 235 generated = [] 232 236 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): 234 240 continue 235 241 # if scripts is a flat list it is simply saying … … 246 252 invoke_program = '%s.%s(%s)' % (module_name, attrs, s[1]) 247 253 else: 248 invoke_program = '%s.%s(%s)' % (module_name, attrs, arguments) 254 invoke_program = '%s.%s(%s)' % (module_name, attrs, 255 arguments or '') 249 256 250 257 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 ) 255 264 256 265 return generated 257 266 258 267 259 def generate_script(module_name, path, dest, extrapath, 260 executable, initialization, 261 invoke_program, template, **xtemplatekw): 268 def generate_script(module_name, path, dest, **opts): 262 269 """Generate script `dest` with bake in relative paths to items in `path` 263 270 264 271 """ 265 272 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 266 287 if invoke_program is None: 267 invoke_program = '%s.run( )' % module_name288 invoke_program = '%s.run(%s)' % (module_name, arguments or '') 268 289 if not template: 269 290 template = script_template … … 272 293 if initialization is None: 273 294 initialization = '' 295 if not xtemplatekw: 296 xtemplatekw = {} 274 297 275 298 generated = [] … … 309 332 # generate exe file and give the script a magic name: 310 333 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 ) 314 338 generated.append(exe) 339 315 340 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) 319 343 try: 320 344 os.chmod(dest, 0755) … … 397 421 # command line tool 398 422 423 def 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 399 437 def get_options(parser=None): 400 438 """Create (or update) an optparse.OptionParser. … … 407 445 408 446 parser.add_option('-q', '--quiet', default=False, action='store_true') 447 parser.add_option('-N', '--dry-run', default=False, action='store_true') 409 448 parser.add_option('-R', dest='requirements', default=[], action='append') 410 449 parser.add_option('-P', dest='projects', default=[], action='append') … … 416 455 def run(argv=None): 417 456 457 logging.basicConfig( 458 level=logging.INFO, 459 format='%(message)s' 460 ) 461 log.setLevel(logging.INFO) 462 463 418 464 ensure_pkg_resources() 419 465 ensure_pyrun() … … 430 476 parser.disable_interspersed_args() 431 477 opts, args = parser.parse_args(args=argv[1:]) 478 479 dry_run = getattr(opts, 'dry_run', False) 432 480 legal_postpath_opts={ 433 481 '-R': opts.requirements, '-P' : opts.projects, '-s':opts.scripts … … 452 500 log.warning('\t' + '\n\t'.join(doesnotexist)) 453 501 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]] 469 504 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 470 518 471 519 generated = generate_egg_scripts(pthextend, … … 473 521 opts.projects, 474 522 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) 477 529 478 530