home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyos2bin.zip / Lib / site.py < prev    next >
Text File  |  1997-09-07  |  4KB  |  125 lines

  1. """Append module search paths for third-party packages to sys.path.
  2.  
  3. ****************************************************************
  4. * This module is automatically imported during initialization. *
  5. ****************************************************************
  6.  
  7. In earlier versions of Python (up to 1.5a3), scripts or modules that
  8. needed to use site-specific modules would place ``import site''
  9. somewhere near the top of their code.  Because of the automatic
  10. import, this is no longer necessary (but code that does it still
  11. works).
  12.  
  13. This will append site-specific paths to to the module search path.  It
  14. starts with sys.prefix and sys.exec_prefix (if different) and appends
  15. lib/python<version>/site-packages as well as lib/site-python.  The
  16. resulting directories, if they exist, are appended to sys.path, and
  17. also inspected for path configuration files.
  18.  
  19. A path configuration file is a file whose name has the form
  20. <package>.pth; its contents are additional directories (one per line)
  21. to be added to sys.path.  Non-existing directories (or
  22. non-directories) are never added to sys.path; no directory is added to
  23. sys.path more than once.  Blank lines and lines beginning with
  24. \code{#} are skipped.
  25.  
  26. For example, suppose sys.prefix and sys.exec_prefix are set to
  27. /usr/local and there is a directory /usr/local/python1.5/site-packages
  28. with three subdirectories, foo, bar and spam, and two path
  29. configuration files, foo.pth and bar.pth.  Assume foo.pth contains the
  30. following:
  31.  
  32.   # foo package configuration
  33.   foo
  34.   bar
  35.   bletch
  36.  
  37. and bar.pth contains:
  38.  
  39.   # bar package configuration
  40.   bar
  41.  
  42. Then the following directories are added to sys.path, in this order:
  43.  
  44.   /usr/local/lib/python1.5/site-packages/bar
  45.   /usr/local/lib/python1.5/site-packages/foo
  46.  
  47. Note that bletch is omitted because it doesn't exist; bar precedes foo
  48. because bar.pth comes alphabetically before foo.pth; and spam is
  49. omitted because it is not mentioned in either path configuration file.
  50.  
  51. After these path manipulations, an attempt is made to import a module
  52. named sitecustomize, which can perform arbitrary additional
  53. site-specific customizations.  If this import fails with an
  54. ImportError exception, it is silently ignored.
  55.  
  56. Note that for some non-Unix systems, sys.prefix and sys.exec_prefix
  57. are empty, and then the path manipulations are skipped; however the
  58. import of sitecustomize is still attempted.
  59.  
  60. """
  61.  
  62. import sys, os
  63.  
  64. def addsitedir(sitedir):
  65.     if sitedir not in sys.path:
  66.     sys.path.append(sitedir)    # Add path component
  67.     try:
  68.     names = os.listdir(sitedir)
  69.     except os.error:
  70.     return
  71.     names = map(os.path.normcase, names)
  72.     names.sort()
  73.     for name in names:
  74.     if name[-4:] == ".pth":
  75.         addpackage(sitedir, name)
  76.  
  77. def addpackage(sitedir, name):
  78.     fullname = os.path.join(sitedir, name)
  79.     try:
  80.     f = open(fullname)
  81.     except IOError:
  82.     return
  83.     while 1:
  84.     dir = f.readline()
  85.     if not dir:
  86.         break
  87.     if dir[0] == '#':
  88.         continue
  89.     if dir[-1] == '\n':
  90.         dir = dir[:-1]
  91.     dir = os.path.join(sitedir, dir)
  92.     if dir not in sys.path and os.path.exists(dir):
  93.         sys.path.append(dir)
  94.  
  95. prefixes = [sys.prefix]
  96. if sys.exec_prefix != sys.prefix:
  97.     prefixes.append(sys.exec_prefix)
  98. for prefix in prefixes:
  99.     if prefix:
  100.     if os.sep == '/':
  101.         sitedirs = [os.path.join(prefix,
  102.                      "lib",
  103.                      "python" + sys.version[:3],
  104.                      "site-packages"),
  105.             os.path.join(prefix, "lib", "site-python")]
  106.     else:
  107.         sitedirs = [prefix]
  108.     for sitedir in sitedirs:
  109.         if os.path.isdir(sitedir):
  110.         addsitedir(sitedir)
  111.  
  112. try:
  113.     import sitecustomize        # Run arbitrary site specific code
  114. except ImportError:
  115.     pass                # No site customization module
  116.  
  117. def _test():
  118.     print "sys.path = ["
  119.     for dir in sys.path:
  120.     print "    %s," % `dir`
  121.     print "]"
  122.  
  123. if __name__ == '__main__':
  124.     _test()
  125.