-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp-check
executable file
·383 lines (318 loc) · 11.5 KB
/
mp-check
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/usr/bin/env python3
"""
A quick-and-dirty MacPorts check tool for port maintainers.
Checks are:
- Livecheck: a `port livecheck` of the maintainer's ports.
- Tickets: the opened Trac tickets for the maintainer's ports.
- PullRequests: the opened Pull Requests for the
- Lint: the opened Trac tickets for the maintainer's ports.
The `~/.macports/mp-check.conf` config file contains the maintainer's name as
well as ports to add or to exclude from the checks.
The Livecheck `exclude-subports` also allow to exclude subports from the
checks.
"""
# FIXME implement exclude-ports support in Tickets
# TODO package the script
# TODO document config file format
import argparse
import configparser
import logging
import logging.config
import re
import sys
from abc import ABC, abstractmethod
from functools import partial
from itertools import chain
from pathlib import Path
from typing import (
Any,
ClassVar,
Dict,
Iterable,
List,
Optional,
Sequence,
Set,
Union,
)
import pandas as pd
from github import Github
from multidict import MultiDict
from sh import port
from yarl import URL
ConfigParserDefaultsType = Optional[Dict[str, str]]
DictConfigType = Dict[str, Any]
FilenameType = Union[str, Path]
FilenamesType = Sequence[FilenameType]
CHECKS = ("Livecheck", "Tickets", "PullRequests", "Lint")
CONFIG_FILENAMES: FilenamesType = [Path(p).expanduser() for p in (
"mp-check.conf",
"~/.macports/mp-check.conf",
)]
LOGGING_CONFIG: DictConfigType = {
"version": 1,
"formatters": {
# "brief": {"format": "%(levelname)s: %(message)s"},
"brief": {"format": "%(message)s"},
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"formatter": "brief",
"stream": "ext://sys.stdout",
}
},
"loggers": {__name__: {"handlers": ["console"]}},
}
logging.config.dictConfig(LOGGING_CONFIG)
log = logging.getLogger(__name__)
port = port.bake("-q")
def parse_args() -> argparse.Namespace:
"""Parse CLI arguments."""
parser = argparse.ArgumentParser(
description="Check MacPorts port maintainer information."
)
check_group = parser.add_argument_group("checks")
for check in CHECKS:
check_group.add_argument(
"--" + check.lower(),
action="append_const",
dest="checks",
const=check,
)
verbosity = parser.add_mutually_exclusive_group()
verbosity.add_argument(
"-v",
"--verbose",
action="store_const",
dest="loglevel",
const=logging.INFO,
default=logging.WARN,
)
verbosity.add_argument(
"-d",
"--debug",
action="store_const",
dest="loglevel",
const=logging.DEBUG,
)
return parser.parse_args()
def parse_config(
filenames: FilenamesType, defaults: ConfigParserDefaultsType = None
) -> configparser.ConfigParser:
"""Parse config files."""
def getlist(
option: str, sep: Optional[str] = ",", chars: Optional[str] = None
) -> List[str]:
"""Return a list from a ConfigParser option. By default,
split on a comma and strip whitespaces."""
# return [chunk.strip(chars) for chunk in option.split(sep)]
values = [chunk.strip(chars)
for chunk in re.compile(sep).split(option)]
return [v for v in values if v]
def optionxform(option: str) -> str:
"""Convert a ConfigParser option name to lowercase and replace dashes
with underscores."""
return option.lower().replace("-", "_")
config = configparser.ConfigParser(
converters={"list": partial(getlist, sep=r"[,\s\n]+")},
defaults=defaults
)
config.optionxform = optionxform # type: ignore
config.read(filenames)
return config
class _Base(ABC):
"""An abstract base class for MacPorts checks"""
def __init__(self, options: configparser.SectionProxy) -> None:
log.debug("%s: options are %s", self, dict(options))
self.options = options
self.maintainer: str = options.get("maintainer")
self.include_ports: Set[str] = set(
options.getlist("include_ports", [])
)
self.exclude_ports: Set[str] = set(
options.getlist("exclude_ports", [])
)
def __str__(self):
return self.__class__.__name__
def port_filters(self) -> List[str]:
"""Make a list of port filters."""
exclude_ports = "".join(
f" and not name:{p}" for p in self.exclude_ports
)
filters = [f"maintainer:{self.maintainer}{exclude_ports}"]
filters.extend(f"name:{p}" for p in self.include_ports)
return filters
def ports(self, filters: Iterable[str]) -> Set[str]:
"""List the ports matching the given filters."""
ports = set(
chain.from_iterable(
port.echo(f).split() for f in self.port_filters()
)
)
return ports
def subports(self, ports: Iterable[str]) -> Set[str]:
"""List the subports of given ports."""
subports = set(
chain.from_iterable(
sp.strip().split(", ")
for sp in port.info("--subports", *ports).split("--")
)
)
return subports
@abstractmethod
def check(self) -> Any:
raise NotImplementedError()
class Lint(_Base):
"""Lint maintainer's ports."""
EXCLUDE_SUBPORTS: ClassVar[bool] = True
def __init__(self, options: configparser.SectionProxy) -> None:
super().__init__(options)
self.exclude_subports: bool = options.getboolean(
"exclude_subports", fallback=Lint.EXCLUDE_SUBPORTS
)
def check(self) -> str:
log.warning("---> %s", self)
filters = self.port_filters()
log.debug("%s: port filters are %s", self, filters)
ports = self.ports(filters)
log.debug("%s: found ports %s", self, ports)
if self.exclude_subports:
subports = self.subports(ports)
log.debug("%s: excluding subports %s", self, subports)
ports = ports - subports
NOTOK_RE = re.compile(r"^(?!OK:\s+).+$", re.MULTILINE)
for p in sorted(ports):
lint = port.bake("-v").lint(p).stdout.decode(sys.stdin.encoding)
if log.isEnabledFor(logging.INFO):
log.info(lint)
else:
notok = NOTOK_RE.findall(lint)
if notok:
log.warning("\n".join(notok))
class Livecheck(_Base):
"""Livecheck maintainer's ports."""
EXCLUDE_SUBPORTS: ClassVar[bool] = True
def __init__(self, options: configparser.SectionProxy) -> None:
super().__init__(options)
self.exclude_subports: bool = options.getboolean(
"exclude_subports", fallback=Livecheck.EXCLUDE_SUBPORTS
)
def check(self) -> str:
log.warning("---> %s", self)
filters = self.port_filters()
log.debug("%s: port filters are %s", self, filters)
ports = self.ports(filters)
log.debug("%s: found ports %s", self, ports)
if self.exclude_subports:
subports = self.subports(ports)
log.debug("%s: excluding subports %s", self, subports)
ports = ports - subports
log.debug("%s: livechecking ports", self)
livechecks = port.bake("-v").livecheck(sorted(ports)).stdout.decode(
sys.stdin.encoding)
if log.isEnabledFor(logging.INFO):
log.info(livechecks)
else:
UPDATED_RE = re.compile(
r"^.+seems to have been updated.+$",
re.MULTILINE)
updated = UPDATED_RE.findall(livechecks)
if updated:
for update in updated:
log.warning(update)
class PullRequests(_Base):
"""Check MacPorts Pull Requests."""
EXCLUDE_SUBPORTS: ClassVar[bool] = True
def __init__(self, options: configparser.SectionProxy) -> None:
super().__init__(options)
self.exclude_subports: bool = options.getboolean(
"exclude_subports", fallback=Livecheck.EXCLUDE_SUBPORTS
)
gh = Github()
self.repo = gh.get_repo("macports/macports-ports")
def check(self) -> str:
log.warning("---> %s", self)
filters = self.port_filters()
log.debug("%s: port filters are %s", self, filters)
ports = self.ports(filters)
log.debug("%s: found ports %s", self, ports)
if self.exclude_subports:
subports = self.subports(ports)
log.debug("%s: excluding subports %s", self, subports)
ports = ports - subports
log.debug("%s: getting pull requests", self)
SEP_RE = re.compile(r":\s")
COLS = ("number", "title", "user.login", "labels")
def getter(port):
return (port.number, port.title, port.user.login,
", ".join(label.name for label in port.get_labels()))
pulls = pd.DataFrame.from_records(
(getter(p) for p in self.repo.get_pulls(state="open")
if (p.user.login == self.maintainer)
or (set(SEP_RE.split(p.title)[0].split()) & ports)),
columns=COLS, index=COLS[0])
log.warning(pulls)
class Tickets(_Base):
"""Get Trac open tickets for maintainer's ports."""
TRAC_QUERY_URL: ClassVar[URL] = URL("https://trac.macports.org/query")
DEFAULT_TRAC_CRITERIA: ClassVar[MultiDict] = MultiDict(
(
("status", "accepted"),
("status", "assigned"),
("status", "new"),
("status", "reopened"),
)
)
TRAC_COLS: ClassVar[Iterable[str]] = (
"id",
"port",
"summary",
"status",
"type",
)
TRAC_ORDER: ClassVar[str] = "port"
TRAC_FORMAT: ClassVar[str] = "tab"
def trac_filters(self) -> List[Dict[str, str]]:
"""Make a list of Trac filters."""
filters = [{"owner": self.maintainer}]
for p in self.include_ports:
filters.append({"port": f"~{p}"})
return filters
def trac_query(self) -> MultiDict:
"""Make a MultiDict-based Trac query."""
query: MultiDict = MultiDict()
for f in self.trac_filters():
criteria = Tickets.DEFAULT_TRAC_CRITERIA.copy()
criteria.extend(f)
if query:
query.add("or", "")
query.extend(criteria)
query.extend(("col", c) for c in Tickets.TRAC_COLS)
query.add("order", Tickets.TRAC_ORDER)
query.add("format", Tickets.TRAC_FORMAT)
return query
def check(self) -> pd.DataFrame:
"""Query MacPorts Trac tickets based on given filters."""
log.warning("---> %s", self)
query = self.trac_query()
log.debug("%s: Trac query is %s", self, query)
url = Tickets.TRAC_QUERY_URL.with_query(query)
log.debug("%s: Trac query url is %s", self, url)
log.debug("%s: querying Trac", self)
tickets = pd.read_csv(str(url), delimiter="\t", index_col=0)
if not tickets.empty:
log.warning(tickets)
else:
log.info("Found no relevant Trac tickets.")
def main():
args = parse_args()
log.setLevel(args.loglevel or logging.WARN)
log.debug("args are %s", args)
log.debug("reading config from %s", CONFIG_FILENAMES)
config = parse_config(CONFIG_FILENAMES)
for cls in (Livecheck, Tickets, PullRequests, Lint):
cls(config[cls.__name__]).check()
if __name__ == "__main__":
sys.exit(main())
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4