forked from opensandbox-group/OpenSandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-license.sh
More file actions
executable file
·220 lines (189 loc) · 4.97 KB
/
Copy pathadd-license.sh
File metadata and controls
executable file
·220 lines (189 loc) · 4.97 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
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
#!/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.
# Add Apache 2.0 license headers to source files that are missing them.
# Usage: run from repo root: ./scripts/add-license.sh
set -euo pipefail
LICENSE_YEAR=$(date +%Y)
LICENSE_OWNER="Alibaba Group Holding Ltd."
LICENSE_MARKER_REGEX="Copyright [0-9]{4} ${LICENSE_OWNER// / }"
LICENSE_TEXT_TEMPLATE=$(
cat <<'EOF'
Copyright __YEAR__ 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.
EOF
)
# Basenames to include regardless of extension.
INCLUDE_BASENAMES=("Dockerfile")
# Paths to ignore.
IGNORES=(
".git"
"node_modules"
".venv"
"venv"
"dist"
"build"
"__pycache__"
"LICENSE"
"NOTICE"
"README.md"
"README_zh.md"
"scripts/spec-doc/index.html"
)
has_license() {
local file="$1"
head -n 40 "$file" | grep -Eq "$LICENSE_MARKER_REGEX"
}
comment_header() {
local style="$1"
local text="$2"
case "$style" in
"line:#")
printf '%s\n' "$text" | sed 's/^/# /'
;;
"line://")
printf '%s\n' "$text" | sed 's:^:// :'
;;
"block:html")
printf "<!--\n%s\n-->\n" "$text"
;;
"block:css")
printf "/*\n%s\n*/\n" "$text"
;;
*)
return 1
;;
esac
}
should_ignore_path() {
local file="$1"
for ignore in "${IGNORES[@]}"; do
if [[ "$file" == "$ignore" || "$file" == "$ignore"/* ]]; then
return 0
fi
done
return 1
}
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_go() {
local file="${1-}"
[[ -z "$file" ]] && return 1
[[ "${file##*.}" != "go" ]] && return 1
local base
base="$(basename "$file")"
case "$base" in
zz_generated.*|*.pb.go|*_pb.go|*_gen.go|*_generated.go)
return 0
;;
esac
if head -n 5 "$file" | grep -qi "code generated"; then
return 0
fi
return 1
}
style_for_file() {
local file="${1-}"
[[ -z "$file" ]] && { echo ""; return; }
local base ext
base="$(basename "$file")"
ext="${file##*.}"
case "$ext" in
sh|py|toml|tf|sql) echo "line:#"; return ;;
go|java|kt|kts|ts|tsx|js|jsx|mjs|cjs|mts|cts) echo "line://"; return ;;
css) echo "block:css"; return ;;
html) echo "block:html"; return ;;
esac
for b in "${INCLUDE_BASENAMES[@]}"; do
if [[ "$base" == "$b" ]]; then
echo "line:#"
return
fi
done
echo ""
}
process_file() {
local file="${1-}"
[[ -z "$file" ]] && return
local style
if should_ignore_path "$file"; then
return
fi
if is_k8s_mock_go "$file"; then
return
fi
style="$(style_for_file "$file")"
[[ -z "$style" ]] && return
if is_generated_go "$file"; then
return
fi
if has_license "$file"; then
return
fi
local license_text header
license_text="${LICENSE_TEXT_TEMPLATE/__YEAR__/$LICENSE_YEAR}"
header="$(comment_header "$style" "$license_text")"
# Respect shebang: insert after the first line if it starts with #!
if head -n1 "$file" | grep -q "^#!"; then
local first rest
first="$(head -n1 "$file")"
rest="$(tail -n +2 "$file")"
printf '%s\n\n%s\n\n%s' "$first" "$header" "$rest" >"$file"
# Place before DOCTYPE for HTML to avoid breaking rendering.
elif head -n1 "$file" | grep -qi "^<!doctype"; then
local body
body="$(cat "$file")"
printf '%s\n\n%s' "$header" "$body" >"$file"
else
local body
body="$(cat "$file")"
printf '%s\n\n%s' "$header" "$body" >"$file"
fi
echo "Added license: $file"
}
main() {
local files
if [[ "$#" -gt 0 ]]; then
IFS=$'\n' read -r -d '' -a files < <(git ls-files -- "$@" && printf '\0')
else
IFS=$'\n' read -r -d '' -a files < <(git ls-files && printf '\0')
fi
for f in "${files[@]}"; do
process_file "$f"
done
}
main "$@"