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 |
| 4 | one of the other *util.py modules.""" |
Greg Ward | 2689e3d | 1999-03-22 14:52:19 +0000 | [diff] [blame] | 5 | |
| 6 | # created 1999/03/08, Greg Ward |
| 7 | |
Greg Ward | 3ce77fd | 2000-03-02 01:49:45 +0000 | [diff] [blame] | 8 | __revision__ = "$Id$" |
Greg Ward | 2689e3d | 1999-03-22 14:52:19 +0000 | [diff] [blame] | 9 | |
Greg Ward | a7540bd | 2000-03-23 04:39:16 +0000 | [diff] [blame] | 10 | import sys, os, string, re, shutil |
Greg Ward | 2689e3d | 1999-03-22 14:52:19 +0000 | [diff] [blame] | 11 | from distutils.errors import * |
Greg Ward | 7c1a6d4 | 2000-03-29 02:48:40 +0000 | [diff] [blame] | 12 | from distutils.spawn import spawn |
Greg Ward | 2689e3d | 1999-03-22 14:52:19 +0000 | [diff] [blame] | 13 | |
Greg Ward | aebf706 | 2000-04-04 02:05:59 +0000 | [diff] [blame] | 14 | # for backwards compatibility: |
| 15 | from distutils.file_util import * |
| 16 | from distutils.dir_util import * |
| 17 | from distutils.dep_util import * |
| 18 | from distutils.archive_util import * |
Greg Ward | 585df89 | 2000-03-01 14:40:15 +0000 | [diff] [blame] | 19 | |
| 20 | |
Greg Ward | aa458bc | 2000-04-22 15:14:58 +0000 | [diff] [blame] | 21 | # Need to define 'abspath()', because it was new with Python 1.5.2 |
| 22 | if hasattr (os.path, 'abspath'): |
| 23 | abspath = os.path.abspath |
| 24 | else: |
| 25 | def abspath(path): |
| 26 | if not os.path.isabs(path): |
| 27 | path = os.path.join(os.getcwd(), path) |
| 28 | return os.path.normpath(path) |
| 29 | |
| 30 | |
Jeremy Hylton | a05e293 | 2000-06-28 14:48:01 +0000 | [diff] [blame] | 31 | # More backwards compatibility hacks |
Greg Ward | aa458bc | 2000-04-22 15:14:58 +0000 | [diff] [blame] | 32 | def extend (list, new_list): |
| 33 | """Appends the list 'new_list' to 'list', just like the 'extend()' |
| 34 | list method does in Python 1.5.2 -- but this works on earlier |
| 35 | versions of Python too.""" |
| 36 | |
| 37 | if hasattr (list, 'extend'): |
| 38 | list.extend (new_list) |
| 39 | else: |
| 40 | list[len(list):] = new_list |
| 41 | |
| 42 | # extend () |
| 43 | |
| 44 | |
Greg Ward | 585df89 | 2000-03-01 14:40:15 +0000 | [diff] [blame] | 45 | def get_platform (): |
| 46 | """Return a string (suitable for tacking onto directory names) that |
Greg Ward | b75c485 | 2000-06-18 15:45:55 +0000 | [diff] [blame] | 47 | identifies the current platform. Currently, this is just |
| 48 | 'sys.platform'. |
| 49 | """ |
| 50 | return sys.platform |
Greg Ward | 5091929 | 2000-03-07 03:27:08 +0000 | [diff] [blame] | 51 | |
| 52 | |
Greg Ward | d8dfb4c | 2000-05-31 02:32:10 +0000 | [diff] [blame] | 53 | def convert_path (pathname): |
Greg Ward | 5091929 | 2000-03-07 03:27:08 +0000 | [diff] [blame] | 54 | """Return 'pathname' as a name that will work on the native |
| 55 | filesystem, i.e. split it on '/' and put it back together again |
| 56 | using the current directory separator. Needed because filenames in |
| 57 | the setup script are always supplied in Unix style, and have to be |
| 58 | converted to the local convention before we can actually use them in |
Greg Ward | 02a1a2b | 2000-04-15 22:15:07 +0000 | [diff] [blame] | 59 | the filesystem. Raises ValueError if 'pathname' is |
Greg Ward | 5091929 | 2000-03-07 03:27:08 +0000 | [diff] [blame] | 60 | absolute (starts with '/') or contains local directory separators |
| 61 | (unless the local separator is '/', of course).""" |
| 62 | |
| 63 | if pathname[0] == '/': |
Greg Ward | 02a1a2b | 2000-04-15 22:15:07 +0000 | [diff] [blame] | 64 | raise ValueError, "path '%s' cannot be absolute" % pathname |
Greg Ward | 5091929 | 2000-03-07 03:27:08 +0000 | [diff] [blame] | 65 | if pathname[-1] == '/': |
Greg Ward | 02a1a2b | 2000-04-15 22:15:07 +0000 | [diff] [blame] | 66 | raise ValueError, "path '%s' cannot end with '/'" % pathname |
Greg Ward | 464023f | 2000-04-25 01:33:11 +0000 | [diff] [blame] | 67 | if os.sep != '/': |
Greg Ward | d8dfb4c | 2000-05-31 02:32:10 +0000 | [diff] [blame] | 68 | paths = string.split (pathname, '/') |
| 69 | return apply (os.path.join, paths) |
Greg Ward | 5091929 | 2000-03-07 03:27:08 +0000 | [diff] [blame] | 70 | else: |
| 71 | return pathname |
| 72 | |
Greg Ward | d8dfb4c | 2000-05-31 02:32:10 +0000 | [diff] [blame] | 73 | # convert_path () |
Greg Ward | 1b4ede5 | 2000-03-22 00:22:44 +0000 | [diff] [blame] | 74 | |
| 75 | |
Greg Ward | 67f75d4 | 2000-04-27 01:53:46 +0000 | [diff] [blame] | 76 | def change_root (new_root, pathname): |
Greg Ward | 67f75d4 | 2000-04-27 01:53:46 +0000 | [diff] [blame] | 77 | """Return 'pathname' with 'new_root' prepended. If 'pathname' is |
| 78 | relative, this is equivalent to "os.path.join(new_root,pathname)". |
| 79 | Otherwise, it requires making 'pathname' relative and then joining the |
Greg Ward | 4b46ef9 | 2000-05-31 02:14:32 +0000 | [diff] [blame] | 80 | two, which is tricky on DOS/Windows and Mac OS. |
| 81 | """ |
| 82 | if os.name == 'posix': |
| 83 | if not os.path.isabs (pathname): |
| 84 | return os.path.join (new_root, pathname) |
| 85 | else: |
| 86 | return os.path.join (new_root, pathname[1:]) |
Greg Ward | 67f75d4 | 2000-04-27 01:53:46 +0000 | [diff] [blame] | 87 | |
| 88 | elif os.name == 'nt': |
Greg Ward | 67f75d4 | 2000-04-27 01:53:46 +0000 | [diff] [blame] | 89 | (drive, path) = os.path.splitdrive (pathname) |
Greg Ward | 4b46ef9 | 2000-05-31 02:14:32 +0000 | [diff] [blame] | 90 | if path[0] == '\\': |
| 91 | path = path[1:] |
| 92 | return os.path.join (new_root, path) |
Greg Ward | 67f75d4 | 2000-04-27 01:53:46 +0000 | [diff] [blame] | 93 | |
| 94 | elif os.name == 'mac': |
| 95 | raise RuntimeError, "no clue how to do this on Mac OS" |
| 96 | |
| 97 | else: |
| 98 | raise DistutilsPlatformError, \ |
| 99 | "nothing known about platform '%s'" % os.name |
| 100 | |
| 101 | |
Gregory P. Smith | e7e35ac | 2000-05-12 00:40:00 +0000 | [diff] [blame] | 102 | _environ_checked = 0 |
| 103 | def check_environ (): |
Greg Ward | 1b4ede5 | 2000-03-22 00:22:44 +0000 | [diff] [blame] | 104 | """Ensure that 'os.environ' has all the environment variables we |
| 105 | guarantee that users can use in config files, command-line |
| 106 | options, etc. Currently this includes: |
| 107 | HOME - user's home directory (Unix only) |
| 108 | PLAT - desription of the current platform, including hardware |
| 109 | and OS (see 'get_platform()') |
| 110 | """ |
| 111 | |
Gregory P. Smith | e7e35ac | 2000-05-12 00:40:00 +0000 | [diff] [blame] | 112 | global _environ_checked |
| 113 | if _environ_checked: |
| 114 | return |
| 115 | |
Greg Ward | 1b4ede5 | 2000-03-22 00:22:44 +0000 | [diff] [blame] | 116 | if os.name == 'posix' and not os.environ.has_key('HOME'): |
| 117 | import pwd |
| 118 | os.environ['HOME'] = pwd.getpwuid (os.getuid())[5] |
| 119 | |
| 120 | if not os.environ.has_key('PLAT'): |
| 121 | os.environ['PLAT'] = get_platform () |
| 122 | |
Gregory P. Smith | e7e35ac | 2000-05-12 00:40:00 +0000 | [diff] [blame] | 123 | _environ_checked = 1 |
| 124 | |
Greg Ward | 1b4ede5 | 2000-03-22 00:22:44 +0000 | [diff] [blame] | 125 | |
| 126 | def subst_vars (str, local_vars): |
| 127 | """Perform shell/Perl-style variable substitution on 'string'. |
| 128 | Every occurence of '$' followed by a name, or a name enclosed in |
| 129 | braces, is considered a variable. Every variable is substituted by |
| 130 | the value found in the 'local_vars' dictionary, or in 'os.environ' |
| 131 | if it's not in 'local_vars'. 'os.environ' is first checked/ |
| 132 | augmented to guarantee that it contains certain values: see |
| 133 | '_check_environ()'. Raise ValueError for any variables not found in |
| 134 | either 'local_vars' or 'os.environ'.""" |
| 135 | |
Gregory P. Smith | e7e35ac | 2000-05-12 00:40:00 +0000 | [diff] [blame] | 136 | check_environ () |
Greg Ward | 1b4ede5 | 2000-03-22 00:22:44 +0000 | [diff] [blame] | 137 | def _subst (match, local_vars=local_vars): |
| 138 | var_name = match.group(1) |
| 139 | if local_vars.has_key (var_name): |
| 140 | return str (local_vars[var_name]) |
| 141 | else: |
| 142 | return os.environ[var_name] |
| 143 | |
| 144 | return re.sub (r'\$([a-zA-Z_][a-zA-Z_0-9]*)', _subst, str) |
| 145 | |
| 146 | # subst_vars () |
Greg Ward | 7c1a6d4 | 2000-03-29 02:48:40 +0000 | [diff] [blame] | 147 | |
| 148 | |
Greg Ward | e905513 | 2000-06-17 02:16:46 +0000 | [diff] [blame] | 149 | def grok_environment_error (exc, prefix="error: "): |
| 150 | """Generate a useful error message from an EnvironmentError (IOError or |
| 151 | OSError) exception object. Handles Python 1.5.1 and 1.5.2 styles, and |
| 152 | does what it can to deal with exception objects that don't have a |
| 153 | filename (which happens when the error is due to a two-file operation, |
| 154 | such as 'rename()' or 'link()'. Returns the error message as a string |
| 155 | prefixed with 'prefix'. |
| 156 | """ |
| 157 | # check for Python 1.5.2-style {IO,OS}Error exception objects |
| 158 | if hasattr (exc, 'filename') and hasattr (exc, 'strerror'): |
| 159 | if exc.filename: |
| 160 | error = prefix + "%s: %s" % (exc.filename, exc.strerror) |
| 161 | else: |
| 162 | # two-argument functions in posix module don't |
| 163 | # include the filename in the exception object! |
| 164 | error = prefix + "%s" % exc.strerror |
| 165 | else: |
| 166 | error = prefix + str(exc[-1]) |
| 167 | |
| 168 | return error |
Greg Ward | 6a2a3db | 2000-06-24 20:40:02 +0000 | [diff] [blame] | 169 | |
| 170 | |
| 171 | # Needed by 'split_quoted()' |
| 172 | _wordchars_re = re.compile(r'[^\\\'\"\ ]*') |
| 173 | _squote_re = re.compile(r"'(?:[^'\\]|\\.)*'") |
| 174 | _dquote_re = re.compile(r'"(?:[^"\\]|\\.)*"') |
| 175 | |
| 176 | def split_quoted (s): |
| 177 | """Split a string up according to Unix shell-like rules for quotes and |
| 178 | backslashes. In short: words are delimited by spaces, as long as those |
| 179 | spaces are not escaped by a backslash, or inside a quoted string. |
| 180 | Single and double quotes are equivalent, and the quote characters can |
| 181 | be backslash-escaped. The backslash is stripped from any two-character |
| 182 | escape sequence, leaving only the escaped character. The quote |
| 183 | characters are stripped from any quoted string. Returns a list of |
| 184 | words. |
| 185 | """ |
| 186 | |
| 187 | # This is a nice algorithm for splitting up a single string, since it |
| 188 | # doesn't require character-by-character examination. It was a little |
| 189 | # bit of a brain-bender to get it working right, though... |
| 190 | |
| 191 | s = string.strip(s) |
| 192 | words = [] |
| 193 | pos = 0 |
| 194 | |
| 195 | while s: |
| 196 | m = _wordchars_re.match(s, pos) |
| 197 | end = m.end() |
| 198 | if end == len(s): |
| 199 | words.append(s[:end]) |
| 200 | break |
| 201 | |
| 202 | if s[end] == ' ': # unescaped, unquoted space: now |
| 203 | words.append(s[:end]) # we definitely have a word delimiter |
| 204 | s = string.lstrip(s[end:]) |
| 205 | pos = 0 |
| 206 | |
| 207 | elif s[end] == '\\': # preserve whatever is being escaped; |
| 208 | # will become part of the current word |
| 209 | s = s[:end] + s[end+1:] |
| 210 | pos = end+1 |
| 211 | |
| 212 | else: |
| 213 | if s[end] == "'": # slurp singly-quoted string |
| 214 | m = _squote_re.match(s, end) |
| 215 | elif s[end] == '"': # slurp doubly-quoted string |
| 216 | m = _dquote_re.match(s, end) |
| 217 | else: |
| 218 | raise RuntimeError, \ |
| 219 | "this can't happen (bad char '%c')" % s[end] |
| 220 | |
| 221 | if m is None: |
| 222 | raise ValueError, \ |
| 223 | "bad string (mismatched %s quotes?)" % s[end] |
| 224 | |
| 225 | (beg, end) = m.span() |
| 226 | s = s[:beg] + s[beg+1:end-1] + s[end:] |
| 227 | pos = m.end() - 2 |
| 228 | |
| 229 | if pos >= len(s): |
| 230 | words.append(s) |
| 231 | break |
| 232 | |
| 233 | return words |
| 234 | |
| 235 | # split_quoted () |