Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/distutils/cmd.py

    r2 r391  
    55"""
    66
    7 # This module should be kept compatible with Python 2.1.
    8 
    9 __revision__ = "$Id: cmd.py 37828 2004-11-10 22:23:15Z loewis $"
    10 
    11 import sys, os, string, re
    12 from types import *
    13 from distutils.errors import *
     7__revision__ = "$Id$"
     8
     9import sys, os, re
     10from distutils.errors import DistutilsOptionError
    1411from distutils import util, dir_util, file_util, archive_util, dep_util
    1512from distutils import log
     
    5047    # -- Creation/initialization methods -------------------------------
    5148
    52     def __init__ (self, dist):
     49    def __init__(self, dist):
    5350        """Create and initialize a new Command object.  Most importantly,
    5451        invokes the 'initialize_options()' method, which is the real
     
    9794        self.finalized = 0
    9895
    99     # __init__ ()
    100 
    101 
    10296    # XXX A more explicit way to customize dry_run would be better.
    103 
    104     def __getattr__ (self, attr):
     97    def __getattr__(self, attr):
    10598        if attr == 'dry_run':
    10699            myval = getattr(self, "_" + attr)
     
    112105            raise AttributeError, attr
    113106
    114 
    115     def ensure_finalized (self):
     107    def ensure_finalized(self):
    116108        if not self.finalized:
    117109            self.finalize_options()
    118110        self.finalized = 1
    119 
    120111
    121112    # Subclasses must define:
     
    132123    #     controlled by the command's various option values
    133124
    134     def initialize_options (self):
     125    def initialize_options(self):
    135126        """Set default values for all the options that this command
    136127        supports.  Note that these defaults may be overridden by other
     
    145136              "abstract method -- subclass %s must override" % self.__class__
    146137
    147     def finalize_options (self):
     138    def finalize_options(self):
    148139        """Set final values for all the options that this command supports.
    149140        This is always called as late as possible, ie.  after any option
     
    160151
    161152
    162     def dump_options (self, header=None, indent=""):
     153    def dump_options(self, header=None, indent=""):
    163154        from distutils.fancy_getopt import longopt_xlate
    164155        if header is None:
    165156            header = "command options for '%s':" % self.get_command_name()
    166         print indent + header
     157        self.announce(indent + header, level=log.INFO)
    167158        indent = indent + "  "
    168159        for (option, _, _) in self.user_options:
    169             option = string.translate(option, longopt_xlate)
     160            option = option.translate(longopt_xlate)
    170161            if option[-1] == "=":
    171162                option = option[:-1]
    172163            value = getattr(self, option)
    173             print indent + "%s = %s" % (option, value)
    174 
    175 
    176     def run (self):
     164            self.announce(indent + "%s = %s" % (option, value),
     165                          level=log.INFO)
     166
     167    def run(self):
    177168        """A command's raison d'etre: carry out the action it exists to
    178169        perform, controlled by the options initialized in
     
    184175        This method must be implemented by all command classes.
    185176        """
    186 
    187177        raise RuntimeError, \
    188178              "abstract method -- subclass %s must override" % self.__class__
    189179
    190     def announce (self, msg, level=1):
     180    def announce(self, msg, level=1):
    191181        """If the current verbosity level is of greater than or equal to
    192182        'level' print 'msg' to stdout.
     
    194184        log.log(level, msg)
    195185
    196     def debug_print (self, msg):
     186    def debug_print(self, msg):
    197187        """Print 'msg' to stdout if the global DEBUG (taken from the
    198188        DISTUTILS_DEBUG environment variable) flag is true.
     
    202192            print msg
    203193            sys.stdout.flush()
    204 
    205194
    206195
     
    218207    # a list of strings.
    219208
    220     def _ensure_stringlike (self, option, what, default=None):
     209    def _ensure_stringlike(self, option, what, default=None):
    221210        val = getattr(self, option)
    222211        if val is None:
    223212            setattr(self, option, default)
    224213            return default
    225         elif type(val) is not StringType:
     214        elif not isinstance(val, str):
    226215            raise DistutilsOptionError, \
    227216                  "'%s' must be a %s (got `%s`)" % (option, what, val)
    228217        return val
    229218
    230     def ensure_string (self, option, default=None):
     219    def ensure_string(self, option, default=None):
    231220        """Ensure that 'option' is a string; if not defined, set it to
    232221        'default'.
     
    234223        self._ensure_stringlike(option, "string", default)
    235224
    236     def ensure_string_list (self, option):
     225    def ensure_string_list(self, option):
    237226        """Ensure that 'option' is a list of strings.  If 'option' is
    238227        currently a string, we split it either on /,\s*/ or /\s+/, so
     
    243232        if val is None:
    244233            return
    245         elif type(val) is StringType:
     234        elif isinstance(val, str):
    246235            setattr(self, option, re.split(r',\s*|\s+', val))
    247236        else:
    248             if type(val) is ListType:
    249                 types = map(type, val)
    250                 ok = (types == [StringType] * len(val))
     237            if isinstance(val, list):
     238                # checks if all elements are str
     239                ok = 1
     240                for element in val:
     241                    if not isinstance(element, str):
     242                        ok = 0
     243                        break
    251244            else:
    252245                ok = 0
     
    254247            if not ok:
    255248                raise DistutilsOptionError, \
    256                       "'%s' must be a list of strings (got %r)" % \
    257                       (option, val)
    258 
    259     def _ensure_tested_string (self, option, tester,
    260                                what, error_fmt, default=None):
     249                    "'%s' must be a list of strings (got %r)" % \
     250                        (option, val)
     251
     252
     253    def _ensure_tested_string(self, option, tester,
     254                              what, error_fmt, default=None):
    261255        val = self._ensure_stringlike(option, what, default)
    262256        if val is not None and not tester(val):
     
    264258                  ("error in '%s' option: " + error_fmt) % (option, val)
    265259
    266     def ensure_filename (self, option):
     260    def ensure_filename(self, option):
    267261        """Ensure that 'option' is the name of an existing file."""
    268262        self._ensure_tested_string(option, os.path.isfile,
     
    270264                                   "'%s' does not exist or is not a file")
    271265
    272     def ensure_dirname (self, option):
     266    def ensure_dirname(self, option):
    273267        self._ensure_tested_string(option, os.path.isdir,
    274268                                   "directory name",
     
    278272    # -- Convenience methods for commands ------------------------------
    279273
    280     def get_command_name (self):
     274    def get_command_name(self):
    281275        if hasattr(self, 'command_name'):
    282276            return self.command_name
     
    284278            return self.__class__.__name__
    285279
    286 
    287     def set_undefined_options (self, src_cmd, *option_pairs):
     280    def set_undefined_options(self, src_cmd, *option_pairs):
    288281        """Set the values of any "undefined" options from corresponding
    289282        option values in some other command object.  "Undefined" here means
     
    310303
    311304
    312     def get_finalized_command (self, command, create=1):
     305    def get_finalized_command(self, command, create=1):
    313306        """Wrapper around Distribution's 'get_command_obj()' method: find
    314307        (create if necessary and 'create' is true) the command object for
     
    322315    # XXX rename to 'get_reinitialized_command()'? (should do the
    323316    # same in dist.py, if so)
    324     def reinitialize_command (self, command, reinit_subcommands=0):
     317    def reinitialize_command(self, command, reinit_subcommands=0):
    325318        return self.distribution.reinitialize_command(
    326319            command, reinit_subcommands)
    327320
    328     def run_command (self, command):
     321    def run_command(self, command):
    329322        """Run some other command: uses the 'run_command()' method of
    330323        Distribution, which creates and finalizes the command object if
     
    333326        self.distribution.run_command(command)
    334327
    335 
    336     def get_sub_commands (self):
     328    def get_sub_commands(self):
    337329        """Determine the sub-commands that are relevant in the current
    338330        distribution (ie., that need to be run).  This is based on the
     
    350342    # -- External world manipulation -----------------------------------
    351343
    352     def warn (self, msg):
    353         sys.stderr.write("warning: %s: %s\n" %
    354                          (self.get_command_name(), msg))
    355 
    356 
    357     def execute (self, func, args, msg=None, level=1):
     344    def warn(self, msg):
     345        log.warn("warning: %s: %s\n" %
     346                (self.get_command_name(), msg))
     347
     348    def execute(self, func, args, msg=None, level=1):
    358349        util.execute(func, args, msg, dry_run=self.dry_run)
    359350
    360 
    361     def mkpath (self, name, mode=0777):
     351    def mkpath(self, name, mode=0777):
    362352        dir_util.mkpath(name, mode, dry_run=self.dry_run)
    363353
    364 
    365     def copy_file (self, infile, outfile,
     354    def copy_file(self, infile, outfile,
    366355                   preserve_mode=1, preserve_times=1, link=None, level=1):
    367356        """Copy a file respecting verbose, dry-run and force flags.  (The
     
    376365            dry_run=self.dry_run)
    377366
    378 
    379     def copy_tree (self, infile, outfile,
     367    def copy_tree(self, infile, outfile,
    380368                   preserve_mode=1, preserve_times=1, preserve_symlinks=0,
    381369                   level=1):
     
    390378
    391379    def move_file (self, src, dst, level=1):
    392         """Move a file respectin dry-run flag."""
     380        """Move a file respecting dry-run flag."""
    393381        return file_util.move_file(src, dst, dry_run = self.dry_run)
    394382
     
    398386        spawn(cmd, search_path, dry_run= self.dry_run)
    399387
    400     def make_archive (self, base_name, format,
    401                       root_dir=None, base_dir=None):
    402         return archive_util.make_archive(
    403             base_name, format, root_dir, base_dir, dry_run=self.dry_run)
    404 
    405 
    406     def make_file (self, infiles, outfile, func, args,
    407                    exec_msg=None, skip_msg=None, level=1):
     388    def make_archive(self, base_name, format, root_dir=None, base_dir=None,
     389                     owner=None, group=None):
     390        return archive_util.make_archive(base_name, format, root_dir,
     391                                         base_dir, dry_run=self.dry_run,
     392                                         owner=owner, group=group)
     393
     394    def make_file(self, infiles, outfile, func, args,
     395                  exec_msg=None, skip_msg=None, level=1):
    408396        """Special case of 'execute()' for operations that process one or
    409397        more input files and generate one output file.  Works just like
     
    414402        timestamp checks.
    415403        """
     404        if skip_msg is None:
     405            skip_msg = "skipping %s (inputs unchanged)" % outfile
     406
     407        # Allow 'infiles' to be a single string
     408        if isinstance(infiles, str):
     409            infiles = (infiles,)
     410        elif not isinstance(infiles, (list, tuple)):
     411            raise TypeError, \
     412                  "'infiles' must be a string, or a list or tuple of strings"
     413
    416414        if exec_msg is None:
    417415            exec_msg = "generating %s from %s" % \
    418                        (outfile, string.join(infiles, ', '))
    419         if skip_msg is None:
    420             skip_msg = "skipping %s (inputs unchanged)" % outfile
    421 
    422 
    423         # Allow 'infiles' to be a single string
    424         if type(infiles) is StringType:
    425             infiles = (infiles,)
    426         elif type(infiles) not in (ListType, TupleType):
    427             raise TypeError, \
    428                   "'infiles' must be a string, or a list or tuple of strings"
     416                       (outfile, ', '.join(infiles))
    429417
    430418        # If 'outfile' must be regenerated (either because it doesn't
    431419        # exist, is out-of-date, or the 'force' flag is true) then
    432420        # perform the action that presumably regenerates it
    433         if self.force or dep_util.newer_group (infiles, outfile):
     421        if self.force or dep_util.newer_group(infiles, outfile):
    434422            self.execute(func, args, exec_msg, level)
    435423
     
    437425        else:
    438426            log.debug(skip_msg)
    439 
    440     # make_file ()
    441 
    442 # class Command
    443 
    444427
    445428# XXX 'install_misc' class not currently used -- it was the base class for
     
    448431# for the time being.
    449432
    450 class install_misc (Command):
     433class install_misc(Command):
    451434    """Common base class for installing some files in a subdirectory.
    452435    Currently used by install_data and install_scripts.
     
    459442        self.outfiles = []
    460443
    461     def _install_dir_from (self, dirname):
     444    def _install_dir_from(self, dirname):
    462445        self.set_undefined_options('install', (dirname, 'install_dir'))
    463446
    464     def _copy_files (self, filelist):
     447    def _copy_files(self, filelist):
    465448        self.outfiles = []
    466449        if not filelist:
     
    471454            self.outfiles.append(os.path.join(self.install_dir, f))
    472455
    473     def get_outputs (self):
     456    def get_outputs(self):
    474457        return self.outfiles
    475 
    476 
    477 if __name__ == "__main__":
    478     print "ok"
Note: See TracChangeset for help on using the changeset viewer.