Back to index

python3.2  3.2.2
Classes | Functions | Variables
distutils.util Namespace Reference

Classes

class  Mixin2to3

Functions

def get_platform
def convert_path
def change_root
def check_environ
def subst_vars
def grok_environment_error
def _init_regex
def split_quoted
def execute
def strtobool
def byte_compile
def rfc822_escape
def run_2to3
def copydir_run_2to3

Variables

int _environ_checked = 0
 _wordchars_re = _squote_re_dquote_reNone

Detailed Description

distutils.util

Miscellaneous utility functions -- anything that doesn't fit into
one of the other *util.py modules.

Function Documentation

def distutils.util._init_regex ( ) [private]

Definition at line 310 of file util.py.

00310 
00311 def _init_regex():
00312     global _wordchars_re, _squote_re, _dquote_re
00313     _wordchars_re = re.compile(r'[^\\\'\"%s ]*' % string.whitespace)
00314     _squote_re = re.compile(r"'(?:[^'\\]|\\.)*'")
00315     _dquote_re = re.compile(r'"(?:[^"\\]|\\.)*"')

Here is the call graph for this function:

Here is the caller graph for this function:

def distutils.util.byte_compile (   py_files,
  optimize = 0,
  force = 0,
  prefix = None,
  base_dir = None,
  verbose = 1,
  dry_run = 0,
  direct = 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
timestamps.

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
affect the filesystem.

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
it set to None.

Definition at line 416 of file util.py.

00416 
00417                   direct=None):
00418     """Byte-compile a collection of Python source files to either .pyc
00419     or .pyo files in the same directory.  'py_files' is a list of files
00420     to compile; any files that don't end in ".py" are silently skipped.
00421     'optimize' must be one of the following:
00422       0 - don't optimize (generate .pyc)
00423       1 - normal optimization (like "python -O")
00424       2 - extra optimization (like "python -OO")
00425     If 'force' is true, all files are recompiled regardless of
00426     timestamps.
00427 
00428     The source filename encoded in each bytecode file defaults to the
00429     filenames listed in 'py_files'; you can modify these with 'prefix' and
00430     'basedir'.  'prefix' is a string that will be stripped off of each
00431     source filename, and 'base_dir' is a directory name that will be
00432     prepended (after 'prefix' is stripped).  You can supply either or both
00433     (or neither) of 'prefix' and 'base_dir', as you wish.
00434 
00435     If 'dry_run' is true, doesn't actually do anything that would
00436     affect the filesystem.
00437 
00438     Byte-compilation is either done directly in this interpreter process
00439     with the standard py_compile module, or indirectly by writing a
00440     temporary script and executing it.  Normally, you should let
00441     'byte_compile()' figure out to use direct compilation or not (see
00442     the source for details).  The 'direct' flag is used by the script
00443     generated in indirect mode; unless you know what you're doing, leave
00444     it set to None.
00445     """
00446     # nothing is done if sys.dont_write_bytecode is True
00447     if sys.dont_write_bytecode:
00448         raise DistutilsByteCompileError('byte-compiling is disabled.')
00449 
00450     # First, if the caller didn't force us into direct or indirect mode,
00451     # figure out which mode we should be in.  We take a conservative
00452     # approach: choose direct mode *only* if the current interpreter is
00453     # in debug mode and optimize is 0.  If we're not in debug mode (-O
00454     # or -OO), we don't know which level of optimization this
00455     # interpreter is running with, so we can't do direct
00456     # byte-compilation and be certain that it's the right thing.  Thus,
00457     # always compile indirectly if the current interpreter is in either
00458     # optimize mode, or if either optimization level was requested by
00459     # the caller.
00460     if direct is None:
00461         direct = (__debug__ and optimize == 0)
00462 
00463     # "Indirect" byte-compilation: write a temporary script and then
00464     # run it with the appropriate flags.
00465     if not direct:
00466         try:
00467             from tempfile import mkstemp
00468             (script_fd, script_name) = mkstemp(".py")
00469         except ImportError:
00470             from tempfile import mktemp
00471             (script_fd, script_name) = None, mktemp(".py")
00472         log.info("writing byte-compilation script '%s'", script_name)
00473         if not dry_run:
00474             if script_fd is not None:
00475                 script = os.fdopen(script_fd, "w")
00476             else:
00477                 script = open(script_name, "w")
00478 
00479             script.write("""\
00480 from distutils.util import byte_compile
00481 files = [
00482 """)
00483 
00484             # XXX would be nice to write absolute filenames, just for
00485             # safety's sake (script should be more robust in the face of
00486             # chdir'ing before running it).  But this requires abspath'ing
00487             # 'prefix' as well, and that breaks the hack in build_lib's
00488             # 'byte_compile()' method that carefully tacks on a trailing
00489             # slash (os.sep really) to make sure the prefix here is "just
00490             # right".  This whole prefix business is rather delicate -- the
00491             # problem is that it's really a directory, but I'm treating it
00492             # as a dumb string, so trailing slashes and so forth matter.
00493 
00494             #py_files = map(os.path.abspath, py_files)
00495             #if prefix:
00496             #    prefix = os.path.abspath(prefix)
00497 
00498             script.write(",\n".join(map(repr, py_files)) + "]\n")
00499             script.write("""
00500 byte_compile(files, optimize=%r, force=%r,
00501              prefix=%r, base_dir=%r,
00502              verbose=%r, dry_run=0,
00503              direct=1)
00504 """ % (optimize, force, prefix, base_dir, verbose))
00505 
00506             script.close()
00507 
00508         cmd = [sys.executable, script_name]
00509         if optimize == 1:
00510             cmd.insert(1, "-O")
00511         elif optimize == 2:
00512             cmd.insert(1, "-OO")
00513         spawn(cmd, dry_run=dry_run)
00514         execute(os.remove, (script_name,), "removing %s" % script_name,
00515                 dry_run=dry_run)
00516 
00517     # "Direct" byte-compilation: use the py_compile module to compile
00518     # right here, right now.  Note that the script generated in indirect
00519     # mode simply calls 'byte_compile()' in direct mode, a weird sort of
00520     # cross-process recursion.  Hey, it works!
00521     else:
00522         from py_compile import compile
00523 
00524         for file in py_files:
00525             if file[-3:] != ".py":
00526                 # This lets us be lazy and not filter filenames in
00527                 # the "install_lib" command.
00528                 continue
00529 
00530             # Terminology from the py_compile module:
00531             #   cfile - byte-compiled file
00532             #   dfile - purported source filename (same as 'file' by default)
00533             cfile = file + (__debug__ and "c" or "o")
00534             dfile = file
00535             if prefix:
00536                 if file[:len(prefix)] != prefix:
00537                     raise ValueError("invalid prefix: filename %r doesn't start with %r"
00538                            % (file, prefix))
00539                 dfile = dfile[len(prefix):]
00540             if base_dir:
00541                 dfile = os.path.join(base_dir, dfile)
00542 
00543             cfile_base = os.path.basename(cfile)
00544             if direct:
00545                 if force or newer(file, cfile):
00546                     log.info("byte-compiling %s to %s", file, cfile_base)
00547                     if not dry_run:
00548                         compile(file, cfile, dfile)
00549                 else:
00550                     log.debug("skipping byte-compilation of %s to %s",
00551                               file, cfile_base)
00552 
00553 # byte_compile ()

