185 lines
6.3 KiB
Python
185 lines
6.3 KiB
Python
"""
|
||
开源项目质量分析系统 - 主应用
|
||
基于九格大模型的智能分析系统
|
||
"""
|
||
import os
|
||
import json
|
||
from flask import Flask, render_template, request, jsonify, send_file
|
||
from werkzeug.utils import secure_filename
|
||
from config import config
|
||
from analyzer.code_analyzer import CodeAnalyzer
|
||
from analyzer.doc_analyzer import DocumentationAnalyzer
|
||
from analyzer.activity_analyzer import ActivityAnalyzer
|
||
from analyzer.ai_analyzer import AIAnalyzer
|
||
from analyzer.report_generator import ReportGenerator
|
||
|
||
# 创建Flask应用
|
||
app = Flask(__name__)
|
||
app.config.from_object(config)
|
||
|
||
# 确保上传文件夹存在
|
||
os.makedirs(config.UPLOAD_FOLDER, exist_ok=True)
|
||
os.makedirs('reports', exist_ok=True)
|
||
|
||
def allowed_file(filename):
|
||
"""检查文件扩展名是否允许"""
|
||
return '.' in filename and \
|
||
filename.rsplit('.', 1)[1].lower() in config.ALLOWED_EXTENSIONS
|
||
|
||
@app.route('/')
|
||
def index():
|
||
"""主页"""
|
||
return render_template('index.html')
|
||
|
||
@app.route('/favicon.ico')
|
||
def favicon():
|
||
"""返回favicon"""
|
||
return '', 204
|
||
|
||
@app.route('/analyze', methods=['POST'])
|
||
def analyze_project():
|
||
"""
|
||
分析项目质量
|
||
支持GitHub URL或上传项目文件
|
||
"""
|
||
try:
|
||
project_path = None
|
||
project_name = None
|
||
analysis_type = request.form.get('analysis_type')
|
||
|
||
if analysis_type == 'github':
|
||
# GitHub仓库分析
|
||
repo_url = request.form.get('repo_url')
|
||
if not repo_url:
|
||
return jsonify({'error': '请提供GitHub仓库URL'}), 400
|
||
|
||
# 克隆或分析GitHub仓库
|
||
project_name = repo_url.split('/')[-1].replace('.git', '')
|
||
project_path = os.path.join(config.UPLOAD_FOLDER, project_name)
|
||
|
||
# 这里应该使用git clone,简化处理
|
||
if not os.path.exists(project_path):
|
||
import git
|
||
git.Repo.clone_from(repo_url, project_path)
|
||
|
||
elif analysis_type == 'upload':
|
||
# 上传文件分析
|
||
if 'file' not in request.files:
|
||
return jsonify({'error': '未找到上传文件'}), 400
|
||
|
||
file = request.files['file']
|
||
if file.filename == '':
|
||
return jsonify({'error': '未选择文件'}), 400
|
||
|
||
if file and allowed_file(file.filename):
|
||
filename = secure_filename(file.filename)
|
||
filepath = os.path.join(config.UPLOAD_FOLDER, filename)
|
||
file.save(filepath)
|
||
|
||
# 解压文件
|
||
import zipfile
|
||
import tarfile
|
||
|
||
project_name = filename.rsplit('.', 1)[0]
|
||
project_path = os.path.join(config.UPLOAD_FOLDER, project_name)
|
||
|
||
if filename.endswith('.zip'):
|
||
with zipfile.ZipFile(filepath, 'r') as zip_ref:
|
||
zip_ref.extractall(project_path)
|
||
elif filename.endswith(('.tar', '.tar.gz', '.tgz')):
|
||
with tarfile.open(filepath, 'r:*') as tar_ref:
|
||
tar_ref.extractall(project_path)
|
||
else:
|
||
return jsonify({'error': '未知的分析类型'}), 400
|
||
|
||
# 执行各项分析
|
||
print(f"开始分析项目: {project_name}")
|
||
|
||
# 1. 代码质量分析
|
||
code_analyzer = CodeAnalyzer(project_path)
|
||
code_results = code_analyzer.analyze()
|
||
|
||
# 2. 文档分析
|
||
doc_analyzer = DocumentationAnalyzer(project_path)
|
||
doc_results = doc_analyzer.analyze()
|
||
|
||
# 3. 活跃度分析
|
||
activity_analyzer = ActivityAnalyzer(project_path)
|
||
activity_results = activity_analyzer.analyze()
|
||
|
||
# 4. AI智能分析
|
||
ai_analyzer = AIAnalyzer()
|
||
ai_insights = ai_analyzer.generate_insights({
|
||
'code': code_results,
|
||
'documentation': doc_results,
|
||
'activity': activity_results,
|
||
'project_name': project_name
|
||
})
|
||
|
||
# 5. 生成综合报告
|
||
report_gen = ReportGenerator()
|
||
final_report = report_gen.generate_report({
|
||
'project_name': project_name,
|
||
'code_quality': code_results,
|
||
'documentation': doc_results,
|
||
'activity': activity_results,
|
||
'ai_insights': ai_insights
|
||
})
|
||
|
||
# 保存报告
|
||
report_path = os.path.join('reports', f'{project_name}_report.json')
|
||
with open(report_path, 'w', encoding='utf-8') as f:
|
||
json.dump(final_report, f, ensure_ascii=False, indent=2)
|
||
|
||
return jsonify({
|
||
'success': True,
|
||
'project_name': project_name,
|
||
'report': final_report
|
||
})
|
||
|
||
except Exception as e:
|
||
print(f"分析过程出错: {str(e)}")
|
||
import traceback
|
||
traceback.print_exc()
|
||
return jsonify({'error': f'分析失败: {str(e)}'}), 500
|
||
|
||
@app.route('/report/<project_name>')
|
||
def view_report(project_name):
|
||
"""查看分析报告"""
|
||
report_path = os.path.join('reports', f'{project_name}_report.json')
|
||
|
||
if not os.path.exists(report_path):
|
||
return "报告不存在", 404
|
||
|
||
with open(report_path, 'r', encoding='utf-8') as f:
|
||
report_data = json.load(f)
|
||
|
||
return render_template('report.html', report=report_data)
|
||
|
||
@app.route('/api/download-report/<project_name>')
|
||
def download_report(project_name):
|
||
"""下载报告"""
|
||
report_path = os.path.join('reports', f'{project_name}_report.json')
|
||
|
||
if not os.path.exists(report_path):
|
||
return jsonify({'error': '报告不存在'}), 404
|
||
|
||
return send_file(report_path, as_attachment=True)
|
||
|
||
@app.route('/health')
|
||
def health_check():
|
||
"""健康检查接口"""
|
||
return jsonify({
|
||
'status': 'healthy',
|
||
'service': '开源项目质量分析系统',
|
||
'version': '1.0.0'
|
||
})
|
||
|
||
if __name__ == '__main__':
|
||
print("=" * 60)
|
||
print("🚀 开源项目质量分析系统启动中...")
|
||
print("📊 基于九格大模型的智能分析")
|
||
print("🌐 访问地址: http://localhost:5000")
|
||
print("=" * 60)
|
||
app.run(debug=True, host='0.0.0.0', port=5000)
|