source: titan/mediathek/localhoster/lib/python2.7/site.py @ 40099

Last change on this file since 40099 was 40099, checked in by obi, 6 years ago

fix

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