summaryrefslogtreecommitdiffstats
path: root/Lib/packaging/command/check.py
blob: 94c4a97c15cba4c3c377f493a0f602f32f3cd301 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""Check PEP compliance of metadata."""

from packaging import logger
from packaging.command.cmd import Command
from packaging.errors import PackagingSetupError
from packaging.util import resolve_name

class check(Command):

    description = "check PEP compliance of metadata"

    user_options = [('metadata', 'm', 'Verify metadata'),
                    ('all', 'a',
                     ('runs extended set of checks')),
                    ('strict', 's',
                     'Will exit with an error if a check fails')]

    boolean_options = ['metadata', 'all', 'strict']

    def initialize_options(self):
        """Sets default values for options."""
        self.all = False
        self.metadata = True
        self.strict = False
        self._warnings = []

    def finalize_options(self):
        pass

    def warn(self, msg, *args):
        """Wrapper around logging that also remembers messages."""
        # XXX we could use a special handler for this, but would need to test
        # if it works even if the logger has a too high level
        self._warnings.append((msg, args))
        return logger.warning(self.get_command_name() + msg, *args)

    def run(self):
        """Runs the command."""
        # perform the various tests
        if self.metadata:
            self.check_metadata()
        if self.all:
            self.check_restructuredtext()
            self.check_hooks_resolvable()

        # let's raise an error in strict mode, if we have at least
        # one warning
        if self.strict and len(self._warnings) > 0:
            msg = '\n'.join(msg % args for msg, args in self._warnings)
            raise PackagingSetupError(msg)

    def check_metadata(self):
        """Ensures that all required elements of metadata are supplied.

        name, version, URL, author

        Warns if any are missing.
        """
        missing, warnings = self.distribution.metadata.check(strict=True)
        if missing != []:
            self.warn('missing required metadata: %s', ', '.join(missing))
        for warning in warnings:
            self.warn(warning)

    def check_restructuredtext(self):
        """Checks if the long string fields are reST-compliant."""
        missing, warnings = self.distribution.metadata.check(restructuredtext=True)
        if self.distribution.metadata.docutils_support:
            for warning in warnings:
                line = warning[-1].get('line')
                if line is None:
                    warning = warning[1]
                else:
                    warning = '%s (line %s)' % (warning[1], line)
                self.warn(warning)
        elif self.strict:
            raise PackagingSetupError('The docutils package is needed.')

    def check_hooks_resolvable(self):
        for options in self.distribution.command_options.values():
            for hook_kind in ("pre_hook", "post_hook"):
                if hook_kind not in options:
                    break
                for hook_name in options[hook_kind][1].values():
                    try:
                        resolve_name(hook_name)
                    except ImportError:
                        self.warn('name %r cannot be resolved', hook_name)