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