-
Notifications
You must be signed in to change notification settings - Fork 10
/
regenerate-go-license.sh
executable file
·171 lines (137 loc) · 3.45 KB
/
regenerate-go-license.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
#!/usr/bin/env bash
set -e
function print {
cat <<EOF>>$XMLFILE
<dependency>
<packageName>$1</packageName>
<version>$2</version>
<licenses>
<license>
<name>$3</name>
<url>$4</url>
</license>
</licenses>
</dependency>
EOF
}
function get_version() {
module=$1
while true
do
version=$(GO111MODULE=on go list -m "${module}" 2>/dev/null| awk '{print $2}')
if [[ -z ${version} ]]; then
module=$(echo "${module}" | awk '@include "join"; {split($1,parts,"/"); if (length(parts) > 1) print join(parts,1,length(parts)-1,"/")}')
if [[ -z ${module} ]]; then
echo Unknown
return
fi
else
echo "$version"
return
fi
done
}
do_usage_and_exit () {
1>&2 echo "$0: Usage $0 [--keep-work-dir] [--tag <tag>] <repo>"
exit 0;
}
clean() {
rm -rf ${1}
}
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
DOCKER_ORG=amq7
VERSION=dev
KEEP_WORK_DIR=0
declare -a POS
while [[ $# -gt 0 ]]
do
case "$1" in
--keep-work-dir)
KEEP_WORK_DIR=1
shift
;;
--tag)
TAG=$2
shift 2
;;
--help)
do_usage_and_exit
;;
*)
POS+=("$1")
shift
;;
esac
done
set -- "${POS[@]}"
REPO=${1}
if [[ -z "${REPO}" ]]; then
do_usage_and_exit
fi
TARGET_LICENSE_DIR="${DIR}/modules/common/go/added/licenses"
if [[ ! -d "${TARGET_LICENSE_DIR}" ]]; then
1>&2 echo "$0: Target license directory ${TARGET_LICENSE_DIR} does not exist."
exit 1
fi
WORKDIR=$(mktemp -d)
declare -a GITARGS
GITARGS+=("--depth")
GITARGS+=("1")
if [[ ! -z "${TAG}" ]]; then
GITARGS+=("--branch")
GITARGS+=("${TAG}")
fi
GITARGS+=("--")
GITARGS+=("${REPO}")
GITARGS+=("${WORKDIR}")
if [[ ${KEEP_WORK_DIR} -eq 0 ]]; then
trap clean EXIT
fi
echo Shallow cloning ${REPO} ${TAG}
git clone ${GITARGS[*]}
rc=$?
if [[ $rc != 0 ]]; then
1>&2 echo "$0: Git clone failed."
exit $rc
fi
pushd ${WORKDIR}
VENDORLIST=${WORKDIR}/vendor-list.txt
XMLFILE=$WORKDIR/licenses.xml
HTMLFILE=$WORKDIR/licenses.html
XSLFILE=$WORKDIR/scripts/licenses.xsl
LISTFILE=$WORKDIR/licenses.txt
ARCHIVE=$WORKDIR/licenses.tgz
go-licenses csv ./pkg/... 2>/dev/null > ${VENDORLIST}
touch ${LISTFILE}
cat <<EOF>>$XMLFILE
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<licenseSummary>
<dependencies>
EOF
#github.com/prometheus/client_model/go,https://github.com/prometheus/client_model/blob/master/go/LICENSE,Apache-2.0
pushd ${WORKDIR}/vendor
while IFS= read -r line ; do
module=$(echo $line | awk -F ',' '{print $1}')
licenseurl=$(echo $line | awk -F ',' '{print $2}')
licensename=$(echo $line | awk -F ',' '{print $3}')
version=$(get_version "${module}")
if [[ -d "${module}" ]]; then
licensefile=$(find "${module}" -iname 'license*')
if [[ -z "$licensefile" ]]; then
licensefile=$(find "${module}" -iname 'readme*')
fi
if [[ "$licensefile" ]]; then
echo $licensefile >> $LISTFILE
fi
fi
print ${module} ${version} ${licensename} ${licenseurl}
done < ${VENDORLIST}
tar -zcf $ARCHIVE ${WORKDIR}/LICENSE $(cat $LISTFILE)
popd
cat <<EOF>>$XMLFILE
</dependencies>
</licenseSummary>
EOF
xsltproc ${XSLFILE} ${XMLFILE} > ${HTMLFILE}
cp ${XMLFILE} ${LISTFILE} ${ARCHIVE} ${HTMLFILE} ${TARGET_LICENSE_DIR}/
exit 0