buildframework/helium/external/python/lib/common/site.py
author wbernard
Wed, 23 Dec 2009 19:29:07 +0200
changeset 179 d8ac696cc51f
child 217 0f5e3a7fb6af
permissions -rw-r--r--
helium_7.0-r14027
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
179
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     1
# Duplicating setuptools' site.py...
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     2
def __boot():
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     3
    PYTHONPATH = []    
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     4
    if sys.platform=='win32':
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     5
        PYTHONPATH.append(os.path.join(sys.prefix, 'lib'))
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     6
    if not (os.environ.get('PYTHONPATH') is None or (sys.platform=='win32' and not os.environ.get('PYTHONPATH'))):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     7
        PYTHONPATH.extend(os.environ.get('PYTHONPATH').split(os.pathsep))
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     8
    pic = getattr(sys,'path_importer_cache',{})
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     9
    stdpath = sys.path[len(PYTHONPATH):]
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    10
    mydir = os.path.dirname(__file__)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    11
    known_paths = dict([(makepath(item)[1],1) for item in sys.path]) # 2.2 comp
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    12
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    13
    oldpos = getattr(sys,'__egginsert',0)   # save old insertion position
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    14
    sys.__egginsert = 0                     # and reset the current one
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    15
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    16
    for item in PYTHONPATH:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    17
        addsitedir(item)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    18
        item_site_packages = os.path.join(item, 'site-packages')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    19
        if os.path.exists(item_site_packages):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    20
            addsitedir(item_site_packages)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    21
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    22
    sys.__egginsert += oldpos           # restore effective old position
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    23
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    24
    d,nd = makepath(stdpath[0])
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    25
    insert_at = None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    26
    new_path = []
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    27
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    28
    for item in sys.path:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    29
        p,np = makepath(item)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    30
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    31
        if np==nd and insert_at is None:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    32
            # We've hit the first 'system' path entry, so added entries go here
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    33
            insert_at = len(new_path)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    34
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    35
        if np in known_paths or insert_at is None:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    36
            new_path.append(item)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    37
        else:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    38
            # new path after the insert point, back-insert it
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    39
            new_path.insert(insert_at, item)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    40
            insert_at += 1
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    41
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    42
    sys.path[:] = new_path
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    43
    
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    44
import sys
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    45
import os
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    46
import __builtin__
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    47
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    48
def makepath(*paths):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    49
    dir = os.path.abspath(os.path.join(*paths))
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    50
    return dir, os.path.normcase(dir)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    51
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    52
def abs__file__():
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    53
    """Set all module' __file__ attribute to an absolute path"""
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    54
    for m in sys.modules.values():
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    55
        try:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    56
            m.__file__ = os.path.abspath(m.__file__)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    57
        except AttributeError:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    58
            continue
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    59
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    60
try:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    61
    set
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    62
except NameError:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    63
    class set:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    64
        def __init__(self, args=()):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    65
            self.d = {}
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    66
            for v in args:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    67
                self.d[v] = None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    68
        def __contains__(self, key):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    69
            return key in self.d
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    70
        def add(self, key):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    71
            self.d[key] = None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    72
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    73
def removeduppaths():
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    74
    """ Remove duplicate entries from sys.path along with making them
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    75
    absolute"""
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    76
    # This ensures that the initial path provided by the interpreter contains
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    77
    # only absolute pathnames, even if we're running from the build directory.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    78
    L = []
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    79
    known_paths = set()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    80
    for dir in sys.path:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    81
        # Filter out duplicate paths (on case-insensitive file systems also
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    82
        # if they only differ in case); turn relative paths into absolute
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    83
        # paths.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    84
        dir, dircase = makepath(dir)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    85
        if not dircase in known_paths:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    86
            L.append(dir)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    87
            known_paths.add(dircase)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    88
    sys.path[:] = L
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    89
    return known_paths
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    90
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    91
def _init_pathinfo():
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    92
    """Return a set containing all existing directory entries from sys.path"""
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    93
    d = set()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    94
    for dir in sys.path:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    95
        try:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    96
            if os.path.isdir(dir):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    97
                dir, dircase = makepath(dir)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    98
                d.add(dircase)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    99
        except TypeError:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   100
            continue
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   101
    return d
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   102
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   103
def addpackage(sitedir, name, known_paths, exclude_packages=()):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   104
    """Add a new path to known_paths by combining sitedir and 'name' or execute
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   105
    sitedir if it starts with 'import'"""
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   106
    import fnmatch
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   107
    if known_paths is None:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   108
        _init_pathinfo()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   109
        reset = 1
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   110
    else:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   111
        reset = 0
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   112
    fullname = os.path.join(sitedir, name)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   113
    try:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   114
        f = open(fullname, "rU")
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   115
    except IOError:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   116
        return
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   117
    try:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   118
        for line in f:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   119
            if line.startswith("#"):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   120
                continue
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   121
            found_exclude = False
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   122
            for exclude in exclude_packages:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   123
                if exclude(line):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   124
                    found_exclude = True
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   125
                    break
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   126
            if found_exclude:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   127
                continue
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   128
            if line.startswith("import"):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   129
                exec line
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   130
                continue
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   131
            line = line.rstrip()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   132
            dir, dircase = makepath(sitedir, line)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   133
            if not dircase in known_paths and os.path.exists(dir):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   134
                sys.path.append(dir)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   135
                known_paths.add(dircase)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   136
    finally:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   137
        f.close()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   138
    if reset:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   139
        known_paths = None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   140
    return known_paths
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   141
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   142
def addsitedir(sitedir, known_paths=None, exclude_packages=()):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   143
    """Add 'sitedir' argument to sys.path if missing and handle .pth files in
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   144
    'sitedir'"""
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   145
    if known_paths is None:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   146
        known_paths = _init_pathinfo()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   147
        reset = 1
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   148
    else:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   149
        reset = 0
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   150
    sitedir, sitedircase = makepath(sitedir)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   151
    if not sitedircase in known_paths:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   152
        sys.path.append(sitedir)        # Add path component
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   153
    try:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   154
        names = os.listdir(sitedir)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   155
    except os.error:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   156
        return
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   157
    names.sort()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   158
    for name in names:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   159
        if name.endswith(os.extsep + "pth"):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   160
            addpackage(sitedir, name, known_paths,
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   161
                       exclude_packages=exclude_packages)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   162
    if reset:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   163
        known_paths = None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   164
    return known_paths
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   165
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   166
def addsitepackages(known_paths):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   167
    """Add site-packages (and possibly site-python) to sys.path"""
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   168
    prefixes = [os.path.join(sys.prefix, "local"), sys.prefix]
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   169
    if sys.exec_prefix != sys.prefix:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   170
        prefixes.append(os.path.join(sys.exec_prefix, "local"))
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   171
    for prefix in prefixes:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   172
        if prefix:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   173
            if sys.platform in ('os2emx', 'riscos'):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   174
                sitedirs = [os.path.join(prefix, "Lib", "site-packages")]
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   175
            elif os.sep == '/':
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   176
                sitedirs = [os.path.join(prefix,
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   177
                                         "lib",
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   178
                                         "python" + sys.version[:3],
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   179
                                         "site-packages"),
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   180
                            os.path.join(prefix, "lib", "site-python")]
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   181
                try:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   182
                    # sys.getobjects only available in --with-pydebug build
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   183
                    # pylint: disable-msg=E1101
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   184
                    sys.getobjects
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   185
                    sitedirs.insert(0, os.path.join(sitedirs[0], 'debug'))
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   186
                except AttributeError:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   187
                    pass
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   188
            else:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   189
                sitedirs = [prefix, os.path.join(prefix, "lib", "site-packages")]
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   190
            if sys.platform == 'darwin':
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   191
                sitedirs.append( os.path.join('/opt/local', 'lib', 'python' + sys.version[:3], 'site-packages') )
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   192
                # for framework builds *only* we add the standard Apple
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   193
                # locations. Currently only per-user, but /Library and
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   194
                # /Network/Library could be added too
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   195
                if 'Python.framework' in prefix:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   196
                    home = os.environ.get('HOME')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   197
                    if home:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   198
                        sitedirs.append(
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   199
                            os.path.join(home,
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   200
                                         'Library',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   201
                                         'Python',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   202
                                         sys.version[:3],
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   203
                                         'site-packages'))
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   204
            for sitedir in sitedirs:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   205
                if os.path.isdir(sitedir):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   206
                    addsitedir(sitedir, known_paths,
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   207
                               exclude_packages=[lambda line: 'setuptools' in line])
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   208
    return None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   209
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   210
def setquit():
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   211
    """Define new built-ins 'quit' and 'exit'.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   212
    These are simply strings that display a hint on how to exit.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   213
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   214
    """
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   215
    if os.sep == ':':
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   216
        exit = 'Use Cmd-Q to quit.'
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   217
    elif os.sep == '\\':
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   218
        exit = 'Use Ctrl-Z plus Return to exit.'
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   219
    else:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   220
        exit = 'Use Ctrl-D (i.e. EOF) to exit.'
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   221
    __builtin__.quit = __builtin__.exit = exit
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   222
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   223
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   224
class _Printer(object):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   225
    """interactive prompt objects for printing the license text, a list of
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   226
    contributors and the copyright notice."""
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   227
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   228
    MAXLINES = 23
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   229
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   230
    def __init__(self, name, data, files=(), dirs=()):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   231
        self.__name = name
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   232
        self.__data = data
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   233
        self.__files = files
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   234
        self.__dirs = dirs
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   235
        self.__lines = None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   236
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   237
    def __setup(self):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   238
        if self.__lines:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   239
            return
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   240
        data = None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   241
        for dir in self.__dirs:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   242
            for filename in self.__files:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   243
                filename = os.path.join(dir, filename)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   244
                try:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   245
                    fp = file(filename, "rU")
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   246
                    data = fp.read()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   247
                    fp.close()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   248
                    break
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   249
                except IOError:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   250
                    pass
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   251
            if data:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   252
                break
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   253
        if not data:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   254
            data = self.__data
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   255
        self.__lines = data.split('\n')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   256
        self.__linecnt = len(self.__lines)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   257
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   258
    def __repr__(self):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   259
        self.__setup()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   260
        if len(self.__lines) <= self.MAXLINES:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   261
            return "\n".join(self.__lines)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   262
        else:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   263
            return "Type %s() to see the full %s text" % ((self.__name,)*2)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   264
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   265
    def __call__(self):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   266
        self.__setup()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   267
        prompt = 'Hit Return for more, or q (and Return) to quit: '
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   268
        lineno = 0
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   269
        while 1:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   270
            try:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   271
                for i in range(lineno, lineno + self.MAXLINES):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   272
                    print self.__lines[i]
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   273
            except IndexError:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   274
                break
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   275
            else:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   276
                lineno += self.MAXLINES
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   277
                key = None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   278
                while key is None:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   279
                    key = raw_input(prompt)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   280
                    if key not in ('', 'q'):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   281
                        key = None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   282
                if key == 'q':
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   283
                    break
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   284
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   285
def setcopyright():
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   286
    """Set 'copyright' and 'credits' in __builtin__"""
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   287
    __builtin__.copyright = _Printer("copyright", sys.copyright)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   288
    if sys.platform[:4] == 'java':
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   289
        __builtin__.credits = _Printer(
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   290
            "credits",
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   291
            "Jython is maintained by the Jython developers (www.jython.org).")
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   292
    else:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   293
        __builtin__.credits = _Printer("credits", """\
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   294
    Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   295
    for supporting Python development.  See www.python.org for more information.""")
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   296
    here = os.path.dirname(os.__file__)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   297
    __builtin__.license = _Printer(
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   298
        "license", "See http://www.python.org/%.3s/license.html" % sys.version,
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   299
        ["LICENSE.txt", "LICENSE"],
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   300
        [os.path.join(here, os.pardir), here, os.curdir])
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   301
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   302
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   303
class _Helper(object):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   304
    """Define the built-in 'help'.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   305
    This is a wrapper around pydoc.help (with a twist).
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   306
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   307
    """
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   308
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   309
    def __repr__(self):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   310
        return "Type help() for interactive help, " \
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   311
               "or help(object) for help about object."
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   312
    def __call__(self, *args, **kwds):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   313
        import pydoc
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   314
        return pydoc.help(*args, **kwds)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   315
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   316
def sethelper():
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   317
    __builtin__.help = _Helper()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   318
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   319
def aliasmbcs():
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   320
    """On Windows, some default encodings are not provided by Python,
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   321
    while they are always available as "mbcs" in each locale. Make
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   322
    them usable by aliasing to "mbcs" in such a case."""
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   323
    if sys.platform == 'win32':
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   324
        import locale, codecs
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   325
        enc = locale.getdefaultlocale()[1]
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   326
        if enc.startswith('cp'):            # "cp***" ?
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   327
            try:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   328
                codecs.lookup(enc)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   329
            except LookupError:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   330
                import encodings
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   331
                encodings._cache[enc] = encodings._unknown
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   332
                encodings.aliases.aliases[enc] = 'mbcs'
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   333
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   334
def setencoding():
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   335
    """Set the string encoding used by the Unicode implementation.  The
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   336
    default is 'ascii', but if you're willing to experiment, you can
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   337
    change this."""
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   338
    encoding = "ascii" # Default value set by _PyUnicode_Init()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   339
    if 0:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   340
        # Enable to support locale aware default string encodings.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   341
        import locale
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   342
        loc = locale.getdefaultlocale()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   343
        if loc[1]:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   344
            encoding = loc[1]
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   345
    if 0:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   346
        # Enable to switch off string to Unicode coercion and implicit
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   347
        # Unicode to string conversion.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   348
        encoding = "undefined"
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   349
    if encoding != "ascii":
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   350
        # On Non-Unicode builds this will raise an AttributeError...
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   351
        sys.setdefaultencoding(encoding) # Needs Python Unicode build !
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   352
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   353
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   354
def execsitecustomize():
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   355
    """Run custom site specific code, if available."""
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   356
    try:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   357
        import sitecustomize
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   358
    except ImportError:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   359
        pass
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   360
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   361
def fixup_setuptools():
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   362
    """Make sure our setuptools monkeypatch is in place"""
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   363
    for i in range(len(sys.path)):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   364
        if sys.path[i].find('setuptools') != -1:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   365
            path = sys.path[i]
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   366
            del sys.path[i]
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   367
            sys.path.append(path)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   368
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   369
def main():
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   370
    abs__file__()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   371
    paths_in_sys = removeduppaths()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   372
    if include_site_packages:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   373
        paths_in_sys = addsitepackages(paths_in_sys)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   374
    setquit()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   375
    setcopyright()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   376
    sethelper()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   377
    aliasmbcs()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   378
    setencoding()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   379
    execsitecustomize()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   380
    # Remove sys.setdefaultencoding() so that users cannot change the
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   381
    # encoding after initialization.  The test for presence is needed when
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   382
    # this module is run as a script, because this code is executed twice.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   383
    if hasattr(sys, "setdefaultencoding"):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   384
        del sys.setdefaultencoding
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   385
    __boot()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   386
    fixup_setuptools()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   387
    
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   388
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   389
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   390
include_site_packages = False
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   391
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   392
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   393
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   394
main()