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