Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix verify codecgen #25108

Merged
merged 1 commit into from
May 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 12 additions & 5 deletions hack/update-codecgen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ source "${KUBE_ROOT}/hack/lib/init.sh"

kube::golang::setup_env

# The sort at the end makes sure we feed the topological sort a deterministic
# list (since there aren't many dependencies).

generated_files=($(
find . -not \( \
\( \
Expand All @@ -34,7 +37,7 @@ generated_files=($(
-o -wholename '*/Godeps/*' \
-o -wholename '*/codecgen-*-1234.generated.go' \
\) -prune \
\) -name '*.generated.go'))
\) -name '*.generated.go' | sort -r))

# Register function to be called on EXIT to remove codecgen
# binary and also to touch the files that should be regenerated
Expand Down Expand Up @@ -63,13 +66,17 @@ function depends {
file=${generated_files[$1]//\.generated\.go/.go}
deps=$(go list -f "{{.Deps}}" ${file} | tr "[" " " | tr "]" " ")
candidate=$(readlinkdashf "${generated_files[$2]//\.generated\.go/.go}")
result=false
for dep in ${deps}; do
if [[ ${candidate} = *${dep} ]]; then
result=true
# Only look at dependencies within the kubernetes tree-- otherwise the "io"
# package matches the end of one of our directories.
if [[ ${dep} = "k8s.io"* ]]; then
if [[ ${candidate} = *${dep} ]]; then
echo true
return
fi
fi
done
echo ${result}
echo false
}

function tsort {
Expand Down
88 changes: 14 additions & 74 deletions hack/verify-codecgen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,85 +35,25 @@ generated_files=($(
\) -prune \
\) -name '*.generated.go'))

# create a nice clean place to put codecgen there
_tmpdir="$(mktemp -d -t codecgen.XXXXXX)"
function cleanup {
echo "Removing ${_tmpdir}"
rm -rf "${_tmpdir}"
}
trap cleanup EXIT

# Sort all files in the dependency order.
number=${#generated_files[@]}
for (( i=0; i<number; i++ )); do
visited[${i}]=false
for generated_file in ${generated_files[@]}; do
cat "${generated_file}" > "${generated_file}.original"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: why cat instead of cp?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to worry about cp barfing if the file already existed.

done
result=""

function depends {
file=${generated_files[$1]//\.generated\.go/.go}
deps=$(go list -f "{{.Deps}}" ${file} | tr "[" " " | tr "]" " ")
candidate=$(readlinkdashf "${generated_files[$2]//\.generated\.go/.go}")
result=false
for dep in ${deps}; do
if [[ ${candidate} = *${dep} ]]; then
result=true
fi
done
echo ${result}
}

function tsort {
visited[$1]=true
local j=0
for (( j=0; j<number; j++ )); do
if ! ${visited[${j}]}; then
if $(depends "$1" ${j}); then
tsort $j
fi
fi
done
result="${result} $1"
}
for (( i=0; i<number; i++ )); do
if ! ${visited[${i}]}; then
tsort ${i}
fi
done
index=(${result})

CODECGEN="${_tmpdir}/codecgen_binary"
godep go build -o "${CODECGEN}" github.com/ugorji/go/codec/codecgen
${KUBE_ROOT}/hack/update-codecgen.sh

ret=0
# Generate files in the dependency order.
for current in ${index[@]}; do
generated_file=${generated_files[${current}]}
initial_dir=${PWD}
file=${generated_file//\.generated\.go/.go}
# codecgen work only if invoked from directory where the file
# is located.
pushd "$(dirname ${file})" > /dev/null
base_file=$(basename "${file}")
base_generated_file=$(basename "${generated_file}")
# temporarily move the generated file to a non-go file so it doesn't influence the verify codecgen
mv "${base_generated_file}" "${base_generated_file}.bak"
# We use '-d 1234' flag to have a deterministic output every time.
# The constant was just randomly chosen.
${CODECGEN} -d 1234 -o "${base_generated_file}.1tmp" "${base_file}"
# Add boilerplate at the beginning of the generated file.
sed 's/YEAR/2015/' "${initial_dir}/hack/boilerplate/boilerplate.go.txt" > "${base_generated_file}.tmp"
cat "${base_generated_file}.1tmp" >> "${base_generated_file}.tmp"
rm "${base_generated_file}.1tmp"
# restore the generated file
mv "${base_generated_file}.bak" "${base_generated_file}"
ret=0
diff -Naupr -I 'Auto generated by' "${base_generated_file}" "${base_generated_file}.tmp" || ret=$?
if [[ $ret -eq 0 ]]; then
for generated_file in ${generated_files[@]}; do
cur=0
diff -Naupr -I 'Auto generated by' "${generated_file}" "${generated_file}.original" || cur=$?
if [[ $cur -eq 0 ]]; then
echo "${generated_file} up to date."
else
echo "${generated_file} is out of date. Please run hack/update-codecgen.sh"
exit 1
echo "${generated_file} was out of date. Please run hack/update-codecgen.sh. (If you're running locally, this was run for you already.)"
ret=1
fi
rm "${base_generated_file}.tmp"
popd > /dev/null

rm -f "${generated_file}.original"
done

exit $ret