Debian Codegraph @

scripts/drop-mia-uploaders

raw symbols SCIP
1 #!/usr/bin/python3
2 # Copyright (C) 2019 Jelmer Vernooij <[email protected]>
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
18 import json
19 import logging
20 import os
21 from typing import List, Optional
22
23 from debmutate.control import ControlEditor, delete_from_list
24 from debmutate.deb822 import ChangeConflict
25 from debmutate.reformatting import FormattingUnpreservable, GeneratedFile
26
27 BRANCH_NAME = "mia"
28 MIA_EMAIL = "[email protected]"
29 MIA_TEAMMAINT_USERTAG = "mia-teammaint"
30
31
32 def versions_dict():
33 import debian
34 import debmutate
35 return {
36 'debmutate': debmutate.version_string,
37 'debian': debian.__version__,
38 }
39
40
41 class MIAResult(object):
42 def __init__(
43 self, source=None, removed_uploaders=None, bugs=None
44 ):
45 self.source = source
46 self.removed_uploaders = removed_uploaders
47 self.bugs = bugs
48
49
50 def all_mia_teammaint_bugs():
51 import debianbts
52
53 return set(
54 debianbts.get_usertag(MIA_EMAIL, [MIA_TEAMMAINT_USERTAG])[MIA_TEAMMAINT_USERTAG]
55 )
56
57
58 def get_package_bugs(source):
59 import debianbts
60
61 return set(debianbts.get_bugs(src=source, status="open"))
62
63
64 def get_mia_maintainers(bug) -> Optional[List[str]]:
65 import debianbts
66
67 log = debianbts.get_bug_log(bug)
68 return log[0]["message"].get_all("X-Debbugs-CC")
69
70
71 def drop_uploaders(editor, mia_people):
72 removed_mia = []
73 try:
74 uploaders = editor.source["Uploaders"].split(",")
75 except KeyError:
76 return []
77
78 for person in mia_people:
79 if person in [uploader.strip() for uploader in uploaders]:
80 editor.source["Uploaders"] = delete_from_list(
81 editor.source["Uploaders"], person
82 )
83 removed_mia.append(person)
84
85 if not editor.source['Uploaders']:
86 del editor.source['Uploaders']
87 return removed_mia
88
89
90 def control_drop_mia_uploaders(editor):
91 changelog_entries = []
92 source = editor.source["Source"]
93 bugs = all_mia_teammaint_bugs().intersection(get_package_bugs(source))
94 if not bugs:
95 return MIAResult()
96 removed_uploaders = []
97 fixed_bugs = []
98 for bug in bugs:
99 mia_people = get_mia_maintainers(bug)
100
101 if mia_people is None:
102 logging.warning(
103 'No MIA people (X-Debbugs-CC) found in bug %d', bug)
104 continue
105
106 removed_mia = drop_uploaders(editor, mia_people)
107
108 if len(removed_mia) == 0:
109 continue
110
111 if len(removed_mia) == 1:
112 description = "Remove MIA uploader %s." % removed_mia[0]
113 else:
114 description = "Remove MIA uploaders %s." % (
115 ", ".join(removed_mia)
116 )
117 if removed_mia == mia_people:
118 description += " Closes: #%d" % bug
119 changelog_entries.append(description)
120 removed_uploaders.extend(removed_mia)
121 fixed_bugs.append(bug)
122
123 if not changelog_entries:
124 return MIAResult(source, removed_uploaders=[], bugs=fixed_bugs)
125 return MIAResult(
126 source, removed_uploaders=removed_uploaders, bugs=fixed_bugs)
127
128
129 def report_fatal(code: str, description: str) -> None:
130 if os.environ.get('SVP_API') == '1':
131 with open(os.environ['SVP_RESULT'], 'w') as f:
132 json.dump({
133 'versions': versions_dict(),
134 'result_code': code,
135 'description': description}, f)
136 logging.fatal('%s', description)
137
138
139 def main():
140 import argparse
141 parser = argparse.ArgumentParser()
142 parser.parse_args()
143
144 logging.basicConfig(level=logging.INFO, format='%(message)s')
145
146 try:
147 if os.path.exists('debian/debcargo.toml'):
148 from debmutate.debcargo import (DebcargoControlShimEditor,
149 DebcargoEditor)
150
151 control = DebcargoControlShimEditor.from_debian_dir('debian')
152 else:
153 control = ControlEditor(path='debian/control')
154
155 with control:
156 result = control_drop_mia_uploaders(control)
157 except FormattingUnpreservable as e:
158 report_fatal(
159 "formatting-unpreservable",
160 "unable to preserve formatting while editing %s" % e.path,
161 )
162 return 1
163 except (ChangeConflict, GeneratedFile) as e:
164 report_fatal(
165 "generated-file", "unable to edit generated file: %r" % e
166 )
167 return 1
168 except FileNotFoundError as e:
169 report_fatal(
170 "missing-control-file",
171 "Unable to find debian control file: %s" % e)
172 return 1
173
174 if not result.bugs:
175 report_fatal("nothing-to-do", "No MIA people")
176 return 0
177
178 if not result.removed_uploaders:
179 report_fatal(
180 "nothing-to-do", "Unable to remove any MIA uploaders"
181 )
182 return 1
183
184 if 'SVP_RESULT' in os.environ:
185 with open(os.environ['SVP_RESULT'], 'w') as f:
186 json.dump({
187 'versions': versions_dict(),
188 'description': 'Remove MIA uploaders',
189 'context': {
190 'bugs': result.bugs,
191 'removed_uploaders': result.removed_uploaders,
192 }}, f)
193
194 logging.info(
195 'Dropped MIA uploaders (%r) from Uploaders.',
196 result.removed_uploaders)
197
198
199 if __name__ == "__main__":
200 import sys
201
202 sys.exit(main())