summaryrefslogtreecommitdiff
path: root/scripts/check_license.py
blob: c1d2a296af064bf3baf3512157073b0a27c961ab (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', type=bool, default=True, help='ISC [default=True]')
    parser.add_argument('--gpl2', type=bool, default=True, help='GPLv2+ [default=True]')
    parser.add_argument('--gpl3', type=bool, default=True, help='GPLv3 only [default=True]')
    args = parser.parse_args()

    licenses = []
    if args.isc:
        licenses.append('ISC')
    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

        print(f'{ file.name }')
        # 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 in licenses:
                    print(f'found license: { found }')
                else:
                    print(f'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()