Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame^] | 1 | """Main command line parser. Implements the pysetup script.""" |
| 2 | |
| 3 | import os |
| 4 | import re |
| 5 | import sys |
| 6 | import getopt |
| 7 | import logging |
| 8 | |
| 9 | from packaging import logger |
| 10 | from packaging.dist import Distribution |
| 11 | from packaging.util import _is_archive_file |
| 12 | from packaging.command import get_command_class, STANDARD_COMMANDS |
| 13 | from packaging.install import install, install_local_project, remove |
| 14 | from packaging.database import get_distribution, get_distributions |
| 15 | from packaging.depgraph import generate_graph |
| 16 | from packaging.fancy_getopt import FancyGetopt |
| 17 | from packaging.errors import (PackagingArgError, PackagingError, |
| 18 | PackagingModuleError, PackagingClassError, |
| 19 | CCompilerError) |
| 20 | |
| 21 | |
| 22 | command_re = re.compile(r'^[a-zA-Z]([a-zA-Z0-9_]*)$') |
| 23 | |
| 24 | common_usage = """\ |
| 25 | Actions: |
| 26 | %(actions)s |
| 27 | |
| 28 | To get more help on an action, use: |
| 29 | |
| 30 | pysetup action --help |
| 31 | """ |
| 32 | |
| 33 | create_usage = """\ |
| 34 | Usage: pysetup create |
| 35 | or: pysetup create --help |
| 36 | |
| 37 | Create a new Python package. |
| 38 | """ |
| 39 | |
| 40 | graph_usage = """\ |
| 41 | Usage: pysetup graph dist |
| 42 | or: pysetup graph --help |
| 43 | |
| 44 | Print dependency graph for the distribution. |
| 45 | |
| 46 | positional arguments: |
| 47 | dist installed distribution name |
| 48 | """ |
| 49 | |
| 50 | install_usage = """\ |
| 51 | Usage: pysetup install [dist] |
| 52 | or: pysetup install [archive] |
| 53 | or: pysetup install [src_dir] |
| 54 | or: pysetup install --help |
| 55 | |
| 56 | Install a Python distribution from the indexes, source directory, or sdist. |
| 57 | |
| 58 | positional arguments: |
| 59 | archive path to source distribution (zip, tar.gz) |
| 60 | dist distribution name to install from the indexes |
| 61 | scr_dir path to source directory |
| 62 | |
| 63 | """ |
| 64 | |
| 65 | metadata_usage = """\ |
| 66 | Usage: pysetup metadata [dist] [-f field ...] |
| 67 | or: pysetup metadata [dist] [--all] |
| 68 | or: pysetup metadata --help |
| 69 | |
| 70 | Print metadata for the distribution. |
| 71 | |
| 72 | positional arguments: |
| 73 | dist installed distribution name |
| 74 | |
| 75 | optional arguments: |
| 76 | -f metadata field to print |
| 77 | --all print all metadata fields |
| 78 | """ |
| 79 | |
| 80 | remove_usage = """\ |
| 81 | Usage: pysetup remove dist [-y] |
| 82 | or: pysetup remove --help |
| 83 | |
| 84 | Uninstall a Python distribution. |
| 85 | |
| 86 | positional arguments: |
| 87 | dist installed distribution name |
| 88 | |
| 89 | optional arguments: |
| 90 | -y auto confirm package removal |
| 91 | """ |
| 92 | |
| 93 | run_usage = """\ |
| 94 | Usage: pysetup run [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...] |
| 95 | or: pysetup run --help |
| 96 | or: pysetup run --list-commands |
| 97 | or: pysetup run cmd --help |
| 98 | """ |
| 99 | |
| 100 | list_usage = """\ |
| 101 | Usage: pysetup list dist [dist ...] |
| 102 | or: pysetup list --help |
| 103 | or: pysetup list --all |
| 104 | |
| 105 | Print name, version and location for the matching installed distributions. |
| 106 | |
| 107 | positional arguments: |
| 108 | dist installed distribution name |
| 109 | |
| 110 | optional arguments: |
| 111 | --all list all installed distributions |
| 112 | """ |
| 113 | |
| 114 | search_usage = """\ |
| 115 | Usage: pysetup search [project] [--simple [url]] [--xmlrpc [url] [--fieldname value ...] --operator or|and] |
| 116 | or: pysetup search --help |
| 117 | |
| 118 | Search the indexes for the matching projects. |
| 119 | |
| 120 | positional arguments: |
| 121 | project the project pattern to search for |
| 122 | |
| 123 | optional arguments: |
| 124 | --xmlrpc [url] wether to use the xmlrpc index or not. If an url is |
| 125 | specified, it will be used rather than the default one. |
| 126 | |
| 127 | --simple [url] wether to use the simple index or not. If an url is |
| 128 | specified, it will be used rather than the default one. |
| 129 | |
| 130 | --fieldname value Make a search on this field. Can only be used if |
| 131 | --xmlrpc has been selected or is the default index. |
| 132 | |
| 133 | --operator or|and Defines what is the operator to use when doing xmlrpc |
| 134 | searchs with multiple fieldnames. Can only be used if |
| 135 | --xmlrpc has been selected or is the default index. |
| 136 | """ |
| 137 | |
| 138 | global_options = [ |
| 139 | # The fourth entry for verbose means that it can be repeated. |
| 140 | ('verbose', 'v', "run verbosely (default)", True), |
| 141 | ('quiet', 'q', "run quietly (turns verbosity off)"), |
| 142 | ('dry-run', 'n', "don't actually do anything"), |
| 143 | ('help', 'h', "show detailed help message"), |
| 144 | ('no-user-cfg', None, 'ignore pydistutils.cfg in your home directory'), |
| 145 | ('version', None, 'Display the version'), |
| 146 | ] |
| 147 | |
| 148 | negative_opt = {'quiet': 'verbose'} |
| 149 | |
| 150 | display_options = [ |
| 151 | ('help-commands', None, "list all available commands"), |
| 152 | ] |
| 153 | |
| 154 | display_option_names = [x[0].replace('-', '_') for x in display_options] |
| 155 | |
| 156 | |
| 157 | def _parse_args(args, options, long_options): |
| 158 | """Transform sys.argv input into a dict. |
| 159 | |
| 160 | :param args: the args to parse (i.e sys.argv) |
| 161 | :param options: the list of options to pass to getopt |
| 162 | :param long_options: the list of string with the names of the long options |
| 163 | to be passed to getopt. |
| 164 | |
| 165 | The function returns a dict with options/long_options as keys and matching |
| 166 | values as values. |
| 167 | """ |
| 168 | optlist, args = getopt.gnu_getopt(args, options, long_options) |
| 169 | optdict = {} |
| 170 | optdict['args'] = args |
| 171 | for k, v in optlist: |
| 172 | k = k.lstrip('-') |
| 173 | if k not in optdict: |
| 174 | optdict[k] = [] |
| 175 | if v: |
| 176 | optdict[k].append(v) |
| 177 | else: |
| 178 | optdict[k].append(v) |
| 179 | return optdict |
| 180 | |
| 181 | |
| 182 | class action_help: |
| 183 | """Prints a help message when the standard help flags: -h and --help |
| 184 | are used on the commandline. |
| 185 | """ |
| 186 | |
| 187 | def __init__(self, help_msg): |
| 188 | self.help_msg = help_msg |
| 189 | |
| 190 | def __call__(self, f): |
| 191 | def wrapper(*args, **kwargs): |
| 192 | f_args = args[1] |
| 193 | if '--help' in f_args or '-h' in f_args: |
| 194 | print(self.help_msg) |
| 195 | return |
| 196 | return f(*args, **kwargs) |
| 197 | return wrapper |
| 198 | |
| 199 | |
| 200 | @action_help(create_usage) |
| 201 | def _create(distpatcher, args, **kw): |
| 202 | from packaging.create import main |
| 203 | return main() |
| 204 | |
| 205 | |
| 206 | @action_help(graph_usage) |
| 207 | def _graph(dispatcher, args, **kw): |
| 208 | name = args[1] |
| 209 | dist = get_distribution(name, use_egg_info=True) |
| 210 | if dist is None: |
| 211 | print('Distribution not found.') |
| 212 | else: |
| 213 | dists = get_distributions(use_egg_info=True) |
| 214 | graph = generate_graph(dists) |
| 215 | print(graph.repr_node(dist)) |
| 216 | |
| 217 | |
| 218 | @action_help(install_usage) |
| 219 | def _install(dispatcher, args, **kw): |
| 220 | # first check if we are in a source directory |
| 221 | if len(args) < 2: |
| 222 | # are we inside a project dir? |
| 223 | listing = os.listdir(os.getcwd()) |
| 224 | if 'setup.py' in listing or 'setup.cfg' in listing: |
| 225 | args.insert(1, os.getcwd()) |
| 226 | else: |
| 227 | logger.warning('no project to install') |
| 228 | return |
| 229 | |
| 230 | # installing from a source dir or archive file? |
| 231 | if os.path.isdir(args[1]) or _is_archive_file(args[1]): |
| 232 | install_local_project(args[1]) |
| 233 | else: |
| 234 | # download from PyPI |
| 235 | install(args[1]) |
| 236 | |
| 237 | |
| 238 | @action_help(metadata_usage) |
| 239 | def _metadata(dispatcher, args, **kw): |
| 240 | opts = _parse_args(args[1:], 'f:', ['all']) |
| 241 | if opts['args']: |
| 242 | name = opts['args'][0] |
| 243 | dist = get_distribution(name, use_egg_info=True) |
| 244 | if dist is None: |
| 245 | logger.warning('%s not installed', name) |
| 246 | return |
| 247 | else: |
| 248 | logger.info('searching local dir for metadata') |
| 249 | dist = Distribution() |
| 250 | dist.parse_config_files() |
| 251 | |
| 252 | metadata = dist.metadata |
| 253 | |
| 254 | if 'all' in opts: |
| 255 | keys = metadata.keys() |
| 256 | else: |
| 257 | if 'f' in opts: |
| 258 | keys = (k for k in opts['f'] if k in metadata) |
| 259 | else: |
| 260 | keys = () |
| 261 | |
| 262 | for key in keys: |
| 263 | if key in metadata: |
| 264 | print(metadata._convert_name(key) + ':') |
| 265 | value = metadata[key] |
| 266 | if isinstance(value, list): |
| 267 | for v in value: |
| 268 | print(' ' + v) |
| 269 | else: |
| 270 | print(' ' + value.replace('\n', '\n ')) |
| 271 | |
| 272 | |
| 273 | @action_help(remove_usage) |
| 274 | def _remove(distpatcher, args, **kw): |
| 275 | opts = _parse_args(args[1:], 'y', []) |
| 276 | if 'y' in opts: |
| 277 | auto_confirm = True |
| 278 | else: |
| 279 | auto_confirm = False |
| 280 | |
| 281 | for dist in set(opts['args']): |
| 282 | try: |
| 283 | remove(dist, auto_confirm=auto_confirm) |
| 284 | except PackagingError: |
| 285 | logger.warning('%s not installed', dist) |
| 286 | |
| 287 | |
| 288 | @action_help(run_usage) |
| 289 | def _run(dispatcher, args, **kw): |
| 290 | parser = dispatcher.parser |
| 291 | args = args[1:] |
| 292 | |
| 293 | commands = STANDARD_COMMANDS # + extra commands |
| 294 | |
| 295 | if args == ['--list-commands']: |
| 296 | print('List of available commands:') |
| 297 | cmds = sorted(commands) |
| 298 | |
| 299 | for cmd in cmds: |
| 300 | cls = dispatcher.cmdclass.get(cmd) or get_command_class(cmd) |
| 301 | desc = getattr(cls, 'description', |
| 302 | '(no description available)') |
| 303 | print(' %s: %s' % (cmd, desc)) |
| 304 | return |
| 305 | |
| 306 | while args: |
| 307 | args = dispatcher._parse_command_opts(parser, args) |
| 308 | if args is None: |
| 309 | return |
| 310 | |
| 311 | # create the Distribution class |
| 312 | # need to feed setup.cfg here ! |
| 313 | dist = Distribution() |
| 314 | |
| 315 | # Find and parse the config file(s): they will override options from |
| 316 | # the setup script, but be overridden by the command line. |
| 317 | |
| 318 | # XXX still need to be extracted from Distribution |
| 319 | dist.parse_config_files() |
| 320 | |
| 321 | try: |
| 322 | for cmd in dispatcher.commands: |
| 323 | dist.run_command(cmd, dispatcher.command_options[cmd]) |
| 324 | |
| 325 | except KeyboardInterrupt: |
| 326 | raise SystemExit("interrupted") |
| 327 | except (IOError, os.error, PackagingError, CCompilerError) as msg: |
| 328 | raise SystemExit("error: " + str(msg)) |
| 329 | |
| 330 | # XXX this is crappy |
| 331 | return dist |
| 332 | |
| 333 | |
| 334 | @action_help(list_usage) |
| 335 | def _list(dispatcher, args, **kw): |
| 336 | opts = _parse_args(args[1:], '', ['all']) |
| 337 | dists = get_distributions(use_egg_info=True) |
| 338 | if 'all' in opts: |
| 339 | results = dists |
| 340 | else: |
| 341 | results = [d for d in dists if d.name.lower() in opts['args']] |
| 342 | |
| 343 | for dist in results: |
| 344 | print('%s %s at %s' % (dist.name, dist.metadata['version'], dist.path)) |
| 345 | |
| 346 | |
| 347 | @action_help(search_usage) |
| 348 | def _search(dispatcher, args, **kw): |
| 349 | """The search action. |
| 350 | |
| 351 | It is able to search for a specific index (specified with --index), using |
| 352 | the simple or xmlrpc index types (with --type xmlrpc / --type simple) |
| 353 | """ |
| 354 | opts = _parse_args(args[1:], '', ['simple', 'xmlrpc']) |
| 355 | # 1. what kind of index is requested ? (xmlrpc / simple) |
| 356 | |
| 357 | |
| 358 | actions = [ |
| 359 | ('run', 'Run one or several commands', _run), |
| 360 | ('metadata', 'Display the metadata of a project', _metadata), |
| 361 | ('install', 'Install a project', _install), |
| 362 | ('remove', 'Remove a project', _remove), |
| 363 | ('search', 'Search for a project in the indexes', _search), |
| 364 | ('list', 'Search for local projects', _list), |
| 365 | ('graph', 'Display a graph', _graph), |
| 366 | ('create', 'Create a Project', _create), |
| 367 | ] |
| 368 | |
| 369 | |
| 370 | class Dispatcher: |
| 371 | """Reads the command-line options |
| 372 | """ |
| 373 | def __init__(self, args=None): |
| 374 | self.verbose = 1 |
| 375 | self.dry_run = False |
| 376 | self.help = False |
| 377 | self.script_name = 'pysetup' |
| 378 | self.cmdclass = {} |
| 379 | self.commands = [] |
| 380 | self.command_options = {} |
| 381 | |
| 382 | for attr in display_option_names: |
| 383 | setattr(self, attr, False) |
| 384 | |
| 385 | self.parser = FancyGetopt(global_options + display_options) |
| 386 | self.parser.set_negative_aliases(negative_opt) |
| 387 | # FIXME this parses everything, including command options (e.g. "run |
| 388 | # build -i" errors with "option -i not recognized") |
| 389 | args = self.parser.getopt(args=args, object=self) |
| 390 | |
| 391 | # if first arg is "run", we have some commands |
| 392 | if len(args) == 0: |
| 393 | self.action = None |
| 394 | else: |
| 395 | self.action = args[0] |
| 396 | |
| 397 | allowed = [action[0] for action in actions] + [None] |
| 398 | if self.action not in allowed: |
| 399 | msg = 'Unrecognized action "%s"' % self.action |
| 400 | raise PackagingArgError(msg) |
| 401 | |
| 402 | # setting up the logging level from the command-line options |
| 403 | # -q gets warning, error and critical |
| 404 | if self.verbose == 0: |
| 405 | level = logging.WARNING |
| 406 | # default level or -v gets info too |
| 407 | # XXX there's a bug somewhere: the help text says that -v is default |
| 408 | # (and verbose is set to 1 above), but when the user explicitly gives |
| 409 | # -v on the command line, self.verbose is incremented to 2! Here we |
| 410 | # compensate for that (I tested manually). On a related note, I think |
| 411 | # it's a good thing to use -q/nothing/-v/-vv on the command line |
| 412 | # instead of logging constants; it will be easy to add support for |
| 413 | # logging configuration in setup.cfg for advanced users. --merwok |
| 414 | elif self.verbose in (1, 2): |
| 415 | level = logging.INFO |
| 416 | else: # -vv and more for debug |
| 417 | level = logging.DEBUG |
| 418 | |
| 419 | # for display options we return immediately |
| 420 | option_order = self.parser.get_option_order() |
| 421 | |
| 422 | self.args = args |
| 423 | |
| 424 | if self.help or self.action is None: |
| 425 | self._show_help(self.parser, display_options_=False) |
| 426 | |
| 427 | def _parse_command_opts(self, parser, args): |
| 428 | # Pull the current command from the head of the command line |
| 429 | command = args[0] |
| 430 | if not command_re.match(command): |
| 431 | raise SystemExit("invalid command name %r" % (command,)) |
| 432 | self.commands.append(command) |
| 433 | |
| 434 | # Dig up the command class that implements this command, so we |
| 435 | # 1) know that it's a valid command, and 2) know which options |
| 436 | # it takes. |
| 437 | try: |
| 438 | cmd_class = get_command_class(command) |
| 439 | except PackagingModuleError as msg: |
| 440 | raise PackagingArgError(msg) |
| 441 | |
| 442 | # XXX We want to push this in packaging.command |
| 443 | # |
| 444 | # Require that the command class be derived from Command -- want |
| 445 | # to be sure that the basic "command" interface is implemented. |
| 446 | for meth in ('initialize_options', 'finalize_options', 'run'): |
| 447 | if hasattr(cmd_class, meth): |
| 448 | continue |
| 449 | raise PackagingClassError( |
| 450 | 'command %r must implement %r' % (cmd_class, meth)) |
| 451 | |
| 452 | # Also make sure that the command object provides a list of its |
| 453 | # known options. |
| 454 | if not (hasattr(cmd_class, 'user_options') and |
| 455 | isinstance(cmd_class.user_options, list)): |
| 456 | raise PackagingClassError( |
| 457 | "command class %s must provide " |
| 458 | "'user_options' attribute (a list of tuples)" % cmd_class) |
| 459 | |
| 460 | # If the command class has a list of negative alias options, |
| 461 | # merge it in with the global negative aliases. |
| 462 | _negative_opt = negative_opt.copy() |
| 463 | |
| 464 | if hasattr(cmd_class, 'negative_opt'): |
| 465 | _negative_opt.update(cmd_class.negative_opt) |
| 466 | |
| 467 | # Check for help_options in command class. They have a different |
| 468 | # format (tuple of four) so we need to preprocess them here. |
| 469 | if (hasattr(cmd_class, 'help_options') and |
| 470 | isinstance(cmd_class.help_options, list)): |
| 471 | help_options = cmd_class.help_options[:] |
| 472 | else: |
| 473 | help_options = [] |
| 474 | |
| 475 | # All commands support the global options too, just by adding |
| 476 | # in 'global_options'. |
| 477 | parser.set_option_table(global_options + |
| 478 | cmd_class.user_options + |
| 479 | help_options) |
| 480 | parser.set_negative_aliases(_negative_opt) |
| 481 | args, opts = parser.getopt(args[1:]) |
| 482 | |
| 483 | if hasattr(opts, 'help') and opts.help: |
| 484 | self._show_command_help(cmd_class) |
| 485 | return |
| 486 | |
| 487 | if (hasattr(cmd_class, 'help_options') and |
| 488 | isinstance(cmd_class.help_options, list)): |
| 489 | help_option_found = False |
| 490 | for help_option, short, desc, func in cmd_class.help_options: |
| 491 | if hasattr(opts, help_option.replace('-', '_')): |
| 492 | help_option_found = True |
| 493 | if hasattr(func, '__call__'): |
| 494 | func() |
| 495 | else: |
| 496 | raise PackagingClassError( |
| 497 | "invalid help function %r for help option %r: " |
| 498 | "must be a callable object (function, etc.)" |
| 499 | % (func, help_option)) |
| 500 | |
| 501 | if help_option_found: |
| 502 | return |
| 503 | |
| 504 | # Put the options from the command line into their official |
| 505 | # holding pen, the 'command_options' dictionary. |
| 506 | opt_dict = self.get_option_dict(command) |
| 507 | for name, value in vars(opts).items(): |
| 508 | opt_dict[name] = ("command line", value) |
| 509 | |
| 510 | return args |
| 511 | |
| 512 | def get_option_dict(self, command): |
| 513 | """Get the option dictionary for a given command. If that |
| 514 | command's option dictionary hasn't been created yet, then create it |
| 515 | and return the new dictionary; otherwise, return the existing |
| 516 | option dictionary. |
| 517 | """ |
| 518 | d = self.command_options.get(command) |
| 519 | if d is None: |
| 520 | d = self.command_options[command] = {} |
| 521 | return d |
| 522 | |
| 523 | def show_help(self): |
| 524 | self._show_help(self.parser) |
| 525 | |
| 526 | def print_usage(self, parser): |
| 527 | parser.set_option_table(global_options) |
| 528 | |
| 529 | actions_ = [' %s: %s' % (name, desc) for name, desc, __ in actions] |
| 530 | usage = common_usage % {'actions': '\n'.join(actions_)} |
| 531 | |
| 532 | parser.print_help(usage + "\nGlobal options:") |
| 533 | |
| 534 | def _show_help(self, parser, global_options_=True, display_options_=True, |
| 535 | commands=[]): |
| 536 | # late import because of mutual dependence between these modules |
| 537 | from packaging.command.cmd import Command |
| 538 | |
| 539 | print('Usage: pysetup [options] action [action_options]') |
| 540 | print('') |
| 541 | if global_options_: |
| 542 | self.print_usage(self.parser) |
| 543 | print('') |
| 544 | |
| 545 | if display_options_: |
| 546 | parser.set_option_table(display_options) |
| 547 | parser.print_help( |
| 548 | "Information display options (just display " + |
| 549 | "information, ignore any commands)") |
| 550 | print('') |
| 551 | |
| 552 | for command in commands: |
| 553 | if isinstance(command, type) and issubclass(command, Command): |
| 554 | cls = command |
| 555 | else: |
| 556 | cls = get_command_class(command) |
| 557 | if (hasattr(cls, 'help_options') and |
| 558 | isinstance(cls.help_options, list)): |
| 559 | parser.set_option_table(cls.user_options + cls.help_options) |
| 560 | else: |
| 561 | parser.set_option_table(cls.user_options) |
| 562 | |
| 563 | parser.print_help("Options for %r command:" % cls.__name__) |
| 564 | print('') |
| 565 | |
| 566 | def _show_command_help(self, command): |
| 567 | if isinstance(command, str): |
| 568 | command = get_command_class(command) |
| 569 | |
| 570 | name = command.get_command_name() |
| 571 | |
| 572 | desc = getattr(command, 'description', '(no description available)') |
| 573 | print('Description: %s' % desc) |
| 574 | print('') |
| 575 | |
| 576 | if (hasattr(command, 'help_options') and |
| 577 | isinstance(command.help_options, list)): |
| 578 | self.parser.set_option_table(command.user_options + |
| 579 | command.help_options) |
| 580 | else: |
| 581 | self.parser.set_option_table(command.user_options) |
| 582 | |
| 583 | self.parser.print_help("Options:") |
| 584 | print('') |
| 585 | |
| 586 | def _get_command_groups(self): |
| 587 | """Helper function to retrieve all the command class names divided |
| 588 | into standard commands (listed in |
| 589 | packaging.command.STANDARD_COMMANDS) and extra commands (given in |
| 590 | self.cmdclass and not standard commands). |
| 591 | """ |
| 592 | extra_commands = [cmd for cmd in self.cmdclass |
| 593 | if cmd not in STANDARD_COMMANDS] |
| 594 | return STANDARD_COMMANDS, extra_commands |
| 595 | |
| 596 | def print_commands(self): |
| 597 | """Print out a help message listing all available commands with a |
| 598 | description of each. The list is divided into standard commands |
| 599 | (listed in packaging.command.STANDARD_COMMANDS) and extra commands |
| 600 | (given in self.cmdclass and not standard commands). The |
| 601 | descriptions come from the command class attribute |
| 602 | 'description'. |
| 603 | """ |
| 604 | std_commands, extra_commands = self._get_command_groups() |
| 605 | max_length = max(len(command) |
| 606 | for commands in (std_commands, extra_commands) |
| 607 | for command in commands) |
| 608 | |
| 609 | self.print_command_list(std_commands, "Standard commands", max_length) |
| 610 | if extra_commands: |
| 611 | print() |
| 612 | self.print_command_list(extra_commands, "Extra commands", |
| 613 | max_length) |
| 614 | |
| 615 | def print_command_list(self, commands, header, max_length): |
| 616 | """Print a subset of the list of all commands -- used by |
| 617 | 'print_commands()'. |
| 618 | """ |
| 619 | print(header + ":") |
| 620 | |
| 621 | for cmd in commands: |
| 622 | cls = self.cmdclass.get(cmd) or get_command_class(cmd) |
| 623 | description = getattr(cls, 'description', |
| 624 | '(no description available)') |
| 625 | |
| 626 | print(" %-*s %s" % (max_length, cmd, description)) |
| 627 | |
| 628 | def __call__(self): |
| 629 | if self.action is None: |
| 630 | return |
| 631 | for action, desc, func in actions: |
| 632 | if action == self.action: |
| 633 | return func(self, self.args) |
| 634 | return -1 |
| 635 | |
| 636 | |
| 637 | def main(args=None): |
| 638 | dispatcher = Dispatcher(args) |
| 639 | if dispatcher.action is None: |
| 640 | return |
| 641 | |
| 642 | return dispatcher() |
| 643 | |
| 644 | if __name__ == '__main__': |
| 645 | sys.exit(main()) |