Debian Codegraph @

utils/dependency_license.sh

raw
1 #!/bin/bash
2
3 set -eo pipefail
4
5 src=go.mod
6 tgt=COPYING_NOTES.md
7
8 STARTAUTOGEN="<!-- START AUTOGEN -->"
9 ENDAUTOGEN="<!-- END AUTOGEN -->"
10 RE_STARTAUTOGEN="^${STARTAUTOGEN}$"
11 RE_ENDAUTOGEN="^${ENDAUTOGEN}$"
12 tmpDepLicenses=""
13
14 error(){
15 echo "Error: $*"
16 exit 1
17 }
18
19 generate_dep_licenses(){
20 [ -r $src ] || error "Cannot read file '$src'"
21
22
23 tmpDepLicenses="$(mktemp)"
24
25 # Collect all go.mod lines beginig with tab:
26 # * which no replace
27 # * which have replace
28 grep -E $'^\t[^=>]*$' $src | sed -r 's/\t([^ ]*) v.*/\1/g' > "$tmpDepLicenses"
29 # Replace each line with formated link
30 sed -i -r '/^github.com\/therecipe\/qt\/internal\/binding\/files\/docs\//d;' "$tmpDepLicenses"
31 sed -i -r 's|^(.*)/([[:alnum:]-]+)/(v[[:digit:]]+)$|* [\2](https://\1/\2/\3)|g' "$tmpDepLicenses"
32 sed -i -r 's|^(.*)/([[:alnum:]-]+)$|* [\2](https://\1/\2)|g' "$tmpDepLicenses"
33 sed -i -r 's|^(.*)/([[:alnum:]-]+).(v[[:digit:]]+)$|* [\2](https://\1/\2.\3)|g' "$tmpDepLicenses"
34
35 ## add license file to github links, and others
36 sed -i -r '/github.com/s|^(.*(https://[^)]+).*)$|\1 available under [license](\2/blob/master/LICENSE) |g' "$tmpDepLicenses"
37 sed -i -r '/golang.org\/x/s|^(.*golang.org/x/([^)]+).*)$|\1 available under [license](https://cs.opensource.google/go/x/\2/+/master:LICENSE) |g' "$tmpDepLicenses"
38 sed -i -r '/google.golang.org\/grpc/s|^(.*)$|\1 available under [license](https://github.com/grpc/grpc-go/blob/master/LICENSE) |g' "$tmpDepLicenses"
39 sed -i -r '/google.golang.org\/protobuf/s|^(.*)$|\1 available under [license](https://github.com/protocolbuffers/protobuf/blob/main/LICENSE) |g' "$tmpDepLicenses"
40 sed -i -r '/google.golang.org\/genproto/s|^(.*)$|\1 available under [license](https://pkg.go.dev/google.golang.org/genproto?tab=licenses) |g' "$tmpDepLicenses"
41 sed -i -r '/go.uber.org\/goleak/s|^(.*)$|\1 available under [license](https://pkg.go.dev/go.uber.org/goleak?tab=licenses) |g' "$tmpDepLicenses"
42 sed -i -r '/gopkg.in\/yaml\.v3/s|^(.*)$|\1 available under [license](https://github.com/go-yaml/yaml/blob/v3.0.1/LICENSE) |g' "$tmpDepLicenses"
43
44 }
45
46
47 check_dependecies(){
48 generate_dep_licenses
49
50 tmpHaveLicenses=$(mktemp)
51 sed "/${RE_STARTAUTOGEN}/,/${RE_ENDAUTOGEN}/!d;//d" $tgt > "$tmpHaveLicenses"
52
53 diffOK=0
54 if ! diff "$tmpHaveLicenses" "$tmpDepLicenses"; then diffOK=1; fi
55
56 rm "$tmpDepLicenses" || echo "Failed to clean tmp file"
57 rm "$tmpHaveLicenses" || echo "Failed to clean tmp file"
58
59 [ $diffOK -eq 0 ] || error "Dependency licenses are not up-to-date"
60 exit 0
61 }
62
63 update_dependecies(){
64 generate_dep_licenses
65
66 sed -i -e "/${RE_STARTAUTOGEN}/,/${RE_ENDAUTOGEN}/!b" \
67 -e "/${RE_ENDAUTOGEN}/i ${STARTAUTOGEN}" \
68 -e "/${RE_ENDAUTOGEN}/r $tmpDepLicenses" \
69 -e "/${RE_ENDAUTOGEN}/a ${ENDAUTOGEN}" \
70 -e "d" \
71 $tgt
72
73
74 rm "$tmpDepLicenses" || echo "Failed to clean tmp file"
75
76 exit 0
77 }
78
79 case $1 in
80 "check") check_dependecies;;
81 "update") update_dependecies;;
82 *) error "One of actions needed: check update" ;;
83 esac
84