Merge "add license scan to stage_2"
[osm/devops.git] / tools / license_scan.sh
1 #
2 # Copyright 2016 Telefónica Investigación y Desarrollo, S.A.U.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 #!/bin/sh
17
18 echo GERRIT BRANCH is $GERRIT_BRANCH
19 dpkg -l wget &>/dev/null ||sudo apt-get install -y wget
20 dpkg -l curl &>/dev/null ||sudo apt-get install -y curl
21 #Curl can be used instead of wget:
22 #curl -s -X POST -d @$file https://osm.etsi.org/fossology/?mod=agent_nomos_once
23
24 apache=0
25 nolicense=0
26 other=0
27
28 git fetch
29
30 RE="FATAL: your file did not get passed through"
31
32 for file in $(git diff --name-only origin/$GERRIT_BRANCH); do
33 if [ -f $file ]; then
34 if [ -s $file ]; then
35 license=$(wget -qO - --post-file $file https://osm.etsi.org/fossology/?mod=agent_nomos_once |sed "s/^[ \t]*//;s/[ \t]*$//")
36 if [[ $license =~ $RE ]]; then
37 # possibly we have exceeded the post rate
38 sleep 10
39 license=$(wget -qO - --post-file $file https://osm.etsi.org/fossology/?mod=agent_nomos_once |sed "s/^[ \t]*//;s/[ \t]*$//")
40 fi
41 else
42 license="No_license_found"
43 fi
44 else
45 license="DELETED"
46 fi
47 echo "$file $license"
48 case "$license" in
49 "Apache-2.0")
50 apache=$((apache + 1))
51 ;;
52 "No_license_found")
53 nolicense=$((nolicense + 1))
54 ;;
55 "DELETED")
56 ;;
57 "FATAL:*")
58 ;;
59 *)
60 echo "BAD LICENSE ON FILE $file"
61 other=$((other + 1))
62 ;;
63 esac
64 done
65
66 if [ $other -gt 0 ]; then
67 echo "FATAL: Non-apache licenses detected"
68 exit 2
69 fi
70
71 if [ $nolicense -gt 0 ]; then
72 echo "WARNING: Unlicensed files found"
73 fi
74
75 exit 0