aboutsummaryrefslogtreecommitdiff
path: root/contrib/check-MAINTAINERS.py
blob: ba2cdb4012987373c3c92be66182493cafb9281a (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
#!/usr/bin/env python3

# Copyright (C) 2022-2025 Free Software Foundation, Inc.
#
# This file is part of GCC.
#
# GCC 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 3, or (at your option)
# any later version.
#
# GCC 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 GCC; see the file COPYING.  If not, write to
# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.

# Check that names in the file are sorted
# alphabetically by surname.

import locale
import sys
from difflib import ndiff
from itertools import groupby

import unidecode

locale.setlocale(locale.LC_ALL, 'en_US.utf8')

exit_code = 0

if len(sys.argv) != 2:
    print('Usage: ./check-MAINTAINERS.py path-to/MAINTAINERS')
    sys.exit(1)


def get_surname(name):
    parts = name.split()
    surname = parts[-1]

    # Special-case some names
    if name == 'Stefan Schulze Frielinghaus':
        surname = parts[1]
    elif name == 'Kris Van Hees':
        surname = parts[1]
    elif surname == "d'Humieres":
        surname = 'Humieres'

    # Remove accents
    return unidecode.unidecode(surname)


def check_group(name, lines, columns):
    global exit_code

    named_lines = []
    for line in lines:
        if line.startswith(' '):
            print(f'Line should not start with space: "{line}"')
            exit_code = 2
            continue

        if line.endswith(' '):
            print(f'Line should not end with space: "{line}"')
            exit_code = 3
            continue

        # Special-case some names
        if line == 'James Norris':
            named_lines.append((get_surname(line), line + "\n"))
            continue

        pieces = []
        for i, column in enumerate(columns):
            piece = ""
            if len(line) <= column:
                print(f'Line too short: "{line}"')
                exit_code = 4
            elif column > 0 and line[column - 1] != ' ':
                print(f'Column {column - 1} should be empty: "{line}"')
                exit_code = 5
            elif line[column] == ' ':
                print(f'Column {column} should be nonempty: "{line}"')
                exit_code = 6
            elif i == len(columns) - 1:
                piece = line[column:].rstrip()
            else:
                piece = line[column:columns[i + 1]].rstrip()

            if "  " in piece:
                print(f'Malformed field at column {column}: "{line}"')
                exit_code = 7

            pieces.append(piece)

        named_lines.append((get_surname(pieces[0]), line + "\n"))

        email = pieces[-1]
        if email and (not email.startswith('<') or not email.endswith('>')):
            print(f'Malformed email address: "{line}"')
            exit_code = 8

    lines = [line + "\n" for line in lines]
    sorted_lines = [line for _, line in sorted(named_lines)]
    if lines != sorted_lines:
        exit_code = 1
        diff = ndiff(lines, sorted_lines)
        print(f'Wrong order for {name}:\n')
        print(''.join(diff))
    else:
        print(f'{name} are fine!')


text = open(sys.argv[1]).read()
if '\t' in text:
    print('The file should not contain tabs')
    exit_code = 9

sections = [
    # heading, paragraph index, column numbers
    ('Global Reviewers', 1, [0, 48]),
    ('Write After Approval', 2, [0, 32, 48]),
    ('Bug database only accounts', 1, [0, 48]),
    ('Contributing under the DCO', 2, [0, 48])
]

i = 0
count = 0
for is_empty, lines in groupby(text.splitlines(), lambda x: not x):
    if is_empty:
        continue
    lines = list(lines)
    if count > 0:
        count -= 1
        if count == 0:
            check_group(sections[i][0], lines, sections[i][2])
            i += 1
    elif len(lines) == 1 and i < len(sections) and sections[i][0] in lines[0]:
        count = sections[i][1]

if i < len(sections):
    print(f'Missing "{sections[i][0]}" section')
    exit_code = 10

sys.exit(exit_code)