forked from mesosphere-backup/godep-licenses
-
Notifications
You must be signed in to change notification settings - Fork 0
/
godep-licenses.sh
executable file
·244 lines (219 loc) · 6.85 KB
/
godep-licenses.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env bash
# Extract Godep package license information.
# Generates a (csv or markdown) table of Godep dependencies and their license.
# Requires:
# jq (https://stedolan.github.io/jq/)
# ninka (http://ninka.turingmachine.org/)
# NINKA env var (optional) set to the path of the ninka executable
# Bash version new enough to support associative arrays
# Usage Example:
# NINKA=~/Downloads/ninka-1.3/ninka.pl ./godep-license.sh -e github.com/stretchr/testify:MIT
set -o errexit
set -o nounset
set -o pipefail
# File name
readonly PROGNAME=$(basename $0)
# American and UK spelling
readonly LICENSE_FILE_PATTERN="licen[sc]e*"
readonly README_FILE_PATTERN="readme*"
readonly GODEP_SRC="Godeps/_workspace/src"
# default to the current directory
REPO_PATH="$(pwd)"
# default to comma sepperated values
OUTPUT_FORMAT="csv"
declare -A EXEMPTIONS
function main() {
parse-args "$@"
[ -z "${OUTPUT_FORMAT:-}" ] && echo "Invalid output. Use --help to see the valid syntax." >&2 && exit 1
find-ninka
find-jq
case "${OUTPUT_FORMAT}" in
csv) print-csv;;
md) print-markdown;;
*) echo "Invalid output format. Use --help to see the valid formats." >&2 && exit 1
esac
}
function find-ninka() {
if [ -z "${NINKA:-}" ]; then
NINKA="$(which ninka)"
[ -z "${NINKA:-}" ] && echo "No ninka found in PATH. Install ninka or define its path with the NINKA environment variable." >&2 && exit 1
fi
echo "Using Ninka: ${NINKA}" >&2
}
function find-jq() {
JQ="$(which jq)"
[ -z "${JQ:-}" ] && echo "No jq found in PATH. Install jq." >&2 && exit 1
echo "Using jq: ${JQ}" >&2
}
function usage() {
echo "godep-licenses - godep dependency license report generation tool"
echo
echo "Usage: $PROGNAME [options]..."
echo
echo "Options:"
echo
echo " -h, --help"
echo " This help text."
echo
echo " -e <package:license>, --exemption <package:license>"
echo " Exemption license for a specific package"
echo
echo " -o <format>, --output <format>"
echo " Output format. One of:"
echo " csv - comma separated values (default)"
echo " md - markdown"
echo
echo " -p <repo>, --path <repo>"
echo " Path to the root of a Golang repository that contains a Godep dir (defaults to current dir)"
echo
echo " --"
echo " Do not interpret any more arguments as options."
echo
}
function parse-args() {
while [ "$#" -gt 0 ]; do
case "$1" in
-h|--help)
usage
exit 0
;;
-e|--exemption)
local exemption="$2"
[ -z "${exemption:-}" ] && echo "Invalid exemption. Use --help to see the valid syntax." >&2 && exit 1
local pkg="$(echo "${exemption}" | cut -d':' -f1)"
[ -z "${pkg:-}" ] && echo "Invalid exemption package. Use --help to see the valid syntax." >&2 && exit 1
local license="$(echo "${exemption}" | cut -d':' -f2)"
[ -z "${license:-}" ] && echo "Invalid exemption license. Use --help to see the valid syntax." >&2 && exit 1
EXEMPTIONS["${pkg}"]="${license}"
# Jump over <file>, in case "-" is a valid input file (keyword to standard input).
# Jumping here prevents reaching "-*)" case when parsing <file>
shift
;;
-o|--output)
OUTPUT_FORMAT="$2"
[ -z "${OUTPUT_FORMAT:-}" ] && echo "Invalid output. Use --help to see the valid syntax." >&2 && exit 1
;;
-p|--path)
REPO_PATH="$2"
[ -z "${REPO_PATH:-}" ] && echo "Invalid path. Use --help to see the valid syntax." >&2 && exit 1
;;
--)
break
;;
-*)
echo "Invalid option '$1'. Use --help to see the valid options" >&2
exit 1
;;
# an option argument, continue
*) ;;
esac
shift
done
}
function license-file() {
local PKG="${1}"
pushd "${REPO_PATH}" > /dev/null
local license="$(find "${GODEP_SRC}/${PKG}" -xdev -follow -maxdepth 1 -type f -iname "${LICENSE_FILE_PATTERN}" | head -1)"
if [ -n "${license}" ]; then
echo "${license}"
echo "Found: ${license}" >&2
return
fi
local readme="$(find "${GODEP_SRC}/${PKG}" -xdev -follow -maxdepth 1 -type f -iname "${README_FILE_PATTERN}" | head -1)"
if [ -n "${readme}" ]; then
echo "${readme}"
echo "Found: ${readme}" >&2
return
fi
popd > /dev/null
}
function license() {
local FILE_PATH="${1}"
local NINKA_OUT="$("${NINKA}" "${FILE_PATH}")"
echo "${NINKA_OUT}" | sed \
-e 's/;/,/g' \
-e 's/,UNKNOWN//g' \
-e 's/,Copyright//g' \
-e 's/,-*[0-9][0-9]*//g' \
-e 's/^.*,//' \
-e "s;^${FILE_PATH}$;NONE;"
}
function pkg-file-license() {
cat "${REPO_PATH}/Godeps/Godeps.json" | "${JQ}" -r ".Deps[].ImportPath" | sort -f |
while read PACKAGE; do
local P="${PACKAGE}"
local F=""
local L="NONE"
while [ "${P}" != "." ]; do
# check for packages with hard-coded exceptions
if [ -n "${EXEMPTIONS["${P}"]:-}" ]; then
L="${EXEMPTIONS["${P}"]}"
echo "${PACKAGE},${P}/EXEMPTION,${L}"
echo "Exemption: ${P} -> ${L}" >&2
break
fi
F="$(license-file "${P}")"
if [ -n "${F}" ]; then
L="$(license "${REPO_PATH}/${F}")"
if [ "${L}" != "NONE" ]; then
echo "${PACKAGE},${F},${L}"
echo "License: ${F} -> ${L}" >&2
break
fi
fi
P="$(dirname "${P}")"
done
if [ "${L}" != "NONE" ]; then
continue
fi
FILE_LICENSE=$(
(cd "${REPO_PATH}/${GODEP_SRC}"; find "${PACKAGE}" -name "*.go") | while read GO_FILE; do
local LL="$(license "${REPO_PATH}/${GODEP_SRC}/${GO_FILE}")"
if [ "${LL}" != "NONE" ] && [ "${LL}" != "${L}" ]; then
L="${LL}"
echo "${GO_FILE},${L}"
echo "License: ${GO_FILE} -> ${L}" >&2
fi
done
)
if [ -z "${FILE_LICENSE}" ]; then
FILE_LICENSE="UNKNOWN,UNKNOWN"
echo "License: ${PACKAGE} -> UNKNOWN" >&2
fi
echo "${PACKAGE},${FILE_LICENSE}"
done
}
# de-dupes pkg-file-license based on license file
function print-csv-body() {
declare -A PKG_LICENSE_MAP
pkg-file-license | while read LINE; do
local PKG="$(echo "${LINE}" | cut -d',' -f1)"
local LICENSE_FILE="$(echo "${LINE}" | cut -d',' -f2)"
local LICENSE="$(echo "${LINE}" | cut -d',' -f3)"
if [ "${LICENSE_FILE}" == "UNKNOWN" ]; then
echo "${PKG},${LICENSE}"
else
PKG="$(dirname "${LICENSE_FILE}")"
PKG="${PKG#${GODEP_SRC}/}" # strip prefix
if [ -z "${PKG_LICENSE_MAP["${PKG}"]:-}" ]; then
echo "${PKG},${LICENSE}"
PKG_LICENSE_MAP["${PKG}"]="${LICENSE}"
fi
fi
done
}
function print-csv() {
echo "Generating CSV" >&2
echo "Package,License"
print-csv-body
}
function print-markdown() {
echo "Generating Markdown" >&2
echo "Dependency Licenses"
echo "-------------------"
echo
echo "Package | License"
echo "------- | -------"
echo "$(print-csv-body | sed 's/,/ | /')"
}
main "$@"