forked from alibaba/OpenSandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify-license.sh
More file actions
executable file
·151 lines (133 loc) · 3.62 KB
/
verify-license.sh
File metadata and controls
executable file
·151 lines (133 loc) · 3.62 KB
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
#!/bin/bash
# Copyright 2025 Alibaba Group Holding Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This script verifies that required files contain the Apache 2.0 license header.
# It scans tracked source files and fails with a list of violations if any header
# is missing.
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
CURRENT_YEAR="$(date +%Y)"
MIN_YEAR="2025"
LICENSE_OWNER="Alibaba Group Holding Ltd."
LICENSE_REGEX="Copyright [0-9]{4} ${LICENSE_OWNER// / }"
# File extensions that are expected to carry a license header.
LICENSE_EXTS=(
go py sh kt kts java ts tsx js jsx toml html css sql tf
)
# Explicit file basenames that should also be checked (e.g., Dockerfile)
LICENSE_BASENAMES=(
Dockerfile
)
# Paths to ignore entirely.
IGNORED_PATHS=(
"LICENSE"
"NOTICE"
"docs/"
"scripts/spec-doc/index.html" # Generated doc
)
is_k8s_mock_go() {
local file="${1-}"
[[ -z "$file" ]] && return 1
# Skip any Go mocks under kubernetes/internal:
# - filenames ending with _mock.go
# - any file under a /mock/ directory
if [[ "$file" != kubernetes/internal/* ]]; then
return 1
fi
if [[ "$file" == *"_mock.go" ]]; then
return 0
fi
if [[ "$file" == */mock/*.go ]]; then
return 0
fi
return 1
}
is_generated_to_skip() {
local file="$1"
# Skip common generated files
if [[ "$file" == *"deepcopy.go" ]]; then
return 0
fi
return 1
}
cd "$REPO_ROOT"
is_ignored() {
local file="$1"
for ignore in "${IGNORED_PATHS[@]}"; do
if [[ "$ignore" == */ ]]; then
if [[ "$file" == "$ignore"* ]]; then
return 0
fi
elif [[ "$file" == "$ignore" ]]; then
return 0
fi
done
return 1
}
has_expected_extension() {
local file="$1"
local ext="${file##*.}"
for candidate in "${LICENSE_EXTS[@]}"; do
if [[ "$ext" == "$candidate" ]]; then
return 0
fi
done
return 1
}
has_expected_basename() {
local file="$1"
local base
base="$(basename "$file")"
for candidate in "${LICENSE_BASENAMES[@]}"; do
if [[ "$base" == "$candidate" ]]; then
return 0
fi
done
return 1
}
missing=()
while IFS= read -r file; do
# Skip ignored paths
if is_ignored "$file"; then
continue
fi
# Skip kubernetes internal mock go files
if is_k8s_mock_go "$file"; then
continue
fi
# Skip generated files
if is_generated_to_skip "$file"; then
continue
fi
# Only check files with expected extensions or basenames
if ! has_expected_extension "$file" && ! has_expected_basename "$file"; then
continue
fi
# Limit scan to the first 25 lines to allow shebangs/DOCTYPE above the header.
header="$(head -n 25 "$file")"
if ! echo "$header" | grep -Eq "$LICENSE_REGEX"; then
missing+=("$file")
continue
fi
found_year="$(echo "$header" | grep -Eo "$LICENSE_REGEX" | head -n1 | grep -Eo '[0-9]{4}')"
if [[ -z "$found_year" || "$found_year" -gt "$CURRENT_YEAR" || "$found_year" -lt "$MIN_YEAR" ]]; then
missing+=("$file")
fi
done < <(git -C "$REPO_ROOT" ls-files)
if ((${#missing[@]} > 0)); then
echo "Missing license header in the following files:"
printf ' - %s\n' "${missing[@]}"
exit 1
fi
echo "License headers verified."