summaryrefslogtreecommitdiffstats
path: root/scripts/contrib/convert-spdx-licenses.py
blob: 4e194dee3ff138ac058d176f0bc072c16961782f (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
#!/usr/bin/env python3
#
# Conversion script to change LICENSE entries to SPDX identifiers
#
# Copyright (C) 2021-2022 Richard Purdie
#
# SPDX-License-Identifier: GPL-2.0-only
#

import re
import os
import sys
import tempfile
import shutil
import mimetypes

if len(sys.argv) < 2:
    print("Please specify a directory to run the conversion script against.")
    sys.exit(1)

license_map = {
"AGPL-3" : "AGPL-3.0-only",
"AGPL-3+" : "AGPL-3.0-or-later",
"AGPLv3" : "AGPL-3.0-only",
"AGPLv3+" : "AGPL-3.0-or-later",
"AGPLv3.0" : "AGPL-3.0-only",
"AGPLv3.0+" : "AGPL-3.0-or-later",
"AGPL-3.0" : "AGPL-3.0-only",
"AGPL-3.0+" : "AGPL-3.0-or-later",
"BSD-0-Clause" : "0BSD",
"GPL-1" : "GPL-1.0-only",
"GPL-1+" : "GPL-1.0-or-later",
"GPLv1" : "GPL-1.0-only",
"GPLv1+" : "GPL-1.0-or-later",
"GPLv1.0" : "GPL-1.0-only",
"GPLv1.0+" : "GPL-1.0-or-later",
"GPL-1.0" : "GPL-1.0-only",
"GPL-1.0+" : "GPL-1.0-or-later",
"GPL-2" : "GPL-2.0-only",
"GPL-2+" : "GPL-2.0-or-later",
"GPLv2" : "GPL-2.0-only",
"GPLv2+" : "GPL-2.0-or-later",
"GPLv2.0" : "GPL-2.0-only",
"GPLv2.0+" : "GPL-2.0-or-later",
"GPL-2.0" : "GPL-2.0-only",
"GPL-2.0+" : "GPL-2.0-or-later",
"GPL-3" : "GPL-3.0-only",
"GPL-3+" : "GPL-3.0-or-later",
"GPLv3" : "GPL-3.0-only",
"GPLv3+" : "GPL-3.0-or-later",
"GPLv3.0" : "GPL-3.0-only",
"GPLv3.0+" : "GPL-3.0-or-later",
"GPL-3.0" : "GPL-3.0-only",
"GPL-3.0+" : "GPL-3.0-or-later",
"LGPLv2" : "LGPL-2.0-only",
"LGPLv2+" : "LGPL-2.0-or-later",
"LGPLv2.0" : "LGPL-2.0-only",
"LGPLv2.0+" : "LGPL-2.0-or-later",
"LGPL-2.0" : "LGPL-2.0-only",
"LGPL-2.0+" : "LGPL-2.0-or-later",
"LGPL2.1" : "LGPL-2.1-only",
"LGPL2.1+" : "LGPL-2.1-or-later",
"LGPLv2.1" : "LGPL-2.1-only",
"LGPLv2.1+" : "LGPL-2.1-or-later",
"LGPL-2.1" : "LGPL-2.1-only",
"LGPL-2.1+" : "LGPL-2.1-or-later",
"LGPLv3" : "LGPL-3.0-only",
"LGPLv3+" : "LGPL-3.0-or-later",
"LGPL-3.0" : "LGPL-3.0-only",
"LGPL-3.0+" : "LGPL-3.0-or-later",
"MPL-1" : "MPL-1.0",
"MPLv1" : "MPL-1.0",
"MPLv1.1" : "MPL-1.1",
"MPLv2" : "MPL-2.0",
"MIT-X" : "MIT",
"MIT-style" : "MIT",
"openssl" : "OpenSSL",
"PSF" : "PSF-2.0",
"PSFv2" : "PSF-2.0",
"Python-2" : "Python-2.0",
"Apachev2" : "Apache-2.0",
"Apache-2" : "Apache-2.0",
"Artisticv1" : "Artistic-1.0",
"Artistic-1" : "Artistic-1.0",
"AFL-2" : "AFL-2.0",
"AFL-1" : "AFL-1.2",
"AFLv2" : "AFL-2.0",
"AFLv1" : "AFL-1.2",
"CDDLv1" : "CDDL-1.0",
"CDDL-1" : "CDDL-1.0",
"EPLv1.0" : "EPL-1.0",
"FreeType" : "FTL",
"Nauman" : "Naumen",
"tcl" : "TCL",
"vim" : "Vim",
"SGIv1" : "SGI-1",
}

def processfile(fn):
    print("processing file '%s'" % fn)
    try:
        fh, abs_path = tempfile.mkstemp()
        modified = False
        with os.fdopen(fh, 'w') as new_file:
            with open(fn, "r") as old_file:
                for line in old_file:
                    if not line.startswith("LICENSE"):
                        new_file.write(line)
                        continue
                    orig = line
                    for license in sorted(license_map, key=len, reverse=True):
                        for ending in ['"', "'", " ", ")"]:
                            line = line.replace(license + ending, license_map[license] + ending)
                    if orig != line:
                        modified = True
                    new_file.write(line)
        new_file.close()
        if modified:
            shutil.copymode(fn, abs_path)
            os.remove(fn)
            shutil.move(abs_path, fn)
    except UnicodeDecodeError:
        pass

ourname = os.path.basename(sys.argv[0])
ourversion = "0.01"

if os.path.isfile(sys.argv[1]):
    processfile(sys.argv[1])
    sys.exit(0)

for targetdir in sys.argv[1:]:
    print("processing directory '%s'" % targetdir)
    for root, dirs, files in os.walk(targetdir):
        for name in files:
            if name == ourname:
                continue
            fn = os.path.join(root, name)
            if os.path.islink(fn):
                continue
            if "/.git/" in fn or fn.endswith(".html") or fn.endswith(".patch") or fn.endswith(".m4") or fn.endswith(".diff") or fn.endswith(".orig"):
                continue
            processfile(fn)

print("All files processed with version %s" % ourversion)