3 Miscellaneous utility functions -- anything that doesn't fit into
4 one of the other *util.py modules.
16 from .errors
import DistutilsPlatformError
, DistutilsByteCompileError
17 from .dep_util
import newer
18 from .spawn
import spawn
22 def get_host_platform():
24 Return a string that identifies the current platform. Use this
25 function to distinguish platform-specific build directories and
26 platform-specific built distributions.
29 # This function initially exposed platforms as defined in Python 3.9
30 # even with older Python versions when distutils was split out.
31 # Now it delegates to stdlib sysconfig, but maintains compatibility.
33 if sys
.version_info
< (3, 8):
35 if '(arm)' in sys
.version
.lower():
37 if '(arm64)' in sys
.version
.lower():
40 if sys
.version_info
< (3, 9):
41 if os
.name
== "posix" and hasattr(os
, 'uname'):
42 osname
, host
, release
, version
, machine
= os
.uname()
43 if osname
[:3] == "aix":
44 from .py38compat
import aix_platform
46 return aix_platform(osname
, version
, release
)
48 return sysconfig
.get_platform()
59 target
= os
.environ
.get('VSCMD_ARG_TGT_ARCH')
60 return TARGET_TO_PLAT
.get(target
) or get_host_platform()
61 return get_host_platform()
64 if sys
.platform
== 'darwin':
65 _syscfg_macosx_ver
= None # cache the version pulled from sysconfig
66 MACOSX_VERSION_VAR
= 'MACOSX_DEPLOYMENT_TARGET'
69 def _clear_cached_macosx_ver():
70 """For testing only. Do not call."""
71 global _syscfg_macosx_ver
72 _syscfg_macosx_ver
= None
75 def get_macosx_target_ver_from_syscfg():
76 """Get the version of macOS latched in the Python interpreter configuration.
77 Returns the version as a string or None if can't obtain one. Cached."""
78 global _syscfg_macosx_ver
79 if _syscfg_macosx_ver
is None:
80 from distutils
import sysconfig
82 ver
= sysconfig
.get_config_var(MACOSX_VERSION_VAR
) or ''
84 _syscfg_macosx_ver
= ver
85 return _syscfg_macosx_ver
88 def get_macosx_target_ver():
89 """Return the version of macOS for which we are building.
91 The target version defaults to the version in sysconfig latched at time
92 the Python interpreter was built, unless overridden by an environment
93 variable. If neither source has a value, then None is returned"""
95 syscfg_ver
= get_macosx_target_ver_from_syscfg()
96 env_ver
= os
.environ
.get(MACOSX_VERSION_VAR
)
99 # Validate overridden version against sysconfig version, if have both.
100 # Ensure that the deployment target of the build process is not less
101 # than 10.3 if the interpreter was built for 10.3 or later. This
102 # ensures extension modules are built with correct compatibility
103 # values, specifically LDSHARED which can use
104 # '-undefined dynamic_lookup' which only works on >= 10.3.
107 and split_version(syscfg_ver
) >= [10, 3]
108 and split_version(env_ver
) < [10, 3]
111 '$' + MACOSX_VERSION_VAR
+ ' mismatch: '
112 'now "%s" but "%s" during configure; '
113 'must use 10.3 or later' % (env_ver
, syscfg_ver
)
115 raise DistutilsPlatformError(my_msg
)
120 def split_version(s
):
121 """Convert a dot-separated string into a list of numbers for comparisons"""
122 return [int(n
) for n
in s
.split('.')]
125 def convert_path(pathname
):
126 """Return 'pathname' as a name that will work on the native filesystem,
127 i.e. split it on '/' and put it back together again using the current
128 directory separator. Needed because filenames in the setup script are
129 always supplied in Unix style, and have to be converted to the local
130 convention before we can actually use them in the filesystem. Raises
131 ValueError on non-Unix-ish systems if 'pathname' either starts or
138 if pathname
[0] == '/':
139 raise ValueError("path '%s' cannot be absolute" % pathname
)
140 if pathname
[-1] == '/':
141 raise ValueError("path '%s' cannot end with '/'" % pathname
)
143 paths
= pathname
.split('/')
148 return os
.path
.join(*paths
)
154 def change_root(new_root
, pathname
):
155 """Return 'pathname' with 'new_root' prepended. If 'pathname' is
156 relative, this is equivalent to "os.path.join(new_root,pathname)".
157 Otherwise, it requires making 'pathname' relative and then joining the
158 two, which is tricky on DOS/Windows and Mac OS.
160 if os
.name
== 'posix':
161 if not os
.path
.isabs(pathname
):
162 return os
.path
.join(new_root
, pathname
)
164 return os
.path
.join(new_root
, pathname
[1:])
166 elif os
.name
== 'nt':
167 (drive
, path
) = os
.path
.splitdrive(pathname
)
170 return os
.path
.join(new_root
, path
)
172 raise DistutilsPlatformError(f
"nothing known about platform '{os.name}'")
175 @functools.lru_cache()
177 """Ensure that 'os.environ' has all the environment variables we
178 guarantee that users can use in config files, command-line options,
179 etc. Currently this includes:
180 HOME - user's home directory (Unix only)
181 PLAT - description of the current platform, including hardware
182 and OS (see 'get_platform()')
184 if os
.name
== 'posix' and 'HOME' not in os
.environ
:
188 os
.environ
['HOME'] = pwd
.getpwuid(os
.getuid())[5]
189 except (ImportError, KeyError):
190 # bpo-10496: if the current user identifier doesn't exist in the
191 # password database, do nothing
194 if 'PLAT' not in os
.environ
:
195 os
.environ
['PLAT'] = get_platform()
198 def subst_vars(s
, local_vars
):
200 Perform variable substitution on 'string'.
201 Variables are indicated by format-style braces ("{var}").
202 Variable is substituted by the value found in the 'local_vars'
203 dictionary or in 'os.environ' if it's not in 'local_vars'.
204 'os.environ' is first checked/augmented to guarantee that it contains
205 certain values: see 'check_environ()'. Raise ValueError for any
206 variables not found in either 'local_vars' or 'os.environ'.
209 lookup
= dict(os
.environ
)
210 lookup
.update((name
, str(value
)) for name
, value
in local_vars
.items())
212 return _subst_compat(s
).format_map(lookup
)
213 except KeyError as var
:
214 raise ValueError(f
"invalid variable {var}")
217 def _subst_compat(s
):
219 Replace shell/Perl-style variable substitution with
220 format-style. For compatibility.
224 return f
'{{{match.group(1)}}}'
226 repl
= re
.sub(r
'\$([a-zA-Z_][a-zA-Z_0-9]*)', _subst
, s
)
231 "shell/Perl-style substitions are deprecated",
237 def grok_environment_error(exc
, prefix
="error: "):
238 # Function kept for backward compatibility.
239 # Used to try clever things with EnvironmentErrors,
240 # but nowadays str(exception) produces good messages.
241 return prefix
+ str(exc
)
244 # Needed by 'split_quoted()'
245 _wordchars_re
= _squote_re
= _dquote_re
= None
249 global _wordchars_re
, _squote_re
, _dquote_re
250 _wordchars_re
= re
.compile(r
'[^\\\'\"%s ]*' % string.whitespace)
251 _squote_re = re.compile(r"'(?
:[^
'\\]|\\.)*'")
252 _dquote_re = re.compile(r'"(?
:[^
"\\]|\\.)*"')
256 """Split a string up according to Unix shell-like rules for quotes and
257 backslashes. In short: words are delimited by spaces, as long as those
258 spaces are not escaped by a backslash, or inside a quoted string.
259 Single and double quotes are equivalent, and the quote characters can
260 be backslash-escaped. The backslash is stripped from any two-character
261 escape sequence, leaving only the escaped character. The quote
262 characters are stripped from any quoted string. Returns a list of
266 # This is a nice algorithm for splitting up a single string, since it
267 # doesn't require character
-by
-character examination
. It was a little
268 # bit of a brain-bender to get it working right, though...
269 if _wordchars_re
is None:
277 m
= _wordchars_re
.match(s
, pos
)
280 words
.append(s
[:end
])
283 if s
[end
] in string
.whitespace
:
284 # unescaped, unquoted whitespace: now
285 # we definitely have a word delimiter
286 words
.append(s
[:end
])
291 # preserve whatever is being escaped;
292 # will become part of the current word
293 s
= s
[:end
] + s
[end
+ 1 :]
297 if s
[end
] == "'": # slurp singly-quoted string
298 m
= _squote_re
.match(s
, end
)
299 elif s
[end
] == '"': # slurp doubly-quoted string
300 m
= _dquote_re
.match(s
, end
)
302 raise RuntimeError("this can't happen (bad char '%c')" % s
[end
])
305 raise ValueError("bad string (mismatched %s quotes?)" % s
[end
])
307 (beg
, end
) = m
.span()
308 s
= s
[:beg
] + s
[beg
+ 1 : end
- 1] + s
[end
:]
321 def execute(func
, args
, msg
=None, verbose
=0, dry_run
=0):
322 """Perform some action that affects the outside world (eg. by
323 writing to the filesystem). Such actions are special because they
324 are disabled by the 'dry_run' flag. This method takes care of all
325 that bureaucracy for you; all you have to do is supply the
326 function to call and an argument tuple for it (to embody the
327 "external action" being performed), and an optional message to
331 msg
= "{}{!r}".format(func
.__name
__, args
)
332 if msg
[-2:] == ',)': # correct for singleton tuple
333 msg
= msg
[0:-2] + ')'
341 """Convert a string representation of truth to true (1) or false (0).
343 True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
344 are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if
345 'val' is anything else.
348 if val
in ('y', 'yes', 't', 'true', 'on', '1'):
350 elif val
in ('n', 'no', 'f', 'false', 'off', '0'):
353 raise ValueError("invalid truth value {!r}".format(val
))
356 def byte_compile( # noqa: C901
366 """Byte-compile a collection of Python source files to .pyc
367 files in a __pycache__ subdirectory. 'py_files' is a list
368 of files to compile; any files that don't end in ".py" are silently
369 skipped. 'optimize' must be one of the following:
371 1 - normal optimization (like "python -O")
372 2 - extra optimization (like "python -OO")
373 If 'force' is true, all files are recompiled regardless of
376 The source filename encoded in each bytecode file defaults to the
377 filenames listed in 'py_files'; you can modify these with 'prefix' and
378 'basedir'. 'prefix' is a string that will be stripped off of each
379 source filename, and 'base_dir' is a directory name that will be
380 prepended (after 'prefix' is stripped). You can supply either or both
381 (or neither) of 'prefix' and 'base_dir', as you wish.
383 If 'dry_run' is true, doesn't actually do anything that would
384 affect the filesystem.
386 Byte-compilation is either done directly in this interpreter process
387 with the standard py_compile module, or indirectly by writing a
388 temporary script and executing it. Normally, you should let
389 'byte_compile()' figure out to use direct compilation or not (see
390 the source for details). The 'direct' flag is used by the script
391 generated in indirect mode; unless you know what you're doing, leave
395 # nothing is done if sys.dont_write_bytecode is True
396 if sys
.dont_write_bytecode
:
397 raise DistutilsByteCompileError('byte-compiling is disabled.')
399 # First, if the caller didn't force us into direct or indirect mode,
400 # figure out which mode we should be in. We take a conservative
401 # approach: choose direct mode *only* if the current interpreter is
402 # in debug mode and optimize is 0. If we're not in debug mode (-O
403 # or -OO), we don't know which level of optimization this
404 # interpreter is running with, so we can't do direct
405 # byte-compilation and be certain that it's the right thing. Thus,
406 # always compile indirectly if the current interpreter is in either
407 # optimize mode, or if either optimization level was requested by
410 direct
= __debug__
and optimize
== 0
412 # "Indirect" byte-compilation: write a temporary script and then
413 # run it with the appropriate flags.
416 from tempfile
import mkstemp
418 (script_fd
, script_name
) = mkstemp(".py")
420 from tempfile
import mktemp
422 (script_fd
, script_name
) = None, mktemp(".py")
423 log
.info("writing byte-compilation script '%s'", script_name
)
425 if script_fd
is not None:
426 script
= os
.fdopen(script_fd
, "w")
428 script
= open(script_name
, "w")
433 from distutils.util import byte_compile
438 # XXX would be nice to write absolute filenames, just for
439 # safety's sake (script should be more robust in the face of
440 # chdir'ing before running it). But this requires abspath'ing
441 # 'prefix' as well, and that breaks the hack in build_lib's
442 # 'byte_compile()' method that carefully tacks on a trailing
443 # slash (os.sep really) to make sure the prefix here is "just
444 # right". This whole prefix business is rather delicate -- the
445 # problem is that it's really a directory, but I'm treating it
446 # as a dumb string, so trailing slashes and so forth matter.
448 script
.write(",\n".join(map(repr, py_files
)) + "]\n")
451 byte_compile(files, optimize=%r, force=%r,
452 prefix=%r, base_dir=%r,
453 verbose=%r, dry_run=0,
456 % (optimize
, force
, prefix
, base_dir
, verbose
)
459 cmd
= [sys
.executable
]
460 cmd
.extend(subprocess
._optim
_args
_from
_interpreter
_flags
())
461 cmd
.append(script_name
)
462 spawn(cmd
, dry_run
=dry_run
)
463 execute(os
.remove
, (script_name
,), "removing %s" % script_name
, dry_run
=dry_run
)
465 # "Direct" byte-compilation: use the py_compile module to compile
466 # right here, right now. Note that the script generated in indirect
467 # mode simply calls 'byte_compile()' in direct mode, a weird sort of
468 # cross-process recursion. Hey, it works!
470 from py_compile
import compile
472 for file in py_files
:
473 if file[-3:] != ".py":
474 # This lets us be lazy and not filter filenames in
475 # the "install_lib" command.
478 # Terminology from the py_compile module:
479 # cfile - byte-compiled file
480 # dfile - purported source filename (same as 'file' by default)
482 opt
= '' if optimize
== 0 else optimize
483 cfile
= importlib
.util
.cache_from_source(file, optimization
=opt
)
485 cfile
= importlib
.util
.cache_from_source(file)
488 if file[: len(prefix
)] != prefix
:
490 "invalid prefix: filename %r doesn't start with %r"
493 dfile
= dfile
[len(prefix
) :]
495 dfile
= os
.path
.join(base_dir
, dfile
)
497 cfile_base
= os
.path
.basename(cfile
)
499 if force
or newer(file, cfile
):
500 log
.info("byte-compiling %s to %s", file, cfile_base
)
502 compile(file, cfile
, dfile
)
504 log
.debug("skipping byte-compilation of %s to %s", file, cfile_base
)
507 def rfc822_escape(header
):
508 """Return a version of the string escaped for inclusion in an
509 RFC-822 header, by ensuring there are 8 spaces space after each newline.
511 lines
= header
.split('\n')
513 return sep
.join(lines
)