repo_sync.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. @Contact : liuyuqi.gov@msn.cn
  5. @Time : 2023/04/27 03:09:58
  6. @License : Copyright © 2017-2022 liuyuqi. All Rights Reserved.
  7. @Desc : sync utils
  8. '''
  9. import os,csv,re
  10. import logging
  11. from .platform import gen_extractor_classes
  12. from .repo import Repo
  13. class RepoSync(object):
  14. '''
  15. SyncUtils class
  16. '''
  17. repo_list_path = 'repo_list.csv'
  18. def __init__(self, params: dict, debug=False):
  19. self.args = None
  20. self.logger = None
  21. self.init_logger(debug)
  22. self.params = params
  23. self.params['logger'] = self.logger
  24. self.platforms = []
  25. self.repos = []
  26. for p in gen_extractor_classes():
  27. self.platforms.append(p)
  28. if params.get('repo_path', None) is not None:
  29. self.repo_path = params.get('repo_path', None)
  30. self.get_local_repo_list(params.get('repo_path', None))
  31. def get_local_repo_list(self, repo_path):
  32. """get git repo list from a folder"""
  33. if os.path.isdir(repo_path) and os.path.exists(os.path.join(repo_path, '.git')):
  34. self._find_git_repo(
  35. path=repo_path, repo_name=repo_path.split(os.path.sep)[-1])
  36. else:
  37. for dir in os.listdir(repo_path):
  38. current_path = os.path.join(repo_path, dir)
  39. if os.path.isdir(current_path) and os.path.exists(os.path.join(current_path, '.git')):
  40. self._find_git_repo(path=current_path, repo_name=dir)
  41. with open(self.repo_list_path, 'w') as f:
  42. if len(self.repos) == 0:
  43. print('repo list is empty, please delete repo_list.csv and try again')
  44. return
  45. writer = csv.DictWriter(
  46. f, fieldnames=self.repos[0].__dict__.keys(), lineterminator='\n'
  47. )
  48. writer.writeheader()
  49. for repo in self.repos:
  50. writer.writerow(repo.__dict__)
  51. def _find_git_repo(self, path: str, repo_name:str =None):
  52. try:
  53. with open('{}/.git/config'.format(path), 'r') as f:
  54. # get the remote url
  55. repo =Repo()
  56. try:
  57. url = re.findall(r'url\s+=\ (.*)', f.read())[0]
  58. # print(url)
  59. repo.name = url.split('/')[-1].replace('.git', '')
  60. repo.url = url
  61. except Exception as e:
  62. repo.name = repo_name
  63. repo.local_path = path
  64. self.repos.append(repo)
  65. except Exception as e:
  66. print("skip {} because of {}".format(path, e))
  67. def run(self):
  68. '''
  69. run repo
  70. '''
  71. command = self.params.get('command')
  72. platform = self.params.get('platform', 'github')
  73. current_platform = None
  74. for p in self.platforms:
  75. if p.suitable(platform):
  76. current_platform = p
  77. if current_platform is not None:
  78. username = self.params.get(f'{platform}_username', None)
  79. token = self.params.get(f'{platform}_token', None)
  80. host = self.params.get(f'{platform}_host', None)
  81. if command == 'clone':
  82. current_platform(username, token, host, self.params).clone(self.repo_path)
  83. return
  84. if os.path.exists(self.repo_list_path):
  85. with open(self.repo_list_path, 'r', encoding='utf8') as f:
  86. reader = csv.DictReader(f)
  87. for row in reader:
  88. repo = Repo()
  89. repo.__dict__ = row
  90. if command == 'create':
  91. current_platform(username,token, host, self.params).create_repo(repo.name)
  92. if command == 'push':
  93. current_platform(username,token, host, self.params).push(repo.local_path)
  94. elif command == 'delete':
  95. current_platform(username,token, host, self.params).delete(repo.name)
  96. elif command =='pull':
  97. current_platform(username,token, host, self.params).pull(repo.local_path)
  98. else:
  99. logging.info(
  100. 'repo list is not exist, please run list_local command first'
  101. )
  102. def init_logger(self, debug:bool):
  103. '''
  104. init logger
  105. '''
  106. self.logger = logging.getLogger(__name__)
  107. if debug:
  108. self.logger.setLevel(logging.DEBUG)
  109. else:
  110. self.logger.setLevel(logging.INFO)
  111. formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  112. console_handler = logging.StreamHandler()
  113. console_handler.setLevel(logging.DEBUG)
  114. console_handler.setFormatter(formatter)
  115. self.logger.addHandler(console_handler)
  116. def update(self):
  117. '''
  118. update repo_sync software, download and install the latest version
  119. '''
  120. pass