home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 December / CHIP_CD_2004-12.iso / bonus / oo / OOo_1.1.3_ru_RU_infra_WinIntel_install.exe / $PLUGINSDIR / f_0372 / python-core-2.2.2 / lib / distutils / sysconfig.py < prev    next >
Text File  |  2004-10-09  |  16KB  |  445 lines

  1. """Provide access to Python's configuration information.  The specific
  2. configuration variables available depend heavily on the platform and
  3. configuration.  The values may be retrieved using
  4. get_config_var(name), and the list of variables is available via
  5. get_config_vars().keys().  Additional convenience functions are also
  6. available.
  7.  
  8. Written by:   Fred L. Drake, Jr.
  9. Email:        <fdrake@acm.org>
  10. Initial date: 17-Dec-1998
  11. """
  12.  
  13. __revision__ = "$Id: sysconfig.py,v 1.44.6.2 2002/10/08 14:59:43 mwh Exp $"
  14.  
  15. import os
  16. import re
  17. import string
  18. import sys
  19.  
  20. from errors import DistutilsPlatformError
  21.  
  22. # These are needed in a couple of spots, so just compute them once.
  23. PREFIX = os.path.normpath(sys.prefix)
  24. EXEC_PREFIX = os.path.normpath(sys.exec_prefix)
  25.  
  26. # python_build: (Boolean) if true, we're either building Python or
  27. # building an extension with an un-installed Python, so we use
  28. # different (hard-wired) directories.
  29.  
  30. argv0_path = os.path.dirname(os.path.abspath(sys.executable))
  31. landmark = os.path.join(argv0_path, "Modules", "Setup")
  32. if not os.path.isfile(landmark):
  33.     python_build = 0
  34. elif os.path.isfile(os.path.join(argv0_path, "Lib", "os.py")):
  35.     python_build = 1
  36. else:
  37.     python_build = os.path.isfile(os.path.join(os.path.dirname(argv0_path),
  38.                                                "Lib", "os.py"))
  39. del argv0_path, landmark
  40.  
  41. # set_python_build() was present in 2.2 and 2.2.1; it's not needed
  42. # any more, but so 3rd party build scripts don't break, we leave
  43. # a do-nothing version:
  44. def set_python_build():
  45.     pass
  46.  
  47. def get_python_inc(plat_specific=0, prefix=None):
  48.     """Return the directory containing installed Python header files.
  49.  
  50.     If 'plat_specific' is false (the default), this is the path to the
  51.     non-platform-specific header files, i.e. Python.h and so on;
  52.     otherwise, this is the path to platform-specific header files
  53.     (namely pyconfig.h).
  54.  
  55.     If 'prefix' is supplied, use it instead of sys.prefix or
  56.     sys.exec_prefix -- i.e., ignore 'plat_specific'.
  57.     """
  58.     if prefix is None:
  59.         prefix = plat_specific and EXEC_PREFIX or PREFIX
  60.     if os.name == "posix":
  61.         if python_build:
  62.             base = os.path.dirname(os.path.abspath(sys.executable))
  63.             if plat_specific:
  64.                 inc_dir = base
  65.             else:
  66.                 inc_dir = os.path.join(base, "Include")
  67.                 if not os.path.exists(inc_dir):
  68.                     inc_dir = os.path.join(os.path.dirname(base), "Include")
  69.             return inc_dir
  70.         return os.path.join(prefix, "include", "python" + sys.version[:3])
  71.     elif os.name == "nt":
  72.         return os.path.join(prefix, "include")
  73.     elif os.name == "mac":
  74.         return os.path.join(prefix, "Include")
  75.     else:
  76.         raise DistutilsPlatformError(
  77.             "I don't know where Python installs its C header files "
  78.             "on platform '%s'" % os.name)
  79.  
  80.  
  81. def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
  82.     """Return the directory containing the Python library (standard or
  83.     site additions).
  84.  
  85.     If 'plat_specific' is true, return the directory containing
  86.     platform-specific modules, i.e. any module from a non-pure-Python
  87.     module distribution; otherwise, return the platform-shared library
  88.     directory.  If 'standard_lib' is true, return the directory
  89.     containing standard Python library modules; otherwise, return the
  90.     directory for site-specific modules.
  91.  
  92.     If 'prefix' is supplied, use it instead of sys.prefix or
  93.     sys.exec_prefix -- i.e., ignore 'plat_specific'.
  94.     """
  95.     if prefix is None:
  96.         prefix = plat_specific and EXEC_PREFIX or PREFIX
  97.  
  98.     if os.name == "posix":
  99.         libpython = os.path.join(prefix,
  100.                                  "lib", "python" + sys.version[:3])
  101.         if standard_lib:
  102.             return libpython
  103.         else:
  104.             return os.path.join(libpython, "site-packages")
  105.  
  106.     elif os.name == "nt":
  107.         if standard_lib:
  108.             return os.path.join(prefix, "Lib")
  109.         else:
  110.             if sys.version < "2.2":
  111.                 return prefix
  112.             else:
  113.                 return os.path.join(PREFIX, "Lib", "site-packages")
  114.  
  115.     elif os.name == "mac":
  116.         if plat_specific:
  117.             if standard_lib:
  118.                 return os.path.join(prefix, "Lib", "lib-dynload")
  119.             else:
  120.                 return os.path.join(prefix, "Lib", "site-packages")
  121.         else:
  122.             if standard_lib:
  123.                 return os.path.join(prefix, "Lib")
  124.             else:
  125.                 return os.path.join(prefix, "Lib", "site-packages")
  126.     else:
  127.         raise DistutilsPlatformError(
  128.             "I don't know where Python installs its library "
  129.             "on platform '%s'" % os.name)
  130.  
  131.  
  132. def customize_compiler(compiler):
  133.     """Do any platform-specific customization of a CCompiler instance.
  134.  
  135.     Mainly needed on Unix, so we can plug in the information that
  136.     varies across Unices and is stored in Python's Makefile.
  137.     """
  138.     if compiler.compiler_type == "unix":
  139.         (cc, opt, ccshared, ldshared, so_ext) = \
  140.             get_config_vars('CC', 'OPT', 'CCSHARED', 'LDSHARED', 'SO')
  141.  
  142.         cc_cmd = cc + ' ' + opt
  143.         compiler.set_executables(
  144.             preprocessor=cc + " -E",    # not always!
  145.             compiler=cc_cmd,
  146.             compiler_so=cc_cmd + ' ' + ccshared,
  147.             linker_so=ldshared,
  148.             linker_exe=cc)
  149.  
  150.         compiler.shared_lib_extension = so_ext
  151.  
  152.  
  153. def get_config_h_filename():
  154.     """Return full pathname of installed pyconfig.h file."""
  155.     if python_build:
  156.         inc_dir = os.curdir
  157.     else:
  158.         inc_dir = get_python_inc(plat_specific=1)
  159.     if sys.version < '2.2':
  160.         config_h = 'config.h'
  161.     else:
  162.         # The name of the config.h file changed in 2.2
  163.         config_h = 'pyconfig.h'
  164.     return os.path.join(inc_dir, config_h)
  165.  
  166.  
  167. def get_makefile_filename():
  168.     """Return full pathname of installed Makefile from the Python build."""
  169.     if python_build:
  170.         return os.path.join(os.path.dirname(sys.executable), "Makefile")
  171.     lib_dir = get_python_lib(plat_specific=1, standard_lib=1)
  172.     return os.path.join(lib_dir, "config", "Makefile")
  173.  
  174.  
  175. def parse_config_h(fp, g=None):
  176.     """Parse a config.h-style file.
  177.  
  178.     A dictionary containing name/value pairs is returned.  If an
  179.     optional dictionary is passed in as the second argument, it is
  180.     used instead of a new dictionary.
  181.     """
  182.     if g is None:
  183.         g = {}
  184.     define_rx = re.compile("#define ([A-Z][A-Z0-9_]+) (.*)\n")
  185.     undef_rx = re.compile("/[*] #undef ([A-Z][A-Z0-9_]+) [*]/\n")
  186.     #
  187.     while 1:
  188.         line = fp.readline()
  189.         if not line:
  190.             break
  191.         m = define_rx.match(line)
  192.         if m:
  193.             n, v = m.group(1, 2)
  194.             try: v = string.atoi(v)
  195.             except ValueError: pass
  196.             g[n] = v
  197.         else:
  198.             m = undef_rx.match(line)
  199.             if m:
  200.                 g[m.group(1)] = 0
  201.     return g
  202.  
  203.  
  204. # Regexes needed for parsing Makefile (and similar syntaxes,
  205. # like old-style Setup files).
  206. _variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
  207. _findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
  208. _findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
  209.  
  210. def parse_makefile(fn, g=None):
  211.     """Parse a Makefile-style file.
  212.  
  213.     A dictionary containing name/value pairs is returned.  If an
  214.     optional dictionary is passed in as the second argument, it is
  215.     used instead of a new dictionary.
  216.     """
  217.     from distutils.text_file import TextFile
  218.     fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1)
  219.  
  220.     if g is None:
  221.         g = {}
  222.     done = {}
  223.     notdone = {}
  224.  
  225.     while 1:
  226.         line = fp.readline()
  227.         if line is None:                # eof
  228.             break
  229.         m = _variable_rx.match(line)
  230.         if m:
  231.             n, v = m.group(1, 2)
  232.             v = string.strip(v)
  233.             if "$" in v:
  234.                 notdone[n] = v
  235.             else:
  236.                 try: v = string.atoi(v)
  237.                 except ValueError: pass
  238.                 done[n] = v
  239.  
  240.     # do variable interpolation here
  241.     while notdone:
  242.         for name in notdone.keys():
  243.             value = notdone[name]
  244.             m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
  245.             if m:
  246.                 n = m.group(1)
  247.                 if done.has_key(n):
  248.                     after = value[m.end():]
  249.                     value = value[:m.start()] + str(done[n]) + after
  250.                     if "$" in after:
  251.                         notdone[name] = value
  252.                     else:
  253.                         try: value = string.atoi(value)
  254.                         except ValueError:
  255.                             done[name] = string.strip(value)
  256.                         else:
  257.                             done[name] = value
  258.                         del notdone[name]
  259.                 elif notdone.has_key(n):
  260.                     # get it on a subsequent round
  261.                     pass
  262.                 else:
  263.                     done[n] = ""
  264.                     after = value[m.end():]
  265.                     value = value[:m.start()] + after
  266.                     if "$" in after:
  267.                         notdone[name] = value
  268.                     else:
  269.                         try: value = string.atoi(value)
  270.                         except ValueError:
  271.                             done[name] = string.strip(value)
  272.                         else:
  273.                             done[name] = value
  274.                         del notdone[name]
  275.             else:
  276.                 # bogus variable reference; just drop it since we can't deal
  277.                 del notdone[name]
  278.  
  279.     fp.close()
  280.  
  281.     # save the results in the global dictionary
  282.     g.update(done)
  283.     return g
  284.  
  285.  
  286. def expand_makefile_vars(s, vars):
  287.     """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
  288.     'string' according to 'vars' (a dictionary mapping variable names to
  289.     values).  Variables not present in 'vars' are silently expanded to the
  290.     empty string.  The variable values in 'vars' should not contain further
  291.     variable expansions; if 'vars' is the output of 'parse_makefile()',
  292.     you're fine.  Returns a variable-expanded version of 's'.
  293.     """
  294.  
  295.     # This algorithm does multiple expansion, so if vars['foo'] contains
  296.     # "${bar}", it will expand ${foo} to ${bar}, and then expand
  297.     # ${bar}... and so forth.  This is fine as long as 'vars' comes from
  298.     # 'parse_makefile()', which takes care of such expansions eagerly,
  299.     # according to make's variable expansion semantics.
  300.  
  301.     while 1:
  302.         m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
  303.         if m:
  304.             name = m.group(1)
  305.             (beg, end) = m.span()
  306.             s = s[0:beg] + vars.get(m.group(1)) + s[end:]
  307.         else:
  308.             break
  309.     return s
  310.  
  311.  
  312. _config_vars = None
  313.  
  314. def _init_posix():
  315.     """Initialize the module as appropriate for POSIX systems."""
  316.     g = {}
  317.     # load the installed Makefile:
  318.     try:
  319.         filename = get_makefile_filename()
  320.         parse_makefile(filename, g)
  321.     except IOError, msg:
  322.         my_msg = "invalid Python installation: unable to open %s" % filename
  323.         if hasattr(msg, "strerror"):
  324.             my_msg = my_msg + " (%s)" % msg.strerror
  325.  
  326.         raise DistutilsPlatformError(my_msg)
  327.  
  328.  
  329.     # On AIX, there are wrong paths to the linker scripts in the Makefile
  330.     # -- these paths are relative to the Python source, but when installed
  331.     # the scripts are in another directory.
  332.     if python_build:
  333.         g['LDSHARED'] = g['BLDSHARED']
  334.  
  335.     elif sys.version < '2.1':
  336.         # The following two branches are for 1.5.2 compatibility.
  337.         if sys.platform == 'aix4':          # what about AIX 3.x ?
  338.             # Linker script is in the config directory, not in Modules as the
  339.             # Makefile says.
  340.             python_lib = get_python_lib(standard_lib=1)
  341.             ld_so_aix = os.path.join(python_lib, 'config', 'ld_so_aix')
  342.             python_exp = os.path.join(python_lib, 'config', 'python.exp')
  343.  
  344.             g['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix, g['CC'], python_exp)
  345.  
  346.         elif sys.platform == 'beos':
  347.             # Linker script is in the config directory.  In the Makefile it is
  348.             # relative to the srcdir, which after installation no longer makes
  349.             # sense.
  350.             python_lib = get_python_lib(standard_lib=1)
  351.             linkerscript_name = os.path.basename(string.split(g['LDSHARED'])[0])
  352.             linkerscript = os.path.join(python_lib, 'config', linkerscript_name)
  353.  
  354.             # XXX this isn't the right place to do this: adding the Python
  355.             # library to the link, if needed, should be in the "build_ext"
  356.             # command.  (It's also needed for non-MS compilers on Windows, and
  357.             # it's taken care of for them by the 'build_ext.get_libraries()'
  358.             # method.)
  359.             g['LDSHARED'] = ("%s -L%s/lib -lpython%s" %
  360.                              (linkerscript, PREFIX, sys.version[0:3]))
  361.  
  362.     global _config_vars
  363.     _config_vars = g
  364.  
  365.  
  366. def _init_nt():
  367.     """Initialize the module as appropriate for NT"""
  368.     g = {}
  369.     # set basic install directories
  370.     g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
  371.     g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
  372.  
  373.     # XXX hmmm.. a normal install puts include files here
  374.     g['INCLUDEPY'] = get_python_inc(plat_specific=0)
  375.  
  376.     g['SO'] = '.pyd'
  377.     g['EXE'] = ".exe"
  378.  
  379.     global _config_vars
  380.     _config_vars = g
  381.  
  382.  
  383. def _init_mac():
  384.     """Initialize the module as appropriate for Macintosh systems"""
  385.     g = {}
  386.     # set basic install directories
  387.     g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
  388.     g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
  389.  
  390.     # XXX hmmm.. a normal install puts include files here
  391.     g['INCLUDEPY'] = get_python_inc(plat_specific=0)
  392.  
  393.     import MacOS
  394.     if not hasattr(MacOS, 'runtimemodel'):
  395.         g['SO'] = '.ppc.slb'
  396.     else:
  397.         g['SO'] = '.%s.slb' % MacOS.runtimemodel
  398.  
  399.     # XXX are these used anywhere?
  400.     g['install_lib'] = os.path.join(EXEC_PREFIX, "Lib")
  401.     g['install_platlib'] = os.path.join(EXEC_PREFIX, "Mac", "Lib")
  402.  
  403.     global _config_vars
  404.     _config_vars = g
  405.  
  406.  
  407. def get_config_vars(*args):
  408.     """With no arguments, return a dictionary of all configuration
  409.     variables relevant for the current platform.  Generally this includes
  410.     everything needed to build extensions and install both pure modules and
  411.     extensions.  On Unix, this means every variable defined in Python's
  412.     installed Makefile; on Windows and Mac OS it's a much smaller set.
  413.  
  414.     With arguments, return a list of values that result from looking up
  415.     each argument in the configuration variable dictionary.
  416.     """
  417.     global _config_vars
  418.     if _config_vars is None:
  419.         func = globals().get("_init_" + os.name)
  420.         if func:
  421.             func()
  422.         else:
  423.             _config_vars = {}
  424.  
  425.         # Normalized versions of prefix and exec_prefix are handy to have;
  426.         # in fact, these are the standard versions used most places in the
  427.         # Distutils.
  428.         _config_vars['prefix'] = PREFIX
  429.         _config_vars['exec_prefix'] = EXEC_PREFIX
  430.  
  431.     if args:
  432.         vals = []
  433.         for name in args:
  434.             vals.append(_config_vars.get(name))
  435.         return vals
  436.     else:
  437.         return _config_vars
  438.  
  439. def get_config_var(name):
  440.     """Return the value of a single variable using the dictionary
  441.     returned by 'get_config_vars()'.  Equivalent to
  442.     get_config_vars().get(name)
  443.     """
  444.     return get_config_vars().get(name)
  445.