Here is the call graph for this function:

Here is the caller graph for this function:

def distutils.util.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.

Definition at line 210 of file util.py.

00210 
00211 def change_root (new_root, pathname):
00212     """Return 'pathname' with 'new_root' prepended.  If 'pathname' is
00213     relative, this is equivalent to "os.path.join(new_root,pathname)".
00214     Otherwise, it requires making 'pathname' relative and then joining the
00215     two, which is tricky on DOS/Windows and Mac OS.
00216     """
00217     if os.name == 'posix':
00218         if not os.path.isabs(pathname):
00219             return os.path.join(new_root, pathname)
00220         else:
00221             return os.path.join(new_root, pathname[1:])
00222 
00223     elif os.name == 'nt':
00224         (drive, path) = os.path.splitdrive(pathname)
00225         if path[0] == '\\':
00226             path = path[1:]
00227         return os.path.join(new_root, path)
00228 
00229     elif os.name == 'os2':
00230         (drive, path) = os.path.splitdrive(pathname)
00231         if path[0] == os.sep:
00232             path = path[1:]
00233         return os.path.join(new_root, path)
00234 
00235     else:
00236         raise DistutilsPlatformError("nothing known about platform '%s'" % os.name)
00237 

Here is the caller graph for this function:

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()')

Definition at line 239 of file util.py.

