SEESenv/lib/python2.6/site.py
author amit@thunder
Mon, 12 Apr 2010 15:12:41 +0530
changeset 49 3b5f1341d6c6
parent 3 6cee07c589cb
permissions -rw-r--r--
Some small changes ... bug fixes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
     1
"""Append module search paths for third-party packages to sys.path.
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
     2
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
     3
****************************************************************
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
     4
* This module is automatically imported during initialization. *
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
     5
****************************************************************
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
     6
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
     7
In earlier versions of Python (up to 1.5a3), scripts or modules that
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
     8
needed to use site-specific modules would place ``import site''
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
     9
somewhere near the top of their code.  Because of the automatic
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    10
import, this is no longer necessary (but code that does it still
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    11
works).
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    12
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    13
This will append site-specific paths to the module search path.  On
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    14
Unix, it starts with sys.prefix and sys.exec_prefix (if different) and
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    15
appends lib/python<version>/site-packages as well as lib/site-python.
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    16
It also supports the Debian convention of
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    17
lib/python<version>/dist-packages.  On other platforms (mainly Mac and
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    18
Windows), it uses just sys.prefix (and sys.exec_prefix, if different,
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    19
but this is unlikely).  The resulting directories, if they exist, are
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    20
appended to sys.path, and also inspected for path configuration files.
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    21
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    22
FOR DEBIAN, this sys.path is augmented with directories in /usr/local.
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    23
Local addons go into /usr/local/lib/python<version>/site-packages
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    24
(resp. /usr/local/lib/site-python), Debian addons install into
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    25
/usr/{lib,share}/python<version>/dist-packages.
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    26
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    27
A path configuration file is a file whose name has the form
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    28
<package>.pth; its contents are additional directories (one per line)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    29
to be added to sys.path.  Non-existing directories (or
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    30
non-directories) are never added to sys.path; no directory is added to
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    31
sys.path more than once.  Blank lines and lines beginning with
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    32
'#' are skipped. Lines starting with 'import' are executed.
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    33
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    34
For example, suppose sys.prefix and sys.exec_prefix are set to
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    35
/usr/local and there is a directory /usr/local/lib/python2.X/site-packages
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    36
with three subdirectories, foo, bar and spam, and two path
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    37
configuration files, foo.pth and bar.pth.  Assume foo.pth contains the
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    38
following:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    39
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    40
  # foo package configuration
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    41
  foo
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    42
  bar
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    43
  bletch
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    44
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    45
and bar.pth contains:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    46
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    47
  # bar package configuration
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    48
  bar
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    49
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    50
Then the following directories are added to sys.path, in this order:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    51
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    52
  /usr/local/lib/python2.X/site-packages/bar
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    53
  /usr/local/lib/python2.X/site-packages/foo
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    54
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    55
Note that bletch is omitted because it doesn't exist; bar precedes foo
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    56
because bar.pth comes alphabetically before foo.pth; and spam is
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    57
omitted because it is not mentioned in either path configuration file.
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    58
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    59
After these path manipulations, an attempt is made to import a module
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    60
named sitecustomize, which can perform arbitrary additional
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    61
site-specific customizations.  If this import fails with an
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    62
ImportError exception, it is silently ignored.
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    63
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    64
"""
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    65
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    66
import sys
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    67
import os
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    68
import __builtin__
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    69
try:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    70
    set
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    71
except NameError:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    72
    from sets import Set as set
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    73
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    74
# Prefixes for site-packages; add additional prefixes like /usr/local here
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    75
PREFIXES = [sys.prefix, sys.exec_prefix]
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    76
# Enable per user site-packages directory
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    77
# set it to False to disable the feature or True to force the feature
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    78
ENABLE_USER_SITE = None
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    79
# for distutils.commands.install
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    80
USER_SITE = None
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    81
USER_BASE = None
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    82
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    83
_is_jython = sys.platform[:4] == 'java'
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    84
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    85
def makepath(*paths):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    86
    dir = os.path.join(*paths)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    87
    if _is_jython and (dir == '__classpath__' or
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    88
                       dir.startswith('__pyclasspath__')):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    89
        return dir, dir
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    90
    dir = os.path.abspath(dir)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    91
    return dir, os.path.normcase(dir)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    92
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    93
def abs__file__():
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    94
    """Set all module' __file__ attribute to an absolute path"""
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    95
    for m in sys.modules.values():
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    96
        f = getattr(m, '__file__', None)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    97
        if f is None:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    98
            continue
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
    99
        m.__file__ = os.path.abspath(f)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   100
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   101
def removeduppaths():
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   102
    """ Remove duplicate entries from sys.path along with making them
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   103
    absolute"""
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   104
    # This ensures that the initial path provided by the interpreter contains
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   105
    # only absolute pathnames, even if we're running from the build directory.
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   106
    L = []
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   107
    known_paths = set()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   108
    for dir in sys.path:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   109
        # Filter out duplicate paths (on case-insensitive file systems also
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   110
        # if they only differ in case); turn relative paths into absolute
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   111
        # paths.
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   112
        dir, dircase = makepath(dir)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   113
        if not dircase in known_paths:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   114
            L.append(dir)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   115
            known_paths.add(dircase)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   116
    sys.path[:] = L
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   117
    return known_paths
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   118
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   119
# XXX This should not be part of site.py, since it is needed even when
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   120
# using the -S option for Python.  See http://www.python.org/sf/586680
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   121
def addbuilddir():
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   122
    """Append ./build/lib.<platform> in case we're running in the build dir
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   123
    (especially for Guido :-)"""
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   124
    from distutils.util import get_platform
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   125
    s = "build/lib.%s-%.3s" % (get_platform(), sys.version)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   126
    if hasattr(sys, 'gettotalrefcount'):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   127
        s += '-pydebug'
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   128
    s = os.path.join(os.path.dirname(sys.path[-1]), s)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   129
    sys.path.append(s)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   130
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   131
def _init_pathinfo():
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   132
    """Return a set containing all existing directory entries from sys.path"""
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   133
    d = set()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   134
    for dir in sys.path:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   135
        try:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   136
            if os.path.isdir(dir):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   137
                dir, dircase = makepath(dir)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   138
                d.add(dircase)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   139
        except TypeError:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   140
            continue
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   141
    return d
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   142
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   143
def addpackage(sitedir, name, known_paths):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   144
    """Add a new path to known_paths by combining sitedir and 'name' or execute
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   145
    sitedir if it starts with 'import'"""
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   146
    if known_paths is None:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   147
        _init_pathinfo()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   148
        reset = 1
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   149
    else:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   150
        reset = 0
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   151
    fullname = os.path.join(sitedir, name)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   152
    try:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   153
        f = open(fullname, "rU")
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   154
    except IOError:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   155
        return
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   156
    try:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   157
        for line in f:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   158
            if line.startswith("#"):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   159
                continue
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   160
            if line.startswith("import"):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   161
                exec line
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   162
                continue
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   163
            line = line.rstrip()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   164
            dir, dircase = makepath(sitedir, line)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   165
            if not dircase in known_paths and os.path.exists(dir):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   166
                sys.path.append(dir)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   167
                known_paths.add(dircase)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   168
    finally:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   169
        f.close()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   170
    if reset:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   171
        known_paths = None
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   172
    return known_paths
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   173
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   174
def addsitedir(sitedir, known_paths=None):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   175
    """Add 'sitedir' argument to sys.path if missing and handle .pth files in
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   176
    'sitedir'"""
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   177
    if known_paths is None:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   178
        known_paths = _init_pathinfo()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   179
        reset = 1
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   180
    else:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   181
        reset = 0
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   182
    sitedir, sitedircase = makepath(sitedir)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   183
    if not sitedircase in known_paths:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   184
        sys.path.append(sitedir)        # Add path component
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   185
    try:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   186
        names = os.listdir(sitedir)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   187
    except os.error:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   188
        return
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   189
    names.sort()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   190
    for name in names:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   191
        if name.endswith(os.extsep + "pth"):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   192
            addpackage(sitedir, name, known_paths)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   193
    if reset:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   194
        known_paths = None
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   195
    return known_paths
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   196
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   197
def addsitepackages(known_paths, sys_prefix=sys.prefix, exec_prefix=sys.exec_prefix):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   198
    """Add site-packages (and possibly site-python) to sys.path"""
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   199
    prefixes = [os.path.join(sys_prefix, "local"), sys_prefix]
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   200
    if exec_prefix != sys_prefix:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   201
        prefixes.append(os.path.join(exec_prefix, "local"))
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   202
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   203
    for prefix in prefixes:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   204
        if prefix:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   205
            if sys.platform in ('os2emx', 'riscos') or _is_jython:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   206
                sitedirs = [os.path.join(prefix, "Lib", "site-packages")]
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   207
            elif sys.platform == 'darwin' and prefix == sys_prefix:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   208
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   209
                if prefix.startswith("/System/Library/Frameworks/"): # Apple's Python
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   210
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   211
                    sitedirs = [os.path.join("/Library/Python", sys.version[:3], "site-packages"),
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   212
                                os.path.join(prefix, "Extras", "lib", "python")]
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   213
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   214
                else: # any other Python distros on OSX work this way
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   215
                    sitedirs = [os.path.join(prefix, "lib",
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   216
                                             "python" + sys.version[:3], "site-packages")]
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   217
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   218
            elif os.sep == '/':
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   219
                sitedirs = [os.path.join(prefix,
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   220
                                         "lib",
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   221
                                         "python" + sys.version[:3],
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   222
                                         "site-packages"),
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   223
                            os.path.join(prefix, "lib", "site-python"),
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   224
                            os.path.join(prefix, "python" + sys.version[:3], "lib-dynload")]
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   225
                lib64_dir = os.path.join(prefix, "lib64", "python" + sys.version[:3], "site-packages")
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   226
                if (os.path.exists(lib64_dir) and 
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   227
                    os.path.realpath(lib64_dir) not in [os.path.realpath(p) for p in sitedirs]):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   228
                    sitedirs.append(lib64_dir)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   229
                try:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   230
                    # sys.getobjects only available in --with-pydebug build
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   231
                    sys.getobjects
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   232
                    sitedirs.insert(0, os.path.join(sitedirs[0], 'debug'))
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   233
                except AttributeError:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   234
                    pass
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   235
                # Debian-specific dist-packages directories:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   236
                sitedirs.append(os.path.join(prefix, "lib",
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   237
                                             "python" + sys.version[:3],
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   238
                                             "dist-packages"))
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   239
                sitedirs.append(os.path.join(prefix, "local/lib",
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   240
                                             "python" + sys.version[:3],
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   241
                                             "dist-packages"))
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   242
                sitedirs.append(os.path.join(prefix, "lib", "dist-python"))
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   243
            else:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   244
                sitedirs = [prefix, os.path.join(prefix, "lib", "site-packages")]
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   245
            if sys.platform == 'darwin':
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   246
                # for framework builds *only* we add the standard Apple
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   247
                # locations. Currently only per-user, but /Library and
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   248
                # /Network/Library could be added too
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   249
                if 'Python.framework' in prefix:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   250
                    home = os.environ.get('HOME')
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   251
                    if home:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   252
                        sitedirs.append(
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   253
                            os.path.join(home,
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   254
                                         'Library',
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   255
                                         'Python',
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   256
                                         sys.version[:3],
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   257
                                         'site-packages'))
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   258
            for sitedir in sitedirs:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   259
                if os.path.isdir(sitedir):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   260
                    addsitedir(sitedir, known_paths)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   261
    return None
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   262
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   263
def check_enableusersite():
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   264
    """Check if user site directory is safe for inclusion
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   265
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   266
    The function tests for the command line flag (including environment var),
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   267
    process uid/gid equal to effective uid/gid.
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   268
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   269
    None: Disabled for security reasons
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   270
    False: Disabled by user (command line option)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   271
    True: Safe and enabled
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   272
    """
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   273
    if hasattr(sys, 'flags') and getattr(sys.flags, 'no_user_site', False):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   274
        return False
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   275
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   276
    if hasattr(os, "getuid") and hasattr(os, "geteuid"):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   277
        # check process uid == effective uid
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   278
        if os.geteuid() != os.getuid():
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   279
            return None
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   280
    if hasattr(os, "getgid") and hasattr(os, "getegid"):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   281
        # check process gid == effective gid
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   282
        if os.getegid() != os.getgid():
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   283
            return None
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   284
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   285
    return True
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   286
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   287
def addusersitepackages(known_paths):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   288
    """Add a per user site-package to sys.path
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   289
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   290
    Each user has its own python directory with site-packages in the
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   291
    home directory.
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   292
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   293
    USER_BASE is the root directory for all Python versions
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   294
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   295
    USER_SITE is the user specific site-packages directory
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   296
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   297
    USER_SITE/.. can be used for data.
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   298
    """
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   299
    global USER_BASE, USER_SITE, ENABLE_USER_SITE
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   300
    env_base = os.environ.get("PYTHONUSERBASE", None)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   301
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   302
    def joinuser(*args):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   303
        return os.path.expanduser(os.path.join(*args))
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   304
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   305
    #if sys.platform in ('os2emx', 'riscos'):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   306
    #    # Don't know what to put here
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   307
    #    USER_BASE = ''
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   308
    #    USER_SITE = ''
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   309
    if os.name == "nt":
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   310
        base = os.environ.get("APPDATA") or "~"
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   311
        if env_base:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   312
            USER_BASE = env_base
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   313
        else:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   314
            USER_BASE = joinuser(base, "Python")
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   315
        USER_SITE = os.path.join(USER_BASE,
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   316
                                 "Python" + sys.version[0] + sys.version[2],
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   317
                                 "site-packages")
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   318
    else:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   319
        if env_base:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   320
            USER_BASE = env_base
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   321
        else:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   322
            USER_BASE = joinuser("~", ".local")
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   323
        USER_SITE = os.path.join(USER_BASE, "lib",
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   324
                                 "python" + sys.version[:3],
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   325
                                 "site-packages")
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   326
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   327
    if ENABLE_USER_SITE and os.path.isdir(USER_SITE):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   328
        addsitedir(USER_SITE, known_paths)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   329
    if ENABLE_USER_SITE:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   330
        for dist_libdir in ("lib", "local/lib"):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   331
            user_site = os.path.join(USER_BASE, dist_libdir,
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   332
                                     "python" + sys.version[:3],
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   333
                                     "dist-packages")
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   334
            if os.path.isdir(user_site):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   335
                addsitedir(user_site, known_paths)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   336
    return known_paths
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   337
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   338
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   339
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   340
def setBEGINLIBPATH():
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   341
    """The OS/2 EMX port has optional extension modules that do double duty
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   342
    as DLLs (and must use the .DLL file extension) for other extensions.
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   343
    The library search path needs to be amended so these will be found
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   344
    during module import.  Use BEGINLIBPATH so that these are at the start
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   345
    of the library search path.
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   346
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   347
    """
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   348
    dllpath = os.path.join(sys.prefix, "Lib", "lib-dynload")
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   349
    libpath = os.environ['BEGINLIBPATH'].split(';')
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   350
    if libpath[-1]:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   351
        libpath.append(dllpath)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   352
    else:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   353
        libpath[-1] = dllpath
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   354
    os.environ['BEGINLIBPATH'] = ';'.join(libpath)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   355
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   356
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   357
def setquit():
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   358
    """Define new built-ins 'quit' and 'exit'.
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   359
    These are simply strings that display a hint on how to exit.
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   360
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   361
    """
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   362
    if os.sep == ':':
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   363
        eof = 'Cmd-Q'
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   364
    elif os.sep == '\\':
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   365
        eof = 'Ctrl-Z plus Return'
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   366
    else:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   367
        eof = 'Ctrl-D (i.e. EOF)'
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   368
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   369
    class Quitter(object):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   370
        def __init__(self, name):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   371
            self.name = name
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   372
        def __repr__(self):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   373
            return 'Use %s() or %s to exit' % (self.name, eof)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   374
        def __call__(self, code=None):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   375
            # Shells like IDLE catch the SystemExit, but listen when their
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   376
            # stdin wrapper is closed.
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   377
            try:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   378
                sys.stdin.close()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   379
            except:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   380
                pass
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   381
            raise SystemExit(code)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   382
    __builtin__.quit = Quitter('quit')
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   383
    __builtin__.exit = Quitter('exit')
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   384
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   385
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   386
class _Printer(object):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   387
    """interactive prompt objects for printing the license text, a list of
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   388
    contributors and the copyright notice."""
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   389
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   390
    MAXLINES = 23
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   391
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   392
    def __init__(self, name, data, files=(), dirs=()):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   393
        self.__name = name
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   394
        self.__data = data
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   395
        self.__files = files
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   396
        self.__dirs = dirs
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   397
        self.__lines = None
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   398
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   399
    def __setup(self):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   400
        if self.__lines:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   401
            return
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   402
        data = None
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   403
        for dir in self.__dirs:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   404
            for filename in self.__files:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   405
                filename = os.path.join(dir, filename)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   406
                try:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   407
                    fp = file(filename, "rU")
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   408
                    data = fp.read()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   409
                    fp.close()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   410
                    break
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   411
                except IOError:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   412
                    pass
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   413
            if data:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   414
                break
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   415
        if not data:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   416
            data = self.__data
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   417
        self.__lines = data.split('\n')
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   418
        self.__linecnt = len(self.__lines)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   419
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   420
    def __repr__(self):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   421
        self.__setup()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   422
        if len(self.__lines) <= self.MAXLINES:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   423
            return "\n".join(self.__lines)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   424
        else:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   425
            return "Type %s() to see the full %s text" % ((self.__name,)*2)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   426
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   427
    def __call__(self):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   428
        self.__setup()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   429
        prompt = 'Hit Return for more, or q (and Return) to quit: '
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   430
        lineno = 0
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   431
        while 1:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   432
            try:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   433
                for i in range(lineno, lineno + self.MAXLINES):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   434
                    print self.__lines[i]
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   435
            except IndexError:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   436
                break
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   437
            else:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   438
                lineno += self.MAXLINES
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   439
                key = None
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   440
                while key is None:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   441
                    key = raw_input(prompt)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   442
                    if key not in ('', 'q'):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   443
                        key = None
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   444
                if key == 'q':
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   445
                    break
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   446
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   447
def setcopyright():
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   448
    """Set 'copyright' and 'credits' in __builtin__"""
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   449
    __builtin__.copyright = _Printer("copyright", sys.copyright)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   450
    if _is_jython:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   451
        __builtin__.credits = _Printer(
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   452
            "credits",
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   453
            "Jython is maintained by the Jython developers (www.jython.org).")
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   454
    else:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   455
        __builtin__.credits = _Printer("credits", """\
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   456
    Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   457
    for supporting Python development.  See www.python.org for more information.""")
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   458
    here = os.path.dirname(os.__file__)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   459
    __builtin__.license = _Printer(
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   460
        "license", "See http://www.python.org/%.3s/license.html" % sys.version,
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   461
        ["LICENSE.txt", "LICENSE"],
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   462
        [os.path.join(here, os.pardir), here, os.curdir])
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   463
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   464
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   465
class _Helper(object):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   466
    """Define the built-in 'help'.
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   467
    This is a wrapper around pydoc.help (with a twist).
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   468
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   469
    """
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   470
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   471
    def __repr__(self):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   472
        return "Type help() for interactive help, " \
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   473
               "or help(object) for help about object."
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   474
    def __call__(self, *args, **kwds):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   475
        import pydoc
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   476
        return pydoc.help(*args, **kwds)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   477
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   478
def sethelper():
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   479
    __builtin__.help = _Helper()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   480
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   481
def aliasmbcs():
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   482
    """On Windows, some default encodings are not provided by Python,
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   483
    while they are always available as "mbcs" in each locale. Make
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   484
    them usable by aliasing to "mbcs" in such a case."""
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   485
    if sys.platform == 'win32':
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   486
        import locale, codecs
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   487
        enc = locale.getdefaultlocale()[1]
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   488
        if enc.startswith('cp'):            # "cp***" ?
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   489
            try:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   490
                codecs.lookup(enc)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   491
            except LookupError:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   492
                import encodings
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   493
                encodings._cache[enc] = encodings._unknown
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   494
                encodings.aliases.aliases[enc] = 'mbcs'
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   495
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   496
def setencoding():
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   497
    """Set the string encoding used by the Unicode implementation.  The
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   498
    default is 'ascii', but if you're willing to experiment, you can
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   499
    change this."""
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   500
    encoding = "ascii" # Default value set by _PyUnicode_Init()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   501
    if 0:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   502
        # Enable to support locale aware default string encodings.
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   503
        import locale
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   504
        loc = locale.getdefaultlocale()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   505
        if loc[1]:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   506
            encoding = loc[1]
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   507
    if 0:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   508
        # Enable to switch off string to Unicode coercion and implicit
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   509
        # Unicode to string conversion.
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   510
        encoding = "undefined"
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   511
    if encoding != "ascii":
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   512
        # On Non-Unicode builds this will raise an AttributeError...
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   513
        sys.setdefaultencoding(encoding) # Needs Python Unicode build !
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   514
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   515
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   516
def execsitecustomize():
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   517
    """Run custom site specific code, if available."""
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   518
    try:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   519
        import sitecustomize
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   520
    except ImportError:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   521
        pass
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   522
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   523
def virtual_install_main_packages():
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   524
    f = open(os.path.join(os.path.dirname(__file__), 'orig-prefix.txt'))
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   525
    sys.real_prefix = f.read().strip()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   526
    f.close()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   527
    pos = 2
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   528
    if sys.path[0] == '':
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   529
        pos += 1
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   530
    if sys.platform == 'win32':
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   531
        paths = [os.path.join(sys.real_prefix, 'Lib'), os.path.join(sys.real_prefix, 'DLLs')]
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   532
    elif _is_jython:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   533
        paths = [os.path.join(sys.real_prefix, 'Lib')]
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   534
    else:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   535
        paths = [os.path.join(sys.real_prefix, 'lib', 'python'+sys.version[:3])]
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   536
        lib64_path = os.path.join(sys.real_prefix, 'lib64', 'python'+sys.version[:3])
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   537
        if os.path.exists(lib64_path):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   538
            paths.append(lib64_path)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   539
        # This is hardcoded in the Python executable, but relative to sys.prefix:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   540
        plat_path = os.path.join(sys.real_prefix, 'lib', 'python'+sys.version[:3],
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   541
                                 'plat-%s' % sys.platform)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   542
        if os.path.exists(plat_path):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   543
            paths.append(plat_path)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   544
    # This is hardcoded in the Python executable, but
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   545
    # relative to sys.prefix, so we have to fix up:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   546
    for path in list(paths):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   547
        tk_dir = os.path.join(path, 'lib-tk')
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   548
        if os.path.exists(tk_dir):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   549
            paths.append(tk_dir)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   550
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   551
    # These are hardcoded in the Apple's Python executable,
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   552
    # but relative to sys.prefix, so we have to fix them up:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   553
    if sys.platform == 'darwin':
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   554
        hardcoded_paths = [os.path.join(sys.real_prefix, 'lib', 'python'+sys.version[:3], module)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   555
                           for module in ('plat-darwin', 'plat-mac', 'plat-mac/lib-scriptpackages')]
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   556
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   557
        for path in hardcoded_paths:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   558
            if os.path.exists(path):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   559
                paths.append(path)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   560
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   561
    sys.path.extend(paths)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   562
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   563
def force_global_eggs_after_local_site_packages():
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   564
    """
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   565
    Force easy_installed eggs in the global environment to get placed
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   566
    in sys.path after all packages inside the virtualenv.  This
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   567
    maintains the "least surprise" result that packages in the
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   568
    virtualenv always mask global packages, never the other way
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   569
    around.
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   570
    
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   571
    """
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   572
    egginsert = getattr(sys, '__egginsert', 0)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   573
    for i, path in enumerate(sys.path):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   574
        if i > egginsert and path.startswith(sys.prefix):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   575
            egginsert = i
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   576
    sys.__egginsert = egginsert + 1
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   577
    
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   578
def virtual_addsitepackages(known_paths):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   579
    force_global_eggs_after_local_site_packages()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   580
    return addsitepackages(known_paths, sys_prefix=sys.real_prefix)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   581
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   582
def fixclasspath():
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   583
    """Adjust the special classpath sys.path entries for Jython. These
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   584
    entries should follow the base virtualenv lib directories.
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   585
    """
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   586
    paths = []
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   587
    classpaths = []
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   588
    for path in sys.path:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   589
        if path == '__classpath__' or path.startswith('__pyclasspath__'):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   590
            classpaths.append(path)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   591
        else:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   592
            paths.append(path)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   593
    sys.path = paths
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   594
    sys.path.extend(classpaths)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   595
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   596
def execusercustomize():
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   597
    """Run custom user specific code, if available."""
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   598
    try:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   599
        import usercustomize
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   600
    except ImportError:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   601
        pass
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   602
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   603
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   604
def main():
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   605
    global ENABLE_USER_SITE
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   606
    virtual_install_main_packages()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   607
    abs__file__()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   608
    paths_in_sys = removeduppaths()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   609
    if (os.name == "posix" and sys.path and
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   610
        os.path.basename(sys.path[-1]) == "Modules"):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   611
        addbuilddir()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   612
    if _is_jython:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   613
        fixclasspath()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   614
    GLOBAL_SITE_PACKAGES = not os.path.exists(os.path.join(os.path.dirname(__file__), 'no-global-site-packages.txt'))
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   615
    if not GLOBAL_SITE_PACKAGES:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   616
        ENABLE_USER_SITE = False
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   617
    if ENABLE_USER_SITE is None:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   618
        ENABLE_USER_SITE = check_enableusersite()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   619
    paths_in_sys = addsitepackages(paths_in_sys)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   620
    paths_in_sys = addusersitepackages(paths_in_sys)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   621
    if GLOBAL_SITE_PACKAGES:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   622
        paths_in_sys = virtual_addsitepackages(paths_in_sys)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   623
    if sys.platform == 'os2emx':
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   624
        setBEGINLIBPATH()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   625
    setquit()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   626
    setcopyright()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   627
    sethelper()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   628
    aliasmbcs()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   629
    setencoding()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   630
    execsitecustomize()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   631
    if ENABLE_USER_SITE:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   632
        execusercustomize()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   633
    # Remove sys.setdefaultencoding() so that users cannot change the
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   634
    # encoding after initialization.  The test for presence is needed when
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   635
    # this module is run as a script, because this code is executed twice.
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   636
    if hasattr(sys, "setdefaultencoding"):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   637
        del sys.setdefaultencoding
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   638
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   639
main()
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   640
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   641
def _script():
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   642
    help = """\
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   643
    %s [--user-base] [--user-site]
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   644
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   645
    Without arguments print some useful information
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   646
    With arguments print the value of USER_BASE and/or USER_SITE separated
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   647
    by '%s'.
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   648
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   649
    Exit codes with --user-base or --user-site:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   650
      0 - user site directory is enabled
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   651
      1 - user site directory is disabled by user
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   652
      2 - uses site directory is disabled by super user
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   653
          or for security reasons
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   654
     >2 - unknown error
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   655
    """
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   656
    args = sys.argv[1:]
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   657
    if not args:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   658
        print "sys.path = ["
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   659
        for dir in sys.path:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   660
            print "    %r," % (dir,)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   661
        print "]"
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   662
        def exists(path):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   663
            if os.path.isdir(path):
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   664
                return "exists"
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   665
            else:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   666
                return "doesn't exist"
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   667
        print "USER_BASE: %r (%s)" % (USER_BASE, exists(USER_BASE))
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   668
        print "USER_SITE: %r (%s)" % (USER_SITE, exists(USER_BASE))
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   669
        print "ENABLE_USER_SITE: %r" %  ENABLE_USER_SITE
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   670
        sys.exit(0)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   671
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   672
    buffer = []
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   673
    if '--user-base' in args:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   674
        buffer.append(USER_BASE)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   675
    if '--user-site' in args:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   676
        buffer.append(USER_SITE)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   677
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   678
    if buffer:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   679
        print os.pathsep.join(buffer)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   680
        if ENABLE_USER_SITE:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   681
            sys.exit(0)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   682
        elif ENABLE_USER_SITE is False:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   683
            sys.exit(1)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   684
        elif ENABLE_USER_SITE is None:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   685
            sys.exit(2)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   686
        else:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   687
            sys.exit(3)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   688
    else:
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   689
        import textwrap
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   690
        print textwrap.dedent(help % (sys.argv[0], os.pathsep))
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   691
        sys.exit(10)
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   692
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   693
if __name__ == '__main__':
6cee07c589cb Changes in path of some of the files ...
amit@thunder
parents:
diff changeset
   694
    _script()