summaryrefslogtreecommitdiff
path: root/scripts/check_license.py
blob: 5f5b0351d74406e25591e75cb1ba7ae8f5ef5583 (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
#!/usr/bin/env python3
# ============================================================
#     The rekonq project
# ============================================================
# SPDX-License-Identifier: GPL-3.0-only
# Copyright (C) 2022 aqua <aqua@iserlohn-fortress.net>
# ============================================================
""" Check C/C++ files for license headers """


import argparse
import sys
import re


def main():
    ''' main function '''
    parser = argparse.ArgumentParser(description='Check file for enabled license')
    parser.add_argument('file', type=open, nargs='+', help='File to check')
    parser.add_argument('--isc', action='store_false', help='ISC [default=True]')
    parser.add_argument('--bsd', action='store_false', help='BSD-3-Clause [default=True]')
    parser.add_argument('--gpl2', action='store_false', help='GPL-2.0-or-later [default=True]')
    parser.add_argument('--gpl3', action='store_false', help='GPL-3.0-only [default=True]')
    args = parser.parse_args()

    licenses = []
    if args.isc:
        licenses.append('ISC')
    if args.bsd:
        licenses.append('BSD-3-Clause')
    if args.gpl2:
        licenses.append('GPL-2.0-or-later')
    if args.gpl3:
        licenses.append('GPL-3.0-only')

    print('enabled licenses: ', licenses)
    has_errors = False

    for file in args.file:
        if not file.name.endswith(('.h', '.hh', '.hpp', '.c', '.cc', '.cpp')):
            continue

        # check for header start
        line = file.readline()
        if re.search(r'^\/\* ={60}$', line) is None:
            print(f'{ file.name }: missing header')
            has_errors = True
            continue

        line = file.readline()

        while line.startswith(' *'):
            lic = re.search(r'^\s\* SPDX-License-Identifier: ([\w\d.-]{3,})$', line)
            if lic is not None:
                found = lic.group(1)
                if found not in licenses:
                    print(f'{ file.name }: found license { found } not in licenses { licenses }')
                    has_errors = True

            line = file.readline()

    sys.exit('Errors during check' if has_errors else 0)


if __name__ == '__main__':
    main()