run.sh 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/bin/bash
  2. # Execute `main.py` to get the data, output it to a temp file, then merge the
  3. # results into an existing `usage.tsv`, removing duplicate rows.
  4. #
  5. # If `usage.tsv` doesn't exist, it will be created.
  6. set -e
  7. NEW=new.tsv
  8. USAGE=usage.tsv
  9. TEMP=temp.tsv
  10. printf "\n=== $(date) ===\n"
  11. cd "$(dirname $0)" # Make sure local paths work.
  12. echo "Activating venv"
  13. source ./env/bin/activate
  14. echo "Fetching new records into ${NEW}"
  15. python3 main.py > ${NEW}
  16. echo "Retrieved $(wc -l ${NEW}) records"
  17. if [[ ! -f ${USAGE} ]]; then
  18. echo "'${USAGE}' does not exist. Renaming '$NEW' to '$USAGE'."
  19. mv ${NEW} ${USAGE}
  20. else
  21. echo "'${USAGE}' exists, with $(cat ${USAGE} | wc -l) lines."
  22. echo "Sorting & adding new records from '${USAGE}' and '${NEW}' to '${TEMP}'."
  23. header="$(head -n1 ${USAGE})"
  24. echo "${header}" > ${TEMP}
  25. cat <(tail -n +2 ${USAGE}) <(echo) <(tail -n +2 ${NEW}) \
  26. | sort | uniq \
  27. >> ${TEMP}
  28. echo "$(wc -l ${TEMP})"
  29. echo "Replacing '${USAGE}' with contents of '${TEMP}'"
  30. rm ${USAGE}
  31. mv ${TEMP} ${USAGE}
  32. fi
  33. rm ${NEW} 2> /dev/null