aboutsummaryrefslogtreecommitdiff
blob: 97e6e22be2b16f70558de66e5558d4f817055ccd (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import os
import re
import subprocess

from pkgcore.operations import observer as observer_mod
from pkgcore.restrictions import packages, values
from pkgcore.util.parserestrict import parse_match
from snakeoil.cli import arghparse

from .. import cli, git
from .argparsers import cwd_repo_argparser

manifest = cli.ArgumentParser(
    prog='pkgdev manifest', description='update package manifests',
    parents=(cwd_repo_argparser,))
manifest.add_argument(
    'target', nargs='*',
    help='packages to target',
    docs="""
        Packages matching any of these restrictions will have their manifest
        entries updated. If no target is specified a path restriction is
        created based on the current working directory. In other words, if
        ``pkgdev manifest`` is run within an ebuild's directory, all the
        ebuilds within that directory will be manifested.
    """)
manifest_opts = manifest.add_argument_group('manifest options')
manifest_opts.add_argument(
    '-d', '--distdir', type=arghparse.create_dir, help='target download directory',
    docs="""
        Use a specified target directory for downloads instead of the
        configured DISTDIR.
    """)
manifest_opts.add_argument(
    '-f', '--force', help='forcibly remanifest packages',
    action='store_true',
    docs="""
        Force package manifest files to be rewritten. Note that this requires
        downloading all distfiles.
    """)
manifest_opts.add_argument(
    '-m', '--mirrors', help='enable fetching from Gentoo mirrors',
    action='store_true',
    docs="""
        Enable checking Gentoo mirrors first for distfiles. This is disabled by
        default because manifest generation is often performed when adding new
        ebuilds with distfiles that aren't on Gentoo mirrors yet.
    """)
manifest_opts.add_argument(
    '--if-modified', dest='if_modified', help='Only check packages that have uncommitted modifications',
    action='store_true',
    docs="""
        In addition to matching the specified restriction, restrict to targets
        which are marked as modified by git, including untracked files.
    """)
manifest_opts.add_argument(
    '--ignore-fetch-restricted', dest='ignore_fetch_restricted', help='Ignore fetch restricted ebuilds',
    action='store_true',
    docs="""
        Ignore attempting to update manifest entries for ebuilds which are
        fetch restricted.
    """)


def _restrict_targets(repo, targets):
    restrictions = []
    for target in targets:
        if os.path.exists(target):
            try:
                restrictions.append(repo.path_restrict(target))
            except ValueError as e:
                manifest.error(e)
        else:
            try:
                restrictions.append(parse_match(target))
            except ValueError:
                manifest.error(f'invalid atom: {target!r}')
    return packages.OrRestriction(*restrictions)


def _restrict_modified_files(repo):
    ebuild_re = re.compile(r'^[ MTARC?]{2} (?P<path>[^/]+/[^/]+/[^/]+\.ebuild)$')
    p = git.run('status', '--porcelain=v1', '-z', "*.ebuild",
                cwd=repo.location, stdout=subprocess.PIPE)

    restrictions = []
    for line in p.stdout.strip('\x00').split('\x00'):
        if mo := ebuild_re.match(line):
            restrictions.append(repo.path_restrict(mo.group('path')))
    return packages.OrRestriction(*restrictions)


@manifest.bind_final_check
def _manifest_validate(parser, namespace):
    targets = namespace.target if namespace.target else [namespace.cwd]

    restrictions = [_restrict_targets(namespace.repo, targets)]
    if namespace.if_modified:
        restrictions.append(_restrict_modified_files(namespace.repo))
    if namespace.ignore_fetch_restricted:
        restrictions.append(packages.PackageRestriction('restrict', values.ContainmentMatch('fetch', negate=True)))
    namespace.restriction = packages.AndRestriction(*restrictions)


@manifest.bind_main_func
def _manifest(options, out, err):
    failed = options.repo.operations.manifest(
        domain=options.domain,
        restriction=options.restriction,
        observer=observer_mod.formatter_output(out),
        mirrors=options.mirrors,
        force=options.force,
        distdir=options.distdir)

    return int(any(failed))