util.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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 overriden 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 overriden 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. """Perform shell/Perl-style variable substitution on 'string'. Every
  205. occurrence of '$' followed by a name is considered a variable, and
  206. variable is substituted by the value found in the 'local_vars'
  207. dictionary, or in 'os.environ' if it's not in 'local_vars'.
  208. 'os.environ' is first checked/augmented to guarantee that it contains
  209. certain values: see 'check_environ()'. Raise ValueError for any
  210. variables not found in either 'local_vars' or 'os.environ'.
  211. """
  212. check_environ()
  213. def _subst (match, local_vars=local_vars):
  214. var_name = match.group(1)
  215. if var_name in local_vars:
  216. return str(local_vars[var_name])
  217. else:
  218. return os.environ[var_name]
  219. try:
  220. return re.sub(r'\$([a-zA-Z_][a-zA-Z_0-9]*)', _subst, s)
  221. except KeyError as var:
  222. raise ValueError("invalid variable '$%s'" % var)
  223. # subst_vars ()
  224. def grok_environment_error (exc, prefix="error: "):
  225. # Function kept for backward compatibility.
  226. # Used to try clever things with EnvironmentErrors,
  227. # but nowadays str(exception) produces good messages.
  228. return prefix + str(exc)
  229. # Needed by 'split_quoted()'
  230. _wordchars_re = _squote_re = _dquote_re = None
  231. def _init_regex():
  232. global _wordchars_re, _squote_re, _dquote_re
  233. _wordchars_re = re.compile(r'[^\\\'\"%s ]*' % string.whitespace)
  234. _squote_re = re.compile(r"'(?:[^'\\]|\\.)*'")
  235. _dquote_re = re.compile(r'"(?:[^"\\]|\\.)*"')
  236. def split_quoted (s):
  237. """Split a string up according to Unix shell-like rules for quotes and
  238. backslashes. In short: words are delimited by spaces, as long as those
  239. spaces are not escaped by a backslash, or inside a quoted string.
  240. Single and double quotes are equivalent, and the quote characters can
  241. be backslash-escaped. The backslash is stripped from any two-character
  242. escape sequence, leaving only the escaped character. The quote
  243. characters are stripped from any quoted string. Returns a list of
  244. words.
  245. """
  246. # This is a nice algorithm for splitting up a single string, since it
  247. # doesn't require character-by-character examination. It was a little
  248. # bit of a brain-bender to get it working right, though...
  249. if _wordchars_re is None: _init_regex()
  250. s = s.strip()
  251. words = []
  252. pos = 0
  253. while s:
  254. m = _wordchars_re.match(s, pos)
  255. end = m.end()
  256. if end == len(s):
  257. words.append(s[:end])
  258. break
  259. if s[end] in string.whitespace: # unescaped, unquoted whitespace: now
  260. words.append(s[:end]) # we definitely have a word delimiter
  261. s = s[end:].lstrip()
  262. pos = 0
  263. elif s[end] == '\\': # preserve whatever is being escaped;
  264. # will become part of the current word
  265. s = s[:end] + s[end+1:]
  266. pos = end+1
  267. else:
  268. if s[end] == "'": # slurp singly-quoted string
  269. m = _squote_re.match(s, end)
  270. elif s[end] == '"': # slurp doubly-quoted string
  271. m = _dquote_re.match(s, end)
  272. else:
  273. raise RuntimeError("this can't happen (bad char '%c')" % s[end])
  274. if m is None:
  275. raise ValueError("bad string (mismatched %s quotes?)" % s[end])
  276. (beg, end) = m.span()
  277. s = s[:beg] + s[beg+1:end-1] + s[end:]
  278. pos = m.end() - 2
  279. if pos >= len(s):
  280. words.append(s)
  281. break
  282. return words
  283. # split_quoted ()
  284. def execute (func, args, msg=None, verbose=0, dry_run=0):
  285. """Perform some action that affects the outside world (eg. by
  286. writing to the filesystem). Such actions are special because they
  287. are disabled by the 'dry_run' flag. This method takes care of all
  288. that bureaucracy for you; all you have to do is supply the
  289. function to call and an argument tuple for it (to embody the
  290. "external action" being performed), and an optional message to
  291. print.
  292. """
  293. if msg is None:
  294. msg = "%s%r" % (func.__name__, args)
  295. if msg[-2:] == ',)': # correct for singleton tuple
  296. msg = msg[0:-2] + ')'
  297. log.info(msg)
  298. if not dry_run:
  299. func(*args)
  300. def strtobool (val):
  301. """Convert a string representation of truth to true (1) or false (0).
  302. True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
  303. are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if
  304. 'val' is anything else.
  305. """
  306. val = val.lower()
  307. if val in ('y', 'yes', 't', 'true', 'on', '1'):
  308. return 1
  309. elif val in ('n', 'no', 'f', 'false', 'off', '0'):
  310. return 0
  311. else:
  312. raise ValueError("invalid truth value %r" % (val,))
  313. def byte_compile (py_files,
  314. optimize=0, force=0,
  315. prefix=None, base_dir=None,
  316. verbose=1, dry_run=0,
  317. direct=None):
  318. """Byte-compile a collection of Python source files to .pyc
  319. files in a __pycache__ subdirectory. 'py_files' is a list
  320. of files to compile; any files that don't end in ".py" are silently
  321. skipped. 'optimize' must be one of the following:
  322. 0 - don't optimize
  323. 1 - normal optimization (like "python -O")
  324. 2 - extra optimization (like "python -OO")
  325. If 'force' is true, all files are recompiled regardless of
  326. timestamps.
  327. The source filename encoded in each bytecode file defaults to the
  328. filenames listed in 'py_files'; you can modify these with 'prefix' and
  329. 'basedir'. 'prefix' is a string that will be stripped off of each
  330. source filename, and 'base_dir' is a directory name that will be
  331. prepended (after 'prefix' is stripped). You can supply either or both
  332. (or neither) of 'prefix' and 'base_dir', as you wish.
  333. If 'dry_run' is true, doesn't actually do anything that would
  334. affect the filesystem.
  335. Byte-compilation is either done directly in this interpreter process
  336. with the standard py_compile module, or indirectly by writing a
  337. temporary script and executing it. Normally, you should let
  338. 'byte_compile()' figure out to use direct compilation or not (see
  339. the source for details). The 'direct' flag is used by the script
  340. generated in indirect mode; unless you know what you're doing, leave
  341. it set to None.
  342. """
  343. # Late import to fix a bootstrap issue: _posixsubprocess is built by
  344. # setup.py, but setup.py uses distutils.
  345. import subprocess
  346. # nothing is done if sys.dont_write_bytecode is True
  347. if sys.dont_write_bytecode:
  348. raise DistutilsByteCompileError('byte-compiling is disabled.')
  349. # First, if the caller didn't force us into direct or indirect mode,
  350. # figure out which mode we should be in. We take a conservative
  351. # approach: choose direct mode *only* if the current interpreter is
  352. # in debug mode and optimize is 0. If we're not in debug mode (-O
  353. # or -OO), we don't know which level of optimization this
  354. # interpreter is running with, so we can't do direct
  355. # byte-compilation and be certain that it's the right thing. Thus,
  356. # always compile indirectly if the current interpreter is in either
  357. # optimize mode, or if either optimization level was requested by
  358. # the caller.
  359. if direct is None:
  360. direct = (__debug__ and optimize == 0)
  361. # "Indirect" byte-compilation: write a temporary script and then
  362. # run it with the appropriate flags.
  363. if not direct:
  364. try:
  365. from tempfile import mkstemp
  366. (script_fd, script_name) = mkstemp(".py")
  367. except ImportError:
  368. from tempfile import mktemp
  369. (script_fd, script_name) = None, mktemp(".py")
  370. log.info("writing byte-compilation script '%s'", script_name)
  371. if not dry_run:
  372. if script_fd is not None:
  373. script = os.fdopen(script_fd, "w")
  374. else:
  375. script = open(script_name, "w")
  376. with script:
  377. script.write("""\
  378. from distutils.util import byte_compile
  379. files = [
  380. """)
  381. # XXX would be nice to write absolute filenames, just for
  382. # safety's sake (script should be more robust in the face of
  383. # chdir'ing before running it). But this requires abspath'ing
  384. # 'prefix' as well, and that breaks the hack in build_lib's
  385. # 'byte_compile()' method that carefully tacks on a trailing
  386. # slash (os.sep really) to make sure the prefix here is "just
  387. # right". This whole prefix business is rather delicate -- the
  388. # problem is that it's really a directory, but I'm treating it
  389. # as a dumb string, so trailing slashes and so forth matter.
  390. #py_files = map(os.path.abspath, py_files)
  391. #if prefix:
  392. # prefix = os.path.abspath(prefix)
  393. script.write(",\n".join(map(repr, py_files)) + "]\n")
  394. script.write("""
  395. byte_compile(files, optimize=%r, force=%r,
  396. prefix=%r, base_dir=%r,
  397. verbose=%r, dry_run=0,
  398. direct=1)
  399. """ % (optimize, force, prefix, base_dir, verbose))
  400. cmd = [sys.executable]
  401. cmd.extend(_optim_args_from_interpreter_flags())
  402. cmd.append(script_name)
  403. spawn(cmd, dry_run=dry_run)
  404. execute(os.remove, (script_name,), "removing %s" % script_name,
  405. dry_run=dry_run)
  406. # "Direct" byte-compilation: use the py_compile module to compile
  407. # right here, right now. Note that the script generated in indirect
  408. # mode simply calls 'byte_compile()' in direct mode, a weird sort of
  409. # cross-process recursion. Hey, it works!
  410. else:
  411. from py_compile import compile
  412. for file in py_files:
  413. if file[-3:] != ".py":
  414. # This lets us be lazy and not filter filenames in
  415. # the "install_lib" command.
  416. continue
  417. # Terminology from the py_compile module:
  418. # cfile - byte-compiled file
  419. # dfile - purported source filename (same as 'file' by default)
  420. if optimize >= 0:
  421. opt = '' if optimize == 0 else optimize
  422. cfile = importlib.util.cache_from_source(
  423. file, optimization=opt)
  424. else:
  425. cfile = importlib.util.cache_from_source(file)
  426. dfile = file
  427. if prefix:
  428. if file[:len(prefix)] != prefix:
  429. raise ValueError("invalid prefix: filename %r doesn't start with %r"
  430. % (file, prefix))
  431. dfile = dfile[len(prefix):]
  432. if base_dir:
  433. dfile = os.path.join(base_dir, dfile)
  434. cfile_base = os.path.basename(cfile)
  435. if direct:
  436. if force or newer(file, cfile):
  437. log.info("byte-compiling %s to %s", file, cfile_base)
  438. if not dry_run:
  439. compile(file, cfile, dfile)
  440. else:
  441. log.debug("skipping byte-compilation of %s to %s",
  442. file, cfile_base)
  443. # byte_compile ()
  444. def rfc822_escape (header):
  445. """Return a version of the string escaped for inclusion in an
  446. RFC-822 header, by ensuring there are 8 spaces space after each newline.
  447. """
  448. lines = header.split('\n')
  449. sep = '\n' + 8 * ' '
  450. return sep.join(lines)