115 lines
3.3 KiB
Python
115 lines
3.3 KiB
Python
#!/usr/bin/env python
|
||
# -*- coding: utf-8 -*-
|
||
"""
|
||
生产环境诊断脚本
|
||
检查Python版本、可用模块和推荐的脚本版本
|
||
"""
|
||
|
||
import sys
|
||
import subprocess
|
||
|
||
def check_python_version():
|
||
"""检查Python版本"""
|
||
version = sys.version_info
|
||
version_str = "{}.{}".format(version.major, version.minor)
|
||
print("Python版本: {}".format(version_str))
|
||
|
||
if version.major >= 3 and version.minor >= 7:
|
||
return "full", version_str
|
||
elif version.major >= 3 and version.minor >= 6:
|
||
return "py36", version_str
|
||
else:
|
||
return "py34", version_str
|
||
|
||
def check_module(module_name):
|
||
"""检查模块是否可用"""
|
||
try:
|
||
__import__(module_name)
|
||
return True, "可用"
|
||
except ImportError:
|
||
return False, "不可用"
|
||
|
||
def check_dependencies():
|
||
"""检查关键依赖"""
|
||
deps = ['pymysql', 'pandas', 'sqlalchemy']
|
||
results = {}
|
||
for dep in deps:
|
||
available, status = check_module(dep)
|
||
results[dep] = (available, status)
|
||
print("依赖 {}: {}".format(dep, status))
|
||
return results
|
||
|
||
def recommend_script(python_level, has_typing):
|
||
"""推荐使用的脚本"""
|
||
if python_level == "full" and has_typing:
|
||
return "02_parse_score_data_fixed.py", "完整功能版本"
|
||
elif python_level in ["full", "py36"] and has_typing:
|
||
return "02_parse_score_data_py36_compatible.py", "Python 3.6兼容版本"
|
||
else:
|
||
return "02_parse_score_data_py34_compatible.py", "超兼容版本(无typing)"
|
||
|
||
def test_script_syntax(script_name):
|
||
"""测试脚本语法"""
|
||
try:
|
||
subprocess.check_call([sys.executable, "-m", "py_compile", script_name],
|
||
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||
return True, "语法正确"
|
||
except subprocess.CalledProcessError:
|
||
return False, "语法错误"
|
||
|
||
def main():
|
||
print("=== 生产环境诊断工具 ===")
|
||
print()
|
||
|
||
# 检查Python版本
|
||
python_level, version_str = check_python_version()
|
||
|
||
# 检查typing模块
|
||
has_typing, typing_status = check_module('typing')
|
||
print("typing模块: {}".format(typing_status))
|
||
|
||
print()
|
||
|
||
# 检查依赖
|
||
print("检查依赖包:")
|
||
deps_status = check_dependencies()
|
||
|
||
print()
|
||
|
||
# 推荐脚本
|
||
script_name, script_desc = recommend_script(python_level, has_typing)
|
||
print("推荐脚本: {} ({})".format(script_name, script_desc))
|
||
|
||
# 测试推荐脚本语法
|
||
syntax_ok, syntax_msg = test_script_syntax(script_name)
|
||
print("脚本语法: {}".format(syntax_msg))
|
||
|
||
print()
|
||
|
||
# 给出建议
|
||
print("=== 执行建议 ===")
|
||
|
||
if syntax_ok:
|
||
print("✅ 环境检查通过")
|
||
print("执行命令:")
|
||
print(" nohup python3 {} > 2.log 2>&1 &".format(script_name))
|
||
print(" tail -f 2.log")
|
||
else:
|
||
print("❌ 发现问题,请检查环境配置")
|
||
|
||
# 检查是否有缺失的依赖
|
||
missing_deps = [dep for dep, (available, _) in deps_status.items() if not available]
|
||
if missing_deps:
|
||
print()
|
||
print("缺失依赖,请安装:")
|
||
print(" pip3 install --user -r requirements-prod.txt")
|
||
|
||
print()
|
||
print("=== 版本信息 ===")
|
||
print("Python: {}".format(version_str))
|
||
print("Typing: {}".format(typing_status))
|
||
print("推荐脚本: {}".format(script_name))
|
||
|
||
if __name__ == "__main__":
|
||
main()
|