Miscellaneous utility functions -- anything that doesn't fit into
one of the other *util.py modules.
__revision__
= "$Id: util.py,v 1.76 2004/07/18 06:14:42 tim_one Exp $"
import sys
, os
, string
, re
from distutils
.errors
import DistutilsPlatformError
from distutils
.dep_util
import newer
from distutils
.spawn
import spawn
from distutils
import log
"""Return a string that identifies the current platform. This is used
mainly to distinguish platform-specific build directories and
platform-specific built distributions. Typically includes the OS name
and version and the architecture (as supplied by 'os.uname()'),
although the exact information included depends on the OS; eg. for IRIX
the architecture isn't particularly important (IRIX only runs on SGI
hardware), but for Linux the kernel version isn't particularly
Examples of returned values:
For non-POSIX platforms, currently just returns 'sys.platform'.
if os
.name
!= "posix" or not hasattr(os
, 'uname'):
# XXX what about the architecture? NT is Intel or Alpha,
# Mac OS is M68k or PPC, etc.
# Try to distinguish various flavours of Unix
(osname
, host
, release
, version
, machine
) = os
.uname()
# Convert the OS name to lowercase, remove '/' characters
# (to accommodate BSD/OS), and translate spaces (for "Power Macintosh")
osname
= string
.lower(osname
)
osname
= string
.replace(osname
, '/', '')
machine
= string
.replace(machine
, ' ', '_')
if osname
[:5] == "linux":
# At least on Linux/Intel, 'machine' is the processor --
# XXX what about Alpha, SPARC, etc?
return "%s-%s" % (osname
, machine
)
elif osname
[:5] == "sunos":
if release
[0] >= "5": # SunOS 5 == Solaris 2
release
= "%d.%s" % (int(release
[0]) - 3, release
[2:])
# fall through to standard osname-release-machine representation
elif osname
[:4] == "irix": # could be "irix64"!
return "%s-%s" % (osname
, release
)
elif osname
[:3] == "aix":
return "%s-%s.%s" % (osname
, version
, release
)
elif osname
[:6] == "cygwin":
rel_re
= re
.compile (r
'[\d.]+')
m
= rel_re
.match(release
)
return "%s-%s-%s" % (osname
, release
, machine
)
def convert_path (pathname
):
"""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 the filesystem. Raises
ValueError on non-Unix-ish systems if 'pathname' either starts or
raise ValueError, "path '%s' cannot be absolute" % pathname
raise ValueError, "path '%s' cannot end with '/'" % pathname
paths
= string
.split(pathname
, '/')
return apply(os
.path
.join
, paths
)
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
two, which is tricky on DOS/Windows and Mac OS.
if not os
.path
.isabs(pathname
):
return os
.path
.join(new_root
, pathname
)
return os
.path
.join(new_root
, pathname
[1:])
(drive
, path
) = os
.path
.splitdrive(pathname
)
return os
.path
.join(new_root
, path
)
(drive
, path
) = os
.path
.splitdrive(pathname
)
return os
.path
.join(new_root
, path
)
if not os
.path
.isabs(pathname
):
return os
.path
.join(new_root
, pathname
)
# Chop off volume name from start of path
elements
= string
.split(pathname
, ":", 1)
pathname
= ":" + elements
[1]
return os
.path
.join(new_root
, pathname
)
raise DistutilsPlatformError
, \
"nothing known about platform '%s'" % os
.name
"""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 - description of the current platform, including hardware
and OS (see 'get_platform()')
if os
.name
== 'posix' and not os
.environ
.has_key('HOME'):
os
.environ
['HOME'] = pwd
.getpwuid(os
.getuid())[5]
if not os
.environ
.has_key('PLAT'):
os
.environ
['PLAT'] = get_platform()
def subst_vars (s
, local_vars
):
"""Perform shell/Perl-style variable substitution on 'string'. Every
occurrence of '$' followed by a name is considered a variable, and
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'.
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
])
return os
.environ
[var_name
]
return re
.sub(r
'\$([a-zA-Z_][a-zA-Z_0-9]*)', _subst
, s
)
raise ValueError, "invalid variable '$%s'" % var
def grok_environment_error (exc
, prefix
="error: "):
"""Generate a useful error message from an EnvironmentError (IOError or
OSError) exception object. Handles Python 1.5.1 and 1.5.2 styles, and
does what it can to deal with exception objects that don't have a
filename (which happens when the error is due to a two-file operation,
such as 'rename()' or 'link()'. Returns the error message as a string
# check for Python 1.5.2-style {IO,OS}Error exception objects
if hasattr(exc
, 'filename') and hasattr(exc
, 'strerror'):
error
= prefix
+ "%s: %s" % (exc
.filename
, exc
.strerror
)
# two-argument functions in posix module don't
# include the filename in the exception object!
error
= prefix
+ "%s" % exc
.strerror
error
= prefix
+ str(exc
[-1])
# Needed by 'split_quoted()'
_wordchars_re
= _squote_re
= _dquote_re
= None
global _wordchars_re
, _squote_re
, _dquote_re
_wordchars_re
= re
.compile(r
'[^\\\'\"%s ]*' % string.whitespace)
_squote_re = re.compile(r"'(?
:[^
'\\]|\\.)*'")
_dquote_re = re.compile(r'"(?
:[^
"\\]|\\.)*"')
"""Split a string up according to Unix shell-like rules for quotes and
backslashes. In short: words are delimited by spaces, as long as those
spaces are not escaped by a backslash, or inside a quoted string.
Single and double quotes are equivalent, and the quote characters can
be backslash-escaped. The backslash is stripped from any two-character
escape sequence, leaving only the escaped character. The quote
characters are stripped from any quoted string. Returns a list of
# This is a nice algorithm for splitting up a single string, since it
# doesn't require character
-by
-character examination
. It was a little
# bit of a brain-bender to get it working right, though...
if _wordchars_re
is None: _init_regex()
m
= _wordchars_re
.match(s
, pos
)
if s
[end
] in string
.whitespace
: # unescaped, unquoted whitespace: now
words
.append(s
[:end
]) # we definitely have a word delimiter
s
= string
.lstrip(s
[end
:])
elif s
[end
] == '\\': # preserve whatever is being escaped;
# will become part of the current word
if s
[end
] == "'": # slurp singly-quoted string
m
= _squote_re
.match(s
, end
)
elif s
[end
] == '"': # slurp doubly-quoted string
m
= _dquote_re
.match(s
, end
)
"this can't happen (bad char '%c')" % s
[end
]
"bad string (mismatched %s quotes?)" % s
[end
]
s
= s
[:beg
] + s
[beg
+1:end
-1] + s
[end
:]
def execute (func
, args
, msg
=None, verbose
=0, dry_run
=0):
"""Perform some action that affects the outside world (eg. by
writing to the filesystem). Such actions are special because they
are disabled by the 'dry_run' flag. This method takes care of all
that bureaucracy for you; all you have to do is supply the
function to call and an argument tuple for it (to embody the
"external action" being performed), and an optional message to
msg
= "%s%r" % (func
.__name
__, args
)
if msg
[-2:] == ',)': # correct for singleton tuple
"""Convert a string representation of truth to true (1) or false (0).
True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if
if val
in ('y', 'yes', 't', 'true', 'on', '1'):
elif val
in ('n', 'no', 'f', 'false', 'off', '0'):
raise ValueError, "invalid truth value %r" % (val
,)
def byte_compile (py_files
,
prefix
=None, base_dir
=None,
"""Byte-compile a collection of Python source files to either .pyc
or .pyo files in the same directory. 'py_files' is a list of files
to compile; any files that don't end in ".py" are silently skipped.
'optimize' must be one of the following:
0 - don't optimize (generate .pyc)
1 - normal optimization (like "python -O")
2 - extra optimization (like "python -OO")
If 'force' is true, all files are recompiled regardless of
The source filename encoded in each bytecode file defaults to the
filenames listed in 'py_files'; you can modify these with 'prefix' and
'basedir'. 'prefix' is a string that will be stripped off of each
source filename, and 'base_dir' is a directory name that will be
prepended (after 'prefix' is stripped). You can supply either or both
(or neither) of 'prefix' and 'base_dir', as you wish.
If 'dry_run' is true, doesn't actually do anything that would
Byte-compilation is either done directly in this interpreter process
with the standard py_compile module, or indirectly by writing a
temporary script and executing it. Normally, you should let
'byte_compile()' figure out to use direct compilation or not (see
the source for details). The 'direct' flag is used by the script
generated in indirect mode; unless you know what you're doing, leave
# First, if the caller didn't force us into direct or indirect mode,
# figure out which mode we should be in. We take a conservative
# approach: choose direct mode *only* if the current interpreter is
# in debug mode and optimize is 0. If we're not in debug mode (-O
# or -OO), we don't know which level of optimization this
# interpreter is running with, so we can't do direct
# byte-compilation and be certain that it's the right thing. Thus,
# always compile indirectly if the current interpreter is in either
# optimize mode, or if either optimization level was requested by
direct
= (__debug__
and optimize
== 0)
# "Indirect" byte-compilation: write a temporary script and then
# run it with the appropriate flags.
from tempfile
import mkstemp
(script_fd
, script_name
) = mkstemp(".py")
from tempfile
import mktemp
(script_fd
, script_name
) = None, mktemp(".py")
log
.info("writing byte-compilation script '%s'", script_name
)
if script_fd
is not None:
script
= os
.fdopen(script_fd
, "w")
script
= open(script_name
, "w")
from distutils.util import byte_compile
# XXX would be nice to write absolute filenames, just for
# safety's sake (script should be more robust in the face of
# chdir'ing before running it). But this requires abspath'ing
# 'prefix' as well, and that breaks the hack in build_lib's
# 'byte_compile()' method that carefully tacks on a trailing
# slash (os.sep really) to make sure the prefix here is "just
# right". This whole prefix business is rather delicate -- the
# problem is that it's really a directory, but I'm treating it
# as a dumb string, so trailing slashes and so forth matter.
#py_files = map(os.path.abspath, py_files)
# prefix = os.path.abspath(prefix)
script
.write(string
.join(map(repr, py_files
), ",\n") + "]\n")
byte_compile(files, optimize=%r, force=%r,
""" % (optimize
, force
, prefix
, base_dir
, verbose
))
cmd
= [sys
.executable
, script_name
]
spawn(cmd
, dry_run
=dry_run
)
execute(os
.remove
, (script_name
,), "removing %s" % script_name
,
# "Direct" byte-compilation: use the py_compile module to compile
# right here, right now. Note that the script generated in indirect
# mode simply calls 'byte_compile()' in direct mode, a weird sort of
# cross-process recursion. Hey, it works!
from py_compile
import compile
# This lets us be lazy and not filter filenames in
# the "install_lib" command.
# Terminology from the py_compile module:
# cfile - byte-compiled file
# dfile - purported source filename (same as 'file' by default)
cfile
= file + (__debug__
and "c" or "o")
if file[:len(prefix
)] != prefix
:
("invalid prefix: filename %r doesn't start with %r"
dfile
= dfile
[len(prefix
):]
dfile
= os
.path
.join(base_dir
, dfile
)
cfile_base
= os
.path
.basename(cfile
)
if force
or newer(file, cfile
):
log
.info("byte-compiling %s to %s", file, cfile_base
)
compile(file, cfile
, dfile
)
log
.debug("skipping byte-compilation of %s to %s",
def rfc822_escape (header
):
"""Return a version of the string escaped for inclusion in an
RFC-822 header, by ensuring there are 8 spaces space after each newline.
lines
= string
.split(header
, '\n')
lines
= map(string
.strip
, lines
)
header
= string
.join(lines
, '\n' + 8*' ')