stop_cd_recording.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env python3
  2. """
  3. Copyright © 2024 Noah Vogt <noah@noahvogt.com>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. """
  15. from os import kill
  16. from signal import SIGTERM
  17. from utils import (
  18. get_yyyy_mm_dd_date,
  19. make_sure_file_exists,
  20. is_valid_cd_record_checkfile,
  21. log,
  22. error_msg,
  23. expand_dir,
  24. )
  25. from input import get_cachefile_content, validate_cd_record_config
  26. import config as const
  27. def mark_end_of_recording(cachefile_content: list) -> None:
  28. cachefile = expand_dir(const.CD_RECORD_CACHEFILE)
  29. log("marking end of recording...")
  30. try:
  31. with open(cachefile, mode="w+", encoding="utf-8-sig") as file_writer:
  32. file_writer.write(cachefile_content[0].strip() + "\n")
  33. file_writer.write("9001\n")
  34. file_writer.write(cachefile_content[2].strip() + "\n")
  35. file_writer.write(cachefile_content[3].strip() + "\n")
  36. file_writer.write(cachefile_content[4].strip() + "\n")
  37. except (FileNotFoundError, PermissionError, IOError) as error:
  38. error_msg(
  39. "Failed to write to cachefile '{}'. Reason: {}".format(
  40. cachefile, error
  41. )
  42. )
  43. def stop_cd_recording() -> None:
  44. cachefile_content = get_cachefile_content(const.CD_RECORD_CACHEFILE)
  45. yyyy_mm_dd = get_yyyy_mm_dd_date()
  46. if is_valid_cd_record_checkfile(cachefile_content, yyyy_mm_dd):
  47. try:
  48. kill(int(cachefile_content[2]), SIGTERM)
  49. except ProcessLookupError:
  50. error_msg("Recording not running, cannot be stopped.")
  51. mark_end_of_recording(cachefile_content)
  52. else:
  53. error_msg("CD Record Checkfile is invalid.")
  54. def main() -> None:
  55. validate_cd_record_config()
  56. make_sure_file_exists(const.CD_RECORD_CACHEFILE)
  57. stop_cd_recording()
  58. if __name__ == "__main__":
  59. main()