Changeset 391 for python/trunk/Lib/distutils/cmd.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/distutils/cmd.py
r2 r391 5 5 """ 6 6 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 9 import sys, os, re 10 from distutils.errors import DistutilsOptionError 14 11 from distutils import util, dir_util, file_util, archive_util, dep_util 15 12 from distutils import log … … 50 47 # -- Creation/initialization methods ------------------------------- 51 48 52 def __init__ 49 def __init__(self, dist): 53 50 """Create and initialize a new Command object. Most importantly, 54 51 invokes the 'initialize_options()' method, which is the real … … 97 94 self.finalized = 0 98 95 99 # __init__ ()100 101 102 96 # XXX A more explicit way to customize dry_run would be better. 103 104 def __getattr__ (self, attr): 97 def __getattr__(self, attr): 105 98 if attr == 'dry_run': 106 99 myval = getattr(self, "_" + attr) … … 112 105 raise AttributeError, attr 113 106 114 115 def ensure_finalized (self): 107 def ensure_finalized(self): 116 108 if not self.finalized: 117 109 self.finalize_options() 118 110 self.finalized = 1 119 120 111 121 112 # Subclasses must define: … … 132 123 # controlled by the command's various option values 133 124 134 def initialize_options 125 def initialize_options(self): 135 126 """Set default values for all the options that this command 136 127 supports. Note that these defaults may be overridden by other … … 145 136 "abstract method -- subclass %s must override" % self.__class__ 146 137 147 def finalize_options 138 def finalize_options(self): 148 139 """Set final values for all the options that this command supports. 149 140 This is always called as late as possible, ie. after any option … … 160 151 161 152 162 def dump_options 153 def dump_options(self, header=None, indent=""): 163 154 from distutils.fancy_getopt import longopt_xlate 164 155 if header is None: 165 156 header = "command options for '%s':" % self.get_command_name() 166 print indent + header157 self.announce(indent + header, level=log.INFO) 167 158 indent = indent + " " 168 159 for (option, _, _) in self.user_options: 169 option = string.translate(option,longopt_xlate)160 option = option.translate(longopt_xlate) 170 161 if option[-1] == "=": 171 162 option = option[:-1] 172 163 value = getattr(self, option) 173 print indent + "%s = %s" % (option, value)174 175 176 def run 164 self.announce(indent + "%s = %s" % (option, value), 165 level=log.INFO) 166 167 def run(self): 177 168 """A command's raison d'etre: carry out the action it exists to 178 169 perform, controlled by the options initialized in … … 184 175 This method must be implemented by all command classes. 185 176 """ 186 187 177 raise RuntimeError, \ 188 178 "abstract method -- subclass %s must override" % self.__class__ 189 179 190 def announce 180 def announce(self, msg, level=1): 191 181 """If the current verbosity level is of greater than or equal to 192 182 'level' print 'msg' to stdout. … … 194 184 log.log(level, msg) 195 185 196 def debug_print 186 def debug_print(self, msg): 197 187 """Print 'msg' to stdout if the global DEBUG (taken from the 198 188 DISTUTILS_DEBUG environment variable) flag is true. … … 202 192 print msg 203 193 sys.stdout.flush() 204 205 194 206 195 … … 218 207 # a list of strings. 219 208 220 def _ensure_stringlike 209 def _ensure_stringlike(self, option, what, default=None): 221 210 val = getattr(self, option) 222 211 if val is None: 223 212 setattr(self, option, default) 224 213 return default 225 elif type(val) is not StringType:214 elif not isinstance(val, str): 226 215 raise DistutilsOptionError, \ 227 216 "'%s' must be a %s (got `%s`)" % (option, what, val) 228 217 return val 229 218 230 def ensure_string 219 def ensure_string(self, option, default=None): 231 220 """Ensure that 'option' is a string; if not defined, set it to 232 221 'default'. … … 234 223 self._ensure_stringlike(option, "string", default) 235 224 236 def ensure_string_list 225 def ensure_string_list(self, option): 237 226 """Ensure that 'option' is a list of strings. If 'option' is 238 227 currently a string, we split it either on /,\s*/ or /\s+/, so … … 243 232 if val is None: 244 233 return 245 elif type(val) is StringType:234 elif isinstance(val, str): 246 235 setattr(self, option, re.split(r',\s*|\s+', val)) 247 236 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 251 244 else: 252 245 ok = 0 … … 254 247 if not ok: 255 248 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): 261 255 val = self._ensure_stringlike(option, what, default) 262 256 if val is not None and not tester(val): … … 264 258 ("error in '%s' option: " + error_fmt) % (option, val) 265 259 266 def ensure_filename 260 def ensure_filename(self, option): 267 261 """Ensure that 'option' is the name of an existing file.""" 268 262 self._ensure_tested_string(option, os.path.isfile, … … 270 264 "'%s' does not exist or is not a file") 271 265 272 def ensure_dirname 266 def ensure_dirname(self, option): 273 267 self._ensure_tested_string(option, os.path.isdir, 274 268 "directory name", … … 278 272 # -- Convenience methods for commands ------------------------------ 279 273 280 def get_command_name 274 def get_command_name(self): 281 275 if hasattr(self, 'command_name'): 282 276 return self.command_name … … 284 278 return self.__class__.__name__ 285 279 286 287 def set_undefined_options (self, src_cmd, *option_pairs): 280 def set_undefined_options(self, src_cmd, *option_pairs): 288 281 """Set the values of any "undefined" options from corresponding 289 282 option values in some other command object. "Undefined" here means … … 310 303 311 304 312 def get_finalized_command 305 def get_finalized_command(self, command, create=1): 313 306 """Wrapper around Distribution's 'get_command_obj()' method: find 314 307 (create if necessary and 'create' is true) the command object for … … 322 315 # XXX rename to 'get_reinitialized_command()'? (should do the 323 316 # same in dist.py, if so) 324 def reinitialize_command 317 def reinitialize_command(self, command, reinit_subcommands=0): 325 318 return self.distribution.reinitialize_command( 326 319 command, reinit_subcommands) 327 320 328 def run_command 321 def run_command(self, command): 329 322 """Run some other command: uses the 'run_command()' method of 330 323 Distribution, which creates and finalizes the command object if … … 333 326 self.distribution.run_command(command) 334 327 335 336 def get_sub_commands (self): 328 def get_sub_commands(self): 337 329 """Determine the sub-commands that are relevant in the current 338 330 distribution (ie., that need to be run). This is based on the … … 350 342 # -- External world manipulation ----------------------------------- 351 343 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): 358 349 util.execute(func, args, msg, dry_run=self.dry_run) 359 350 360 361 def mkpath (self, name, mode=0777): 351 def mkpath(self, name, mode=0777): 362 352 dir_util.mkpath(name, mode, dry_run=self.dry_run) 363 353 364 365 def copy_file (self, infile, outfile, 354 def copy_file(self, infile, outfile, 366 355 preserve_mode=1, preserve_times=1, link=None, level=1): 367 356 """Copy a file respecting verbose, dry-run and force flags. (The … … 376 365 dry_run=self.dry_run) 377 366 378 379 def copy_tree (self, infile, outfile, 367 def copy_tree(self, infile, outfile, 380 368 preserve_mode=1, preserve_times=1, preserve_symlinks=0, 381 369 level=1): … … 390 378 391 379 def move_file (self, src, dst, level=1): 392 """Move a file respectin dry-run flag."""380 """Move a file respecting dry-run flag.""" 393 381 return file_util.move_file(src, dst, dry_run = self.dry_run) 394 382 … … 398 386 spawn(cmd, search_path, dry_run= self.dry_run) 399 387 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 407 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): 408 396 """Special case of 'execute()' for operations that process one or 409 397 more input files and generate one output file. Works just like … … 414 402 timestamp checks. 415 403 """ 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 416 414 if exec_msg is None: 417 415 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)) 429 417 430 418 # If 'outfile' must be regenerated (either because it doesn't 431 419 # exist, is out-of-date, or the 'force' flag is true) then 432 420 # perform the action that presumably regenerates it 433 if self.force or dep_util.newer_group 421 if self.force or dep_util.newer_group(infiles, outfile): 434 422 self.execute(func, args, exec_msg, level) 435 423 … … 437 425 else: 438 426 log.debug(skip_msg) 439 440 # make_file ()441 442 # class Command443 444 427 445 428 # XXX 'install_misc' class not currently used -- it was the base class for … … 448 431 # for the time being. 449 432 450 class install_misc 433 class install_misc(Command): 451 434 """Common base class for installing some files in a subdirectory. 452 435 Currently used by install_data and install_scripts. … … 459 442 self.outfiles = [] 460 443 461 def _install_dir_from 444 def _install_dir_from(self, dirname): 462 445 self.set_undefined_options('install', (dirname, 'install_dir')) 463 446 464 def _copy_files 447 def _copy_files(self, filelist): 465 448 self.outfiles = [] 466 449 if not filelist: … … 471 454 self.outfiles.append(os.path.join(self.install_dir, f)) 472 455 473 def get_outputs 456 def get_outputs(self): 474 457 return self.outfiles 475 476 477 if __name__ == "__main__":478 print "ok"
Note:
See TracChangeset
for help on using the changeset viewer.