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 | |
| 31 | # More backwards compatability hacks |
| 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 |
| 47 | identifies the current platform. Under Unix, identifies both the OS |
| 48 | and hardware architecture, e.g. "linux-i586", "solaris-sparc", |
| 49 | "irix-mips". For Windows and Mac OS, just returns 'sys.platform' -- |
| 50 | i.e. "???" or "???".""" |
| 51 | |
| 52 | if os.name == 'posix': |
Greg Ward | 6ce00b4 | 2000-03-31 04:40:25 +0000 | [diff] [blame] | 53 | (OS, _, rel, _, arch) = os.uname() |
| 54 | return "%s%c-%s" % (string.lower (OS), rel[0], string.lower (arch)) |
Greg Ward | 585df89 | 2000-03-01 14:40:15 +0000 | [diff] [blame] | 55 | else: |
| 56 | return sys.platform |
| 57 | |
| 58 | # get_platform() |
Greg Ward | 5091929 | 2000-03-07 03:27:08 +0000 | [diff] [blame] | 59 | |
| 60 | |
| 61 | def native_path (pathname): |
| 62 | """Return 'pathname' as a name that will work on the native |
| 63 | filesystem, i.e. split it on '/' and put it back together again |
| 64 | using the current directory separator. Needed because filenames in |
| 65 | the setup script are always supplied in Unix style, and have to be |
| 66 | converted to the local convention before we can actually use them in |
Greg Ward | 02a1a2b | 2000-04-15 22:15:07 +0000 | [diff] [blame] | 67 | the filesystem. Raises ValueError if 'pathname' is |
Greg Ward | 5091929 | 2000-03-07 03:27:08 +0000 | [diff] [blame] | 68 | absolute (starts with '/') or contains local directory separators |
| 69 | (unless the local separator is '/', of course).""" |
| 70 | |
| 71 | if pathname[0] == '/': |
Greg Ward | 02a1a2b | 2000-04-15 22:15:07 +0000 | [diff] [blame] | 72 | raise ValueError, "path '%s' cannot be absolute" % pathname |
Greg Ward | 5091929 | 2000-03-07 03:27:08 +0000 | [diff] [blame] | 73 | if pathname[-1] == '/': |
Greg Ward | 02a1a2b | 2000-04-15 22:15:07 +0000 | [diff] [blame] | 74 | raise ValueError, "path '%s' cannot end with '/'" % pathname |
Greg Ward | 464023f | 2000-04-25 01:33:11 +0000 | [diff] [blame] | 75 | if os.sep != '/': |
| 76 | if os.sep in pathname: |
| 77 | raise ValueError, \ |
| 78 | "path '%s' cannot contain '%c' character" % (pathname, os.sep) |
| 79 | else: |
| 80 | paths = string.split (pathname, '/') |
| 81 | return apply (os.path.join, paths) |
Greg Ward | 5091929 | 2000-03-07 03:27:08 +0000 | [diff] [blame] | 82 | else: |
| 83 | return pathname |
| 84 | |
| 85 | # native_path () |
Greg Ward | 1b4ede5 | 2000-03-22 00:22:44 +0000 | [diff] [blame] | 86 | |
| 87 | |
Greg Ward | 67f75d4 | 2000-04-27 01:53:46 +0000 | [diff] [blame] | 88 | def change_root (new_root, pathname): |
| 89 | |
| 90 | """Return 'pathname' with 'new_root' prepended. If 'pathname' is |
| 91 | relative, this is equivalent to "os.path.join(new_root,pathname)". |
| 92 | Otherwise, it requires making 'pathname' relative and then joining the |
| 93 | two, which is tricky on DOS/Windows and Mac OS.""" |
| 94 | |
| 95 | if not abspath (pathname): |
| 96 | return os.path.join (new_root, pathname) |
| 97 | |
| 98 | elif os.name == 'posix': |
| 99 | return os.path.join (new_root, pathname[1:]) |
| 100 | |
| 101 | elif os.name == 'nt': |
| 102 | (root_drive, root_path) = os.path.splitdrive (new_root) |
| 103 | (drive, path) = os.path.splitdrive (pathname) |
| 104 | raise RuntimeError, "I give up -- not sure how to do this on Windows" |
| 105 | |
| 106 | elif os.name == 'mac': |
| 107 | raise RuntimeError, "no clue how to do this on Mac OS" |
| 108 | |
| 109 | else: |
| 110 | raise DistutilsPlatformError, \ |
| 111 | "nothing known about platform '%s'" % os.name |
| 112 | |
| 113 | |
Gregory P. Smith | e7e35ac | 2000-05-12 00:40:00 +0000 | [diff] [blame] | 114 | _environ_checked = 0 |
| 115 | def check_environ (): |
Greg Ward | 1b4ede5 | 2000-03-22 00:22:44 +0000 | [diff] [blame] | 116 | """Ensure that 'os.environ' has all the environment variables we |
| 117 | guarantee that users can use in config files, command-line |
| 118 | options, etc. Currently this includes: |
| 119 | HOME - user's home directory (Unix only) |
| 120 | PLAT - desription of the current platform, including hardware |
| 121 | and OS (see 'get_platform()') |
| 122 | """ |
| 123 | |
Gregory P. Smith | e7e35ac | 2000-05-12 00:40:00 +0000 | [diff] [blame] | 124 | global _environ_checked |
| 125 | if _environ_checked: |
| 126 | return |
| 127 | |
Greg Ward | 1b4ede5 | 2000-03-22 00:22:44 +0000 | [diff] [blame] | 128 | if os.name == 'posix' and not os.environ.has_key('HOME'): |
| 129 | import pwd |
| 130 | os.environ['HOME'] = pwd.getpwuid (os.getuid())[5] |
| 131 | |
| 132 | if not os.environ.has_key('PLAT'): |
| 133 | os.environ['PLAT'] = get_platform () |
| 134 | |
Gregory P. Smith | e7e35ac | 2000-05-12 00:40:00 +0000 | [diff] [blame] | 135 | _environ_checked = 1 |
| 136 | |
Greg Ward | 1b4ede5 | 2000-03-22 00:22:44 +0000 | [diff] [blame] | 137 | |
| 138 | def subst_vars (str, local_vars): |
| 139 | """Perform shell/Perl-style variable substitution on 'string'. |
| 140 | Every occurence of '$' followed by a name, or a name enclosed in |
| 141 | braces, is considered a variable. Every variable is substituted by |
| 142 | the value found in the 'local_vars' dictionary, or in 'os.environ' |
| 143 | if it's not in 'local_vars'. 'os.environ' is first checked/ |
| 144 | augmented to guarantee that it contains certain values: see |
| 145 | '_check_environ()'. Raise ValueError for any variables not found in |
| 146 | either 'local_vars' or 'os.environ'.""" |
| 147 | |
Gregory P. Smith | e7e35ac | 2000-05-12 00:40:00 +0000 | [diff] [blame] | 148 | check_environ () |
Greg Ward | 1b4ede5 | 2000-03-22 00:22:44 +0000 | [diff] [blame] | 149 | def _subst (match, local_vars=local_vars): |
| 150 | var_name = match.group(1) |
| 151 | if local_vars.has_key (var_name): |
| 152 | return str (local_vars[var_name]) |
| 153 | else: |
| 154 | return os.environ[var_name] |
| 155 | |
| 156 | return re.sub (r'\$([a-zA-Z_][a-zA-Z_0-9]*)', _subst, str) |
| 157 | |
| 158 | # subst_vars () |
Greg Ward | 7c1a6d4 | 2000-03-29 02:48:40 +0000 | [diff] [blame] | 159 | |
| 160 | |