00239 
00240 def check_environ ():
00241     """Ensure that 'os.environ' has all the environment variables we
00242     guarantee that users can use in config files, command-line options,
00243     etc.  Currently this includes:
00244       HOME - user's home directory (Unix only)
00245       PLAT - description of the current platform, including hardware
00246              and OS (see 'get_platform()')
00247     """
00248     global _environ_checked
00249     if _environ_checked:
00250         return
00251 
00252     if os.name == 'posix' and 'HOME' not in os.environ:
00253         import pwd
00254         os.environ['HOME'] = pwd.getpwuid(os.getuid())[5]
00255 
00256     if 'PLAT' not in os.environ:
00257         os.environ['PLAT'] = get_platform()
00258 
00259     _environ_checked = 1
00260 

Here is the call graph for this function:

Here is the caller graph for this function:

def distutils.util.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
ends with a slash.

Definition at line 182 of file util.py.

00182 
00183 def convert_path (pathname):
00184     """Return 'pathname' as a name that will work on the native filesystem,
00185     i.e. split it on '/' and put it back together again using the current
00186     directory separator.  Needed because filenames in the setup script are
00187     always supplied in Unix style, and have to be converted to the local
00188     convention before we can actually use them in the filesystem.  Raises
00189     ValueError on non-Unix-ish systems if 'pathname' either starts or
00190     ends with a slash.
00191     """
00192     if os.sep == '/':
00193         return pathname
00194     if not pathname:
00195         return pathname
00196     if pathname[0] == '/':
00197         raise ValueError("path '%s' cannot be absolute" % pathname)
00198     if pathname[-1] == '/':
00199         raise ValueError("path '%s' cannot end with '/'" % pathname)
00200 
00201     paths = pathname.split('/')
00202     while '.' in paths:
00203         paths.remove('.')
00204     if not paths:
00205         return os.curdir
00206     return os.path.join(*paths)
00207 
00208 # convert_path ()
00209 

Here is the caller graph for this function:

def distutils.util.copydir_run_2to3 (   src,
  dest,
  template = None,
  fixer_names = None,
  options = None,
  explicit = None 
)
Recursively copy a directory, only copying new and changed files,
running run_2to3 over all newly copied Python modules afterward.

If you give a template string, it's parsed like a MANIFEST.in.

Definition at line 592 of file util.py.

00592 
00593                      options=None, explicit=None):
00594     """Recursively copy a directory, only copying new and changed files,
00595     running run_2to3 over all newly copied Python modules afterward.
00596 
00597     If you give a template string, it's parsed like a MANIFEST.in.
00598     """
00599     from distutils.dir_util import mkpath
00600     from distutils.file_util import copy_file
00601     from distutils.filelist import FileList
00602     filelist = FileList()
00603     curdir = os.getcwd()
00604     os.chdir(src)
00605     try:
00606         filelist.findall()
00607     finally:
00608         os.chdir(curdir)
00609     filelist.files[:] = filelist.allfiles
00610     if template:
00611         for line in template.splitlines():
00612             line = line.strip()
00613             if not line: continue
00614             filelist.process_template_line(line)
00615     copied = []
00616     for filename in filelist.files:
00617         outname = os.path.join(dest, filename)
00618         mkpath(os.path.dirname(outname))
00619         res = copy_file(os.path.join(src, filename), outname, update=1)
00620         if res[1]: copied.append(outname)
00621     run_2to3([fn for fn in copied if fn.lower().endswith('.py')],
00622              fixer_names=fixer_names, options=options, explicit=explicit)
00623     return copied

