summaryrefslogtreecommitdiffstats
path: root/bin/review-update-needed
blob: b931221b711b8220323cdf1a9aeb1d8cd34a92b3 (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
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
#!/usr/bin/python3

import argparse
import collections
from datetime import datetime, timedelta
import os
import re
import subprocess
import sys

try:
    import humanfriendly
except ImportError:
    humanfriendly = None

def format_date(timestamp):
    date_to_format = datetime.utcfromtimestamp(timestamp)
    delta = datetime.utcnow() - date_to_format

    output = date_to_format.strftime('%Y-%m-%d %H:%M')
    if delta.days > 1:
        output += ' ({} days ago)'.format(delta.days)
    elif delta.days == 1:
        output += ' (yesterday)'

    return output

parser = argparse.ArgumentParser(description="Review DSA/DLA needed queues")
parser.add_argument('--lts', action='store_true',
                    help='Review dla-needed.txt instead of dsa-needed.txt')
parser.add_argument('-v', '--verbose', action='store_true',
                    help='Show more information, e.g. notes, commit author and per user stats')
parser.add_argument('--quiet', action='store_true',
                    help='Do not output anything but errors')
parser.add_argument('--sort-by', default='last-update',
                    choices=('last-update', 'claimed-date'),
                    help='Sort by last-update (default) or by claimed-date')
parser.add_argument('--skip-unclaimed', action='store_true',
                    help='Skip unclaimed packages in the review')
if humanfriendly:
    parser.add_argument('--unclaim', default=None, metavar='N',
                        nargs='?', const='1w',
                        help='Automatically unclaim entries older than specified delta (default: %(default)s)')
else:
    parser.add_argument('--unclaim', default=None, metavar='N', type=int,
                        nargs='?', const=604800,
                        help='Automatically unclaim entries older than N seconds (default: %(default)s)')
parser.add_argument('--exclude', nargs='+', metavar='PACKAGE', default=[],
                    help='completely ignore packages specified PACKAGE')
args = parser.parse_args()
if args.verbose and args.quiet:
    args.error("--verbose and --quiet contradiction")

if args.unclaim:
    if humanfriendly:
        unclaim_delta = timedelta(seconds=humanfriendly.parse_timespan(args.unclaim))
    else:
        unclaim_delta = timedelta(seconds=args.unclaim)

if args.lts:
    dsa_dla_needed = 'data/dla-needed.txt'
else:
    dsa_dla_needed = 'data/dsa-needed.txt'

if not os.path.exists(dsa_dla_needed):
    args.error("ERROR: {} not found\n".format(dsa_dla_needed))

if not os.path.exists(".git"):
    args.error("ERROR: works only in a git checkout\n")

process = subprocess.Popen(["git", "blame", "--line-porcelain", "--",
                            dsa_dla_needed], stdout=subprocess.PIPE)
context = {}
in_preamble = True
all_entries = []
per_user = collections.defaultdict(list)
entry = None
for line in process.stdout:
    line = line.decode('utf-8')
    res = re.search(r'^([0-9a-f]{40}) \d+', line)
    if res:
        context['commit'] = res.group(1)
    if line.startswith('author '):
        context['author'] = line.strip().split()[1]
    elif line.startswith('author-time '):
        context['author-time'] = int(line.strip().split()[1])
    elif line.startswith('summary '):
        context['summary'] = line.strip().split(maxsplit=1)[1]
    elif line.startswith("\t"):
        line = line[1:]
        if line.startswith("--"):
            in_preamble = False
            entry = None
        elif in_preamble:
            continue
        elif line[0] == ' ' or line[0] == "\t":
            entry['note'] += line
            if context['author-time'] > entry['last-update']:
                entry['last-update'] = context['author-time']
                entry['last-update-author'] = context['author']
                entry['last-update-summary'] = context['summary']
                entry['last-update-commit'] = context['commit']
        else:
            res = re.match(r'^(\S+)(?:\s+\((.*)\)\s*)?$', line)
            entry = {
                'pkg': res.group(1),
                'claimed-by': res.group(2),
                'claimed-date': context['author-time'],
                'last-update': context['author-time'],
                'last-update-author': context['author'],
                'last-update-summary': context['summary'],
                'last-update-commit': context['commit'],
                'author': context['author'],
                'note': '',
            }
            if entry['pkg'] not in args.exclude:
                per_user[entry['claimed-by']].append(entry['pkg'])
                all_entries.append(entry)

retcode = process.wait()
if retcode != 0:
    sys.stderr.write("WARNING: git blame returned error code {}\n".format(retcode))

all_entries.sort(key=lambda x: x[args.sort_by])

unclaim_pkgs = []
for entry in all_entries:
    if args.skip_unclaimed and not entry['claimed-by']:
        continue
    args.quiet or print("Package: {}".format(entry['pkg']))
    if entry['claimed-by']:
        args.quiet or print("Claimed-By: {}".format(entry['claimed-by']))
        args.quiet or print("Claimed-Date: {}".format(format_date(entry['claimed-date'])))

        if args.unclaim:
            if entry['last-update'] > entry['claimed-date']:
                date_to_format = datetime.utcfromtimestamp(entry['last-update'])
            else:
                date_to_format = datetime.utcfromtimestamp(entry['claimed-date'])
            if datetime.utcnow() - date_to_format > unclaim_delta:
                unclaim_pkgs.append(entry['pkg'])
                args.quiet or print("Unclaimed: idle for more than {}: {}".format(unclaim_delta, datetime.utcnow() - date_to_format))
    else:
        args.quiet or print("Unclaimed-Since: {}".format(format_date(entry['claimed-date'])))
    if entry['last-update'] > entry['claimed-date']:
        args.quiet or print("Last-Update: {}".format(format_date(entry['last-update'])))
    if not args.verbose:
        args.quiet or print("")
        continue
    print("Last-Update-Author: {}".format(entry['last-update-author']))
    print("Last-Update-Summary: {}".format(entry['last-update-summary']))
    print("Last-Update-Commit: {}".format(entry['last-update-commit']))
    if entry['note']:
        print("Notes:\n{}".format(entry['note']))
    else:
        print("")

if args.unclaim:
    args.quiet or print("Editing file to unclaim: {}".format(", ".join(unclaim_pkgs)))
    in_preamble = True
    with open(dsa_dla_needed) as orig, open(dsa_dla_needed + '.new', 'w') as new:
        for line in orig:
            if line.startswith('--'):
                in_preamble = False
            if in_preamble:
                new.write(line) # do not touch preamble
            else:
                # look for packages to unclaim in this line
                for pkg in unclaim_pkgs:
                    if line.startswith(pkg + " ("):
                        new.write(pkg + "\n")
                        break
                else: # nothing found, write untouched line
                    new.write(line)
    os.rename(dsa_dla_needed + '.new', dsa_dla_needed)

if args.verbose:
    # sort by number of claimed packages
    items = sorted(per_user.items(), key=lambda x: len(x[1]))
    for user, pkgs in items:
        print("User: {}\nPackages: {}\nCount: {}\n".format(user, ", ".join(pkgs), len(pkgs)))

© 2014-2020 Faster IT GmbH | imprint | privacy policy