1999-03-22 10:52:19 -04:00
|
|
|
"""distutils.util
|
|
|
|
|
2000-04-03 23:05:59 -03:00
|
|
|
Miscellaneous utility functions -- anything that doesn't fit into
|
|
|
|
one of the other *util.py modules."""
|
1999-03-22 10:52:19 -04:00
|
|
|
|
|
|
|
# created 1999/03/08, Greg Ward
|
|
|
|
|
2000-03-01 21:49:45 -04:00
|
|
|
__revision__ = "$Id$"
|
1999-03-22 10:52:19 -04:00
|
|
|
|
2000-03-23 00:39:16 -04:00
|
|
|
import sys, os, string, re, shutil
|
1999-03-22 10:52:19 -04:00
|
|
|
from distutils.errors import *
|
2000-03-28 22:48:40 -04:00
|
|
|
from distutils.spawn import spawn
|
1999-03-22 10:52:19 -04:00
|
|
|
|
2000-04-03 23:05:59 -03:00
|
|
|
# for backwards compatibility:
|
|
|
|
from distutils.file_util import *
|
|
|
|
from distutils.dir_util import *
|
|
|
|
from distutils.dep_util import *
|
|
|
|
from distutils.archive_util import *
|
2000-03-01 10:40:15 -04:00
|
|
|
|
|
|
|
|
2000-04-22 12:14:58 -03:00
|
|
|
# Need to define 'abspath()', because it was new with Python 1.5.2
|
|
|
|
if hasattr (os.path, 'abspath'):
|
|
|
|
abspath = os.path.abspath
|
|
|
|
else:
|
|
|
|
def abspath(path):
|
|
|
|
if not os.path.isabs(path):
|
|
|
|
path = os.path.join(os.getcwd(), path)
|
|
|
|
return os.path.normpath(path)
|
|
|
|
|
|
|
|
|
|
|
|
# More backwards compatability hacks
|
|
|
|
def extend (list, new_list):
|
|
|
|
"""Appends the list 'new_list' to 'list', just like the 'extend()'
|
|
|
|
list method does in Python 1.5.2 -- but this works on earlier
|
|
|
|
versions of Python too."""
|
|
|
|
|
|
|
|
if hasattr (list, 'extend'):
|
|
|
|
list.extend (new_list)
|
|
|
|
else:
|
|
|
|
list[len(list):] = new_list
|
|
|
|
|
|
|
|
# extend ()
|
|
|
|
|
|
|
|
|
2000-03-01 10:40:15 -04:00
|
|
|
def get_platform ():
|
|
|
|
"""Return a string (suitable for tacking onto directory names) that
|
|
|
|
identifies the current platform. Under Unix, identifies both the OS
|
|
|
|
and hardware architecture, e.g. "linux-i586", "solaris-sparc",
|
|
|
|
"irix-mips". For Windows and Mac OS, just returns 'sys.platform' --
|
|
|
|
i.e. "???" or "???"."""
|
|
|
|
|
|
|
|
if os.name == 'posix':
|
2000-03-31 00:40:25 -04:00
|
|
|
(OS, _, rel, _, arch) = os.uname()
|
|
|
|
return "%s%c-%s" % (string.lower (OS), rel[0], string.lower (arch))
|
2000-03-01 10:40:15 -04:00
|
|
|
else:
|
|
|
|
return sys.platform
|
|
|
|
|
|
|
|
# get_platform()
|
2000-03-06 23:27:08 -04:00
|
|
|
|
|
|
|
|
2000-05-30 23:32:10 -03:00
|
|
|
def convert_path (pathname):
|
2000-03-06 23:27:08 -04:00
|
|
|
"""Return 'pathname' as a name that will work on the native
|
|
|
|
filesystem, i.e. split it on '/' and put it back together again
|
|
|
|
using the current directory separator. Needed because filenames in
|
|
|
|
the setup script are always supplied in Unix style, and have to be
|
|
|
|
converted to the local convention before we can actually use them in
|
2000-04-15 19:15:07 -03:00
|
|
|
the filesystem. Raises ValueError if 'pathname' is
|
2000-03-06 23:27:08 -04:00
|
|
|
absolute (starts with '/') or contains local directory separators
|
|
|
|
(unless the local separator is '/', of course)."""
|
|
|
|
|
|
|
|
if pathname[0] == '/':
|
2000-04-15 19:15:07 -03:00
|
|
|
raise ValueError, "path '%s' cannot be absolute" % pathname
|
2000-03-06 23:27:08 -04:00
|
|
|
if pathname[-1] == '/':
|
2000-04-15 19:15:07 -03:00
|
|
|
raise ValueError, "path '%s' cannot end with '/'" % pathname
|
2000-04-24 22:33:11 -03:00
|
|
|
if os.sep != '/':
|
2000-05-30 23:32:10 -03:00
|
|
|
paths = string.split (pathname, '/')
|
|
|
|
return apply (os.path.join, paths)
|
2000-03-06 23:27:08 -04:00
|
|
|
else:
|
|
|
|
return pathname
|
|
|
|
|
2000-05-30 23:32:10 -03:00
|
|
|
# convert_path ()
|
2000-03-21 20:22:44 -04:00
|
|
|
|
|
|
|
|
2000-04-26 22:53:46 -03:00
|
|
|
def change_root (new_root, pathname):
|
|
|
|
"""Return 'pathname' with 'new_root' prepended. If 'pathname' is
|
|
|
|
relative, this is equivalent to "os.path.join(new_root,pathname)".
|
|
|
|
Otherwise, it requires making 'pathname' relative and then joining the
|
2000-05-30 23:14:32 -03:00
|
|
|
two, which is tricky on DOS/Windows and Mac OS.
|
|
|
|
"""
|
|
|
|
if os.name == 'posix':
|
|
|
|
if not os.path.isabs (pathname):
|
|
|
|
return os.path.join (new_root, pathname)
|
|
|
|
else:
|
|
|
|
return os.path.join (new_root, pathname[1:])
|
2000-04-26 22:53:46 -03:00
|
|
|
|
|
|
|
elif os.name == 'nt':
|
|
|
|
(drive, path) = os.path.splitdrive (pathname)
|
2000-05-30 23:14:32 -03:00
|
|
|
if path[0] == '\\':
|
|
|
|
path = path[1:]
|
|
|
|
return os.path.join (new_root, path)
|
2000-04-26 22:53:46 -03:00
|
|
|
|
|
|
|
elif os.name == 'mac':
|
|
|
|
raise RuntimeError, "no clue how to do this on Mac OS"
|
|
|
|
|
|
|
|
else:
|
|
|
|
raise DistutilsPlatformError, \
|
|
|
|
"nothing known about platform '%s'" % os.name
|
|
|
|
|
|
|
|
|
2000-05-11 21:40:00 -03:00
|
|
|
_environ_checked = 0
|
|
|
|
def check_environ ():
|
2000-03-21 20:22:44 -04:00
|
|
|
"""Ensure that 'os.environ' has all the environment variables we
|
|
|
|
guarantee that users can use in config files, command-line
|
|
|
|
options, etc. Currently this includes:
|
|
|
|
HOME - user's home directory (Unix only)
|
|
|
|
PLAT - desription of the current platform, including hardware
|
|
|
|
and OS (see 'get_platform()')
|
|
|
|
"""
|
|
|
|
|
2000-05-11 21:40:00 -03:00
|
|
|
global _environ_checked
|
|
|
|
if _environ_checked:
|
|
|
|
return
|
|
|
|
|
2000-03-21 20:22:44 -04:00
|
|
|
if os.name == 'posix' and not os.environ.has_key('HOME'):
|
|
|
|
import pwd
|
|
|
|
os.environ['HOME'] = pwd.getpwuid (os.getuid())[5]
|
|
|
|
|
|
|
|
if not os.environ.has_key('PLAT'):
|
|
|
|
os.environ['PLAT'] = get_platform ()
|
|
|
|
|
2000-05-11 21:40:00 -03:00
|
|
|
_environ_checked = 1
|
|
|
|
|
2000-03-21 20:22:44 -04:00
|
|
|
|
|
|
|
def subst_vars (str, local_vars):
|
|
|
|
"""Perform shell/Perl-style variable substitution on 'string'.
|
|
|
|
Every occurence of '$' followed by a name, or a name enclosed in
|
|
|
|
braces, is considered a variable. Every variable is substituted by
|
|
|
|
the value found in the 'local_vars' dictionary, or in 'os.environ'
|
|
|
|
if it's not in 'local_vars'. 'os.environ' is first checked/
|
|
|
|
augmented to guarantee that it contains certain values: see
|
|
|
|
'_check_environ()'. Raise ValueError for any variables not found in
|
|
|
|
either 'local_vars' or 'os.environ'."""
|
|
|
|
|
2000-05-11 21:40:00 -03:00
|
|
|
check_environ ()
|
2000-03-21 20:22:44 -04:00
|
|
|
def _subst (match, local_vars=local_vars):
|
|
|
|
var_name = match.group(1)
|
|
|
|
if local_vars.has_key (var_name):
|
|
|
|
return str (local_vars[var_name])
|
|
|
|
else:
|
|
|
|
return os.environ[var_name]
|
|
|
|
|
|
|
|
return re.sub (r'\$([a-zA-Z_][a-zA-Z_0-9]*)', _subst, str)
|
|
|
|
|
|
|
|
# subst_vars ()
|
2000-03-28 22:48:40 -04:00
|
|
|
|
|
|
|
|