File: drop-mia-uploaders

package info (click to toggle)
debmutate 0.80
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 588 kB
  • sloc: python: 8,346; makefile: 12; sh: 1
file content (202 lines) | stat: -rwxr-xr-x 5,993 bytes parent folder | download | duplicates (3)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/python3
# Copyright (C) 2019 Jelmer Vernooij <jelmer@jelmer.uk>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

import json
import logging
import os
from typing import List, Optional

from debmutate.control import ControlEditor, delete_from_list
from debmutate.deb822 import ChangeConflict
from debmutate.reformatting import FormattingUnpreservable, GeneratedFile

BRANCH_NAME = "mia"
MIA_EMAIL = "mia@qa.debian.org"
MIA_TEAMMAINT_USERTAG = "mia-teammaint"


def versions_dict():
    import debian
    import debmutate
    return {
        'debmutate': debmutate.version_string,
        'debian': debian.__version__,
    }


class MIAResult(object):
    def __init__(
        self, source=None, removed_uploaders=None, bugs=None
    ):
        self.source = source
        self.removed_uploaders = removed_uploaders
        self.bugs = bugs


def all_mia_teammaint_bugs():
    import debianbts

    return set(
        debianbts.get_usertag(MIA_EMAIL, [MIA_TEAMMAINT_USERTAG])[MIA_TEAMMAINT_USERTAG]
    )


def get_package_bugs(source):
    import debianbts

    return set(debianbts.get_bugs(src=source, status="open"))


def get_mia_maintainers(bug) -> Optional[List[str]]:
    import debianbts

    log = debianbts.get_bug_log(bug)
    return log[0]["message"].get_all("X-Debbugs-CC")


def drop_uploaders(editor, mia_people):
    removed_mia = []
    try:
        uploaders = editor.source["Uploaders"].split(",")
    except KeyError:
        return []

    for person in mia_people:
        if person in [uploader.strip() for uploader in uploaders]:
            editor.source["Uploaders"] = delete_from_list(
                editor.source["Uploaders"], person
            )
            removed_mia.append(person)

    if not editor.source['Uploaders']:
        del editor.source['Uploaders']
    return removed_mia


def control_drop_mia_uploaders(editor):
    changelog_entries = []
    source = editor.source["Source"]
    bugs = all_mia_teammaint_bugs().intersection(get_package_bugs(source))
    if not bugs:
        return MIAResult()
    removed_uploaders = []
    fixed_bugs = []
    for bug in bugs:
        mia_people = get_mia_maintainers(bug)

        if mia_people is None:
            logging.warning(
                'No MIA people (X-Debbugs-CC) found in bug %d', bug)
            continue

        removed_mia = drop_uploaders(editor, mia_people)

        if len(removed_mia) == 0:
            continue

        if len(removed_mia) == 1:
            description = "Remove MIA uploader %s." % removed_mia[0]
        else:
            description = "Remove MIA uploaders %s." % (
                ", ".join(removed_mia)
            )
        if removed_mia == mia_people:
            description += " Closes: #%d" % bug
        changelog_entries.append(description)
        removed_uploaders.extend(removed_mia)
        fixed_bugs.append(bug)

    if not changelog_entries:
        return MIAResult(source, removed_uploaders=[], bugs=fixed_bugs)
    return MIAResult(
        source, removed_uploaders=removed_uploaders, bugs=fixed_bugs)


def report_fatal(code: str, description: str) -> None:
    if os.environ.get('SVP_API') == '1':
        with open(os.environ['SVP_RESULT'], 'w') as f:
            json.dump({
                'versions': versions_dict(),
                'result_code': code,
                'description': description}, f)
    logging.fatal('%s', description)


def main():
    import argparse
    parser = argparse.ArgumentParser()
    parser.parse_args()

    logging.basicConfig(level=logging.INFO, format='%(message)s')

    try:
        if os.path.exists('debian/debcargo.toml'):
            from debmutate.debcargo import (DebcargoControlShimEditor,
                                            DebcargoEditor)

            control = DebcargoControlShimEditor.from_debian_dir('debian')
        else:
            control = ControlEditor(path='debian/control')

        with control:
            result = control_drop_mia_uploaders(control)
    except FormattingUnpreservable as e:
        report_fatal(
            "formatting-unpreservable",
            "unable to preserve formatting while editing %s" % e.path,
        )
        return 1
    except (ChangeConflict, GeneratedFile) as e:
        report_fatal(
            "generated-file", "unable to edit generated file: %r" % e
        )
        return 1
    except FileNotFoundError as e:
        report_fatal(
            "missing-control-file",
            "Unable to find debian control file: %s" % e)
        return 1

    if not result.bugs:
        report_fatal("nothing-to-do", "No MIA people")
        return 0

    if not result.removed_uploaders:
        report_fatal(
            "nothing-to-do", "Unable to remove any MIA uploaders"
        )
        return 1

    if 'SVP_RESULT' in os.environ:
        with open(os.environ['SVP_RESULT'], 'w') as f:
            json.dump({
                'versions': versions_dict(),
                'description': 'Remove MIA uploaders',
                'context': {
                    'bugs': result.bugs,
                    'removed_uploaders': result.removed_uploaders,
                    }}, f)

    logging.info(
        'Dropped MIA uploaders (%r) from Uploaders.',
        result.removed_uploaders)


if __name__ == "__main__":
    import sys

    sys.exit(main())