auto_commit.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. """
  4. @Contact : liuyuqi.gov@msn.cn
  5. @Time : 2024/07/22
  6. @License : Copyright © 2017-2022 liuyuqi. All Rights Reserved.
  7. @Desc : auto commit
  8. """
  9. import os
  10. from auto_commit.utils.colors import bcolors
  11. class AutoCommit(object):
  12. depth =2
  13. def __init__(self, params: dict, debug=False):
  14. self.params = params
  15. def _commit(self, path):
  16. """ git commit """
  17. os.chdir(path)
  18. print(f"{bcolors.OKGREEN} commiting {path}{bcolors.ENDC}")
  19. os.system("git add .")
  20. os.system("git commit -m \"Automatic Commit By liuyuqi\"")
  21. print(f"{bcolors.OKGREEN} commit finish{bcolors.ENDC}")
  22. # os.chdir(work_dir)
  23. def _sync(self, path):
  24. """ git pull """
  25. os.chdir(path)
  26. print(f"{bcolors.OKGREEN} syncing {path}{bcolors.ENDC}")
  27. os.system("git pull")
  28. os.system("git push")
  29. print(f"{bcolors.OKGREEN} sync finish{bcolors.ENDC}")
  30. # os.chdir(work_dir)
  31. def commit(self):
  32. """ run """
  33. if ".git" in os.listdir(self.params['path']):
  34. self._commit(self.params['path'])
  35. else:
  36. for root, dirs, files in os.walk(self.params['path']):
  37. if ".git" in dirs:
  38. self._commit(root)
  39. os.system("pause")
  40. def sync(self):
  41. """ sync """
  42. if ".git" in os.listdir(self.params['path']):
  43. self._sync(self.params['path'])
  44. else:
  45. for root, dirs, files in os.walk(self.params['path']):
  46. if ".git" in dirs:
  47. self._sync(root)
  48. os.system("pause")