Here is the call graph for this function:

Here is the caller graph for this function:

def distutils.util.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
print.

Definition at line 377 of file util.py.

00377 
00378 def execute (func, args, msg=None, verbose=0, dry_run=0):
00379     """Perform some action that affects the outside world (eg.  by
00380     writing to the filesystem).  Such actions are special because they
00381     are disabled by the 'dry_run' flag.  This method takes care of all
00382     that bureaucracy for you; all you have to do is supply the
00383     function to call and an argument tuple for it (to embody the
00384     "external action" being performed), and an optional message to
00385     print.
00386     """
00387     if msg is None:
00388         msg = "%s%r" % (func.__name__, args)
00389         if msg[-2:] == ',)':        # correct for singleton tuple
00390             msg = msg[0:-2] + ')'
00391 
00392     log.info(msg)
00393     if not dry_run:
00394         func(*args)
00395 

Here is the caller graph for this function:

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
important.

Examples of returned values:
   linux-i586
   linux-alpha (?)
   solaris-2.6-sun4u
   irix-5.3
   irix64-6.2

Windows will return one of:
   win-amd64 (64bit Windows on AMD64 (aka x86_64, Intel64, EM64T, etc)
   win-ia64 (64bit Windows on Itanium)
   win32 (all others - specifically, sys.platform is returned)

For other non-POSIX platforms, currently just returns 'sys.platform'.

Definition at line 14 of file util.py.

00014 
00015 def get_platform ():
00016     """Return a string that identifies the current platform.  This is used
00017     mainly to distinguish platform-specific build directories and
00018     platform-specific built distributions.  Typically includes the OS name
00019     and version and the architecture (as supplied by 'os.uname()'),
00020     although the exact information included depends on the OS; eg. for IRIX
00021     the architecture isn't particularly important (IRIX only runs on SGI
00022     hardware), but for Linux the kernel version isn't particularly
00023     important.
00024 
00025     Examples of returned values:
00026        linux-i586
00027        linux-alpha (?)
00028        solaris-2.6-sun4u
00029        irix-5.3
00030        irix64-6.2
00031 
00032     Windows will return one of:
00033        win-amd64 (64bit Windows on AMD64 (aka x86_64, Intel64, EM64T, etc)
00034        win-ia64 (64bit Windows on Itanium)
00035        win32 (all others - specifically, sys.platform is returned)
00036 
00037     For other non-POSIX platforms, currently just returns 'sys.platform'.
00038     """
00039     if os.name == 'nt':
00040         # sniff sys.version for architecture.
00041         prefix = " bit ("
00042         i = sys.version.find(prefix)
00043         if i == -1:
00044             return sys.platform
00045         j = sys.version.find(")", i)
00046         look = sys.version[i+len(prefix):j].lower()
00047         if look == 'amd64':
00048             return 'win-amd64'
00049         if look == 'itanium':
00050             return 'win-ia64'
00051         return sys.platform
00052 
00053     if os.name != "posix" or not hasattr(os, 'uname'):
00054         # XXX what about the architecture? NT is Intel or Alpha,
00055         # Mac OS is M68k or PPC, etc.
00056         return sys.platform
00057 
00058     # Try to distinguish various flavours of Unix
00059 
00060     (osname, host, release, version, machine) = os.uname()
00061 
00062     # Convert the OS name to lowercase, remove '/' characters
00063     # (to accommodate BSD/OS), and translate spaces (for "Power Macintosh")
00064     osname = osname.lower().replace('/', '')
00065     machine = machine.replace(' ', '_')
00066     machine = machine.replace('/', '-')
00067 
00068     if osname[:5] == "linux":
00069         # At least on Linux/Intel, 'machine' is the processor --
00070         # i386, etc.
00071         # XXX what about Alpha, SPARC, etc?
00072         return  "%s-%s" % (osname, machine)
00073     elif osname[:5] == "sunos":
00074         if release[0] >= "5":           # SunOS 5 == Solaris 2
00075             osname = "solaris"
00076             release = "%d.%s" % (int(release[0]) - 3, release[2:])
00077         # fall through to standard osname-release-machine representation
00078     elif osname[:4] == "irix":              # could be "irix64"!
00079         return "%s-%s" % (osname, release)
00080     elif osname[:3] == "aix":
00081         return "%s-%s.%s" % (osname, version, release)
00082     elif osname[:6] == "cygwin":
00083         osname = "cygwin"
00084         rel_re = re.compile (r'[\d.]+', re.ASCII)
00085         m = rel_re.match(release)
00086         if m:
00087             release = m.group()
00088     elif osname[:6] == "darwin":
00089         #
00090         # For our purposes, we'll assume that the system version from
00091         # distutils' perspective is what MACOSX_DEPLOYMENT_TARGET is set
00092         # to. This makes the compatibility story a bit more sane because the
00093         # machine is going to compile and link as if it were
00094         # MACOSX_DEPLOYMENT_TARGET.
00095         from distutils.sysconfig import get_config_vars
00096         cfgvars = get_config_vars()
00097 
00098         macver = cfgvars.get('MACOSX_DEPLOYMENT_TARGET')
00099 
00100         if 1:
00101             # Always calculate the release of the running machine,
00102             # needed to determine if we can build fat binaries or not.
00103 
00104             macrelease = macver
00105             # Get the system version. Reading this plist is a documented
00106             # way to get the system version (see the documentation for
00107             # the Gestalt Manager)
00108             try:
00109                 f = open('/System/Library/CoreServices/SystemVersion.plist')
00110             except IOError:
00111                 # We're on a plain darwin box, fall back to the default
00112                 # behaviour.
00113                 pass
00114             else:
00115                 try:
00116                     m = re.search(
00117                             r'<key>ProductUserVisibleVersion</key>\s*' +
00118                             r'<string>(.*?)</string>', f.read())
00119                     if m is not None:
00120                         macrelease = '.'.join(m.group(1).split('.')[:2])
00121                     # else: fall back to the default behaviour
00122                 finally:
00123                     f.close()
00124 
00125         if not macver:
00126             macver = macrelease
00127 
00128         if macver:
00129             from distutils.sysconfig import get_config_vars
00130             release = macver
00131             osname = "macosx"
00132 
00133             if (macrelease + '.') >= '10.4.' and \
00134                     '-arch' in get_config_vars().get('CFLAGS', '').strip():
00135                 # The universal build will build fat binaries, but not on
00136                 # systems before 10.4
00137                 #
00138                 # Try to detect 4-way universal builds, those have machine-type
00139                 # 'universal' instead of 'fat'.
00140 
00141                 machine = 'fat'
00142                 cflags = get_config_vars().get('CFLAGS')
00143 
00144                 archs = re.findall('-arch\s+(\S+)', cflags)
00145                 archs = tuple(sorted(set(archs)))
00146 
00147                 if len(archs) == 1:
00148                     machine = archs[0]
00149                 elif archs == ('i386', 'ppc'):
00150                     machine = 'fat'
00151                 elif archs == ('i386', 'x86_64'):
00152                     machine = 'intel'
00153                 elif archs == ('i386', 'ppc', 'x86_64'):
00154                     machine = 'fat3'
00155                 elif archs == ('ppc64', 'x86_64'):
00156                     machine = 'fat64'
00157                 elif archs == ('i386', 'ppc', 'ppc64', 'x86_64'):
00158                     machine = 'universal'
00159                 else:
00160                     raise ValueError(
00161                        "Don't know machine value for archs=%r"%(archs,))
00162 
00163             elif machine == 'i386':
00164                 # On OSX the machine type returned by uname is always the
00165                 # 32-bit variant, even if the executable architecture is
00166                 # the 64-bit variant
00167                 if sys.maxsize >= 2**32:
00168                     machine = 'x86_64'
00169 
00170             elif machine in ('PowerPC', 'Power_Macintosh'):
00171                 # Pick a sane name for the PPC architecture.
00172                 machine = 'ppc'
00173 
00174                 # See 'i386' case
00175                 if sys.maxsize >= 2**32:
00176                     machine = 'ppc64'
00177 
00178     return "%s-%s-%s" % (osname, release, machine)
00179 
00180 # get_platform ()
00181 

Here is the call graph for this function:

Here is the caller graph for this function:

def distutils.util.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
prefixed with 'prefix'.

Definition at line 286 of file util.py.

00286 
00287 def grok_environment_error (exc, prefix="error: "):
00288     """Generate a useful error message from an EnvironmentError (IOError or
00289     OSError) exception object.  Handles Python 1.5.1 and 1.5.2 styles, and
00290     does what it can to deal with exception objects that don't have a
00291     filename (which happens when the error is due to a two-file operation,
00292     such as 'rename()' or 'link()'.  Returns the error message as a string
00293     prefixed with 'prefix'.
00294     """
00295     # check for Python 1.5.2-style {IO,OS}Error exception objects
00296     if hasattr(exc, 'filename') and hasattr(exc, 'strerror'):
00297         if exc.filename:
00298             error = prefix + "%s: %s" % (exc.filename, exc.strerror)
00299         else:
00300             # two-argument functions in posix module don't
00301             # include the filename in the exception object!
00302             error = prefix + "%s" % exc.strerror
00303     else:
00304         error = prefix + str(exc.args[-1])
00305 
00306     return error
00307 
00308 
# Needed by 'split_quoted()'

Here is the caller graph for this function:

def distutils.util.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.

Definition at line 554 of file util.py.

00554 
00555 def rfc822_escape (header):
00556     """Return a version of the string escaped for inclusion in an
00557     RFC-822 header, by ensuring there are 8 spaces space after each newline.
00558     """
00559     lines = header.split('\n')
00560     sep = '\n' + 8 * ' '
00561     return sep.join(lines)
00562 
00563 # 2to3 support

Here is the caller graph for this function:

def distutils.util.run_2to3 (   files,
  fixer_names = None,
  options = None,
  explicit = None 
)
Invoke 2to3 on a list of Python files.
The files should all come from the build area, as the
modification is done in-place. To reduce the build time,
only files modified since the last invocation of this
function should be passed in the files argument.

Definition at line 564 of file util.py.

00564 
00565 def run_2to3(files, fixer_names=None, options=None, explicit=None):
00566     """Invoke 2to3 on a list of Python files.
00567     The files should all come from the build area, as the
00568     modification is done in-place. To reduce the build time,
00569     only files modified since the last invocation of this
00570     function should be passed in the files argument."""
00571 
00572     if not files:
00573         return
00574 
00575     # Make this class local, to delay import of 2to3
00576     from lib2to3.refactor import RefactoringTool, get_fixers_from_package
00577     class DistutilsRefactoringTool(RefactoringTool):
00578         def log_error(self, msg, *args, **kw):
00579             log.error(msg, *args)
00580 
00581         def log_message(self, msg, *args):
00582             log.info(msg, *args)
00583 
00584         def log_debug(self, msg, *args):
00585             log.debug(msg, *args)
00586 
00587     if fixer_names is None:
00588         fixer_names = get_fixers_from_package('lib2to3.fixes')
00589     r = DistutilsRefactoringTool(fixer_names, options=options)
00590     r.refactor(files, write=True)

Here is the call graph for this function:

Here is the caller graph for this function:

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
words.

Definition at line 316 of file util.py.

00316 
00317 def split_quoted (s):
00318     """Split a string up according to Unix shell-like rules for quotes and
00319     backslashes.  In short: words are delimited by spaces, as long as those
00320     spaces are not escaped by a backslash, or inside a quoted string.
00321     Single and double quotes are equivalent, and the quote characters can
00322     be backslash-escaped.  The backslash is stripped from any two-character
00323     escape sequence, leaving only the escaped character.  The quote
00324     characters are stripped from any quoted string.  Returns a list of
00325     words.
00326     """
00327 
00328     # This is a nice algorithm for splitting up a single string, since it
00329     # doesn't require character-by-character examination.  It was a little
00330     # bit of a brain-bender to get it working right, though...
00331     if _wordchars_re is None: _init_regex()
00332 
00333     s = s.strip()
00334     words = []
00335     pos = 0
00336 
00337     while s:
00338         m = _wordchars_re.match(s, pos)
00339         end = m.end()
00340         if end == len(s):
00341             words.append(s[:end])
00342             break
00343 
00344         if s[end] in string.whitespace: # unescaped, unquoted whitespace: now
00345             words.append(s[:end])       # we definitely have a word delimiter
00346             s = s[end:].lstrip()
00347             pos = 0
00348 
00349         elif s[end] == '\\':            # preserve whatever is being escaped;
00350                                         # will become part of the current word
00351             s = s[:end] + s[end+1:]
00352             pos = end+1
00353 
00354         else:
00355             if s[end] == "'":           # slurp singly-quoted string
00356                 m = _squote_re.match(s, end)
00357             elif s[end] == '"':         # slurp doubly-quoted string
00358                 m = _dquote_re.match(s, end)
00359             else:
00360                 raise RuntimeError("this can't happen (bad char '%c')" % s[end])
00361 
00362             if m is None:
00363                 raise ValueError("bad string (mismatched %s quotes?)" % s[end])
00364 
00365             (beg, end) = m.span()
00366             s = s[:beg] + s[beg+1:end-1] + s[end:]
00367             pos = m.end() - 2
00368 
00369         if pos >= len(s):
00370             words.append(s)
00371             break
00372 
00373     return words
00374 
00375 # split_quoted ()
00376 

Here is the call graph for this function:

Here is the caller graph for this function:

def distutils.util.strtobool (   val)
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
'val' is anything else.

Definition at line 396 of file util.py.

00396 
00397 def strtobool (val):
00398     """Convert a string representation of truth to true (1) or false (0).
00399 
00400     True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
00401     are 'n', 'no', 'f', 'false', 'off', and '0'.  Raises ValueError if
00402     'val' is anything else.
00403     """
00404     val = val.lower()
00405     if val in ('y', 'yes', 't', 'true', 'on', '1'):
00406         return 1
00407     elif val in ('n', 'no', 'f', 'false', 'off', '0'):
00408         return 0
00409     else:
00410         raise ValueError("invalid truth value %r" % (val,))
00411 

Here is the call graph for this function:

Here is the caller graph for this function:

def distutils.util.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'.

Definition at line 261 of file util.py.

00261 
00262 def subst_vars (s, local_vars):
00263     """Perform shell/Perl-style variable substitution on 'string'.  Every
00264     occurrence of '$' followed by a name is considered a variable, and
00265     variable is substituted by the value found in the 'local_vars'
00266     dictionary, or in 'os.environ' if it's not in 'local_vars'.
00267     'os.environ' is first checked/augmented to guarantee that it contains
00268     certain values: see 'check_environ()'.  Raise ValueError for any
00269     variables not found in either 'local_vars' or 'os.environ'.
00270     """
00271     check_environ()
00272     def _subst (match, local_vars=local_vars):
00273         var_name = match.group(1)
00274         if var_name in local_vars:
00275             return str(local_vars[var_name])
00276         else:
00277             return os.environ[var_name]
00278 
00279     try:
00280         return re.sub(r'\$([a-zA-Z_][a-zA-Z_0-9]*)', _subst, s)
00281     except KeyError as var:
00282         raise ValueError("invalid variable '$%s'" % var)
00283 
00284 # subst_vars ()
00285 

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 238 of file util.py.

distutils.util._wordchars_re = _squote_re_dquote_reNone

Definition at line 309 of file util.py.