source: titan/mediathek/localhoster/lib/python2.7/distutils/sysconfig.py @ 40114

Last change on this file since 40114 was 40094, checked in by obi, 7 years ago

tithek add yoztube-dl support

File size: 16.8 KB
Line 
1"""Provide access to Python's configuration information.  The specific
2configuration variables available depend heavily on the platform and
3configuration.  The values may be retrieved using
4get_config_var(name), and the list of variables is available via
5get_config_vars().keys().  Additional convenience functions are also
6available.
7
8Written by:   Fred L. Drake, Jr.
9Email:        <fdrake@acm.org>
10"""
11
12__revision__ = "$Id$"
13
14import os
15import re
16import string
17import sys
18
19from distutils.errors import DistutilsPlatformError
20
21# These are needed in a couple of spots, so just compute them once.
22PREFIX = os.path.normpath(sys.prefix)
23EXEC_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.
28project_base = os.path.dirname(os.path.abspath(sys.executable))
29if 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
32if 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
36if 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# set for cross builds
41if "_PYTHON_PROJECT_BASE" in os.environ:
42    # this is the build directory, at least for posix
43    project_base = os.path.normpath(os.environ["_PYTHON_PROJECT_BASE"])
44
45# python_build: (Boolean) if true, we're either building Python or
46# building an extension with an un-installed Python, so we use
47# different (hard-wired) directories.
48# Setup.local is available for Makefile builds including VPATH builds,
49# Setup.dist is available on Windows
50def _python_build():
51    for fn in ("Setup.dist", "Setup.local"):
52        if os.path.isfile(os.path.join(project_base, "Modules", fn)):
53            return True
54    return False
55python_build = _python_build()
56
57
58def get_python_version():
59    """Return a string containing the major and minor Python version,
60    leaving off the patchlevel.  Sample return values could be '1.5'
61    or '2.2'.
62    """
63    return sys.version[:3]
64
65
66def get_python_inc(plat_specific=0, prefix=None):
67    """Return the directory containing installed Python header files.
68
69    If 'plat_specific' is false (the default), this is the path to the
70    non-platform-specific header files, i.e. Python.h and so on;
71    otherwise, this is the path to platform-specific header files
72    (namely pyconfig.h).
73
74    If 'prefix' is supplied, use it instead of sys.prefix or
75    sys.exec_prefix -- i.e., ignore 'plat_specific'.
76    """
77    if prefix is None:
78        prefix = plat_specific and EXEC_PREFIX or PREFIX
79
80    if os.name == "posix":
81        if python_build:
82            buildir = os.path.dirname(sys.executable)
83            if plat_specific:
84                # python.h is located in the buildir
85                inc_dir = buildir
86            else:
87                # the source dir is relative to the buildir
88                srcdir = os.path.abspath(os.path.join(buildir,
89                                         get_config_var('srcdir')))
90                # Include is located in the srcdir
91                inc_dir = os.path.join(srcdir, "Include")
92            return inc_dir
93        return os.path.join(prefix, "include", "python" + get_python_version())
94    elif os.name == "nt":
95        return os.path.join(prefix, "include")
96    elif os.name == "os2":
97        return os.path.join(prefix, "Include")
98    else:
99        raise DistutilsPlatformError(
100            "I don't know where Python installs its C header files "
101            "on platform '%s'" % os.name)
102
103
104def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
105    """Return the directory containing the Python library (standard or
106    site additions).
107
108    If 'plat_specific' is true, return the directory containing
109    platform-specific modules, i.e. any module from a non-pure-Python
110    module distribution; otherwise, return the platform-shared library
111    directory.  If 'standard_lib' is true, return the directory
112    containing standard Python library modules; otherwise, return the
113    directory for site-specific modules.
114
115    If 'prefix' is supplied, use it instead of sys.prefix or
116    sys.exec_prefix -- i.e., ignore 'plat_specific'.
117    """
118    if prefix is None:
119        prefix = plat_specific and EXEC_PREFIX or PREFIX
120
121    if os.name == "posix":
122        if plat_specific or standard_lib:
123            lib = sys.lib
124        else:
125            lib = "lib"
126        libpython = os.path.join(prefix, lib, "python" + get_python_version())
127        if standard_lib:
128            return libpython
129        else:
130            return os.path.join(libpython, "site-packages")
131
132    elif os.name == "nt":
133        if standard_lib:
134            return os.path.join(prefix, "Lib")
135        else:
136            if get_python_version() < "2.2":
137                return prefix
138            else:
139                return os.path.join(prefix, "Lib", "site-packages")
140
141    elif os.name == "os2":
142        if standard_lib:
143            return os.path.join(prefix, "Lib")
144        else:
145            return os.path.join(prefix, "Lib", "site-packages")
146
147    else:
148        raise DistutilsPlatformError(
149            "I don't know where Python installs its library "
150            "on platform '%s'" % os.name)
151
152
153
154def customize_compiler(compiler):
155    """Do any platform-specific customization of a CCompiler instance.
156
157    Mainly needed on Unix, so we can plug in the information that
158    varies across Unices and is stored in Python's Makefile.
159    """
160    if compiler.compiler_type == "unix":
161        if sys.platform == "darwin":
162            # Perform first-time customization of compiler-related
163            # config vars on OS X now that we know we need a compiler.
164            # This is primarily to support Pythons from binary
165            # installers.  The kind and paths to build tools on
166            # the user system may vary significantly from the system
167            # that Python itself was built on.  Also the user OS
168            # version and build tools may not support the same set
169            # of CPU architectures for universal builds.
170            global _config_vars
171            # Use get_config_var() to ensure _config_vars is initialized.
172            if not get_config_var('CUSTOMIZED_OSX_COMPILER'):
173                import _osx_support
174                _osx_support.customize_compiler(_config_vars)
175                _config_vars['CUSTOMIZED_OSX_COMPILER'] = 'True'
176
177        (cc, cxx, opt, cflags, ccshared, ldshared, so_ext, ar, ar_flags) = \
178            get_config_vars('CC', 'CXX', 'OPT', 'CFLAGS',
179                            'CCSHARED', 'LDSHARED', 'SO', 'AR',
180                            'ARFLAGS')
181
182        if 'CC' in os.environ:
183            newcc = os.environ['CC']
184            if (sys.platform == 'darwin'
185                    and 'LDSHARED' not in os.environ
186                    and ldshared.startswith(cc)):
187                # On OS X, if CC is overridden, use that as the default
188                #       command for LDSHARED as well
189                ldshared = newcc + ldshared[len(cc):]
190            cc = newcc
191        if 'CXX' in os.environ:
192            cxx = os.environ['CXX']
193        if 'LDSHARED' in os.environ:
194            ldshared = os.environ['LDSHARED']
195        if 'CPP' in os.environ:
196            cpp = os.environ['CPP']
197        else:
198            cpp = cc + " -E"           # not always
199        if 'LDFLAGS' in os.environ:
200            ldshared = ldshared + ' ' + os.environ['LDFLAGS']
201        if 'CFLAGS' in os.environ:
202            cflags = opt + ' ' + os.environ['CFLAGS']
203            ldshared = ldshared + ' ' + os.environ['CFLAGS']
204        if 'CPPFLAGS' in os.environ:
205            cpp = cpp + ' ' + os.environ['CPPFLAGS']
206            cflags = cflags + ' ' + os.environ['CPPFLAGS']
207            ldshared = ldshared + ' ' + os.environ['CPPFLAGS']
208        if 'AR' in os.environ:
209            ar = os.environ['AR']
210        if 'ARFLAGS' in os.environ:
211            archiver = ar + ' ' + os.environ['ARFLAGS']
212        else:
213            archiver = ar + ' ' + ar_flags
214
215        cc_cmd = cc + ' ' + cflags
216        compiler.set_executables(
217            preprocessor=cpp,
218            compiler=cc_cmd,
219            compiler_so=cc_cmd + ' ' + ccshared,
220            compiler_cxx=cxx,
221            linker_so=ldshared,
222            linker_exe=cc,
223            archiver=archiver)
224
225        compiler.shared_lib_extension = so_ext
226
227
228def get_config_h_filename():
229    """Return full pathname of installed pyconfig.h file."""
230    if python_build:
231        if os.name == "nt":
232            inc_dir = os.path.join(project_base, "PC")
233        else:
234            inc_dir = project_base
235    else:
236        inc_dir = get_python_inc(plat_specific=1)
237    if get_python_version() < '2.2':
238        config_h = 'config.h'
239    else:
240        # The name of the config.h file changed in 2.2
241        config_h = 'pyconfig.h'
242    return os.path.join(inc_dir, config_h)
243
244
245def get_makefile_filename():
246    """Return full pathname of installed Makefile from the Python build."""
247    if python_build:
248        return os.path.join(project_base, "Makefile")
249    lib_dir = get_python_lib(plat_specific=1, standard_lib=1)
250    return os.path.join(lib_dir, "config", "Makefile")
251
252
253def parse_config_h(fp, g=None):
254    """Parse a config.h-style file.
255
256    A dictionary containing name/value pairs is returned.  If an
257    optional dictionary is passed in as the second argument, it is
258    used instead of a new dictionary.
259    """
260    if g is None:
261        g = {}
262    define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
263    undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
264    #
265    while 1:
266        line = fp.readline()
267        if not line:
268            break
269        m = define_rx.match(line)
270        if m:
271            n, v = m.group(1, 2)
272            try: v = int(v)
273            except ValueError: pass
274            g[n] = v
275        else:
276            m = undef_rx.match(line)
277            if m:
278                g[m.group(1)] = 0
279    return g
280
281
282# Regexes needed for parsing Makefile (and similar syntaxes,
283# like old-style Setup files).
284_variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
285_findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
286_findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
287
288def parse_makefile(fn, g=None):
289    """Parse a Makefile-style file.
290
291    A dictionary containing name/value pairs is returned.  If an
292    optional dictionary is passed in as the second argument, it is
293    used instead of a new dictionary.
294    """
295    from distutils.text_file import TextFile
296    fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1)
297
298    if g is None:
299        g = {}
300    done = {}
301    notdone = {}
302
303    while 1:
304        line = fp.readline()
305        if line is None:  # eof
306            break
307        m = _variable_rx.match(line)
308        if m:
309            n, v = m.group(1, 2)
310            v = v.strip()
311            # `$$' is a literal `$' in make
312            tmpv = v.replace('$$', '')
313
314            if "$" in tmpv:
315                notdone[n] = v
316            else:
317                try:
318                    v = int(v)
319                except ValueError:
320                    # insert literal `$'
321                    done[n] = v.replace('$$', '$')
322                else:
323                    done[n] = v
324
325    # do variable interpolation here
326    while notdone:
327        for name in notdone.keys():
328            value = notdone[name]
329            m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
330            if m:
331                n = m.group(1)
332                found = True
333                if n in done:
334                    item = str(done[n])
335                elif n in notdone:
336                    # get it on a subsequent round
337                    found = False
338                elif n in os.environ:
339                    # do it like make: fall back to environment
340                    item = os.environ[n]
341                else:
342                    done[n] = item = ""
343                if found:
344                    after = value[m.end():]
345                    value = value[:m.start()] + item + after
346                    if "$" in after:
347                        notdone[name] = value
348                    else:
349                        try: value = int(value)
350                        except ValueError:
351                            done[name] = value.strip()
352                        else:
353                            done[name] = value
354                        del notdone[name]
355            else:
356                # bogus variable reference; just drop it since we can't deal
357                del notdone[name]
358
359    fp.close()
360
361    # strip spurious spaces
362    for k, v in done.items():
363        if isinstance(v, str):
364            done[k] = v.strip()
365
366    # save the results in the global dictionary
367    g.update(done)
368    return g
369
370
371def expand_makefile_vars(s, vars):
372    """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
373    'string' according to 'vars' (a dictionary mapping variable names to
374    values).  Variables not present in 'vars' are silently expanded to the
375    empty string.  The variable values in 'vars' should not contain further
376    variable expansions; if 'vars' is the output of 'parse_makefile()',
377    you're fine.  Returns a variable-expanded version of 's'.
378    """
379
380    # This algorithm does multiple expansion, so if vars['foo'] contains
381    # "${bar}", it will expand ${foo} to ${bar}, and then expand
382    # ${bar}... and so forth.  This is fine as long as 'vars' comes from
383    # 'parse_makefile()', which takes care of such expansions eagerly,
384    # according to make's variable expansion semantics.
385
386    while 1:
387        m = _findvar1_rx.search(s) or _findvar2_rx.search(s)
388        if m:
389            (beg, end) = m.span()
390            s = s[0:beg] + vars.get(m.group(1)) + s[end:]
391        else:
392            break
393    return s
394
395
396_config_vars = None
397
398def _init_posix():
399    """Initialize the module as appropriate for POSIX systems."""
400    # _sysconfigdata is generated at build time, see the sysconfig module
401    from _sysconfigdata import build_time_vars
402    global _config_vars
403    _config_vars = {}
404    _config_vars.update(build_time_vars)
405
406
407def _init_nt():
408    """Initialize the module as appropriate for NT"""
409    g = {}
410    # set basic install directories
411    g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
412    g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
413
414    # XXX hmmm.. a normal install puts include files here
415    g['INCLUDEPY'] = get_python_inc(plat_specific=0)
416
417    g['SO'] = '.pyd'
418    g['EXE'] = ".exe"
419    g['VERSION'] = get_python_version().replace(".", "")
420    g['BINDIR'] = os.path.dirname(os.path.abspath(sys.executable))
421
422    global _config_vars
423    _config_vars = g
424
425
426def _init_os2():
427    """Initialize the module as appropriate for OS/2"""
428    g = {}
429    # set basic install directories
430    g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
431    g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
432
433    # XXX hmmm.. a normal install puts include files here
434    g['INCLUDEPY'] = get_python_inc(plat_specific=0)
435
436    g['SO'] = '.pyd'
437    g['EXE'] = ".exe"
438
439    global _config_vars
440    _config_vars = g
441
442
443def get_config_vars(*args):
444    """With no arguments, return a dictionary of all configuration
445    variables relevant for the current platform.  Generally this includes
446    everything needed to build extensions and install both pure modules and
447    extensions.  On Unix, this means every variable defined in Python's
448    installed Makefile; on Windows and Mac OS it's a much smaller set.
449
450    With arguments, return a list of values that result from looking up
451    each argument in the configuration variable dictionary.
452    """
453    global _config_vars
454    if _config_vars is None:
455        func = globals().get("_init_" + os.name)
456        if func:
457            func()
458        else:
459            _config_vars = {}
460
461        # Normalized versions of prefix and exec_prefix are handy to have;
462        # in fact, these are the standard versions used most places in the
463        # Distutils.
464        _config_vars['prefix'] = PREFIX
465        _config_vars['exec_prefix'] = EXEC_PREFIX
466
467        # OS X platforms require special customization to handle
468        # multi-architecture, multi-os-version installers
469        if sys.platform == 'darwin':
470            import _osx_support
471            _osx_support.customize_config_vars(_config_vars)
472
473    if args:
474        vals = []
475        for name in args:
476            vals.append(_config_vars.get(name))
477        return vals
478    else:
479        return _config_vars
480
481def get_config_var(name):
482    """Return the value of a single variable using the dictionary
483    returned by 'get_config_vars()'.  Equivalent to
484    get_config_vars().get(name)
485    """
486    return get_config_vars().get(name)
Note: See TracBrowser for help on using the repository browser.