ssync.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/env python3
  2. import os
  3. import sys
  4. import shutil
  5. from configparser import ConfigParser
  6. from termcolor import colored
  7. import colorama
  8. def error_msg(msg: str):
  9. print(colored("[*] Error: {}".format(msg), "red"))
  10. sys.exit(1)
  11. def log(message: str):
  12. print(colored("[*] {}".format(message), "green"))
  13. class Ssync:
  14. def __init__(self):
  15. self.parse_config()
  16. def parse_config(self):
  17. config_parser = ConfigParser()
  18. config_parser.read("config.ini")
  19. try:
  20. self.rclone_remote_dir = config_parser["RCLONE"]["remote_dir"]
  21. self.rclone_local_dir = config_parser["RCLONE"]["local_dir"]
  22. self.obs_slides_dir = config_parser["OBS"]["slides_dir"]
  23. self.obs_target_subdir = config_parser["OBS"]["target_subdir"]
  24. self.obs_min_subdirs = int(config_parser["OBS"]["min_subdirs"])
  25. except KeyError:
  26. error_msg("configuration file 'config.ini' could not be parsed")
  27. log("configuration initialised")
  28. def sync_slide_repo(self):
  29. log("syncing with remote slide repository...")
  30. os.system(
  31. "rclone sync -v {} {}".format(
  32. self.rclone_remote_dir, self.rclone_local_dir
  33. )
  34. )
  35. def clear_obs_slides_dir(self):
  36. log("clearing obs slides directory...")
  37. for filename in os.listdir(self.obs_slides_dir):
  38. file_path = os.path.join(self.obs_slides_dir, filename)
  39. try:
  40. if os.path.isfile(file_path) or os.path.islink(file_path):
  41. os.unlink(file_path)
  42. elif os.path.isdir(file_path):
  43. shutil.rmtree(file_path)
  44. except Exception as e:
  45. error_msg("Failed to delete %s. Reason: %s" % (file_path, e))
  46. def create_minimum_subdirs(self, count: int):
  47. if count >= self.obs_min_subdirs:
  48. return
  49. for number in range(count, self.obs_min_subdirs + 1):
  50. dirname = os.path.join(
  51. self.obs_slides_dir, self.obs_target_subdir + " " + str(number)
  52. )
  53. os.mkdir(dirname)
  54. def slide_selection_iterator(self):
  55. iterator_prompt = "Exit now? (default=no) [y/N]: "
  56. dir_list_str = ""
  57. for directory in os.listdir(self.rclone_local_dir):
  58. dir_list_str += directory + "\n"
  59. dir_list_str = dir_list_str[:-1]
  60. tempfile_str = ".chosen-tempfile"
  61. index = 0
  62. while True:
  63. index += 1
  64. prompt_answer = str(
  65. input(
  66. "[{} {}] ".format(self.obs_target_subdir, index)
  67. + iterator_prompt
  68. )
  69. )
  70. if prompt_answer.lower() == "y":
  71. self.create_minimum_subdirs(index)
  72. break
  73. dir_list_str = dir_list_str.replace("\n", "\\n")
  74. os.system(
  75. 'printf "{}" | fzf > {}'.format(dir_list_str, tempfile_str)
  76. )
  77. with open(tempfile_str, mode="r") as tempfile_file_opener:
  78. chosen_slides = tempfile_file_opener.read()[:-1].strip()
  79. if len(chosen_slides) == 0:
  80. log("no slides chosen, skipping...")
  81. else:
  82. log(
  83. "copying slides '{}' to '{} {}'...".format(
  84. chosen_slides, self.obs_target_subdir, index
  85. )
  86. )
  87. src_dir = os.path.join(self.rclone_local_dir, chosen_slides)
  88. dest_dir = os.path.join(
  89. self.obs_slides_dir,
  90. self.obs_target_subdir + " " + str(index),
  91. )
  92. shutil.copytree(src_dir, dest_dir)
  93. if os.path.isfile(tempfile_str):
  94. os.remove(tempfile_str)
  95. def execute(self):
  96. self.sync_slide_repo()
  97. self.clear_obs_slides_dir()
  98. self.slide_selection_iterator()
  99. def main():
  100. colorama.init()
  101. ssync = Ssync()
  102. ssync.execute()
  103. if __name__ == "__main__":
  104. main()