-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathvalidate_format.sh
More file actions
executable file
·177 lines (146 loc) · 4.66 KB
/
validate_format.sh
File metadata and controls
executable file
·177 lines (146 loc) · 4.66 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
#!/usr/bin/env bash
# Copyright (c) 2023-2026, NVIDIA CORPORATION. All rights reserved.
#
# 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.
set -e -o pipefail -o nounset -x
if [[ ${DCGM_BUILD_INSIDE_DOCKER:-0} -eq 0 ]]; then
"$(dirname "$(realpath "${0}")")/intodocker.sh" --pull -- "${0}" "$@"
exit $?
fi
files_from=staged
readonly BATCH_SIZE=$(nproc)
# Python exclusion pattern matching .pep8 config and autopep8 defaults
# Manual exclusions required: autopep8 has no --assume-filename equivalent for stdin
readonly PYTHON_EXCLUDE_PATTERN='(^|/)(_out|sdk|PerfWorks|__pycache__)(/|$)|testing/python3/libs_3rdparty/|(^|/)\.'
function GetChangedFiles(){
case ${files_from} in
staged)
git diff --cached --name-only --diff-filter=d
;;
merged)
git diff-tree --name-only --diff-filter=d -r -m --no-commit-id HEAD
;;
*)
return
;;
esac
}
function GetCppFiles(){
GetChangedFiles | grep -E "\.(cpp|h|hpp|c)$" || true
}
function DetectPythonFiles(){
while IFS= read -r file
do
if (git show :"${file}" 2>/dev/null | file - | grep -q 'Python script')
then
printf '%s\n' "${file}"
fi
done
}
function GetPythonFiles(){
local all_files=$(GetChangedFiles | grep -vE "${PYTHON_EXCLUDE_PATTERN}" || true)
echo "${all_files}" | grep -E "\.py$" || true
echo "${all_files}" | grep -vE "\.py$" | DetectPythonFiles || true
}
function GetFileContent(){
local filename="${1}"
git show :"${filename}"
}
# Generic file validation function
# Args: $1=language_name, $2=format_command; reads file list from stdin
function ValidateFiles(){
local language_name="${1}"
local format_command="${2}"
local -a files=()
while IFS= read -r file; do
pushd "$(dirname "$(realpath "${file}")")" >/dev/null
if ! cmp -s <(GetFileContent "${file}") <(GetFileContent "${file}" | eval "${format_command}"); then
files+=("${file}")
fi
popd >/dev/null
done
if [[ ${#files[@]} -gt 0 ]]; then
echo "${language_name} format error within the following files:" >&2
printf "%s\n" "${files[@]}" >&2
if [ ${show_diff} -eq 1 ]; then
for file in "${files[@]}"; do
pushd "$(dirname "$(realpath "${file}")")" >/dev/null
diff -au --label "${file}" <(GetFileContent "${file}") --label "${file} (Formatted)" \
<(GetFileContent "${file}" | eval "${format_command}") || true
popd >/dev/null
echo ""
done
fi
return 1
fi
return 0
}
function ValidateCppFiles(){
GetCppFiles | ValidateFiles "C/C++" "clang-format --style=file --assume-filename=\${file}"
}
function ValidatePythonFiles(){
if ! command -v autopep8 &> /dev/null; then
echo "WARNING: autopep8 not found. Skipping Python file validation." >&2
return 0
fi
GetPythonFiles | ValidateFiles "Python" "autopep8 -"
}
function usage() {
echo "Formatting validation script for DCGM project
Usage: ${0} [options]
Where options are:
-d --diff : Show diff for files which does not complay
-m --merged : Check merged files instead of staged files (for Jenkins)
-h --help : This information
Without --merged argument this script will validate staged C/CPP/H/HPP and Python files."
}
LONG_OPTS=merged,diff,help
SHORT_OPTS=m,d,h
if ! PARSED=$(getopt --options=${SHORT_OPTS} --longoptions=${LONG_OPTS} --name "${0}" -- "$@"); then
echo "Failed to parse arguments" >&2
exit 1
fi
eval set -- "${PARSED}"
show_diff=0
while true; do
case "${1}" in
-h|--help)
usage
exit 0
;;
-m|--merged)
files_from=merged
shift
;;
-d|--diff)
show_diff=1
shift
;;
--)
shift
break
;;
*)
echo "Wrong arguments" >&2
exit 1
;;
esac
done
has_errors=0
if ! ValidateCppFiles; then
has_errors=1
fi
if ! ValidatePythonFiles; then
has_errors=1
fi
exit $has_errors