home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 June / maximum-cd-2011-06.iso / DiscContents / LibO_3.3.1_Win_x86_install_multi.exe / libreoffice1.cab / sysconfig.py < prev    next >
Encoding:
Python Source  |  2011-02-15  |  20.6 KB  |  576 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. """
  11.  
  12. __revision__ = "$Id: sysconfig.py 63955 2008-06-05 12:58:24Z ronald.oussoren $"
  13.  
  14. import os
  15. import re
  16. import string
  17. import sys
  18.  
  19. from distutils.errors import DistutilsPlatformError
  20.  
  21. # These are needed in a couple of spots, so just compute them once.
  22. PREFIX = os.path.normpath(sys.prefix)
  23. EXEC_PREFIX = os.path.normpath(sys.exec_prefix)
  24.  
  25. # Path to the base directory of the project. On Windows the binary may
  26. # live in project/PCBuild9.  If we're dealing with an x64 Windows build,
  27. # it'll live in project/PCbuild/amd64.
  28. project_base = os.path.dirname(os.path.abspath(sys.executable))
  29. if os.name == "nt" and "pcbuild" in project_base[-8:].lower():
  30.     project_base = os.path.abspath(os.path.join(project_base, os.path.pardir))
  31. # PC/VS7.1
  32. if os.name == "nt" and "\\pc\\v" in project_base[-10:].lower():
  33.     project_base = os.path.abspath(os.path.join(project_base, os.path.pardir,
  34.                                                 os.path.pardir))
  35. # PC/AMD64
  36. if os.name == "nt" and "\\pcbuild\\amd64" in project_base[-14:].lower():
  37.     project_base = os.path.abspath(os.path.join(project_base, os.path.pardir,
  38.                                                 os.path.pardir))
  39.  
  40. # python_build: (Boolean) if true, we're either building Python or
  41. # building an extension with an un-installed Python, so we use
  42. # different (hard-wired) directories.
  43. # Setup.local is available for Makefile builds including VPATH builds,
  44. # Setup.dist is available on Windows
  45. def _python_build():
  46.     for fn in ("Setup.dist", "Setup.local"):
  47.         if os.path.isfile(os.path.join(project_base, "Modules", fn)):
  48.             return True
  49.     return False
  50. python_build = _python_build()
  51.  
  52.  
  53. def get_python_version():
  54.     """Return a string containing the major and minor Python version,
  55.     leaving off the patchlevel.  Sample return values could be '1.5'
  56.     or '2.2'.
  57.     """
  58.     return sys.version[:3]
  59.  
  60.  
  61. def get_python_inc(plat_specific=0, prefix=None):
  62.     """Return the directory containing installed Python header files.
  63.  
  64.     If 'plat_specific' is false (the default), this is the path to the
  65.     non-platform-specific header files, i.e. Python.h and so on;
  66.     otherwise, this is the path to platform-specific header files
  67.     (namely pyconfig.h).
  68.  
  69.     If 'prefix' is supplied, use it instead of sys.prefix or
  70.     sys.exec_prefix -- i.e., ignore 'plat_specific'.
  71.     """
  72.     if prefix is None:
  73.         prefix = plat_specific and EXEC_PREFIX or PREFIX
  74.     if os.name == "posix":
  75.         if python_build:
  76.             base = os.path.dirname(os.path.abspath(sys.executable))
  77.             if plat_specific:
  78.                 inc_dir = base
  79.             else:
  80.                 inc_dir = os.path.join(base, "Include")
  81.                 if not os.path.exists(inc_dir):
  82.                     inc_dir = os.path.join(os.path.dirname(base), "Include")
  83.             return inc_dir
  84.         return os.path.join(prefix, "include", "python" + get_python_version())
  85.     elif os.name == "nt":
  86.         return os.path.join(prefix, "include")
  87.     elif os.name == "mac":
  88.         if plat_specific:
  89.             return os.path.join(prefix, "Mac", "Include")
  90.         else:
  91.             return os.path.join(prefix, "Include")
  92.     elif os.name == "os2":
  93.         return os.path.join(prefix, "Include")
  94.     else:
  95.         raise DistutilsPlatformError(
  96.             "I don't know where Python installs its C header files "
  97.             "on platform '%s'" % os.name)
  98.  
  99.  
  100. def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
  101.     """Return the directory containing the Python library (standard or
  102.     site additions).
  103.  
  104.     If 'plat_specific' is true, return the directory containing
  105.     platform-specific modules, i.e. any module from a non-pure-Python
  106.     module distribution; otherwise, return the platform-shared library
  107.     directory.  If 'standard_lib' is true, return the directory
  108.     containing standard Python library modules; otherwise, return the
  109.     directory for site-specific modules.
  110.  
  111.     If 'prefix' is supplied, use it instead of sys.prefix or
  112.     sys.exec_prefix -- i.e., ignore 'plat_specific'.
  113.     """
  114.     if prefix is None:
  115.         prefix = plat_specific and EXEC_PREFIX or PREFIX
  116.  
  117.     if os.name == "posix":
  118.         libpython = os.path.join(prefix,
  119.                                  "lib", "python" + get_python_version())
  120.         if standard_lib:
  121.             return libpython
  122.         else:
  123.             return os.path.join(libpython, "site-packages")
  124.  
  125.     elif os.name == "nt":
  126.         if standard_lib:
  127.             return os.path.join(prefix, "Lib")
  128.         else:
  129.             if get_python_version() < "2.2":
  130.                 return prefix
  131.             else:
  132.                 return os.path.join(PREFIX, "Lib", "site-packages")
  133.  
  134.     elif os.name == "mac":
  135.         if plat_specific:
  136.             if standard_lib:
  137.                 return os.path.join(prefix, "Lib", "lib-dynload")
  138.             else:
  139.                 return os.path.join(prefix, "Lib", "site-packages")
  140.         else:
  141.             if standard_lib:
  142.                 return os.path.join(prefix, "Lib")
  143.             else:
  144.                 return os.path.join(prefix, "Lib", "site-packages")
  145.  
  146.     elif os.name == "os2":
  147.         if standard_lib:
  148.             return os.path.join(PREFIX, "Lib")
  149.         else:
  150.             return os.path.join(PREFIX, "Lib", "site-packages")
  151.  
  152.     else:
  153.         raise DistutilsPlatformError(
  154.             "I don't know where Python installs its library "
  155.             "on platform '%s'" % os.name)
  156.  
  157.  
  158. def customize_compiler(compiler):
  159.     """Do any platform-specific customization of a CCompiler instance.
  160.  
  161.     Mainly needed on Unix, so we can plug in the information that
  162.     varies across Unices and is stored in Python's Makefile.
  163.     """
  164.     if compiler.compiler_type == "unix":
  165.         (cc, cxx, opt, cflags, ccshared, ldshared, so_ext) = \
  166.             get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
  167.                             'CCSHARED', 'LDSHARED', 'SO')
  168.  
  169.         if 'CC' in os.environ:
  170.             cc = os.environ['CC']
  171.         if 'CXX' in os.environ:
  172.             cxx = os.environ['CXX']
  173.         if 'LDSHARED' in os.environ:
  174.             ldshared = os.environ['LDSHARED']
  175.         if 'CPP' in os.environ:
  176.             cpp = os.environ['CPP']
  177.         else:
  178.             cpp = cc + " -E"           # not always
  179.         if 'LDFLAGS' in os.environ:
  180.             ldshared = ldshared + ' ' + os.environ['LDFLAGS']
  181.         if 'CFLAGS' in os.environ:
  182.             cflags = opt + ' ' + os.environ['CFLAGS']
  183.             ldshared = ldshared + ' ' + os.environ['CFLAGS']
  184.         if 'CPPFLAGS' in os.environ:
  185.             cpp = cpp + ' ' + os.environ['CPPFLAGS']
  186.             cflags = cflags + ' ' + os.environ['CPPFLAGS']
  187.             ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
  188.  
  189.         cc_cmd = cc + ' ' + cflags
  190.         compiler.set_executables(
  191.             preprocessor=cpp,
  192.             compiler=cc_cmd,
  193.             compiler_so=cc_cmd + ' ' + ccshared,
  194.             compiler_cxx=cxx,
  195.             linker_so=ldshared,
  196.             linker_exe=cc)
  197.  
  198.         compiler.shared_lib_extension = so_ext
  199.  
  200.  
  201. def get_config_h_filename():
  202.     """Return full pathname of installed pyconfig.h file."""
  203.     if python_build:
  204.         if os.name == "nt":
  205.             inc_dir = os.path.join(project_base, "PC")
  206.         else:
  207.             inc_dir = project_base
  208.     else:
  209.         inc_dir = get_python_inc(plat_specific=1)
  210.     if get_python_version() < '2.2':
  211.         config_h = 'config.h'
  212.     else:
  213.         # The name of the config.h file changed in 2.2
  214.         config_h = 'pyconfig.h'
  215.     return os.path.join(inc_dir, config_h)
  216.  
  217.  
  218. def get_makefile_filename():
  219.     """Return full pathname of installed Makefile from the Python build."""
  220.     if python_build:
  221.         return os.path.join(os.path.dirname(sys.executable), "Makefile")
  222.     lib_dir = get_python_lib(plat_specific=1, standard_lib=1)
  223.     return os.path.join(lib_dir, "config", "Makefile")
  224.  
  225.  
  226. def parse_config_h(fp, g=None):
  227.     """Parse a config.h-style file.
  228.  
  229.     A dictionary containing name/value pairs is returned.  If an
  230.     optional dictionary is passed in as the second argument, it is
  231.     used instead of a new dictionary.
  232.     """
  233.     if g is None:
  234.         g = {}
  235.     define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
  236.     undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
  237.     #
  238.     while 1:
  239.         line = fp.readline()
  240.         if not line:
  241.             break
  242.         m = define_rx.match(line)
  243.         if m:
  244.             n, v = m.group(1, 2)
  245.             try: v = int(v)
  246.             except ValueError: pass
  247.             g[n] = v
  248.         else:
  249.             m = undef_rx.match(line)
  250.             if m:
  251.                 g[m.group(1)] = 0
  252.     return g
  253.  
  254.  
  255. # Regexes needed for parsing Makefile (and similar syntaxes,
  256. # like old-style Setup files).
  257. _variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
  258. _findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
  259. _findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
  260.  
  261. def parse_makefile(fn, g=None):
  262.     """Parse a Makefile-style file.
  263.  
  264.     A dictionary containing name/value pairs is returned.  If an
  265.     optional dictionary is passed in as the second argument, it is
  266.     used instead of a new dictionary.
  267.     """
  268.     from distutils.text_file import TextFile
  269.     fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1)
  270.  
  271.     if g is None:
  272.         g = {}
  273.     done = {}
  274.     notdone = {}
  275.  
  276.     while 1:
  277.         line = fp.readline()
  278.         if line is None:                # eof
  279.             break
  280.         m = _variable_rx.match(line)
  281.         if m:
  282.             n, v = m.group(1, 2)
  283.             v = string.strip(v)
  284.             if "$" in v:
  285.                 notdone[n] = v
  286.             else:
  287.                 try: v = int(v)
  288.                 except ValueError: pass
  289.                 done[n] = v
  290.  
  291.     # do variable interpolation here
  292.     while notdone:
  293.         for name in notdone.keys():
  294.             value = notdone[name]
  295.             m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
  296.             if m:
  297.                 n = m.group(1)
  298.                 found = True
  299.                 if n in done:
  300.                     item = str(done[n])
  301.                 elif n in notdone:
  302.                     # get it on a subsequent round
  303.                     found = False
  304.                 elif n in os.environ:
  305.                     # do it like make: fall back to environment
  306.                     item = os.environ[n]
  307.                 else:
  308.                     done[n] = item = ""
  309.                 if found:
  310.                     after = value[m.end():]
  311.                     value = value[:m.start()] + item + after
  312.                     if "$" in after:
  313.                         notdone[name] = value
  314.                     else:
  315.                         try: value = int(value)
  316.                         except ValueError:
  317.                             done[name] = string.strip(value)
  318.                         else:
  319.                             done[name] = value
  320.                         del notdone[name]
  321.             else:
  322.                 # bogus variable reference; just drop it since we can't deal
  323.                 del notdone[name]
  324.  
  325.     fp.close()
  326.  
  327.     # save the results in the global dictionary
  328.     g.update(done)
  329.     return g
  330.  
  331.  
  332. def expand_makefile_vars(s, vars):
  333.     """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
  334.     'string' according to 'vars' (a dictionary mapping variable names to
  335.     values).  Variables not present in 'vars' are silently expanded to the
  336.     empty string.  The variable values in 'vars' should not contain further
  337.     variable expansions; if 'vars' is the output of 'parse_makefile()',
  338.     you're fine.  Returns a variable-expanded version of 's'.
  339.     """
  340.  
  341.     # This algorithm does multiple expansion, so if vars['foo'] contains
  342.     # "${bar}", it will expand ${foo} to ${bar}, and then expand
  343.     # ${bar}... and so forth.  This is fine as long as 'vars' comes from
  344.     # 'parse_makefile()', which takes care of such expansions eagerly,
  345.     # according to make's variable expansion semantics.
  346.  
  347.     while 1:
  348.         m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
  349.         if m:
  350.             (beg, end) = m.span()
  351.             s = s[0:beg] + vars.get(m.group(1)) + s[end:]
  352.         else:
  353.             break
  354.     return s
  355.  
  356.  
  357. _config_vars = None
  358.  
  359. def _init_posix():
  360.     """Initialize the module as appropriate for POSIX systems."""
  361.     g = {}
  362.     # load the installed Makefile:
  363.     try:
  364.         filename = get_makefile_filename()
  365.         parse_makefile(filename, g)
  366.     except IOError, msg:
  367.         my_msg = "invalid Python installation: unable to open %s" % filename
  368.         if hasattr(msg, "strerror"):
  369.             my_msg = my_msg + " (%s)" % msg.strerror
  370.  
  371.         raise DistutilsPlatformError(my_msg)
  372.  
  373.     # load the installed pyconfig.h:
  374.     try:
  375.         filename = get_config_h_filename()
  376.         parse_config_h(file(filename), g)
  377.     except IOError, msg:
  378.         my_msg = "invalid Python installation: unable to open %s" % filename
  379.         if hasattr(msg, "strerror"):
  380.             my_msg = my_msg + " (%s)" % msg.strerror
  381.  
  382.         raise DistutilsPlatformError(my_msg)
  383.  
  384.     # On MacOSX we need to check the setting of the environment variable
  385.     # MACOSX_DEPLOYMENT_TARGET: configure bases some choices on it so
  386.     # it needs to be compatible.
  387.     # If it isn't set we set it to the configure-time value
  388.     if sys.platform == 'darwin' and 'MACOSX_DEPLOYMENT_TARGET' in g:
  389.         cfg_target = g['MACOSX_DEPLOYMENT_TARGET']
  390.         cur_target = os.getenv('MACOSX_DEPLOYMENT_TARGET', '')
  391.         if cur_target == '':
  392.             cur_target = cfg_target
  393.             os.putenv('MACOSX_DEPLOYMENT_TARGET', cfg_target)
  394.         elif map(int, cfg_target.split('.')) > map(int, cur_target.split('.')):
  395.             my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: now "%s" but "%s" during configure'
  396.                 % (cur_target, cfg_target))
  397.             raise DistutilsPlatformError(my_msg)
  398.  
  399.     # On AIX, there are wrong paths to the linker scripts in the Makefile
  400.     # -- these paths are relative to the Python source, but when installed
  401.     # the scripts are in another directory.
  402.     if python_build:
  403.         g['LDSHARED'] = g['BLDSHARED']
  404.  
  405.     elif get_python_version() < '2.1':
  406.         # The following two branches are for 1.5.2 compatibility.
  407.         if sys.platform == 'aix4':          # what about AIX 3.x ?
  408.             # Linker script is in the config directory, not in Modules as the
  409.             # Makefile says.
  410.             python_lib = get_python_lib(standard_lib=1)
  411.             ld_so_aix = os.path.join(python_lib, 'config', 'ld_so_aix')
  412.             python_exp = os.path.join(python_lib, 'config', 'python.exp')
  413.  
  414.             g['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix, g['CC'], python_exp)
  415.  
  416.         elif sys.platform == 'beos':
  417.             # Linker script is in the config directory.  In the Makefile it is
  418.             # relative to the srcdir, which after installation no longer makes
  419.             # sense.
  420.             python_lib = get_python_lib(standard_lib=1)
  421.             linkerscript_path = string.split(g['LDSHARED'])[0]
  422.             linkerscript_name = os.path.basename(linkerscript_path)
  423.             linkerscript = os.path.join(python_lib, 'config',
  424.                                         linkerscript_name)
  425.  
  426.             # XXX this isn't the right place to do this: adding the Python
  427.             # library to the link, if needed, should be in the "build_ext"
  428.             # command.  (It's also needed for non-MS compilers on Windows, and
  429.             # it's taken care of for them by the 'build_ext.get_libraries()'
  430.             # method.)
  431.             g['LDSHARED'] = ("%s -L%s/lib -lpython%s" %
  432.                              (linkerscript, PREFIX, get_python_version()))
  433.  
  434.     global _config_vars
  435.     _config_vars = g
  436.  
  437.  
  438. def _init_nt():
  439.     """Initialize the module as appropriate for NT"""
  440.     g = {}
  441.     # set basic install directories
  442.     g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
  443.     g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
  444.  
  445.     # XXX hmmm.. a normal install puts include files here
  446.     g['INCLUDEPY'] = get_python_inc(plat_specific=0)
  447.  
  448.     g['SO'] = '.pyd'
  449.     g['EXE'] = ".exe"
  450.     g['VERSION'] = get_python_version().replace(".", "")
  451.     g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))
  452.  
  453.     global _config_vars
  454.     _config_vars = g
  455.  
  456.  
  457. def _init_mac():
  458.     """Initialize the module as appropriate for Macintosh systems"""
  459.     g = {}
  460.     # set basic install directories
  461.     g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
  462.     g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
  463.  
  464.     # XXX hmmm.. a normal install puts include files here
  465.     g['INCLUDEPY'] = get_python_inc(plat_specific=0)
  466.  
  467.     import MacOS
  468.     if not hasattr(MacOS, 'runtimemodel'):
  469.         g['SO'] = '.ppc.slb'
  470.     else:
  471.         g['SO'] = '.%s.slb' % MacOS.runtimemodel
  472.  
  473.     # XXX are these used anywhere?
  474.     g['install_lib'] = os.path.join(EXEC_PREFIX, "Lib")
  475.     g['install_platlib'] = os.path.join(EXEC_PREFIX, "Mac", "Lib")
  476.  
  477.     # These are used by the extension module build
  478.     g['srcdir'] = ':'
  479.     global _config_vars
  480.     _config_vars = g
  481.  
  482.  
  483. def _init_os2():
  484.     """Initialize the module as appropriate for OS/2"""
  485.     g = {}
  486.     # set basic install directories
  487.     g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
  488.     g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
  489.  
  490.     # XXX hmmm.. a normal install puts include files here
  491.     g['INCLUDEPY'] = get_python_inc(plat_specific=0)
  492.  
  493.     g['SO'] = '.pyd'
  494.     g['EXE'] = ".exe"
  495.  
  496.     global _config_vars
  497.     _config_vars = g
  498.  
  499.  
  500. def get_config_vars(*args):
  501.     """With no arguments, return a dictionary of all configuration
  502.     variables relevant for the current platform.  Generally this includes
  503.     everything needed to build extensions and install both pure modules and
  504.     extensions.  On Unix, this means every variable defined in Python's
  505.     installed Makefile; on Windows and Mac OS it's a much smaller set.
  506.  
  507.     With arguments, return a list of values that result from looking up
  508.     each argument in the configuration variable dictionary.
  509.     """
  510.     global _config_vars
  511.     if _config_vars is None:
  512.         func = globals().get("_init_" + os.name)
  513.         if func:
  514.             func()
  515.         else:
  516.             _config_vars = {}
  517.  
  518.         # Normalized versions of prefix and exec_prefix are handy to have;
  519.         # in fact, these are the standard versions used most places in the
  520.         # Distutils.
  521.         _config_vars['prefix'] = PREFIX
  522.         _config_vars['exec_prefix'] = EXEC_PREFIX
  523.  
  524.         if sys.platform == 'darwin':
  525.             kernel_version = os.uname()[2] # Kernel version (8.4.3)
  526.             major_version = int(kernel_version.split('.')[0])
  527.  
  528.             if major_version < 8:
  529.                 # On Mac OS X before 10.4, check if -arch and -isysroot
  530.                 # are in CFLAGS or LDFLAGS and remove them if they are.
  531.                 # This is needed when building extensions on a 10.3 system
  532.                 # using a universal build of python.
  533.                 for key in ('LDFLAGS', 'BASECFLAGS',
  534.                         # a number of derived variables. These need to be
  535.                         # patched up as well.
  536.                         'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
  537.                     flags = _config_vars[key]
  538.                     flags = re.sub('-arch\s+\w+\s', ' ', flags)
  539.                     flags = re.sub('-isysroot [^ \t]*', ' ', flags)
  540.                     _config_vars[key] = flags
  541.  
  542.             else:
  543.  
  544.                 # Allow the user to override the architecture flags using
  545.                 # an environment variable.
  546.                 # NOTE: This name was introduced by Apple in OSX 10.5 and
  547.                 # is used by several scripting languages distributed with
  548.                 # that OS release.
  549.  
  550.                 if 'ARCHFLAGS' in os.environ:
  551.                     arch = os.environ['ARCHFLAGS']
  552.                     for key in ('LDFLAGS', 'BASECFLAGS',
  553.                         # a number of derived variables. These need to be
  554.                         # patched up as well.
  555.                         'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
  556.  
  557.                         flags = _config_vars[key]
  558.                         flags = re.sub('-arch\s+\w+\s', ' ', flags)
  559.                         flags = flags + ' ' + arch
  560.                         _config_vars[key] = flags
  561.  
  562.     if args:
  563.         vals = []
  564.         for name in args:
  565.             vals.append(_config_vars.get(name))
  566.         return vals
  567.     else:
  568.         return _config_vars
  569.  
  570. def get_config_var(name):
  571.     """Return the value of a single variable using the dictionary
  572.     returned by 'get_config_vars()'.  Equivalent to
  573.     get_config_vars().get(name)
  574.     """
  575.     return get_config_vars().get(name)
  576.