Phillip J. Eby | 069159b | 2006-04-18 04:05:34 +0000 | [diff] [blame] | 1 | def __boot(): |
| 2 | import sys, imp, os, os.path |
| 3 | PYTHONPATH = os.environ.get('PYTHONPATH') |
| 4 | if PYTHONPATH is None or (sys.platform=='win32' and not PYTHONPATH): |
| 5 | PYTHONPATH = [] |
| 6 | else: |
| 7 | PYTHONPATH = PYTHONPATH.split(os.pathsep) |
| 8 | |
| 9 | pic = getattr(sys,'path_importer_cache',{}) |
| 10 | stdpath = sys.path[len(PYTHONPATH):] |
| 11 | mydir = os.path.dirname(__file__) |
| 12 | #print "searching",stdpath,sys.path |
| 13 | |
| 14 | for item in stdpath: |
| 15 | if item==mydir or not item: |
| 16 | continue # skip if current dir. on Windows, or my own directory |
| 17 | importer = pic.get(item) |
| 18 | if importer is not None: |
| 19 | loader = importer.find_module('site') |
| 20 | if loader is not None: |
| 21 | # This should actually reload the current module |
| 22 | loader.load_module('site') |
| 23 | break |
| 24 | else: |
| 25 | try: |
| 26 | stream, path, descr = imp.find_module('site',[item]) |
| 27 | except ImportError: |
| 28 | continue |
| 29 | if stream is None: |
| 30 | continue |
| 31 | try: |
| 32 | # This should actually reload the current module |
| 33 | imp.load_module('site',stream,path,descr) |
| 34 | finally: |
| 35 | stream.close() |
| 36 | break |
| 37 | else: |
| 38 | raise ImportError("Couldn't find the real 'site' module") |
| 39 | |
| 40 | #print "loaded", __file__ |
| 41 | |
| 42 | known_paths = dict([(makepath(item)[1],1) for item in sys.path]) # 2.2 comp |
| 43 | |
| 44 | oldpos = getattr(sys,'__egginsert',0) # save old insertion position |
| 45 | sys.__egginsert = 0 # and reset the current one |
| 46 | |
| 47 | for item in PYTHONPATH: |
| 48 | addsitedir(item) |
| 49 | |
| 50 | sys.__egginsert += oldpos # restore effective old position |
| 51 | |
| 52 | d,nd = makepath(stdpath[0]) |
| 53 | insert_at = None |
| 54 | new_path = [] |
| 55 | |
| 56 | for item in sys.path: |
| 57 | p,np = makepath(item) |
| 58 | |
| 59 | if np==nd and insert_at is None: |
| 60 | # We've hit the first 'system' path entry, so added entries go here |
| 61 | insert_at = len(new_path) |
| 62 | |
| 63 | if np in known_paths or insert_at is None: |
| 64 | new_path.append(item) |
| 65 | else: |
| 66 | # new path after the insert point, back-insert it |
| 67 | new_path.insert(insert_at, item) |
| 68 | insert_at += 1 |
| 69 | |
| 70 | sys.path[:] = new_path |
| 71 | |
| 72 | if __name__=='site': |
| 73 | __boot() |
| 74 | del __boot |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |