git_helper.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import sys
  2. import os
  3. import re
  4. abspath = os.path.abspath(__file__)
  5. root_path = re.sub(r"tendata-warehouse.*", "tendata-warehouse", abspath)
  6. sys.path.append(root_path)
  7. import subprocess
  8. from datetime import datetime
  9. from dw_base.scheduler.mg2es.path_util import PathUtil
  10. class GitHelper:
  11. # def git_pull(self, working_dir):
  12. # subprocess.run(["git", "pull"], cwd=working_dir, check=True)
  13. def git_pull(self, working_dir):
  14. """
  15. 从远程仓库拉取最新的更改。
  16. 参数:
  17. working_dir (str): Git 仓库的目录。
  18. 异常:
  19. subprocess.CalledProcessError: 如果 git pull 命令失败。
  20. FileNotFoundError: 如果工作目录不存在。
  21. """
  22. # 检查工作目录是否存在
  23. if not os.path.exists(working_dir):
  24. raise FileNotFoundError(f"指定的目录不存在: {working_dir}")
  25. try:
  26. subprocess.run(["git", "pull"], cwd=working_dir, check=True)
  27. print("成功拉取最新的更改。")
  28. except subprocess.CalledProcessError as e:
  29. print(f"git pull 过程中出错: {e}")
  30. def git_pull_etlconfig(self):
  31. root_path = PathUtil.get_project_root_path()
  32. # 调用函数并指定文件路径和工作目录
  33. working_dir = root_path + '/../mongo2es-customs'
  34. print(f'【git pull】 working_dir: {working_dir}')
  35. self.git_pull(working_dir)
  36. def get_last_commit_date(self, file_path, working_dir):
  37. try:
  38. # 构建Git命令
  39. git_command = ["git", "log", "-1", "--format=%cd", "--", file_path]
  40. # 执行命令并获取输出
  41. output = subprocess.check_output(git_command, cwd=working_dir, stderr=subprocess.STDOUT,
  42. universal_newlines=True)
  43. date_object = datetime.strptime(output.strip(), '%a %b %d %H:%M:%S %Y %z')
  44. # 格式化日期时间字符串
  45. formatted_date = date_object.strftime('%Y-%m-%d %H:%M:%S')
  46. print("最近一次提交的日期为: "+formatted_date)
  47. # 返回输出(即最近一次提交的日期)
  48. return formatted_date
  49. except subprocess.CalledProcessError as e:
  50. # 如果命令执行失败,输出错误信息
  51. print("Error:", e.output)
  52. return None
  53. def get_etlconfig_last_uptime(self, catalog, database_name):
  54. root_path = PathUtil.get_project_root_path()
  55. # 调用函数并指定文件路径和工作目录
  56. es_json_path,mg2es_mapping_path = PathUtil.get_conf_path(catalog,database_name)
  57. working_dir = root_path + '/../mongo2es-customs'
  58. es_json_date = self.get_last_commit_date(es_json_path, working_dir)
  59. mg2es_mapping_date = self.get_last_commit_date(mg2es_mapping_path, working_dir)
  60. if es_json_date and mg2es_mapping_date:
  61. last_commit_date = max(es_json_date, mg2es_mapping_date)
  62. print("最近一次提交的日期:", last_commit_date)
  63. return last_commit_date
  64. else:
  65. print("获取最近一次提交的日期时出错。")
  66. return None
  67. if __name__ == '__main__':
  68. git_helper = GitHelper()
  69. # last_commit_date = git_helper.get_etlconfig_last_uptime('exports', 'kazakhstan')
  70. git_helper.git_pull_etlconfig()