check_env.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env python3
  2. """MathLab 环境检查脚本"""
  3. import os
  4. import sys
  5. import subprocess
  6. from pathlib import Path
  7. def check_command(cmd: str) -> bool:
  8. """检查命令是否可用"""
  9. try:
  10. result = subprocess.run(["which", cmd], capture_output=True, text=True)
  11. return result.returncode == 0
  12. except:
  13. return False
  14. def check_directory(path: str, required: bool = False) -> bool:
  15. """检查目录是否存在"""
  16. exists = os.path.exists(path)
  17. status = "✅" if exists else ("❌" if required else "⚠️")
  18. print(f"{status} {path}")
  19. return exists
  20. def main():
  21. print("=" * 50)
  22. print("🔧 MathLab 环境检查")
  23. print("=" * 50)
  24. # 必需命令
  25. print("\n📦 必需命令:")
  26. required_commands = ["python3", "git", "pytest"]
  27. for cmd in required_commands:
  28. available = check_command(cmd)
  29. if not available:
  30. print(f" ❌ {cmd} 未安装,请运行:sudo apt install {cmd}")
  31. # PDF 转换工具
  32. print("\n📄 PDF 转换工具:")
  33. pdf_tools = ["pdftotext", "marker_single", "pdf2md"]
  34. found = False
  35. for tool in pdf_tools:
  36. if check_command(tool):
  37. found = True
  38. print(f" ✅ {tool} (可用)")
  39. break
  40. if not found:
  41. print(" ⚠️ 未找到 PDF 转换工具,建议安装 pdftotext")
  42. # Python 依赖
  43. print("\n🐍 Python 依赖:")
  44. python_deps = ["numpy", "matplotlib", "jinja2", "pyyaml"]
  45. try:
  46. import importlib
  47. for dep in python_deps:
  48. try:
  49. importlib.import_module(dep)
  50. print(f" ✅ {dep}")
  51. except ImportError:
  52. print(f" ❌ {dep} - 请运行:pip3 install {dep}")
  53. except Exception as e:
  54. print(f" ❌ 检查失败:{e}")
  55. # 目录结构
  56. print("\n📁 目录结构:")
  57. base_dir = Path(__file__).parent.parent.parent
  58. directories = [
  59. (base_dir / "textbook", False),
  60. (base_dir / "staging", False),
  61. (base_dir / "courseware", True),
  62. (base_dir / "exercises", True),
  63. (base_dir / "tests", True),
  64. ]
  65. for dir_path, required in directories:
  66. check_directory(str(dir_path), required)
  67. # Git 配置
  68. print("\n🔄 Git 配置:")
  69. try:
  70. result = subprocess.run(
  71. ["git", "remote", "get-url", "origin"],
  72. capture_output=True, text=True, cwd=base_dir
  73. )
  74. if result.returncode == 0:
  75. remote_url = result.stdout.strip()
  76. if "example.com" in remote_url:
  77. print(f" ⚠️ Git remote 是示例地址:{remote_url}")
  78. print(f" 💡 请运行:git remote set-url origin \<你的 Gogs 地址>")
  79. else:
  80. print(f" ✅ Git remote: {remote_url}")
  81. else:
  82. print(f" ❌ Git remote 未配置")
  83. except Exception as e:
  84. print(f" ❌ Git 检查失败:{e}")
  85. print("\n" + "=" * 50)
  86. print("✅ 环境检查完成")
  87. print("=" * 50)
  88. if __name__ == "__main__":
  89. main()