ยปCore Development>Code coverage>Lib/packaging/run.py

Python code coverage for Lib/packaging/run.py

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