util.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. """distutils.util
  2. Miscellaneous utility functions -- anything that doesn't fit into
  3. one of the other *util.py modules.
  4. """
  5. import os
  6. import re
  7. import importlib.util
  8. import string
  9. import sys
  10. from distutils.errors import DistutilsPlatformError
  11. from distutils.dep_util import newer
  12. from distutils.spawn import spawn
  13. from distutils import log
  14. from distutils.errors import DistutilsByteCompileError
  15. from .py35compat import _optim_args_from_interpreter_flags
  16. def get_host_platform():
  17. """Return a string that identifies the current platform. This is used mainly to
  18. distinguish platform-specific build directories and platform-specific built
  19. distributions. Typically includes the OS name and version and the
  20. architecture (as supplied by 'os.uname()'), although the exact information
  21. included depends on the OS; eg. on Linux, the kernel version isn't
  22. particularly important.
  23. Examples of returned values:
  24. linux-i586
  25. linux-alpha (?)
  26. solaris-2.6-sun4u
  27. Windows will return one of:
  28. win-amd64 (64bit Windows on AMD64 (aka x86_64, Intel64, EM64T, etc)
  29. win32 (all others - specifically, sys.platform is returned)
  30. For other non-POSIX platforms, currently just returns 'sys.platform'.
  31. """
  32. if os.name == 'nt':
  33. if 'amd64' in sys.version.lower():
  34. return 'win-amd64'
  35. if '(arm)' in sys.version.lower():
  36. return 'win-arm32'
  37. if '(arm64)' in sys.version.lower():
  38. return 'win-arm64'
  39. return sys.platform
  40. # Set for cross builds explicitly
  41. if "_PYTHON_HOST_PLATFORM" in os.environ:
  42. return os.environ["_PYTHON_HOST_PLATFORM"]
  43. if os.name != "posix" or not hasattr(os, 'uname'):
  44. # XXX what about the architecture? NT is Intel or Alpha,
  45. # Mac OS is M68k or PPC, etc.
  46. return sys.platform
  47. # Try to distinguish various flavours of Unix
  48. (osname, host, release, version, machine) = os.uname()
  49. # Convert the OS name to lowercase, remove '/' characters, and translate
  50. # spaces (for "Power Macintosh")
  51. osname = osname.lower().replace('/', '')
  52. machine = machine.replace(' ', '_')
  53. machine = machine.replace('/', '-')
  54. if osname[:5] == "linux":
  55. # At least on Linux/Intel, 'machine' is the processor --
  56. # i386, etc.
  57. # XXX what about Alpha, SPARC, etc?
  58. return "%s-%s" % (osname, machine)
  59. elif osname[:5] == "sunos":
  60. if release[0] >= "5": # SunOS 5 == Solaris 2
  61. osname = "solaris"
  62. release = "%d.%s" % (int(release[0]) - 3, release[2:])
  63. # We can't use "platform.architecture()[0]" because a
  64. # bootstrap problem. We use a dict to get an error
  65. # if some suspicious happens.
  66. bitness = {2147483647:"32bit", 9223372036854775807:"64bit"}
  67. machine += ".%s" % bitness[sys.maxsize]
  68. # fall through to standard osname-release-machine representation
  69. elif osname[:3] == "aix":
  70. from .py38compat import aix_platform
  71. return aix_platform(osname, version, release)
  72. elif osname[:6] == "cygwin":
  73. osname = "cygwin"
  74. rel_re = re.compile (r'[\d.]+', re.ASCII)
  75. m = rel_re.match(release)
  76. if m:
  77. release = m.group()
  78. elif osname[:6] == "darwin":
  79. import _osx_support, distutils.sysconfig
  80. osname, release, machine = _osx_support.get_platform_osx(
  81. distutils.sysconfig.get_config_vars(),
  82. osname, release, machine)
  83. return "%s-%s-%s" % (osname, release, machine)
  84. def get_platform():
  85. if os.name == 'nt':
  86. TARGET_TO_PLAT = {
  87. 'x86' : 'win32',
  88. 'x64' : 'win-amd64',
  89. 'arm' : 'win-arm32',
  90. 'arm64': 'win-arm64',
  91. }
  92. return TARGET_TO_PLAT.get(os.environ.get('VSCMD_ARG_TGT_ARCH')) or get_host_platform()
  93. else:
  94. return get_host_platform()
  95. if sys.platform == 'darwin':
  96. _syscfg_macosx_ver = None # cache the version pulled from sysconfig
  97. MACOSX_VERSION_VAR = 'MACOSX_DEPLOYMENT_TARGET'
  98. def _clear_cached_macosx_ver():
  99. """For testing only. Do not call."""
  100. global _syscfg_macosx_ver
  101. _syscfg_macosx_ver = None
  102. def get_macosx_target_ver_from_syscfg():
  103. """Get the version of macOS latched in the Python interpreter configuration.
  104. Returns the version as a string or None if can't obtain one. Cached."""
  105. global _syscfg_macosx_ver
  106. if _syscfg_macosx_ver is None:
  107. from distutils import sysconfig
  108. ver = sysconfig.get_config_var(MACOSX_VERSION_VAR) or ''
  109. if ver:
  110. _syscfg_macosx_ver = ver
  111. return _syscfg_macosx_ver
  112. def get_macosx_target_ver():
  113. """Return the version of macOS for which we are building.
  114. The target version defaults to the version in sysconfig latched at time
  115. the Python interpreter was built, unless overridden by an environment
  116. variable. If neither source has a value, then None is returned"""
  117. syscfg_ver = get_macosx_target_ver_from_syscfg()
  118. env_ver = os.environ.get(MACOSX_VERSION_VAR)
  119. if env_ver:
  120. # Validate overridden version against sysconfig version, if have both.
  121. # Ensure that the deployment target of the build process is not less
  122. # than 10.3 if the interpreter was built for 10.3 or later. This
  123. # ensures extension modules are built with correct compatibility
  124. # values, specifically LDSHARED which can use
  125. # '-undefined dynamic_lookup' which only works on >= 10.3.
  126. if syscfg_ver and split_version(syscfg_ver) >= [10, 3] and \
  127. split_version(env_ver) < [10, 3]:
  128. my_msg = ('$' + MACOSX_VERSION_VAR + ' mismatch: '
  129. 'now "%s" but "%s" during configure; '
  130. 'must use 10.3 or later'
  131. % (env_ver, syscfg_ver))
  132. raise DistutilsPlatformError(my_msg)
  133. return env_ver
  134. return syscfg_ver
  135. def split_version(s):
  136. """Convert a dot-separated string into a list of numbers for comparisons"""
  137. return [int(n) for n in s.split('.')]
  138. def convert_path (pathname):
  139. """Return 'pathname' as a name that will work on the native filesystem,
  140. i.e. split it on '/' and put it back together again using the current
  141. directory separator. Needed because filenames in the setup script are
  142. always supplied in Unix style, and have to be converted to the local
  143. convention before we can actually use them in the filesystem. Raises
  144. ValueError on non-Unix-ish systems if 'pathname' either starts or
  145. ends with a slash.
  146. """
  147. if os.sep == '/':
  148. return pathname
  149. if not pathname:
  150. return pathname
  151. if pathname[0] == '/':
  152. raise ValueError("path '%s' cannot be absolute" % pathname)
  153. if pathname[-1] == '/':
  154. raise ValueError("path '%s' cannot end with '/'" % pathname)
  155. paths = pathname.split('/')
  156. while '.' in paths:
  157. paths.remove('.')
  158. if not paths:
  159. return os.curdir
  160. return os.path.join(*paths)
  161. # convert_path ()
  162. def change_root (new_root, pathname):
  163. """Return 'pathname' with 'new_root' prepended. If 'pathname' is
  164. relative, this is equivalent to "os.path.join(new_root,pathname)".
  165. Otherwise, it requires making 'pathname' relative and then joining the
  166. two, which is tricky on DOS/Windows and Mac OS.
  167. """
  168. if os.name == 'posix':
  169. if not os.path.isabs(pathname):
  170. return os.path.join(new_root, pathname)
  171. else:
  172. return os.path.join(new_root, pathname[1:])
  173. elif os.name == 'nt':
  174. (drive, path) = os.path.splitdrive(pathname)
  175. if path[0] == '\\':
  176. path = path[1:]
  177. return os.path.join(new_root, path)
  178. else:
  179. raise DistutilsPlatformError("nothing known about platform '%s'" % os.name)
  180. _environ_checked = 0
  181. def check_environ ():
  182. """Ensure that 'os.environ' has all the environment variables we
  183. guarantee that users can use in config files, command-line options,
  184. etc. Currently this includes:
  185. HOME - user's home directory (Unix only)
  186. PLAT - description of the current platform, including hardware
  187. and OS (see 'get_platform()')
  188. """
  189. global _environ_checked
  190. if _environ_checked:
  191. return
  192. if os.name == 'posix' and 'HOME' not in os.environ:
  193. try:
  194. import pwd
  195. os.environ['HOME'] = pwd.getpwuid(os.getuid())[5]
  196. except (ImportError, KeyError):
  197. # bpo-10496: if the current user identifier doesn't exist in the
  198. # password database, do nothing
  199. pass
  200. if 'PLAT' not in os.environ:
  201. os.environ['PLAT'] = get_platform()
  202. _environ_checked = 1
  203. def subst_vars (s, local_vars):
  204. """
  205. Perform variable substitution on 'string'.
  206. Variables are indicated by format-style braces ("{var}").
  207. Variable is substituted by the value found in the 'local_vars'
  208. dictionary or in 'os.environ' if it's not in 'local_vars'.
  209. 'os.environ' is first checked/augmented to guarantee that it contains
  210. certain values: see 'check_environ()'. Raise ValueError for any
  211. variables not found in either 'local_vars' or 'os.environ'.
  212. """
  213. check_environ()
  214. lookup = dict(os.environ)
  215. lookup.update((name, str(value)) for name, value in local_vars.items())
  216. try:
  217. return _subst_compat(s).format_map(lookup)
  218. except KeyError as var:
  219. raise ValueError(f"invalid variable {var}")
  220. # subst_vars ()
  221. def _subst_compat(s):
  222. """
  223. Replace shell/Perl-style variable substitution with
  224. format-style. For compatibility.
  225. """
  226. def _subst(match):
  227. return f'{{{match.group(1)}}}'
  228. repl = re.sub(r'\$([a-zA-Z_][a-zA-Z_0-9]*)', _subst, s)
  229. if repl != s:
  230. import warnings
  231. warnings.warn(
  232. "shell/Perl-style substitions are deprecated",
  233. DeprecationWarning,
  234. )
  235. return repl
  236. def grok_environment_error (exc, prefix="error: "):
  237. # Function kept for backward compatibility.
  238. # Used to try clever things with EnvironmentErrors,
  239. # but nowadays str(exception) produces good messages.
  240. return prefix + str(exc)
  241. # Needed by 'split_quoted()'
  242. _wordchars_re = _squote_re = _dquote_re = None
  243. def _init_regex():
  244. global _wordchars_re, _squote_re, _dquote_re
  245. _wordchars_re = re.compile(r'[^\\\'\"%s ]*' % string.whitespace)
  246. _squote_re = re.compile(r"'(?:[^'\\]|\\.)*'")
  247. _dquote_re = re.compile(r'"(?:[^"\\]|\\.)*"')
  248. def split_quoted (s):
  249. """Split a string up according to Unix shell-like rules for quotes and
  250. backslashes. In short: words are delimited by spaces, as long as those
  251. spaces are not escaped by a backslash, or inside a quoted string.
  252. Single and double quotes are equivalent, and the quote characters can
  253. be backslash-escaped. The backslash is stripped from any two-character
  254. escape sequence, leaving only the escaped character. The quote
  255. characters are stripped from any quoted string. Returns a list of
  256. words.
  257. """
  258. # This is a nice algorithm for splitting up a single string, since it
  259. # doesn't require character-by-character examination. It was a little
  260. # bit of a brain-bender to get it working right, though...
  261. if _wordchars_re is None: _init_regex()
  262. s = s.strip()
  263. words = []
  264. pos = 0
  265. while s:
  266. m = _wordchars_re.match(s, pos)
  267. end = m.end()
  268. if end == len(s):
  269. words.append(s[:end])
  270. break
  271. if s[end] in string.whitespace: # unescaped, unquoted whitespace: now
  272. words.append(s[:end]) # we definitely have a word delimiter
  273. s = s[end:].lstrip()
  274. pos = 0
  275. elif s[end] == '\\': # preserve whatever is being escaped;
  276. # will become part of the current word
  277. s = s[:end] + s[end+1:]
  278. pos = end+1
  279. else:
  280. if s[end] == "'": # slurp singly-quoted string
  281. m = _squote_re.match(s, end)
  282. elif s[end] == '"': # slurp doubly-quoted string
  283. m = _dquote_re.match(s, end)
  284. else:
  285. raise RuntimeError("this can't happen (bad char '%c')" % s[end])
  286. if m is None:
  287. raise ValueError("bad string (mismatched %s quotes?)" % s[end])
  288. (beg, end) = m.span()
  289. s = s[:beg] + s[beg+1:end-1] + s[end:]
  290. pos = m.end() - 2
  291. if pos >= len(s):
  292. words.append(s)
  293. break
  294. return words
  295. # split_quoted ()
  296. def execute (func, args, msg=None, verbose=0, dry_run=0):
  297. """Perform some action that affects the outside world (eg. by
  298. writing to the filesystem). Such actions are special because they
  299. are disabled by the 'dry_run' flag. This method takes care of all
  300. that bureaucracy for you; all you have to do is supply the
  301. function to call and an argument tuple for it (to embody the
  302. "external action" being performed), and an optional message to
  303. print.
  304. """
  305. if msg is None:
  306. msg = "%s%r" % (func.__name__, args)
  307. if msg[-2:] == ',)': # correct for singleton tuple
  308. msg = msg[0:-2] + ')'
  309. log.info(msg)
  310. if not dry_run:
  311. func(*args)
  312. def strtobool (val):
  313. """Convert a string representation of truth to true (1) or false (0).
  314. True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
  315. are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if
  316. 'val' is anything else.
  317. """
  318. val = val.lower()
  319. if val in ('y', 'yes', 't', 'true', 'on', '1'):
  320. return 1
  321. elif val in ('n', 'no', 'f', 'false', 'off', '0'):
  322. return 0
  323. else:
  324. raise ValueError("invalid truth value %r" % (val,))
  325. def byte_compile (py_files,
  326. optimize=0, force=0,
  327. prefix=None, base_dir=None,
  328. verbose=1, dry_run=0,
  329. direct=None):
  330. """Byte-compile a collection of Python source files to .pyc
  331. files in a __pycache__ subdirectory. 'py_files' is a list
  332. of files to compile; any files that don't end in ".py" are silently
  333. skipped. 'optimize' must be one of the following:
  334. 0 - don't optimize
  335. 1 - normal optimization (like "python -O")
  336. 2 - extra optimization (like "python -OO")
  337. If 'force' is true, all files are recompiled regardless of
  338. timestamps.
  339. The source filename encoded in each bytecode file defaults to the
  340. filenames listed in 'py_files'; you can modify these with 'prefix' and
  341. 'basedir'. 'prefix' is a string that will be stripped off of each
  342. source filename, and 'base_dir' is a directory name that will be
  343. prepended (after 'prefix' is stripped). You can supply either or both
  344. (or neither) of 'prefix' and 'base_dir', as you wish.
  345. If 'dry_run' is true, doesn't actually do anything that would
  346. affect the filesystem.
  347. Byte-compilation is either done directly in this interpreter process
  348. with the standard py_compile module, or indirectly by writing a
  349. temporary script and executing it. Normally, you should let
  350. 'byte_compile()' figure out to use direct compilation or not (see
  351. the source for details). The 'direct' flag is used by the script
  352. generated in indirect mode; unless you know what you're doing, leave
  353. it set to None.
  354. """
  355. # Late import to fix a bootstrap issue: _posixsubprocess is built by
  356. # setup.py, but setup.py uses distutils.
  357. import subprocess
  358. # nothing is done if sys.dont_write_bytecode is True
  359. if sys.dont_write_bytecode:
  360. raise DistutilsByteCompileError('byte-compiling is disabled.')
  361. # First, if the caller didn't force us into direct or indirect mode,
  362. # figure out which mode we should be in. We take a conservative
  363. # approach: choose direct mode *only* if the current interpreter is
  364. # in debug mode and optimize is 0. If we're not in debug mode (-O
  365. # or -OO), we don't know which level of optimization this
  366. # interpreter is running with, so we can't do direct
  367. # byte-compilation and be certain that it's the right thing. Thus,
  368. # always compile indirectly if the current interpreter is in either
  369. # optimize mode, or if either optimization level was requested by
  370. # the caller.
  371. if direct is None:
  372. direct = (__debug__ and optimize == 0)
  373. # "Indirect" byte-compilation: write a temporary script and then
  374. # run it with the appropriate flags.
  375. if not direct:
  376. try:
  377. from tempfile import mkstemp
  378. (script_fd, script_name) = mkstemp(".py")
  379. except ImportError:
  380. from tempfile import mktemp
  381. (script_fd, script_name) = None, mktemp(".py")
  382. log.info("writing byte-compilation script '%s'", script_name)
  383. if not dry_run:
  384. if script_fd is not None:
  385. script = os.fdopen(script_fd, "w")
  386. else:
  387. script = open(script_name, "w")
  388. with script:
  389. script.write("""\
  390. from distutils.util import byte_compile
  391. files = [
  392. """)
  393. # XXX would be nice to write absolute filenames, just for
  394. # safety's sake (script should be more robust in the face of
  395. # chdir'ing before running it). But this requires abspath'ing
  396. # 'prefix' as well, and that breaks the hack in build_lib's
  397. # 'byte_compile()' method that carefully tacks on a trailing
  398. # slash (os.sep really) to make sure the prefix here is "just
  399. # right". This whole prefix business is rather delicate -- the
  400. # problem is that it's really a directory, but I'm treating it
  401. # as a dumb string, so trailing slashes and so forth matter.
  402. #py_files = map(os.path.abspath, py_files)
  403. #if prefix:
  404. # prefix = os.path.abspath(prefix)
  405. script.write(",\n".join(map(repr, py_files)) + "]\n")
  406. script.write("""
  407. byte_compile(files, optimize=%r, force=%r,
  408. prefix=%r, base_dir=%r,
  409. verbose=%r, dry_run=0,
  410. direct=1)
  411. """ % (optimize, force, prefix, base_dir, verbose))
  412. cmd = [sys.executable]
  413. cmd.extend(_optim_args_from_interpreter_flags())
  414. cmd.append(script_name)
  415. spawn(cmd, dry_run=dry_run)
  416. execute(os.remove, (script_name,), "removing %s" % script_name,
  417. dry_run=dry_run)
  418. # "Direct" byte-compilation: use the py_compile module to compile
  419. # right here, right now. Note that the script generated in indirect
  420. # mode simply calls 'byte_compile()' in direct mode, a weird sort of
  421. # cross-process recursion. Hey, it works!
  422. else:
  423. from py_compile import compile
  424. for file in py_files:
  425. if file[-3:] != ".py":
  426. # This lets us be lazy and not filter filenames in
  427. # the "install_lib" command.
  428. continue
  429. # Terminology from the py_compile module:
  430. # cfile - byte-compiled file
  431. # dfile - purported source filename (same as 'file' by default)
  432. if optimize >= 0:
  433. opt = '' if optimize == 0 else optimize
  434. cfile = importlib.util.cache_from_source(
  435. file, optimization=opt)
  436. else:
  437. cfile = importlib.util.cache_from_source(file)
  438. dfile = file
  439. if prefix:
  440. if file[:len(prefix)] != prefix:
  441. raise ValueError("invalid prefix: filename %r doesn't start with %r"
  442. % (file, prefix))
  443. dfile = dfile[len(prefix):]
  444. if base_dir:
  445. dfile = os.path.join(base_dir, dfile)
  446. cfile_base = os.path.basename(cfile)
  447. if direct:
  448. if force or newer(file, cfile):
  449. log.info("byte-compiling %s to %s", file, cfile_base)
  450. if not dry_run:
  451. compile(file, cfile, dfile)
  452. else:
  453. log.debug("skipping byte-compilation of %s to %s",
  454. file, cfile_base)
  455. # byte_compile ()
  456. def rfc822_escape (header):
  457. """Return a version of the string escaped for inclusion in an
  458. RFC-822 header, by ensuring there are 8 spaces space after each newline.
  459. """
  460. lines = header.split('\n')
  461. sep = '\n' + 8 * ' '
  462. return sep.join(lines)