#!/bin/bash
set -u
# Script to analyze case variants for instance.
# Usage:
# cd /p4/common/site/cic_blue
# nohup ./acv.sh < /dev/null > acv.extra.log 2>&1 &
function msg () { echo -e "$*"; }
function errmsg () { msg "\\nError: ${1:-Unknown Error}\\n"; ErrorCount+=1; }
function bail () { errmsg "${1:-Unknown Error}"; exit "$ErrorCount"; }
declare ThisScript=${0##*}
declare Version=1.2.1
declare RunUser=
declare RunHost=${HOSTNAME%%.*}
declare -i ErrorCount=0
declare -i TotalSeconds=0
declare AppHome="/p4/common/site/cic_blue"
declare RunFile="${AppHome}/.acv.run"
declare -i RunNum=1
declare RunDir=
declare Log=
declare H1="=============================================================================="
declare H2="------------------------------------------------------------------------------"
declare SDPInstance=1
# shellcheck disable=SC1091
source /p4/common/bin/p4_vars "$SDPInstance" ||\
bail "Could not do: source /p4/common/bin/p4_vars \"$SDPInstance\""
# Prepend AppHome to PATH.
export PATH="$AppHome:$PATH"
cd "$AppHome" || bail "Could not do: cd \"$AppHome\""
if [[ -r "$RunFile" ]]; then
RunNum=$(cat $RunFile)
RunNum+=1
echo "$RunNum" > "$RunFile" ||\
bail "Could not write run file [$RunFile]."
else
echo 1 > "$RunFile" ||\
bail "Could not write run file [$RunFile]."
fi
RunDir="${AppHome}/run${RunNum}"
if [[ ! -d "$RunDir" ]]; then
mkdir "$RunDir" || bail "Could not do: mkdir \"$RunDir\""
else
bail "RunDir [$RunDir] already exists, unexpectedly. Aborting."
fi
cd "$RunDir" || bail "Could not do: cd \"$RunDir\""
Log="$RunDir/log"
exec > >(tee "${Log}")
exec 2>&1
msg "${H1}\\nLog is: $Log"
RunUser=$(id -n -u)
msg "Started $ThisScript v$Version as $RunUser@$RunHost on $(date)."
msg "Operating in $PWD, starting run $RunNum."
msg "Getting depot paths:\\np4 -F %depotFile% files //... > all_file_names.txt 2> all_file_names.err"
p4 -F %depotFile% files //... > all_file_names.txt 2> all_file_names.err
msg "Time: This took $((SECONDS/3600)) hours $((SECONDS%3600/60)) minutes $((SECONDS%60)) seconds.\\n${H2}"
TotalSeconds+=$SECONDS; SECONDS=0
msg "Getting file details:\\np4 files //... > all_file_details.txt 2> all_file_details.err"
p4 files //... > all_file_details.txt 2> all_file_details.err
msg "Time: This took $((SECONDS/3600)) hours $((SECONDS%3600/60)) minutes $((SECONDS%60)) seconds.\\n${H2}"
TotalSeconds+=$SECONDS; SECONDS=0
msg "Sorting:\\nsort -f all_file_names.txt > sorted.all_file_names 2> sorted.all_file_names.err"
sort -f all_file_names.txt > sorted.all_file_names 2> sorted.all_file_names.err
msg "Time: This took $((SECONDS/3600)) hours $((SECONDS%3600/60)) minutes $((SECONDS%60)) seconds.\\n${H2}"
TotalSeconds+=$SECONDS; SECONDS=0
msg "Deduplicating:\\nuniq -Di < sorted.all_file_names > filedups.txt 2> filedups.err"
uniq -Di < sorted.all_file_names > filedups.txt 2> filedups.err
msg "Time: This took $((SECONDS/3600)) hours $((SECONDS%3600/60)) minutes $((SECONDS%60)) seconds.\\n${H2}"
TotalSeconds+=$SECONDS; SECONDS=0
msg "Counting dups:\\nwc -l filedups.txt"
wc -l filedups.txt
msg "Time: This took $((SECONDS/3600)) hours $((SECONDS%3600/60)) minutes $((SECONDS%60)) seconds.\\n${H2}"
TotalSeconds+=$SECONDS; SECONDS=0
msg "Finding case-only variants of each file:\\ with FindCashClash.py filedups.txt > dups.txt 2>dups.err"
python3 ../FindCaseClash.py filedups.txt > dups.txt 2>dups.err
msg "Time: This took $((SECONDS/3600)) hours $((SECONDS%3600/60)) minutes $((SECONDS%60)) seconds.\\n${H2}"
TotalSeconds+=$SECONDS; SECONDS=0
msg "Determining Actions:\\nFindCaseClashAction.py dups.txt > clashes.txt"
python3 ../FindCaseClashAction.py filedups.txt > clashes.txt 2> clashes.err
msg "Time: This took $((SECONDS/3600)) hours $((SECONDS%3600/60)) minutes $((SECONDS%60)) seconds.\\n${H2}"
TotalSeconds+=$SECONDS; SECONDS=0
msg "Counting obliterate actions:\\ngrep -a -c 'Obliterate: ' clashes.txt | sort | uniq -c"
grep -a -c 'Obliterate: ' clashes.txt | sort | uniq -c
msg "Counting clashes:\\ngrep -a -c 'Obliterate: unknown' clashes.txt"
grep -a -c 'Obliterate: unknown' clashes.txt
msg "Counting errors:\\ngrep -a -c '^Error ' clashes.txt"
grep -a -c '^Error ' clashes.txt
msg "Time: This took $((SECONDS/3600)) hours $((SECONDS%3600/60)) minutes $((SECONDS%60)) seconds.\\n${H2}"
TotalSeconds+=$SECONDS; SECONDS=0
msg "Counting clashes with context:\\ngrep -a -B2 'Obliterate: unknown' clashes.txt"
grep -a -B2 'Obliterate: unknown' clashes.txt
msg "Time: This took $((SECONDS/3600)) hours $((SECONDS%3600/60)) minutes $((SECONDS%60)) seconds.\\n${H2}"
TotalSeconds+=$SECONDS; SECONDS=0
msg "Ensure all *.err files are zero-length."
wc -l ./*.err
msg "\\nTime: Total processing took $((TotalSeconds/3600)) hours $((TotalSeconds%3600/60)) minutes $((TotalSeconds%60)) seconds."
msg "\\nLog is: $Log\\n${H1}"