| 493 | | |
|---|
| 494 | | |
|---|
| 495 | | def pth_import(modulename, fallback_paths, promiscuous_eggpaths=True, |
|---|
| 496 | | code_getter=get_module_code, path_adder=None, |
|---|
| 497 | | projectversion=False, pth_filename='pyrun.pth', loginfo=log.info): |
|---|
| 498 | | """Attempt to obtain the module code for modulename. |
|---|
| 499 | | |
|---|
| 500 | | The following heuristic is used to attempt the import from a variety of |
|---|
| 501 | | fallback locations **if the initial get fails**: |
|---|
| 502 | | |
|---|
| 503 | | If any step is successful break out immediately and compute the |
|---|
| 504 | | return path. |
|---|
| 505 | | |
|---|
| 506 | | 1. Make the first attempt without modifying the path. |
|---|
| 507 | | |
|---|
| 508 | | 2. If pth_filename file exists in fallback_paths[0] then extend the |
|---|
| 509 | | path, via ``site.addsitedir`` with the directories listed in that file. |
|---|
| 510 | | |
|---|
| 511 | | 3. Attempt again. |
|---|
| 512 | | |
|---|
| 513 | | 4. If the import failed AND projectversion is not False, |
|---|
| 514 | | contstruct a glob |
|---|
| 515 | | pattern like this:: |
|---|
| 516 | | |
|---|
| 517 | | abs_normpath(fallback_path_item, |
|---|
| 518 | | projectversion+'-py'+sys.version[:3]+'*.egg') |
|---|
| 519 | | |
|---|
| 520 | | and sort the results **in lexically ascending order**. If the result is non |
|---|
| 521 | | empty add it to the path and try again |
|---|
| 522 | | |
|---|
| 523 | | repeat 2., 3., 4. for every remaining path in fallback_paths |
|---|
| 524 | | |
|---|
| 525 | | """ |
|---|
| 526 | | if not path_adder: |
|---|
| 527 | | import site |
|---|
| 528 | | # don't want to muck around with site._init_pathinfo and known_paths |
|---|
| 529 | | added = set([]) |
|---|
| 530 | | def path_adder(p, e=None): |
|---|
| 531 | | return _path_adder(site.addsitedir, added, p, e=e, loginfo=loginfo) |
|---|
| 532 | | |
|---|
| 533 | | try: |
|---|
| 534 | | modcode = code_getter(modulename) |
|---|
| 535 | | return modcode |
|---|
| 536 | | except ImportError: |
|---|
| 537 | | pass |
|---|
| 538 | | for p in fallback_paths: |
|---|
| 539 | | if is_zipfile(p) or isdir(p): |
|---|
| 540 | | adder_key = path_adder(p) |
|---|
| 541 | | try: |
|---|
| 542 | | modcode = code_getter(modulename) |
|---|
| 543 | | return modcode |
|---|
| 544 | | except ImportError, e: |
|---|
| 545 | | path_adder(adder_key, e) |
|---|
| 546 | | if not projectversion and isdir(p): |
|---|
| 547 | | continue |
|---|
| 548 | | # glob patterns only apply when a projectversion is supplied and |
|---|
| 549 | | # the fallback path is a directory. |
|---|
| 550 | | pattern = abs_normpath(p, projectversion+'-py'+sys.version[:3]+'*.egg') |
|---|
| 551 | | loginfo and loginfo('egg glob pattern: %s', pattern) |
|---|
| 552 | | projecteggs = glob(pattern) |
|---|
| 553 | | if not projecteggs or eggpaths: |
|---|
| 554 | | continue |
|---|
| 555 | | projecteggs.sort() |
|---|
| 556 | | adder_key = path_adder(projecteggs[-1]) |
|---|
| 557 | | try: |
|---|
| 558 | | modcode = code_getter(modulename) |
|---|
| 559 | | return modcode |
|---|
| 560 | | except ImportError, e: |
|---|
| 561 | | path_adder(adder_key, e) |
|---|
| 562 | | continue |
|---|
| 563 | | raise ImportError('No module named '+modulename) |
|---|
| 564 | | |
|---|
| 565 | | |
|---|
| 566 | | def module_enter(modulename, argv=None, ns=None, program_name=None): |
|---|
| 567 | | |
|---|
| 568 | | if type(modulename) == types.ModuleType: |
|---|
| 569 | | mod=modulename |
|---|
| 570 | | modulename=mod.__name__ |
|---|
| 571 | | else: |
|---|
| 572 | | ns=ns or globals() |
|---|
| 573 | | mod = __import__( |
|---|
| 574 | | modulename, ns, locals(), |
|---|
| 575 | | modulename.split('.')[-1]) |
|---|
| 576 | | if program_name is None: |
|---|
| 577 | | program_name = modulename |
|---|
| 578 | | log=logging.getLogger(modulename) |
|---|