client.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. """
  4. @Contact : liuyuqi.gov@msn.cn
  5. @Time : 2024/04/09 12:56:07
  6. @License : Copyright © 2017-2022 liuyuqi. All Rights Reserved.
  7. @Desc : client enter point
  8. """
  9. import os,sys,re
  10. import argparse
  11. from fgh.utils.frozen_dir import get_app_path
  12. import requests, subprocess, platform
  13. import dotenv
  14. class Client:
  15. help_str ="""
  16. fgh(fast github) quick clone github repo tools
  17. contact: liuyuqi.gov@msn.cn
  18. Usage: fgh <command> [<args>]
  19. # clone repo
  20. fgh clone https://github.com/xx/yy.git
  21. fgh push https://github.com/xx/yy.git
  22. # download file
  23. fgh wget https://ghproxy.org/https://github.com/microsoft/vscode/archive/refs/tags/1.84.2.zip
  24. fgh wget https://ghproxy.org/https://raw.githubusercontent.com/microsoft/vscode/main/README.md
  25. fgh curl -O https://ghproxy.org/https://github.com/microsoft/vscode/archive/refs/tags/1.84.2.zip
  26. fgh curl -O https://ghproxy.org/https://raw.githubusercontent.com/microsoft/vscode/main/README.md
  27. """
  28. def __init__(self):
  29. self.fgit_host = ''
  30. self.token = ''
  31. self.sess = requests.Session()
  32. self.env_file_path = os.path.join(get_app_path(), '.env')
  33. self.update_host()
  34. self.read_config()
  35. self.args = self.parse_args()
  36. def read_config(self):
  37. """ read config """
  38. current_dir = get_app_path()
  39. dotenv.load_dotenv(self.env_file_path)
  40. fgit_host = os.getenv('FGH_HOST')
  41. try:
  42. if fgit_host[-1] == '/':
  43. self.fgit_host = fgit_host[:-1]
  44. self.token = os.getenv('FGh_TOKEN')
  45. except Exception as e:
  46. print(".env配置错误"+e)
  47. @staticmethod
  48. def ping(host: str) -> None|int:
  49. """ ping host """
  50. try:
  51. if platform.system().lower() == 'windows':
  52. command = ['ping', '-n', '1', '-w', '1000', host]
  53. else:
  54. command = ['ping', '-c', '1', '-W', '1', host]
  55. output = subprocess.run(command, capture_output=True, text=True)
  56. # 从输出中提取延迟信息
  57. if output.returncode == 0:
  58. # 使用正则表达式从输出中匹配延迟时间
  59. pattern = r"time=(\d+)ms"
  60. match = re.search(pattern, output.stdout)
  61. if match:
  62. return int(match.group(1)) # 返回延迟时间(毫秒)
  63. return None # 如果 ping 失败或延迟时间无法提取,则返回 None
  64. except Exception as e:
  65. print(e)
  66. return None
  67. @staticmethod
  68. def parse_args():
  69. """ parse args """
  70. parser = argparse.ArgumentParser(description='fgit client')
  71. parser.add_argument('command', type=str, help='fgit command',
  72. choices=['git','wget','curl','clone', 'push', 'pull', 'commit', 'add', 'status', 'log', 'diff', 'branch', 'checkout', 'merge', 'rebase', 'reset', 'tag', 'fetch', 'remote', 'init', 'config', 'help'])
  73. # all args behind command
  74. parser.add_argument('args', nargs=argparse.REMAINDER, help='git command args')
  75. return parser.parse_args()
  76. def update_host(self):
  77. """update host"""
  78. res = []
  79. try:
  80. res = self.sess.get('https://git.yoqi.me/lyq/fgh/raw/master/docs/fgh_host.json')
  81. fgh_hosts= res.json()["hosts"]
  82. fgh_host= self._get_best_host()
  83. dotenv.set_key(self.env_file_path, 'FGH_HOST', fgh_host)
  84. except Exception as e:
  85. print(e)
  86. def _get_best_host(self, hosts: list) -> str:
  87. """ 在所有 hosts 中,ping 延迟最短的返回 """
  88. print("即将检测网络延迟...")
  89. res =[]
  90. for host in hosts:
  91. res.append({'host': host, 'delay': self.ping(host) if self.ping(host) else 999999})
  92. print("正在ping "+ host + " 延迟为:" + str(res[-1]['delay']) + "ms")
  93. min_delay = 999999
  94. min_host = ''
  95. for item in res:
  96. if item['delay'] and item['delay'] < min_delay:
  97. min_delay = item['delay']
  98. min_host = item['host']
  99. print("选择最佳host: " + min_host + " 延迟为:" + str(min_delay) + "ms")
  100. return min_host
  101. def run(self):
  102. """ run """
  103. args = self.parse_args()
  104. command = args.command
  105. if len(args.args) == 0 and command != 'help':
  106. print('Usage: fgh <command> [<args>]')
  107. sys.exit(1)
  108. if command == "wget":
  109. if len(args.args) > 0:
  110. args.args = [re.sub(r'https://github.com', self.fgit_host+'/https://github.com', arg) for arg in args.args]
  111. # print('wget ' + ' '.join(args.args))
  112. os.system('wget ' + ' '.join(args.args))
  113. elif command =="curl":
  114. if len(args.args) > 0:
  115. args.args = [re.sub(r'https://github.com', self.fgit_host+'/https://github.com', arg) for arg in args.args]
  116. print('curl ' + ' '.join(args.args))
  117. os.system('curl ' + ' '.join(args.args))
  118. elif command == "help":
  119. print(Client.help_str)
  120. else:
  121. if len(args.args) > 0:
  122. args.args = [re.sub(r'https://github.com', self.fgit_host+'/https://github.com', arg) for arg in args.args]
  123. cmd_str = 'git ' + command + ' ' + ' '.join(args.args)
  124. try:
  125. if command == 'git':
  126. cmd_str = command + ' ' + ' '.join(args.args)
  127. os.system(cmd_str)
  128. # print(cmd_str)
  129. except Exception as e:
  130. print(cmd_str + e)
  131. if __name__=='__main__':
  132. client = Client()
  133. client.run()