Repository navigation
Expand file tree
/
Copy pathmaiwei_base.sh
More file actions
executable file
·300 lines (269 loc) · 5.77 KB
/
Copy pathmaiwei_base.sh
File metadata and controls
executable file
·300 lines (269 loc) · 5.77 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/bin/bash
# set -u
export BOLD='\033[1m'
export RED='\033[0;31m'
export BLUE='\033[0;34m'
export GREEN='\033[32m'
export WHITE='\033[34m'
export YELLOW='\033[33m'
export NO_COLOR='\033[0m'
function info() {
(echo >&2 -e "[${WHITE}${BOLD}INFO${NO_COLOR}] $*")
}
function error() {
(echo >&2 -e "${RED}[ERROR] $*${NO_COLOR}")
}
function warning() {
(echo >&2 -e "${YELLOW}[WARNING] $*${NO_COLOR}")
}
function ok() {
(echo >&2 -e "[${GREEN}${BOLD} OK ${NO_COLOR}] $*")
}
function print_green() {
echo >&2 -e "${GREEN} $*${NO_COLOR}"
}
function print_yellow() {
echo >&2 -e "${YELLOW} $*${NO_COLOR}"
}
function print_red() {
echo >&2 -e "${RED} $*${NO_COLOR}"
}
function file_ext() {
local filename
filename="${1##*/}"
local actual_ext
actual_ext="${filename##*.}"
if [[ "${actual_ext}" == "${filename}" ]]; then
actual_ext=""
fi
echo "${actual_ext}"
}
# Get composite file extension for file with extensions such as
# ".pb.txt", ".pb.bin", ".pb.h", ".pb.cc"
# Examples:
# abc => <nil>
# .abc => abc
# abc.txt => txt
# .abc.txt => abc.txt
# a.pb.txt => pb.txt
# a.b.pb.txt => pb.txt
# Ref: https://stackoverflow.com/questions/10586153/how-to-split-a-string-into-an-array-in-bash
function file_comp_ext() {
local filename
filename="$(basename "$1")"
readarray -d . -t fds <<< "${filename}."
unset "fds[-1]"
local n="${#fds[@]}"
case "${n}" in
1)
echo ""
;;
2)
echo "${fds[1]}"
;;
*)
echo "${fds[-2]}.${fds[-1]}"
;;
esac
}
function array_contains() {
local match="$1"
shift
for _ent in "$@"; do
if [[ "${_ent}" == "${match}" ]]; then
return 0
fi
done
return 1
}
# Checks if the file has a c/c++/cuda extension.
function c_family_ext() {
local actual_ext
actual_ext="$(file_ext "$1")"
local _c_family_exts=("h" "hh" "hxx" "hpp" "c" "cc" "cxx" "cpp" "cu" "cuh")
if array_contains "${actual_ext}" "${_c_family_exts[@]}"; then
return 0
else
return 1
fi
}
function c_family_header() {
local ext
ext="$(file_ext "$1")"
[[ "${ext}" == "h" ||
"${ext}" == "hh" ||
"${ext}" == "hpp" ||
"${ext}" == "hxx" ||
"${ext}" == "cuh" ]]
}
# Checks if the specified argument is a proto file.
function proto_ext() {
[[ "$(file_ext "$1")" == "proto" || "$(file_comp_ext "$1")" == "pb.txt" ]]
}
function go_ext() {
[[ "$(file_ext "$1")" == "go" ]]
}
function build_ext() {
local fname
fname="$(basename "$1")"
[[ "${fname}" == "BUILD" ]]
}
function bazel_ext() {
local fname
fname="$(basename "$1")"
if [[ "${fname}" == "BUILD" || "${fname}" == "WORKSPACE" ]]; then
return 0
fi
local __ext
__ext="$(file_ext "${fname}")"
for ext in "bzl" "bazel" "BUILD"; do
if [ "${ext}" == "${__ext}" ]; then
return 0
fi
done
return 1
}
function plain_py_ext() {
local ext
ext="$(file_ext "$1")"
# shellcheck disable=SC2076
[[ "${ext}" == "py" ]] && ! [[ "$1" =~ "_pb2.py" || "$1" =~ "_pb2_grpc.py" ]]
}
function bash_ext() {
local ext
ext="$(file_ext "$1")"
[[ "${ext}" == "sh" || "${ext}" == "bash" ]]
}
function optarg_check_for_opt() {
local opt="$1"
local optarg="$2"
if [[ -z "${optarg}" || "${optarg}" =~ ^-.* ]]; then
error "Missing parameter for ${opt}. Exiting..."
exit 1
fi
}
function read_one_line_file() {
local fpath="$1"
local text
read -r text < "${fpath}"
echo "${text}"
}
function inside_docker() {
[[ -f /.dockerenv ]]
}
function inside_git() {
[[ "$(git rev-parse --is-inside-work-tree 2> /dev/null)" == true ]]
}
# Returns the current git branch.
function current_git_branch() {
git rev-parse --abbrev-ref HEAD
}
###########################################################
# Get canonical office name
# Arguments:
# office name as cmdline argument or env variable
# Outputs:
# Write canonical name to stdout
###########################################################
function canonical_office() {
local result=
local office="$1"
case "${office}" in
beijing-tf | beijing | bj)
result="beijing"
;;
shenzhen-yx | shenzhen)
result="shenzhen"
;;
suzhou-tc | suzhou)
result="suzhou"
;;
bayarea-scott | bayarea | us)
result="bayarea"
;;
guangzhou)
result="guangzhou"
;;
*)
result="${office}"
;;
esac
echo "${result}"
}
#############################################
# Get the country of the "canonical" office
# Arguments:
# Canonical office name
# Outputs:
# Write country name to stdout
#############################################
function country_of_office() {
local country=
case "$1" in
beijing | shenzhen | suzhou | wuhan | guangzhou)
country="cn"
;;
bayarea)
country="us"
;;
*)
country="unknown"
;;
esac
echo "${country}"
}
function determine_arm64_model() {
local arch
arch="$(uname -m)"
if [[ "${arch}" == "x86_64" ]]; then
return 1
fi
local prod
if [[ -f /etc/nv_tegra_release ]]; then
prod="jetson"
else
prod="drive"
fi
local soc
soc="$(tr -d '\0' < /proc/device-tree/compatible | awk -F',' '{print $NF}')"
case "${soc}" in
tegra194)
echo "${prod}-xavier"
;;
tegra23*)
echo "${prod}-orin"
;;
*)
echo "" # E.g. arm64 machines on CI
;;
esac
}
# sudo not exists and default user is root on j5
if ! command -v sudo > /dev/null 2>&1; then
function sudo() {
local -a args=()
local flag=0
while [[ "$#" -gt 0 ]]; do
if [ $flag -eq 1 ]; then
args+=("$1")
shift
else
case "$1" in # ignore args of sudo
-*)
shift
;;
*)
args+=("$1")
flag=1
shift
;;
esac
fi
done
"${args[@]}"
}
fi
function q_sudo() {
cmd="sudo ${*}"
eval "${cmd}"
}