run.sh 919 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. echo "Activating venv"
  11. source ./env/bin/activate
  12. echo "Fetching new records into ${NEW}"
  13. python3 main.py > ${NEW}
  14. echo "Retrieved $(wc -l ${NEW}) records"
  15. if [[ ! -f ${USAGE} ]]; then
  16. echo "${USAGE} does not exist. Renaming $NEW to $USAGE."
  17. mv ${NEW} ${USAGE}
  18. else
  19. echo "${USAGE} exists, with $(cat ${USAGE} | wc -l) lines."
  20. echo "Sorting & adding new records from ${NEW}."
  21. header="$(head -n1 ${USAGE})"
  22. echo "${header}" > ${TEMP}
  23. cat <(tail -n +2 ${USAGE}) <(echo) <(tail -n +2 ${NEW}) \
  24. | sort | uniq \
  25. >> ${TEMP}
  26. echo "$(wc -l ${TEMP})"
  27. echo "Replacing ${USAGE} with contents of ${TEMP}"
  28. rm ${USAGE}
  29. mv ${TEMP} ${USAGE}
  30. fi
  31. rm ${NEW}