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