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 | |
| 21 | def get_platform (): |
| 22 | """Return a string (suitable for tacking onto directory names) that |
| 23 | identifies the current platform. Under Unix, identifies both the OS |
| 24 | and hardware architecture, e.g. "linux-i586", "solaris-sparc", |
| 25 | "irix-mips". For Windows and Mac OS, just returns 'sys.platform' -- |
| 26 | i.e. "???" or "???".""" |
| 27 | |
| 28 | if os.name == 'posix': |
Greg Ward | 6ce00b4 | 2000-03-31 04:40:25 +0000 | [diff] [blame] | 29 | (OS, _, rel, _, arch) = os.uname() |
| 30 | return "%s%c-%s" % (string.lower (OS), rel[0], string.lower (arch)) |
Greg Ward | 585df89 | 2000-03-01 14:40:15 +0000 | [diff] [blame] | 31 | else: |
| 32 | return sys.platform |
| 33 | |
| 34 | # get_platform() |
Greg Ward | 5091929 | 2000-03-07 03:27:08 +0000 | [diff] [blame] | 35 | |
| 36 | |
| 37 | def native_path (pathname): |
| 38 | """Return 'pathname' as a name that will work on the native |
| 39 | filesystem, i.e. split it on '/' and put it back together again |
| 40 | using the current directory separator. Needed because filenames in |
| 41 | the setup script are always supplied in Unix style, and have to be |
| 42 | converted to the local convention before we can actually use them in |
Greg Ward | 02a1a2b | 2000-04-15 22:15:07 +0000 | [diff] [blame^] | 43 | the filesystem. Raises ValueError if 'pathname' is |
Greg Ward | 5091929 | 2000-03-07 03:27:08 +0000 | [diff] [blame] | 44 | absolute (starts with '/') or contains local directory separators |
| 45 | (unless the local separator is '/', of course).""" |
| 46 | |
| 47 | if pathname[0] == '/': |
Greg Ward | 02a1a2b | 2000-04-15 22:15:07 +0000 | [diff] [blame^] | 48 | raise ValueError, "path '%s' cannot be absolute" % pathname |
Greg Ward | 5091929 | 2000-03-07 03:27:08 +0000 | [diff] [blame] | 49 | if pathname[-1] == '/': |
Greg Ward | 02a1a2b | 2000-04-15 22:15:07 +0000 | [diff] [blame^] | 50 | raise ValueError, "path '%s' cannot end with '/'" % pathname |
Greg Ward | 1b4ede5 | 2000-03-22 00:22:44 +0000 | [diff] [blame] | 51 | if os.sep != '/' and os.sep in pathname: |
Greg Ward | 02a1a2b | 2000-04-15 22:15:07 +0000 | [diff] [blame^] | 52 | raise ValueError, \ |
Greg Ward | 1b4ede5 | 2000-03-22 00:22:44 +0000 | [diff] [blame] | 53 | "path '%s' cannot contain '%c' character" % \ |
| 54 | (pathname, os.sep) |
Greg Ward | 5091929 | 2000-03-07 03:27:08 +0000 | [diff] [blame] | 55 | |
| 56 | paths = string.split (pathname, '/') |
| 57 | return apply (os.path.join, paths) |
| 58 | else: |
| 59 | return pathname |
| 60 | |
| 61 | # native_path () |
Greg Ward | 1b4ede5 | 2000-03-22 00:22:44 +0000 | [diff] [blame] | 62 | |
| 63 | |
| 64 | def _check_environ (): |
| 65 | """Ensure that 'os.environ' has all the environment variables we |
| 66 | guarantee that users can use in config files, command-line |
| 67 | options, etc. Currently this includes: |
| 68 | HOME - user's home directory (Unix only) |
| 69 | PLAT - desription of the current platform, including hardware |
| 70 | and OS (see 'get_platform()') |
| 71 | """ |
| 72 | |
| 73 | if os.name == 'posix' and not os.environ.has_key('HOME'): |
| 74 | import pwd |
| 75 | os.environ['HOME'] = pwd.getpwuid (os.getuid())[5] |
| 76 | |
| 77 | if not os.environ.has_key('PLAT'): |
| 78 | os.environ['PLAT'] = get_platform () |
| 79 | |
| 80 | |
| 81 | def subst_vars (str, local_vars): |
| 82 | """Perform shell/Perl-style variable substitution on 'string'. |
| 83 | Every occurence of '$' followed by a name, or a name enclosed in |
| 84 | braces, is considered a variable. Every variable is substituted by |
| 85 | the value found in the 'local_vars' dictionary, or in 'os.environ' |
| 86 | if it's not in 'local_vars'. 'os.environ' is first checked/ |
| 87 | augmented to guarantee that it contains certain values: see |
| 88 | '_check_environ()'. Raise ValueError for any variables not found in |
| 89 | either 'local_vars' or 'os.environ'.""" |
| 90 | |
| 91 | _check_environ () |
| 92 | def _subst (match, local_vars=local_vars): |
| 93 | var_name = match.group(1) |
| 94 | if local_vars.has_key (var_name): |
| 95 | return str (local_vars[var_name]) |
| 96 | else: |
| 97 | return os.environ[var_name] |
| 98 | |
| 99 | return re.sub (r'\$([a-zA-Z_][a-zA-Z_0-9]*)', _subst, str) |
| 100 | |
| 101 | # subst_vars () |
Greg Ward | 7c1a6d4 | 2000-03-29 02:48:40 +0000 | [diff] [blame] | 102 | |
| 103 | |