auto_commit.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 _check(self, path):
  24. """ git status """
  25. os.chdir(path)
  26. print(f"{bcolors.OKGREEN} checking {path}{bcolors.ENDC}")
  27. os.system("git status")
  28. # with os.popen("git status") as p:
  29. # output = p.read()
  30. # resData = re.split("\n", output)
  31. # if len(resData) > 2:
  32. # print(f"{bcolors.FAIL} check error: {resData} {bcolors.ENDC}")
  33. # exit(1)
  34. # print(f"{bcolors.OKGREEN} check finish{bcolors.ENDC}")
  35. # os.chdir(work_dir)
  36. def _sync(self, path):
  37. """ git pull """
  38. os.chdir(path)
  39. print(f"{bcolors.OKGREEN} syncing {path}{bcolors.ENDC}")
  40. os.system("git pull")
  41. os.system("git push")
  42. print(f"{bcolors.OKGREEN} sync finish{bcolors.ENDC}")
  43. # os.chdir(work_dir)
  44. def check(self):
  45. """ check project update"""
  46. if ".git" in os.listdir(self.params['path']):
  47. self._check(self.params['path'])
  48. else:
  49. for root, dirs, files in os.walk(self.params['path']):
  50. if ".git" in dirs:
  51. self._check(root)
  52. os.system("pause")
  53. def commit(self):
  54. """ run """
  55. if ".git" in os.listdir(self.params['path']):
  56. self._commit(self.params['path'])
  57. else:
  58. for root, dirs, files in os.walk(self.params['path']):
  59. if ".git" in dirs:
  60. self._commit(root)
  61. os.system("pause")
  62. def sync(self):
  63. """ sync """
  64. if ".git" in os.listdir(self.params['path']):
  65. self._sync(self.params['path'])
  66. else:
  67. for root, dirs, files in os.walk(self.params['path']):
  68. if ".git" in dirs:
  69. self._sync(root)
  70. os.system("pause")