gitee.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. """
  4. @Contact : liuyuqi.gov@msn.cn
  5. @Time : 2023/11/09 17:40:42
  6. @License : Copyright © 2017-2022 liuyuqi. All Rights Reserved.
  7. @Desc : gitee async
  8. """
  9. from .base_platform import BasePlatform
  10. import csv, subprocess
  11. import os
  12. from repo_sync.repo import Repo
  13. class GiteeIE(BasePlatform):
  14. """gitee async"""
  15. _host = 'https://gitee.com'
  16. _api = _host + '/api/v5'
  17. def __init__(self, username:str, token:str,host:str =None, params: dict = None) -> None:
  18. super().__init__(username=username, token=token)
  19. self.sess.headers.update({'Content-Type': 'multipart/form-data'})
  20. self.repo_private = True if params.get('gitee_private', "true").lower() == 'true' else False
  21. def create_repo(self, repo_name: str):
  22. """create a repo"""
  23. url = f'{self._api}/user/repos'
  24. form_data = {
  25. 'name': repo_name,
  26. 'private': self.repo_private,
  27. }
  28. r = self.sess.post(url, params=form_data)
  29. if r.status_code != 201:
  30. print(
  31. 'create repo {} failed, status code {}'.format(repo_name, r.status_code)
  32. )
  33. return
  34. print('create repo {} success'.format(repo_name))
  35. def delete(self, repo_name: str):
  36. """delete a repo"""
  37. # print("delete repo:"+repo_name)
  38. url = f'{self._api}/repos/{self.username}/{repo_name}'
  39. response = self.sess.delete(url)
  40. if response.status_code == 204:
  41. print(f'Repository: {repo_name} deleted from gitee successfully!')
  42. else:
  43. print(
  44. f'Failed to delete repository: {repo_name} from github. Error {response.status_code}: {response.text}'
  45. )
  46. def get_repo_list(self) -> list:
  47. """get repo list"""
  48. if os.path.exists(self.repo_list_path):
  49. with open(self.repo_list_path, 'r', encoding='utf8') as f:
  50. reader = csv.reader(f)
  51. for row in reader:
  52. repo = Repo()
  53. repo.__dict__ = row
  54. self.repos.append(repo)
  55. return self.repos
  56. url = f'{self._api}/user/repos'
  57. r = self.sess.get(url)
  58. if r.status_code != 200:
  59. print('get repo list failed, status code {}'.format(r.status_code))
  60. return
  61. repo_list = r.json()
  62. self.save_csv()
  63. return repo_list
  64. def clone(self):
  65. pass
  66. def pull(self, local_repo_path: str):
  67. if local_repo_path[-1] == os.path.sep:
  68. local_repo_path = local_repo_path[:-1]
  69. repo_name = local_repo_path.split(os.path.sep)[-1]
  70. print(f'pull repo:{self.username}/{repo_name} from gitee')
  71. os.chdir(local_repo_path)
  72. os.system('git remote remove origin_gitee')
  73. os.system(
  74. f'git remote add origin_gitee https://{self.username}:{self.token}@gitee.com/{self.username}/{repo_name}.git'
  75. )
  76. result = subprocess.run(['git', 'symbolic-ref', '--short', 'HEAD'], capture_output=True, text=True)
  77. current_branch = result.stdout.strip()
  78. os.system(f'git pull origin_gitee {current_branch}')
  79. os.system('git remote remove origin_gitee')
  80. os.chdir('..')
  81. print('pull from gitee success')
  82. def push(self, local_repo_path: str):
  83. if local_repo_path[-1] == os.path.sep:
  84. local_repo_path = local_repo_path[:-1]
  85. repo_name = local_repo_path.split(os.path.sep)[-1]
  86. print(f'push repo:{self.username}/{repo_name} to gitee')
  87. os.chdir(local_repo_path)
  88. os.system('git remote remove origin_gitee')
  89. os.system(
  90. f'git remote add origin_gitee https://{self.username}:{self.token}@gitee.com/{self.username}/{repo_name}.git'
  91. )
  92. result = subprocess.run(['git', 'symbolic-ref', '--short', 'HEAD'], capture_output=True, text=True)
  93. current_branch = result.stdout.strip()
  94. os.system(f'git pull origin_gitee {current_branch}')
  95. os.system(f'git push -u origin_gitee {current_branch}')
  96. os.system('git remote remove origin_gitee')
  97. os.chdir('..')
  98. print('push to gitee success')
  99. @classmethod
  100. def suitable(cls, extractor: str) -> bool:
  101. """check if this extractor is suitable for this platform"""
  102. if extractor == 'gitee':
  103. return True
  104. else:
  105. return False