syncing_needed.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright © 2024 Noah Vogt <noah@noahvogt.com>
  2. # This program is free software: you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation, either version 3 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. import os
  13. import re
  14. from utils import log
  15. import config as const
  16. def syncing_needed(offline_flag_enabled: bool) -> bool:
  17. if offline_flag_enabled:
  18. log("skipping sync with remote", color="cyan")
  19. return False
  20. if not cachefiles_found():
  21. return True
  22. log("checking for remote changes...")
  23. os.system(
  24. 'rclone md5sum "{}" --checkfile "{}" > "{}" 2> "{}"'.format(
  25. const.RCLONE_REMOTE_DIR,
  26. os.path.join(const.SSYNC_CACHE_DIR, const.SSYNC_CHECKFILE_NAMING),
  27. os.devnull,
  28. os.path.join(const.SSYNC_CACHE_DIR, const.SSYNC_CACHEFILE_NAMING),
  29. )
  30. )
  31. with open(
  32. os.path.join(const.SSYNC_CACHE_DIR, const.SSYNC_CACHEFILE_NAMING),
  33. mode="r",
  34. encoding="utf-8-sig",
  35. ) as cachefile_reader:
  36. cachefile_content = cachefile_reader.readlines()
  37. for line in cachefile_content:
  38. if re.search(": ([0-9])+ differences found$", line):
  39. diffs = int(line[line.rfind(":") + 1 : line.find("differences")])
  40. return bool(diffs)
  41. return False
  42. def cachefiles_found() -> bool:
  43. return os.path.isfile(
  44. os.path.join(const.SSYNC_CACHE_DIR, const.SSYNC_CHECKFILE_NAMING)
  45. ) and os.path.isfile(
  46. os.path.join(const.SSYNC_CACHE_DIR, const.SSYNC_CACHEFILE_NAMING)
  47. )