cmd.py (7974B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\stdlib\distutils\cmd.py 2 __revision__ = '$Id: cmd.py 82110 2010-06-20 13:38:51Z kristjan.jonsson $' 3 import sys, os, re 4 from distutils.errors import DistutilsOptionError 5 from distutils import util, dir_util, file_util, archive_util, dep_util 6 from distutils import log 7 8 class Command(): 9 sub_commands = [] 10 11 def __init__(self, dist): 12 from distutils.dist import Distribution 13 if not isinstance(dist, Distribution): 14 raise TypeError, 'dist must be a Distribution instance' 15 if self.__class__ is Command: 16 raise RuntimeError, 'Command is an abstract class' 17 self.distribution = dist 18 self.initialize_options() 19 self._dry_run = None 20 self.verbose = dist.verbose 21 self.force = None 22 self.help = 0 23 self.finalized = 0 24 25 def __getattr__(self, attr): 26 if attr == 'dry_run': 27 myval = getattr(self, '_' + attr) 28 if myval is None: 29 return getattr(self.distribution, attr) 30 else: 31 return myval 32 else: 33 raise AttributeError, attr 34 35 def ensure_finalized(self): 36 if not self.finalized: 37 self.finalize_options() 38 self.finalized = 1 39 40 def initialize_options(self): 41 raise RuntimeError, 'abstract method -- subclass %s must override' % self.__class__ 42 43 def finalize_options(self): 44 raise RuntimeError, 'abstract method -- subclass %s must override' % self.__class__ 45 46 def dump_options(self, header = None, indent = ''): 47 from distutils.fancy_getopt import longopt_xlate 48 if header is None: 49 header = "command options for '%s':" % self.get_command_name() 50 self.announce(indent + header, level=log.INFO) 51 indent = indent + ' ' 52 for option, _, _ in self.user_options: 53 option = option.translate(longopt_xlate) 54 if option[-1] == '=': 55 option = option[:-1] 56 value = getattr(self, option) 57 self.announce(indent + '%s = %s' % (option, value), level=log.INFO) 58 59 def run(self): 60 raise RuntimeError, 'abstract method -- subclass %s must override' % self.__class__ 61 62 def announce(self, msg, level = 1): 63 log.log(level, msg) 64 65 def debug_print(self, msg): 66 from distutils.debug import DEBUG 67 if DEBUG: 68 print msg 69 sys.stdout.flush() 70 71 def _ensure_stringlike(self, option, what, default = None): 72 val = getattr(self, option) 73 if val is None: 74 setattr(self, option, default) 75 return default 76 if not isinstance(val, str): 77 raise DistutilsOptionError, "'%s' must be a %s (got `%s`)" % (option, what, val) 78 return val 79 80 def ensure_string(self, option, default = None): 81 self._ensure_stringlike(option, 'string', default) 82 83 def ensure_string_list(self, option): 84 val = getattr(self, option) 85 if val is None: 86 return 87 if isinstance(val, str): 88 setattr(self, option, re.split(',\\s*|\\s+', val)) 89 else: 90 if isinstance(val, list): 91 ok = 1 92 for element in val: 93 if not isinstance(element, str): 94 ok = 0 95 break 96 97 else: 98 ok = 0 99 if not ok: 100 raise DistutilsOptionError, "'%s' must be a list of strings (got %r)" % (option, val) 101 102 def _ensure_tested_string(self, option, tester, what, error_fmt, default = None): 103 val = self._ensure_stringlike(option, what, default) 104 if val is not None and not tester(val): 105 raise DistutilsOptionError, ("error in '%s' option: " + error_fmt) % (option, val) 106 107 def ensure_filename(self, option): 108 self._ensure_tested_string(option, os.path.isfile, 'filename', "'%s' does not exist or is not a file") 109 110 def ensure_dirname(self, option): 111 self._ensure_tested_string(option, os.path.isdir, 'directory name', "'%s' does not exist or is not a directory") 112 113 def get_command_name(self): 114 if hasattr(self, 'command_name'): 115 return self.command_name 116 else: 117 return self.__class__.__name__ 118 119 def set_undefined_options(self, src_cmd, *option_pairs): 120 src_cmd_obj = self.distribution.get_command_obj(src_cmd) 121 src_cmd_obj.ensure_finalized() 122 for src_option, dst_option in option_pairs: 123 if getattr(self, dst_option) is None: 124 setattr(self, dst_option, getattr(src_cmd_obj, src_option)) 125 126 def get_finalized_command(self, command, create = 1): 127 cmd_obj = self.distribution.get_command_obj(command, create) 128 cmd_obj.ensure_finalized() 129 return cmd_obj 130 131 def reinitialize_command(self, command, reinit_subcommands = 0): 132 return self.distribution.reinitialize_command(command, reinit_subcommands) 133 134 def run_command(self, command): 135 self.distribution.run_command(command) 136 137 def get_sub_commands(self): 138 commands = [] 139 for cmd_name, method in self.sub_commands: 140 if method is None or method(self): 141 commands.append(cmd_name) 142 143 return commands 144 145 def warn(self, msg): 146 log.warn('warning: %s: %s\n' % (self.get_command_name(), msg)) 147 148 def execute(self, func, args, msg = None, level = 1): 149 util.execute(func, args, msg, dry_run=self.dry_run) 150 151 def mkpath(self, name, mode = 511): 152 dir_util.mkpath(name, mode, dry_run=self.dry_run) 153 154 def copy_file(self, infile, outfile, preserve_mode = 1, preserve_times = 1, link = None, level = 1): 155 return file_util.copy_file(infile, outfile, preserve_mode, preserve_times, not self.force, link, dry_run=self.dry_run) 156 157 def copy_tree(self, infile, outfile, preserve_mode = 1, preserve_times = 1, preserve_symlinks = 0, level = 1): 158 return dir_util.copy_tree(infile, outfile, preserve_mode, preserve_times, preserve_symlinks, not self.force, dry_run=self.dry_run) 159 160 def move_file(self, src, dst, level = 1): 161 return file_util.move_file(src, dst, dry_run=self.dry_run) 162 163 def spawn(self, cmd, search_path = 1, level = 1): 164 from distutils.spawn import spawn 165 spawn(cmd, search_path, dry_run=self.dry_run) 166 167 def make_archive(self, base_name, format, root_dir = None, base_dir = None, owner = None, group = None): 168 return archive_util.make_archive(base_name, format, root_dir, base_dir, dry_run=self.dry_run, owner=owner, group=group) 169 170 def make_file(self, infiles, outfile, func, args, exec_msg = None, skip_msg = None, level = 1): 171 if skip_msg is None: 172 skip_msg = 'skipping %s (inputs unchanged)' % outfile 173 if isinstance(infiles, str): 174 infiles = (infiles,) 175 elif not isinstance(infiles, (list, tuple)): 176 raise TypeError, "'infiles' must be a string, or a list or tuple of strings" 177 if exec_msg is None: 178 exec_msg = 'generating %s from %s' % (outfile, ', '.join(infiles)) 179 if self.force or dep_util.newer_group(infiles, outfile): 180 self.execute(func, args, exec_msg, level) 181 else: 182 log.debug(skip_msg) 183 184 185 class install_misc(Command): 186 user_options = [('install-dir=', 'd', 'directory to install the files to')] 187 188 def initialize_options(self): 189 self.install_dir = None 190 self.outfiles = [] 191 192 def _install_dir_from(self, dirname): 193 self.set_undefined_options('install', (dirname, 'install_dir')) 194 195 def _copy_files(self, filelist): 196 self.outfiles = [] 197 if not filelist: 198 return 199 self.mkpath(self.install_dir) 200 for f in filelist: 201 self.copy_file(f, self.install_dir) 202 self.outfiles.append(os.path.join(self.install_dir, f)) 203 204 def get_outputs(self): 205 return self.outfiles