|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# This script finds empty @id: and replace with new uuids |
| 4 | +# Then it finds for duplicates and replaces last occurrence with new uuids |
| 5 | +# finally it fixes '@id:xyz' to '@id: xyz' (missing space after :) |
| 6 | + |
| 7 | +ID_TOKEN="@id:" |
| 8 | + |
| 9 | +if [ "$1" = "--check" ]; then |
| 10 | + CHECK_ONLY=true |
| 11 | + echo "Running in uuid-check only mode..." |
| 12 | +else |
| 13 | + CHECK_ONLY=false |
| 14 | + echo "Running in uuid-fix mode..." |
| 15 | +fi |
| 16 | + |
| 17 | +# finds occurrences of empty id: testimony tags |
| 18 | +EMPTY_IDS=$(grep -E -i -r -n "${ID_TOKEN}(.+[[:blank:]]|$)" tests/foreman/ --include=*.py) |
| 19 | + |
| 20 | +if [ -n "$EMPTY_IDS" ]; then |
| 21 | + if [ $CHECK_ONLY = true ]; then |
| 22 | + echo "Empty $ID_TOKEN found in testimony tags" |
| 23 | + echo $EMPTY_IDS |
| 24 | + exit 1 |
| 25 | + else |
| 26 | + echo "Generating new UUIDS for empty $ID_TOKEN tags..." |
| 27 | + fi |
| 28 | +else |
| 29 | + echo "No empty $ID_TOKEN was found" |
| 30 | +fi |
| 31 | + |
| 32 | +# iterate if any empty @id found |
| 33 | +for output_line in $EMPTY_IDS |
| 34 | +do |
| 35 | + if (echo "$output_line" | grep "tests/foreman"); then |
| 36 | + OLDIFS=$IFS |
| 37 | + # splits the grep output to get filename and occurrence line number |
| 38 | + IFS=':' read -r filename line <<< $output_line |
| 39 | + # generate uuid and place in specific line number |
| 40 | + NEW_ID=$(python -c 'import uuid; print(uuid.uuid4())') |
| 41 | + sed -r -i~ "${line}s/${ID_TOKEN}(.+[[:blank:]]|$)/${ID_TOKEN} ${NEW_ID}/g" $filename |
| 42 | + IFS=$OLDIFS |
| 43 | + fi |
| 44 | +done |
| 45 | + |
| 46 | +# This script finds duplicated @id and replaces with new uuids |
| 47 | + |
| 48 | +# Finds occurrences of @id: in testimony tags then |
| 49 | +# sort the output and filters only the duplicated |
| 50 | +# then looks for existence of "@id:" in final output |
| 51 | +# NOTE: can't print the line number -n here because of uniq -d |
| 52 | +DUP_EXISTS=$(grep -r -i $ID_TOKEN tests/foreman/ --include=*.py | sort | uniq -d | grep $ID_TOKEN) |
| 53 | + |
| 54 | +if [ -n "$DUP_EXISTS" ]; then |
| 55 | + if [ $CHECK_ONLY = true ]; then |
| 56 | + echo "Duplicate $ID_TOKEN found in testimony tags" |
| 57 | + echo $DUP_EXISTS |
| 58 | + exit 1 |
| 59 | + else |
| 60 | + echo "Generating new UUIDS for duplicated $ID_TOKEN tags..." |
| 61 | + fi |
| 62 | +else |
| 63 | + echo "No duplicated $ID_TOKEN was found" |
| 64 | +fi |
| 65 | + |
| 66 | +grep -r -i $ID_TOKEN tests/foreman/ --include=*.py | sort | uniq -d | grep $ID_TOKEN | while read -r line ; do |
| 67 | + OLDIFS=$IFS |
| 68 | + IFS=':' read -r dup_file dup_id <<< $line |
| 69 | + echo "filename: $dup_file" |
| 70 | + echo "Id to replace: $dup_id" |
| 71 | + NEW_ID=$(python -c 'import uuid; print(uuid.uuid4())') |
| 72 | + echo "Replacing with the new id: $NEW_ID" |
| 73 | + LAST_LINE=$(grep -i -n "$dup_id" $dup_file | tail -1) |
| 74 | + |
| 75 | + IFS=':' read -r linenumber linecontent <<< $LAST_LINE |
| 76 | + echo $linenumber |
| 77 | + trimmed_linecontent=$(echo $linecontent) |
| 78 | + sed -i~ "${linenumber}s/${trimmed_linecontent}/${ID_TOKEN} ${NEW_ID}/g" $dup_file |
| 79 | + echo "----------------------------------------------------------------" |
| 80 | + IFS=$OLDIFS |
| 81 | +done |
| 82 | + |
| 83 | +# This script finds id: missing spaces after : |
| 84 | + |
| 85 | + |
| 86 | +MISSING_SPACES=$(grep -E -i -r -n "${ID_TOKEN}[^ ]" tests/foreman/ --include=*.py) |
| 87 | + |
| 88 | +if [ -n "$MISSING_SPACES" ]; then |
| 89 | + if [ $CHECK_ONLY = true ]; then |
| 90 | + echo "Found $ID_TOKEN tags missing space after : ..." |
| 91 | + echo $MISSING_SPACES |
| 92 | + exit 1 |
| 93 | + else |
| 94 | + echo "Fixing $ID_TOKEN tags missing spaces..." |
| 95 | + fi |
| 96 | +else |
| 97 | + echo "No $ID_TOKEN missing spaces was found" |
| 98 | +fi |
| 99 | + |
| 100 | +grep -E -i -r -n "${ID_TOKEN}[^ ]" tests/foreman/ --include=*.py | while read -r line ; do |
| 101 | + OLDIFS=$IFS |
| 102 | + IFS=':' read -r missing_file linenumber linecontent <<< $line |
| 103 | + IFS=':' read -r tag uuid <<< $linecontent |
| 104 | + trimmed_linecontent=$(echo $linecontent) |
| 105 | + sed -i~ "${linenumber}s/${trimmed_linecontent}/${tag}: ${uuid}/g" $missing_file |
| 106 | + IFS=$OLDIFS |
| 107 | +done |
0 commit comments