• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1def __boot():
2    import sys
3    import os
4    PYTHONPATH = os.environ.get('PYTHONPATH')
5    if PYTHONPATH is None or (sys.platform == 'win32' and not PYTHONPATH):
6        PYTHONPATH = []
7    else:
8        PYTHONPATH = PYTHONPATH.split(os.pathsep)
9
10    pic = getattr(sys, 'path_importer_cache', {})
11    stdpath = sys.path[len(PYTHONPATH):]
12    mydir = os.path.dirname(__file__)
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                import imp  # Avoid import loop in Python >= 3.3
27                stream, path, descr = imp.find_module('site', [item])
28            except ImportError:
29                continue
30            if stream is None:
31                continue
32            try:
33                # This should actually reload the current module
34                imp.load_module('site', stream, path, descr)
35            finally:
36                stream.close()
37            break
38    else:
39        raise ImportError("Couldn't find the real 'site' module")
40
41    known_paths = dict([(makepath(item)[1], 1) for item in sys.path])  # 2.2 comp
42
43    oldpos = getattr(sys, '__egginsert', 0)  # save old insertion position
44    sys.__egginsert = 0  # and reset the current one
45
46    for item in PYTHONPATH:
47        addsitedir(item)
48
49    sys.__egginsert += oldpos  # restore effective old position
50
51    d, nd = makepath(stdpath[0])
52    insert_at = None
53    new_path = []
54
55    for item in sys.path:
56        p, np = makepath(item)
57
58        if np == nd and insert_at is None:
59            # We've hit the first 'system' path entry, so added entries go here
60            insert_at = len(new_path)
61
62        if np in known_paths or insert_at is None:
63            new_path.append(item)
64        else:
65            # new path after the insert point, back-insert it
66            new_path.insert(insert_at, item)
67            insert_at += 1
68
69    sys.path[:] = new_path
70
71
72if __name__ == 'site':
73    __boot()
74    del __boot
75