forked from JustAMan/JavascriptSubtitlesOctopus
-
Notifications
You must be signed in to change notification settings - Fork 10
/
license_extract.sh
executable file
·121 lines (98 loc) · 3.75 KB
/
license_extract.sh
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
#!/bin/bash
# Copyright 2021 Oneric
# SPDX-License-Identifier: ISC
# Postprocesses the output of licensecheck to automatically
# generate our distribution notice.
# licensecheck is packaged in Debian and its derivatives
# and can be obtained from CPAN everywhere.
usage() {
echo "$0 <map file> <project name> <dir>" >&2
exit 1
}
set -eu
FINDOPTS="${FINDOPTS:-}"
tabulator="$(printf '\t')"
if [ "$#" -ne 3 ] || [ ! -e "$3" ] \
|| [ ! -e "$1" ] || [ -d "$1" ] ; then
usage
fi
def_license="$(awk 'BEGIN{FS="\t+"} /^'"$2"'/ {print $2" | "$3}' "$1")"
def_copy="${def_license#* | }"
def_license="${def_license%% | *}"
base_dir="$3"
if [ -z "$def_license" ] || [ "$def_license" = "$def_copy" ] ; then
echo "The map file appears to be borked or entry is missing!" >&2
exit 1
fi
if command -v licensecheck >/dev/null ; then
:
else
echo "Could not find licensecheck!" >&2
exit 2
fi
# Do not ignore licensecheck errors
set -o pipefail
find "$base_dir" $FINDOPTS -type f -regextype egrep -regex '.*\.(c|h|cpp|hpp|js)$' -print0 \
| xargs -0 -P1 licensecheck --machine --copyright --deb-fmt --encoding UTF-8 \
| gawk -F"$tabulator" -v base_dir="$base_dir" \
-v def_license="$def_license" -v def_copy="$def_copy" '
BEGIN {
split("", lcfiles) # Clear array with only pre-Issue 8 POSIX
SUBSEP = sprintf("\n") # Seperator for pseudo-multidim arrays
# Add default
if(def_copy && def_license)
++lcfiles[def_license, def_copy]
}
1 {
if ($2 == "UNKNOWN")
if (def_license) {
$2 = def_license
}else {
print "ERROR: Unlicensed file "$1" matched!" | "cat>&2"
print " If there is no default license, then" | "cat>&2"
print " reporting this upstream might be a good idea." | "cat>&2"
exit 1
}
if ($3 == "*No copyright*") {
if (def_copy) {
$3 = def_copy
} else if (def_license != $2) {
print "ERROR: Orphaned file "$1" and no default attribution!" | "cat>&2"
exit 1
} else {
# Appears eg in freetype ; hope default copyright holder is correct
next
}
}
split($3, copyh, " / ")
for(i in copyh)
++lcfiles[$2, copyh[i]];
}
END {
# Group copyright holders per license
# The second pass in END is required to only add each (license, copy) pair once
# using pure POSIX-only awk.
split("", tmp)
for(lc in lcfiles) {
split(lc, lico, SUBSEP)
if(lico[1] in tmp)
tmp[lico[1]] = tmp[lico[1]]""SUBSEP""lico[2]
else
tmp[lico[1]] = lico[2]
}
asorti(tmp, tmp_sorted)
for(i in tmp_sorted) {
license = tmp_sorted[i]
split(tmp[license], sorted_licenses, SUBSEP)
asort(sorted_licenses);
printf "License: %s\n", license
printf "Copyright: "
for (j in sorted_licenses) {
if (j > 1)
printf " "
printf "%s\n", sorted_licenses[j]
}
printf "\n"
}
}
'