From d82c1ba9f6dae05c84a26a81c9715659980c945a Mon Sep 17 00:00:00 2001 From: Bruno Rocha Date: Fri, 23 Sep 2016 13:05:23 -0300 Subject: [PATCH] improved uuid checks on commit hook and make targets --- Makefile | 24 +++--- ...heck_duplicate_uuids.sh => check_uuids.sh} | 20 ++++- scripts/fix_uuids.sh | 75 +++++++++++++++++++ scripts/replace_dup_uuids.sh | 20 ----- scripts/replace_empty_uuids.sh | 25 ------- 5 files changed, 103 insertions(+), 61 deletions(-) rename scripts/{check_duplicate_uuids.sh => check_uuids.sh} (58%) create mode 100755 scripts/fix_uuids.sh delete mode 100755 scripts/replace_dup_uuids.sh delete mode 100755 scripts/replace_empty_uuids.sh diff --git a/Makefile b/Makefile index 02d396cab7e..2181b55c763 100644 --- a/Makefile +++ b/Makefile @@ -47,9 +47,8 @@ help: @echo " logs-join to join xdist log files into one" @echo " logs-clean to delete all xdist log files in the root" @echo " pyc-clean to delete all temporary artifacts" - @echo " uuid-check to check for duplicated @id: in testimony docstring tags" - @echo " uuid-replace-empty to replace empty @id: with new generated uuid" - @echo " uuid-replace-duplicate to replace duplicated @id: with new generated uuid" + @echo " uuid-check to check for duplicated or empty @id: in testimony docstring tags" + @echo " uuid-fix to fix all duplicated or empty @id: in testimony docstring tags" @echo " can-i-push? to check if local changes are suitable to push" @echo " install-commit-hook to install pre-commit hook to check if changes are suitable to push" @@ -132,26 +131,24 @@ logs-join: logs-clean: -rm -f robottelo_gw*.log -uuid-check: ## list duplicated uuids +uuid-check: ## list duplicated or empty uuids $(info "Checking for empty or duplicated @id: in docstrings...") - scripts/check_duplicate_uuids.sh + @scripts/check_uuids.sh -uuid-replace-duplicate: ## list duplicated uuids - scripts/replace_dup_uuids.sh - -uuid-replace-empty: ## list duplicated uuids - scripts/replace_empty_uuids.sh +uuid-fix: + @scripts/fix_uuids.sh flake8: $(info "Checking style and syntax errors with flake8 linter...") - @flake8 . --show-source + @flake8 $(shell git diff --name-only) --show-source can-i-push?: flake8 uuid-check test-docstrings test-robottelo $(info "!!! Congratulations your changes are good to fly, make a great PR! ${USER}++ !!!") install-commit-hook: $(info "Installing git pre-commit hook...") - echo "make can-i-push?" >> .git/hooks/pre-commit + @grep -q '^make uuid-fix' .git/hooks/pre-commit || echo "make uuid-fix" >> .git/hooks/pre-commit + @grep -q '^make can-i-push?' .git/hooks/pre-commit || echo "make can-i-push?" >> .git/hooks/pre-commit # Special Targets ------------------------------------------------------------- @@ -161,5 +158,4 @@ install-commit-hook: test-foreman-tier2 test-foreman-tier3 test-foreman-tier4 \ test-foreman-ui test-foreman-ui-xvfb test-foreman-endtoend \ graph-entities lint logs-join logs-clean pyc-clean \ - uuid-check uuid-replace-duplicate uuid-replace-empty \ - can-i-push? install-commit-hook + uuid-check uuid-fix can-i-push? install-commit-hook diff --git a/scripts/check_duplicate_uuids.sh b/scripts/check_uuids.sh similarity index 58% rename from scripts/check_duplicate_uuids.sh rename to scripts/check_uuids.sh index e8b81f9e0e2..17196757320 100755 --- a/scripts/check_duplicate_uuids.sh +++ b/scripts/check_uuids.sh @@ -3,19 +3,21 @@ # This script checks duplicated or empty uuids and exit with 1 if found # finds occurrences of empty @id: testimony tags -grep -E -i -r -n "@id:(.+[[:blank:]]|$)" tests/foreman/ +grep -E -i -r -n "@id:(.+[[:blank:]]|$)" tests/foreman/ --include=*.py EMPTY=$? if [ $EMPTY = 0 ]; then echo "Empty @id found in testimony tags" exit 1 +else + echo "No empty @id found" fi # Finds occurrences of @id: in testimony tags then # sort the output and filters only the duplicated # then looks for existence of "@id:" in final output # NOTE: can't print the line number -n here because of uniq -d -grep -r -i "@id:" tests/foreman/ | sort | uniq -d | grep "@id:" +grep -r -i "@id:" tests/foreman/ --include=*.py | sort | uniq -d | grep "@id:" DUPLICATE=$? # grep exits with status code 0 if text is found @@ -24,4 +26,18 @@ DUPLICATE=$? if [ $DUPLICATE = 0 ]; then echo "Duplicate @id found in testimony tags" exit 1 +else + echo "No duplicate @id found" fi + + +# finds missing empty space after @id: +grep -E -i -r -n "@id:[^ ]" tests/foreman/ --include=*.py +MISSING_SPACES=$? + +if [ $MISSING_SPACES = 0 ]; then + echo "Found @id: tags missing space after : ..." + exit 1 +else + echo "No @id missing spaces after : was found" +fi \ No newline at end of file diff --git a/scripts/fix_uuids.sh b/scripts/fix_uuids.sh new file mode 100755 index 00000000000..31a5c3ed25d --- /dev/null +++ b/scripts/fix_uuids.sh @@ -0,0 +1,75 @@ +#!/usr/bin/env bash + +# This script finds empty @id: and replace with new uuids +# Then it finds for duplicates and replaces last occurrence with new uuids +# finally it fixes '@id:xyz' to '@id: xyz' (missing space after :) + +# finds occurrences of empty @id: testimony tags +EMPTY_IDS=$(grep -E -i -r -n "@id:(.+[[:blank:]]|$)" tests/foreman/ --include=*.py) + +if [ -n "$EMPTY_IDS" ]; then + echo "Generating new UUIDS for empty @id tags..." +else + echo "No empty @id was found" +fi + +# iterate if any empty @id found +for output_line in $EMPTY_IDS +do + if (echo "$output_line" | grep "tests/foreman"); then + OLDIFS=$IFS + # splits the grep output to get filename and occurrence line number + IFS=':' read -r filename line <<< $output_line + # generate uuid and place in specific line number + NEW_ID=$(python -c 'import uuid; print(uuid.uuid4())') + sed -r -i~ "${line}s/@id:(.+[[:blank:]]|$)/@id: ${NEW_ID}/g" $filename + IFS=$OLDIFS + fi +done + +# This script finds duplicated @id and replaces with new uuids + +DUP_EXISTS=$(grep -r -i "@id:" tests/foreman/ --include=*.py | sort | uniq -d | grep "@id:") + +if [ -n "$DUP_EXISTS" ]; then + echo "Generating new UUIDS for duplicated @id tags..." +else + echo "No duplicated @id was found" +fi + +grep -r -i "@id:" tests/foreman/ --include=*.py | sort | uniq -d | grep "@id:" | while read -r line ; do + OLDIFS=$IFS + IFS=':' read -r dup_file dup_id <<< $line + echo "filename: $dup_file" + echo "Id to replace: $dup_id" + NEW_ID=$(python -c 'import uuid; print(uuid.uuid4())') + echo "Replacing with the new id: $NEW_ID" + LAST_LINE=$(grep -i -n "$dup_id" $dup_file | tail -1) + + IFS=':' read -r linenumber linecontent <<< $LAST_LINE + echo $linenumber + trimmed_linecontent=$(echo $linecontent) + sed -i~ "${linenumber}s/${trimmed_linecontent}/@id: ${NEW_ID}/g" $dup_file + echo "----------------------------------------------------------------" + IFS=$OLDIFS +done + +# This script finds @id: missing spaces after : + + +MISSING_SPACES=$(grep -E -i -r -n "@id:[^ ]" tests/foreman/ --include=*.py) + +if [ -n "$MISSING_SPACES" ]; then + echo "Fixing @id: tags missing spaces..." +else + echo "No @id missing spaces was found" +fi + +grep -E -i -r -n "@id:[^ ]" tests/foreman/ --include=*.py | while read -r line ; do + OLDIFS=$IFS + IFS=':' read -r missing_file linenumber linecontent <<< $line + IFS=':' read -r tag uuid <<< $linecontent + trimmed_linecontent=$(echo $linecontent) + sed -i~ "${linenumber}s/${trimmed_linecontent}/${tag}: ${uuid}/g" $missing_file + IFS=$OLDIFS +done diff --git a/scripts/replace_dup_uuids.sh b/scripts/replace_dup_uuids.sh deleted file mode 100755 index df5bb0109fb..00000000000 --- a/scripts/replace_dup_uuids.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/env bash - -# This script finds duplicated @id and replaces with new uuids - -grep -r -i "@id:" tests/foreman/ | sort | uniq -d | grep "@id:" | while read -r line ; do - OLDIFS=$IFS - IFS=':' read -r dup_file dup_id <<< $line - echo "filename: $dup_file" - echo "Id to replace: $dup_id" - NEW_ID=$(uuidgen) - echo "Replacing with the new id: $NEW_ID" - LAST_LINE=$(grep -i -n "$dup_id" $dup_file | tail -1) - - IFS=':' read -r linenumber linecontent <<< $LAST_LINE - echo $linenumber - trimmed_linecontent=$(echo $linecontent) - sed -i~ "${linenumber}s/${trimmed_linecontent}/@id: ${NEW_ID}/g" $dup_file - echo "----------------------------------------------------------------" - IFS=$OLDIFS -done diff --git a/scripts/replace_empty_uuids.sh b/scripts/replace_empty_uuids.sh deleted file mode 100755 index 9dc96db0c0a..00000000000 --- a/scripts/replace_empty_uuids.sh +++ /dev/null @@ -1,25 +0,0 @@ -#!/usr/bin/env bash - -# this script finds empty @id: and replace with new uuids - -# finds occurrences of empty @id: testimony tags -EMPTY_IDS=$(grep -E -i -r -n "@id:(.+[[:blank:]]|$)" tests/foreman/) - -if [ -n "$EMPTY_IDS" ]; then - echo "Generating new UUIDS for empty @id tags..." -else - echo "No empty @id was found" -fi - -# iterate if any empty @id found -for output_line in $EMPTY_IDS -do - if (echo "$output_line" | grep "tests/foreman"); then - OLDIFS=$IFS - # splits the grep output to get filename and occurrence line number - IFS=':' read -r filename line <<< $output_line - # generate uuid and place in specific line number - sed -r -i~ "${line}s/@id:(.+[[:blank:]]|$)/@id: $(uuidgen)/g" $filename - IFS=$OLDIFS - fi -done