Greg Ward | 2689e3d | 1999-03-22 14:52:19 +0000 | [diff] [blame] | 1 | """distutils.util |
| 2 | |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 3 | Miscellaneous utility functions -- anything that doesn't fit into |
Greg Ward | 4752769 | 2000-09-30 18:49:14 +0000 | [diff] [blame] | 4 | one of the other *util.py modules. |
| 5 | """ |
Greg Ward | 2689e3d | 1999-03-22 14:52:19 +0000 | [diff] [blame] | 6 | |
Greg Ward | 3ce77fd | 2000-03-02 01:49:45 +0000 | [diff] [blame] | 7 | __revision__ = "$Id$" |
Greg Ward | 2689e3d | 1999-03-22 14:52:19 +0000 | [diff] [blame] | 8 | |
Greg Ward | 1297b5c | 2000-09-30 20:37:56 +0000 | [diff] [blame] | 9 | import sys, os, string, re |
Tarek Ziadé | a99dedf | 2009-07-16 15:35:45 +0000 | [diff] [blame] | 10 | |
Greg Ward | 1297b5c | 2000-09-30 20:37:56 +0000 | [diff] [blame] | 11 | from distutils.errors import DistutilsPlatformError |
| 12 | from distutils.dep_util import newer |
Tarek Ziadé | a99dedf | 2009-07-16 15:35:45 +0000 | [diff] [blame] | 13 | from distutils.spawn import spawn, find_executable |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 14 | from distutils import log |
Tarek Ziadé | a99dedf | 2009-07-16 15:35:45 +0000 | [diff] [blame] | 15 | from distutils.version import LooseVersion |
Tarek Ziadé | b9c1cfc | 2009-10-24 15:10:37 +0000 | [diff] [blame] | 16 | from distutils.errors import DistutilsByteCompileError |
Greg Ward | aa458bc | 2000-04-22 15:14:58 +0000 | [diff] [blame] | 17 | |
Tarek Ziadé | 5633a80 | 2010-01-23 09:23:15 +0000 | [diff] [blame] | 18 | _sysconfig = __import__('sysconfig') |
Greg Ward | 5091929 | 2000-03-07 03:27:08 +0000 | [diff] [blame] | 19 | |
Tarek Ziadé | 0276c7a | 2010-01-26 21:21:54 +0000 | [diff] [blame^] | 20 | # kept for backward compatibility |
| 21 | # since this API was relocated |
| 22 | get_platform = _sysconfig.get_platform |
| 23 | |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 24 | def convert_path(pathname): |
| 25 | """Return 'pathname' as a name that will work on the native filesystem. |
Greg Ward | 5091929 | 2000-03-07 03:27:08 +0000 | [diff] [blame] | 26 | |
Greg Ward | b8b263b | 2000-09-30 18:40:42 +0000 | [diff] [blame] | 27 | i.e. split it on '/' and put it back together again using the current |
| 28 | directory separator. Needed because filenames in the setup script are |
| 29 | always supplied in Unix style, and have to be converted to the local |
| 30 | convention before we can actually use them in the filesystem. Raises |
Greg Ward | 4752769 | 2000-09-30 18:49:14 +0000 | [diff] [blame] | 31 | ValueError on non-Unix-ish systems if 'pathname' either starts or |
| 32 | ends with a slash. |
Greg Ward | b8b263b | 2000-09-30 18:40:42 +0000 | [diff] [blame] | 33 | """ |
Greg Ward | 7ec0535 | 2000-09-22 01:05:43 +0000 | [diff] [blame] | 34 | if os.sep == '/': |
| 35 | return pathname |
Neal Norwitz | b0df6a1 | 2002-08-13 17:42:57 +0000 | [diff] [blame] | 36 | if not pathname: |
| 37 | return pathname |
| 38 | if pathname[0] == '/': |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 39 | raise ValueError("path '%s' cannot be absolute" % pathname) |
Neal Norwitz | b0df6a1 | 2002-08-13 17:42:57 +0000 | [diff] [blame] | 40 | if pathname[-1] == '/': |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 41 | raise ValueError("path '%s' cannot end with '/'" % pathname) |
Greg Ward | 7ec0535 | 2000-09-22 01:05:43 +0000 | [diff] [blame] | 42 | |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 43 | paths = pathname.split('/') |
Jack Jansen | b4cd5c1 | 2001-01-28 12:23:32 +0000 | [diff] [blame] | 44 | while '.' in paths: |
| 45 | paths.remove('.') |
| 46 | if not paths: |
| 47 | return os.curdir |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 48 | return os.path.join(*paths) |
Greg Ward | 1b4ede5 | 2000-03-22 00:22:44 +0000 | [diff] [blame] | 49 | |
| 50 | |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 51 | def change_root(new_root, pathname): |
| 52 | """Return 'pathname' with 'new_root' prepended. |
| 53 | |
| 54 | If 'pathname' is relative, this is equivalent to |
| 55 | "os.path.join(new_root,pathname)". |
Greg Ward | 67f75d4 | 2000-04-27 01:53:46 +0000 | [diff] [blame] | 56 | Otherwise, it requires making 'pathname' relative and then joining the |
Greg Ward | 4b46ef9 | 2000-05-31 02:14:32 +0000 | [diff] [blame] | 57 | two, which is tricky on DOS/Windows and Mac OS. |
| 58 | """ |
| 59 | if os.name == 'posix': |
Greg Ward | be86bde | 2000-09-26 01:56:15 +0000 | [diff] [blame] | 60 | if not os.path.isabs(pathname): |
| 61 | return os.path.join(new_root, pathname) |
Greg Ward | 4b46ef9 | 2000-05-31 02:14:32 +0000 | [diff] [blame] | 62 | else: |
Greg Ward | be86bde | 2000-09-26 01:56:15 +0000 | [diff] [blame] | 63 | return os.path.join(new_root, pathname[1:]) |
Greg Ward | 67f75d4 | 2000-04-27 01:53:46 +0000 | [diff] [blame] | 64 | |
| 65 | elif os.name == 'nt': |
Greg Ward | be86bde | 2000-09-26 01:56:15 +0000 | [diff] [blame] | 66 | (drive, path) = os.path.splitdrive(pathname) |
Greg Ward | 4b46ef9 | 2000-05-31 02:14:32 +0000 | [diff] [blame] | 67 | if path[0] == '\\': |
| 68 | path = path[1:] |
Greg Ward | be86bde | 2000-09-26 01:56:15 +0000 | [diff] [blame] | 69 | return os.path.join(new_root, path) |
Greg Ward | 67f75d4 | 2000-04-27 01:53:46 +0000 | [diff] [blame] | 70 | |
Marc-André Lemburg | 2544f51 | 2002-01-31 18:56:00 +0000 | [diff] [blame] | 71 | elif os.name == 'os2': |
| 72 | (drive, path) = os.path.splitdrive(pathname) |
| 73 | if path[0] == os.sep: |
| 74 | path = path[1:] |
| 75 | return os.path.join(new_root, path) |
| 76 | |
Greg Ward | 67f75d4 | 2000-04-27 01:53:46 +0000 | [diff] [blame] | 77 | elif os.name == 'mac': |
Greg Ward | f585574 | 2000-09-21 01:23:35 +0000 | [diff] [blame] | 78 | if not os.path.isabs(pathname): |
| 79 | return os.path.join(new_root, pathname) |
| 80 | else: |
| 81 | # Chop off volume name from start of path |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 82 | elements = pathname.split(":", 1) |
Greg Ward | f585574 | 2000-09-21 01:23:35 +0000 | [diff] [blame] | 83 | pathname = ":" + elements[1] |
| 84 | return os.path.join(new_root, pathname) |
Greg Ward | 67f75d4 | 2000-04-27 01:53:46 +0000 | [diff] [blame] | 85 | |
| 86 | else: |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 87 | raise DistutilsPlatformError("nothing known about " |
| 88 | "platform '%s'" % os.name) |
Greg Ward | 67f75d4 | 2000-04-27 01:53:46 +0000 | [diff] [blame] | 89 | |
Gregory P. Smith | e7e35ac | 2000-05-12 00:40:00 +0000 | [diff] [blame] | 90 | _environ_checked = 0 |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 91 | |
| 92 | def check_environ(): |
| 93 | """Ensure that 'os.environ' has all the environment variables needed. |
| 94 | |
| 95 | We guarantee that users can use in config files, command-line options, |
Greg Ward | b8b263b | 2000-09-30 18:40:42 +0000 | [diff] [blame] | 96 | etc. Currently this includes: |
| 97 | HOME - user's home directory (Unix only) |
| 98 | PLAT - description of the current platform, including hardware |
| 99 | and OS (see 'get_platform()') |
Greg Ward | 1b4ede5 | 2000-03-22 00:22:44 +0000 | [diff] [blame] | 100 | """ |
Gregory P. Smith | e7e35ac | 2000-05-12 00:40:00 +0000 | [diff] [blame] | 101 | global _environ_checked |
| 102 | if _environ_checked: |
| 103 | return |
| 104 | |
Guido van Rossum | 8bc0965 | 2008-02-21 18:18:37 +0000 | [diff] [blame] | 105 | if os.name == 'posix' and 'HOME' not in os.environ: |
Greg Ward | 1b4ede5 | 2000-03-22 00:22:44 +0000 | [diff] [blame] | 106 | import pwd |
Greg Ward | be86bde | 2000-09-26 01:56:15 +0000 | [diff] [blame] | 107 | os.environ['HOME'] = pwd.getpwuid(os.getuid())[5] |
Greg Ward | 1b4ede5 | 2000-03-22 00:22:44 +0000 | [diff] [blame] | 108 | |
Guido van Rossum | 8bc0965 | 2008-02-21 18:18:37 +0000 | [diff] [blame] | 109 | if 'PLAT' not in os.environ: |
Tarek Ziadé | 5633a80 | 2010-01-23 09:23:15 +0000 | [diff] [blame] | 110 | os.environ['PLAT'] = _sysconfig.get_platform() |
Greg Ward | 1b4ede5 | 2000-03-22 00:22:44 +0000 | [diff] [blame] | 111 | |
Gregory P. Smith | e7e35ac | 2000-05-12 00:40:00 +0000 | [diff] [blame] | 112 | _environ_checked = 1 |
| 113 | |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 114 | def subst_vars(s, local_vars): |
| 115 | """Perform shell/Perl-style variable substitution on 'string'. |
Greg Ward | 1b4ede5 | 2000-03-22 00:22:44 +0000 | [diff] [blame] | 116 | |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 117 | Every occurrence of '$' followed by a name is considered a variable, and |
Greg Ward | 4752769 | 2000-09-30 18:49:14 +0000 | [diff] [blame] | 118 | variable is substituted by the value found in the 'local_vars' |
| 119 | dictionary, or in 'os.environ' if it's not in 'local_vars'. |
| 120 | 'os.environ' is first checked/augmented to guarantee that it contains |
| 121 | certain values: see 'check_environ()'. Raise ValueError for any |
| 122 | variables not found in either 'local_vars' or 'os.environ'. |
Greg Ward | b8b263b | 2000-09-30 18:40:42 +0000 | [diff] [blame] | 123 | """ |
Greg Ward | be86bde | 2000-09-26 01:56:15 +0000 | [diff] [blame] | 124 | check_environ() |
Greg Ward | 1b4ede5 | 2000-03-22 00:22:44 +0000 | [diff] [blame] | 125 | def _subst (match, local_vars=local_vars): |
| 126 | var_name = match.group(1) |
Guido van Rossum | 8bc0965 | 2008-02-21 18:18:37 +0000 | [diff] [blame] | 127 | if var_name in local_vars: |
Greg Ward | be86bde | 2000-09-26 01:56:15 +0000 | [diff] [blame] | 128 | return str(local_vars[var_name]) |
Greg Ward | 1b4ede5 | 2000-03-22 00:22:44 +0000 | [diff] [blame] | 129 | else: |
| 130 | return os.environ[var_name] |
| 131 | |
Greg Ward | 4752769 | 2000-09-30 18:49:14 +0000 | [diff] [blame] | 132 | try: |
Jeremy Hylton | 5e2d076 | 2001-01-25 20:10:32 +0000 | [diff] [blame] | 133 | return re.sub(r'\$([a-zA-Z_][a-zA-Z_0-9]*)', _subst, s) |
Greg Ward | 4752769 | 2000-09-30 18:49:14 +0000 | [diff] [blame] | 134 | except KeyError, var: |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 135 | raise ValueError("invalid variable '$%s'" % var) |
Greg Ward | 1b4ede5 | 2000-03-22 00:22:44 +0000 | [diff] [blame] | 136 | |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 137 | def grok_environment_error(exc, prefix="error: "): |
| 138 | """Generate a useful error message from an EnvironmentError. |
Greg Ward | 7c1a6d4 | 2000-03-29 02:48:40 +0000 | [diff] [blame] | 139 | |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 140 | This will generate an IOError or an OSError exception object. |
| 141 | Handles Python 1.5.1 and 1.5.2 styles, and |
Greg Ward | e905513 | 2000-06-17 02:16:46 +0000 | [diff] [blame] | 142 | does what it can to deal with exception objects that don't have a |
| 143 | filename (which happens when the error is due to a two-file operation, |
| 144 | such as 'rename()' or 'link()'. Returns the error message as a string |
| 145 | prefixed with 'prefix'. |
| 146 | """ |
| 147 | # check for Python 1.5.2-style {IO,OS}Error exception objects |
Greg Ward | be86bde | 2000-09-26 01:56:15 +0000 | [diff] [blame] | 148 | if hasattr(exc, 'filename') and hasattr(exc, 'strerror'): |
Greg Ward | e905513 | 2000-06-17 02:16:46 +0000 | [diff] [blame] | 149 | if exc.filename: |
| 150 | error = prefix + "%s: %s" % (exc.filename, exc.strerror) |
| 151 | else: |
| 152 | # two-argument functions in posix module don't |
| 153 | # include the filename in the exception object! |
| 154 | error = prefix + "%s" % exc.strerror |
| 155 | else: |
| 156 | error = prefix + str(exc[-1]) |
| 157 | |
| 158 | return error |
Greg Ward | 6a2a3db | 2000-06-24 20:40:02 +0000 | [diff] [blame] | 159 | |
Greg Ward | 6a2a3db | 2000-06-24 20:40:02 +0000 | [diff] [blame] | 160 | # Needed by 'split_quoted()' |
Martin v. Löwis | 1c0f1f9 | 2004-03-25 14:58:19 +0000 | [diff] [blame] | 161 | _wordchars_re = _squote_re = _dquote_re = None |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 162 | |
Martin v. Löwis | 1c0f1f9 | 2004-03-25 14:58:19 +0000 | [diff] [blame] | 163 | def _init_regex(): |
| 164 | global _wordchars_re, _squote_re, _dquote_re |
| 165 | _wordchars_re = re.compile(r'[^\\\'\"%s ]*' % string.whitespace) |
| 166 | _squote_re = re.compile(r"'(?:[^'\\]|\\.)*'") |
| 167 | _dquote_re = re.compile(r'"(?:[^"\\]|\\.)*"') |
Greg Ward | 6a2a3db | 2000-06-24 20:40:02 +0000 | [diff] [blame] | 168 | |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 169 | def split_quoted(s): |
Greg Ward | 6a2a3db | 2000-06-24 20:40:02 +0000 | [diff] [blame] | 170 | """Split a string up according to Unix shell-like rules for quotes and |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 171 | backslashes. |
| 172 | |
| 173 | In short: words are delimited by spaces, as long as those |
Greg Ward | 6a2a3db | 2000-06-24 20:40:02 +0000 | [diff] [blame] | 174 | spaces are not escaped by a backslash, or inside a quoted string. |
| 175 | Single and double quotes are equivalent, and the quote characters can |
| 176 | be backslash-escaped. The backslash is stripped from any two-character |
| 177 | escape sequence, leaving only the escaped character. The quote |
| 178 | characters are stripped from any quoted string. Returns a list of |
| 179 | words. |
| 180 | """ |
Greg Ward | 6a2a3db | 2000-06-24 20:40:02 +0000 | [diff] [blame] | 181 | # This is a nice algorithm for splitting up a single string, since it |
| 182 | # doesn't require character-by-character examination. It was a little |
| 183 | # bit of a brain-bender to get it working right, though... |
Martin v. Löwis | 1c0f1f9 | 2004-03-25 14:58:19 +0000 | [diff] [blame] | 184 | if _wordchars_re is None: _init_regex() |
Greg Ward | 6a2a3db | 2000-06-24 20:40:02 +0000 | [diff] [blame] | 185 | |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 186 | s = s.strip() |
Greg Ward | 6a2a3db | 2000-06-24 20:40:02 +0000 | [diff] [blame] | 187 | words = [] |
| 188 | pos = 0 |
| 189 | |
| 190 | while s: |
| 191 | m = _wordchars_re.match(s, pos) |
| 192 | end = m.end() |
| 193 | if end == len(s): |
| 194 | words.append(s[:end]) |
| 195 | break |
| 196 | |
Greg Ward | 2b042de | 2000-08-08 14:38:13 +0000 | [diff] [blame] | 197 | if s[end] in string.whitespace: # unescaped, unquoted whitespace: now |
Greg Ward | 6a2a3db | 2000-06-24 20:40:02 +0000 | [diff] [blame] | 198 | words.append(s[:end]) # we definitely have a word delimiter |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 199 | s = s[end:].lstrip() |
Greg Ward | 6a2a3db | 2000-06-24 20:40:02 +0000 | [diff] [blame] | 200 | pos = 0 |
| 201 | |
| 202 | elif s[end] == '\\': # preserve whatever is being escaped; |
| 203 | # will become part of the current word |
| 204 | s = s[:end] + s[end+1:] |
| 205 | pos = end+1 |
| 206 | |
| 207 | else: |
| 208 | if s[end] == "'": # slurp singly-quoted string |
| 209 | m = _squote_re.match(s, end) |
| 210 | elif s[end] == '"': # slurp doubly-quoted string |
| 211 | m = _dquote_re.match(s, end) |
| 212 | else: |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 213 | raise RuntimeError("this can't happen " |
| 214 | "(bad char '%c')" % s[end]) |
Greg Ward | 6a2a3db | 2000-06-24 20:40:02 +0000 | [diff] [blame] | 215 | |
| 216 | if m is None: |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 217 | raise ValueError("bad string (mismatched %s quotes?)" % s[end]) |
Greg Ward | 6a2a3db | 2000-06-24 20:40:02 +0000 | [diff] [blame] | 218 | |
| 219 | (beg, end) = m.span() |
| 220 | s = s[:beg] + s[beg+1:end-1] + s[end:] |
| 221 | pos = m.end() - 2 |
| 222 | |
| 223 | if pos >= len(s): |
| 224 | words.append(s) |
| 225 | break |
| 226 | |
| 227 | return words |
| 228 | |
Greg Ward | 1c16ac3 | 2000-08-02 01:37:30 +0000 | [diff] [blame] | 229 | |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 230 | def execute(func, args, msg=None, verbose=0, dry_run=0): |
| 231 | """Perform some action that affects the outside world. |
Greg Ward | 1c16ac3 | 2000-08-02 01:37:30 +0000 | [diff] [blame] | 232 | |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 233 | eg. by writing to the filesystem). Such actions are special because |
| 234 | they are disabled by the 'dry_run' flag. This method takes care of all |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 235 | that bureaucracy for you; all you have to do is supply the |
| 236 | function to call and an argument tuple for it (to embody the |
| 237 | "external action" being performed), and an optional message to |
| 238 | print. |
Greg Ward | 1c16ac3 | 2000-08-02 01:37:30 +0000 | [diff] [blame] | 239 | """ |
Greg Ward | 1c16ac3 | 2000-08-02 01:37:30 +0000 | [diff] [blame] | 240 | if msg is None: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 241 | msg = "%s%r" % (func.__name__, args) |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 242 | if msg[-2:] == ',)': # correct for singleton tuple |
Greg Ward | 1c16ac3 | 2000-08-02 01:37:30 +0000 | [diff] [blame] | 243 | msg = msg[0:-2] + ')' |
| 244 | |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 245 | log.info(msg) |
Greg Ward | 1c16ac3 | 2000-08-02 01:37:30 +0000 | [diff] [blame] | 246 | if not dry_run: |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 247 | func(*args) |
Greg Ward | 1c16ac3 | 2000-08-02 01:37:30 +0000 | [diff] [blame] | 248 | |
Greg Ward | 817dc09 | 2000-09-25 01:25:06 +0000 | [diff] [blame] | 249 | |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 250 | def strtobool(val): |
Greg Ward | 817dc09 | 2000-09-25 01:25:06 +0000 | [diff] [blame] | 251 | """Convert a string representation of truth to true (1) or false (0). |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 252 | |
Greg Ward | 817dc09 | 2000-09-25 01:25:06 +0000 | [diff] [blame] | 253 | True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values |
| 254 | are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if |
| 255 | 'val' is anything else. |
| 256 | """ |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 257 | val = val.lower() |
Greg Ward | 817dc09 | 2000-09-25 01:25:06 +0000 | [diff] [blame] | 258 | if val in ('y', 'yes', 't', 'true', 'on', '1'): |
| 259 | return 1 |
| 260 | elif val in ('n', 'no', 'f', 'false', 'off', '0'): |
| 261 | return 0 |
| 262 | else: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 263 | raise ValueError, "invalid truth value %r" % (val,) |
Greg Ward | 1297b5c | 2000-09-30 20:37:56 +0000 | [diff] [blame] | 264 | |
| 265 | |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 266 | def byte_compile(py_files, optimize=0, force=0, prefix=None, base_dir=None, |
| 267 | verbose=1, dry_run=0, direct=None): |
Greg Ward | f217e21 | 2000-10-01 23:49:30 +0000 | [diff] [blame] | 268 | """Byte-compile a collection of Python source files to either .pyc |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 269 | or .pyo files in the same directory. |
| 270 | |
| 271 | 'py_files' is a list of files to compile; any files that don't end in |
| 272 | ".py" are silently skipped. 'optimize' must be one of the following: |
Greg Ward | 1297b5c | 2000-09-30 20:37:56 +0000 | [diff] [blame] | 273 | 0 - don't optimize (generate .pyc) |
| 274 | 1 - normal optimization (like "python -O") |
| 275 | 2 - extra optimization (like "python -OO") |
| 276 | If 'force' is true, all files are recompiled regardless of |
| 277 | timestamps. |
| 278 | |
| 279 | The source filename encoded in each bytecode file defaults to the |
| 280 | filenames listed in 'py_files'; you can modify these with 'prefix' and |
| 281 | 'basedir'. 'prefix' is a string that will be stripped off of each |
| 282 | source filename, and 'base_dir' is a directory name that will be |
| 283 | prepended (after 'prefix' is stripped). You can supply either or both |
| 284 | (or neither) of 'prefix' and 'base_dir', as you wish. |
| 285 | |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 286 | If 'dry_run' is true, doesn't actually do anything that would |
| 287 | affect the filesystem. |
Greg Ward | 1297b5c | 2000-09-30 20:37:56 +0000 | [diff] [blame] | 288 | |
| 289 | Byte-compilation is either done directly in this interpreter process |
| 290 | with the standard py_compile module, or indirectly by writing a |
| 291 | temporary script and executing it. Normally, you should let |
| 292 | 'byte_compile()' figure out to use direct compilation or not (see |
| 293 | the source for details). The 'direct' flag is used by the script |
| 294 | generated in indirect mode; unless you know what you're doing, leave |
| 295 | it set to None. |
| 296 | """ |
Tarek Ziadé | b9c1cfc | 2009-10-24 15:10:37 +0000 | [diff] [blame] | 297 | # nothing is done if sys.dont_write_bytecode is True |
| 298 | if sys.dont_write_bytecode: |
Tarek Ziadé | 1733c93 | 2009-10-24 15:51:30 +0000 | [diff] [blame] | 299 | raise DistutilsByteCompileError('byte-compiling is disabled.') |
Tarek Ziadé | b9c1cfc | 2009-10-24 15:10:37 +0000 | [diff] [blame] | 300 | |
Greg Ward | 1297b5c | 2000-09-30 20:37:56 +0000 | [diff] [blame] | 301 | # First, if the caller didn't force us into direct or indirect mode, |
| 302 | # figure out which mode we should be in. We take a conservative |
| 303 | # approach: choose direct mode *only* if the current interpreter is |
| 304 | # in debug mode and optimize is 0. If we're not in debug mode (-O |
| 305 | # or -OO), we don't know which level of optimization this |
| 306 | # interpreter is running with, so we can't do direct |
| 307 | # byte-compilation and be certain that it's the right thing. Thus, |
| 308 | # always compile indirectly if the current interpreter is in either |
| 309 | # optimize mode, or if either optimization level was requested by |
| 310 | # the caller. |
| 311 | if direct is None: |
| 312 | direct = (__debug__ and optimize == 0) |
| 313 | |
| 314 | # "Indirect" byte-compilation: write a temporary script and then |
| 315 | # run it with the appropriate flags. |
| 316 | if not direct: |
Marc-André Lemburg | 0375079 | 2002-12-03 08:45:11 +0000 | [diff] [blame] | 317 | try: |
| 318 | from tempfile import mkstemp |
| 319 | (script_fd, script_name) = mkstemp(".py") |
| 320 | except ImportError: |
| 321 | from tempfile import mktemp |
| 322 | (script_fd, script_name) = None, mktemp(".py") |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 323 | log.info("writing byte-compilation script '%s'", script_name) |
Greg Ward | 1297b5c | 2000-09-30 20:37:56 +0000 | [diff] [blame] | 324 | if not dry_run: |
Marc-André Lemburg | 0375079 | 2002-12-03 08:45:11 +0000 | [diff] [blame] | 325 | if script_fd is not None: |
| 326 | script = os.fdopen(script_fd, "w") |
| 327 | else: |
| 328 | script = open(script_name, "w") |
Greg Ward | 1297b5c | 2000-09-30 20:37:56 +0000 | [diff] [blame] | 329 | |
| 330 | script.write("""\ |
| 331 | from distutils.util import byte_compile |
| 332 | files = [ |
| 333 | """) |
Greg Ward | 9216cfe | 2000-10-03 03:31:05 +0000 | [diff] [blame] | 334 | |
| 335 | # XXX would be nice to write absolute filenames, just for |
| 336 | # safety's sake (script should be more robust in the face of |
| 337 | # chdir'ing before running it). But this requires abspath'ing |
| 338 | # 'prefix' as well, and that breaks the hack in build_lib's |
| 339 | # 'byte_compile()' method that carefully tacks on a trailing |
| 340 | # slash (os.sep really) to make sure the prefix here is "just |
| 341 | # right". This whole prefix business is rather delicate -- the |
| 342 | # problem is that it's really a directory, but I'm treating it |
| 343 | # as a dumb string, so trailing slashes and so forth matter. |
| 344 | |
| 345 | #py_files = map(os.path.abspath, py_files) |
| 346 | #if prefix: |
| 347 | # prefix = os.path.abspath(prefix) |
| 348 | |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 349 | script.write(",\n".join(map(repr, py_files)) + "]\n") |
Greg Ward | 1297b5c | 2000-09-30 20:37:56 +0000 | [diff] [blame] | 350 | script.write(""" |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 351 | byte_compile(files, optimize=%r, force=%r, |
| 352 | prefix=%r, base_dir=%r, |
| 353 | verbose=%r, dry_run=0, |
Greg Ward | 1297b5c | 2000-09-30 20:37:56 +0000 | [diff] [blame] | 354 | direct=1) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 355 | """ % (optimize, force, prefix, base_dir, verbose)) |
Greg Ward | 1297b5c | 2000-09-30 20:37:56 +0000 | [diff] [blame] | 356 | |
| 357 | script.close() |
| 358 | |
| 359 | cmd = [sys.executable, script_name] |
| 360 | if optimize == 1: |
| 361 | cmd.insert(1, "-O") |
| 362 | elif optimize == 2: |
| 363 | cmd.insert(1, "-OO") |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 364 | spawn(cmd, dry_run=dry_run) |
Greg Ward | 9216cfe | 2000-10-03 03:31:05 +0000 | [diff] [blame] | 365 | execute(os.remove, (script_name,), "removing %s" % script_name, |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 366 | dry_run=dry_run) |
Fred Drake | b94b849 | 2001-12-06 20:51:35 +0000 | [diff] [blame] | 367 | |
Greg Ward | 1297b5c | 2000-09-30 20:37:56 +0000 | [diff] [blame] | 368 | # "Direct" byte-compilation: use the py_compile module to compile |
| 369 | # right here, right now. Note that the script generated in indirect |
| 370 | # mode simply calls 'byte_compile()' in direct mode, a weird sort of |
| 371 | # cross-process recursion. Hey, it works! |
| 372 | else: |
| 373 | from py_compile import compile |
| 374 | |
| 375 | for file in py_files: |
| 376 | if file[-3:] != ".py": |
Greg Ward | f217e21 | 2000-10-01 23:49:30 +0000 | [diff] [blame] | 377 | # This lets us be lazy and not filter filenames in |
| 378 | # the "install_lib" command. |
| 379 | continue |
Greg Ward | 1297b5c | 2000-09-30 20:37:56 +0000 | [diff] [blame] | 380 | |
| 381 | # Terminology from the py_compile module: |
| 382 | # cfile - byte-compiled file |
| 383 | # dfile - purported source filename (same as 'file' by default) |
| 384 | cfile = file + (__debug__ and "c" or "o") |
| 385 | dfile = file |
| 386 | if prefix: |
| 387 | if file[:len(prefix)] != prefix: |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 388 | raise ValueError("invalid prefix: filename %r doesn't " |
| 389 | "start with %r" % (file, prefix)) |
Greg Ward | 1297b5c | 2000-09-30 20:37:56 +0000 | [diff] [blame] | 390 | dfile = dfile[len(prefix):] |
| 391 | if base_dir: |
| 392 | dfile = os.path.join(base_dir, dfile) |
| 393 | |
| 394 | cfile_base = os.path.basename(cfile) |
| 395 | if direct: |
| 396 | if force or newer(file, cfile): |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 397 | log.info("byte-compiling %s to %s", file, cfile_base) |
Greg Ward | 1297b5c | 2000-09-30 20:37:56 +0000 | [diff] [blame] | 398 | if not dry_run: |
| 399 | compile(file, cfile, dfile) |
| 400 | else: |
Jeremy Hylton | cd8a114 | 2002-06-04 20:14:43 +0000 | [diff] [blame] | 401 | log.debug("skipping byte-compilation of %s to %s", |
| 402 | file, cfile_base) |
Greg Ward | 1297b5c | 2000-09-30 20:37:56 +0000 | [diff] [blame] | 403 | |
Andrew M. Kuchling | df66df0 | 2001-03-22 03:03:41 +0000 | [diff] [blame] | 404 | |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 405 | def rfc822_escape(header): |
Andrew M. Kuchling | df66df0 | 2001-03-22 03:03:41 +0000 | [diff] [blame] | 406 | """Return a version of the string escaped for inclusion in an |
Andrew M. Kuchling | 88b0884 | 2001-03-23 17:30:26 +0000 | [diff] [blame] | 407 | RFC-822 header, by ensuring there are 8 spaces space after each newline. |
Andrew M. Kuchling | df66df0 | 2001-03-22 03:03:41 +0000 | [diff] [blame] | 408 | """ |
Tarek Ziadé | 4f38317 | 2009-12-06 09:22:40 +0000 | [diff] [blame] | 409 | lines = header.split('\n') |
| 410 | sep = '\n' + 8 * ' ' |
Tarek Ziadé | 3757fbb | 2009-07-02 14:20:47 +0000 | [diff] [blame] | 411 | return sep.join(lines) |
Tarek Ziadé | a99dedf | 2009-07-16 15:35:45 +0000 | [diff] [blame] | 412 | |
| 413 | _RE_VERSION = re.compile('(\d+\.\d+(\.\d+)*)') |
| 414 | _MAC_OS_X_LD_VERSION = re.compile('^@\(#\)PROGRAM:ld PROJECT:ld64-((\d+)(\.\d+)*)') |
| 415 | |
| 416 | def _find_ld_version(): |
| 417 | """Finds the ld version. The version scheme differs under Mac OSX.""" |
| 418 | if sys.platform == 'darwin': |
| 419 | return _find_exe_version('ld -v', _MAC_OS_X_LD_VERSION) |
| 420 | else: |
| 421 | return _find_exe_version('ld -v') |
| 422 | |
| 423 | def _find_exe_version(cmd, pattern=_RE_VERSION): |
| 424 | """Find the version of an executable by running `cmd` in the shell. |
| 425 | |
| 426 | `pattern` is a compiled regular expression. If not provided, default |
| 427 | to _RE_VERSION. If the command is not found, or the output does not |
| 428 | match the mattern, returns None. |
| 429 | """ |
| 430 | from subprocess import Popen, PIPE |
| 431 | executable = cmd.split()[0] |
| 432 | if find_executable(executable) is None: |
| 433 | return None |
| 434 | pipe = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE) |
| 435 | try: |
| 436 | stdout, stderr = pipe.stdout.read(), pipe.stderr.read() |
| 437 | finally: |
| 438 | pipe.stdout.close() |
| 439 | pipe.stderr.close() |
| 440 | # some commands like ld under MacOS X, will give the |
| 441 | # output in the stderr, rather than stdout. |
| 442 | if stdout != '': |
| 443 | out_string = stdout |
| 444 | else: |
| 445 | out_string = stderr |
| 446 | |
| 447 | result = pattern.search(out_string) |
| 448 | if result is None: |
| 449 | return None |
| 450 | return LooseVersion(result.group(1)) |
| 451 | |
| 452 | def get_compiler_versions(): |
| 453 | """Returns a tuple providing the versions of gcc, ld and dllwrap |
| 454 | |
| 455 | For each command, if a command is not found, None is returned. |
| 456 | Otherwise a LooseVersion instance is returned. |
| 457 | """ |
| 458 | gcc = _find_exe_version('gcc -dumpversion') |
| 459 | ld = _find_ld_version() |
| 460 | dllwrap = _find_exe_version('dllwrap --version') |
| 461 | return gcc, ld, dllwrap |