|
| 1 | +//! Python 独立注释入口:复用原 Ruff 扫描,保持文档任务与开发规范任务的分类边界。 |
| 2 | +use crate::check_budget::{budget_record, resolve_project_default, select_check_timeout}; |
| 3 | +use crate::python_comments_arguments::PythonCommentsArguments; |
| 4 | +use serde_json::{Value, json}; |
| 5 | +use std::{ |
| 6 | + process::ExitCode, |
| 7 | + sync::atomic::AtomicBool, |
| 8 | + time::{Duration, Instant}, |
| 9 | +}; |
| 10 | + |
| 11 | +/// 执行项目内已配置 Ruff 的注释观察;参数为语种后 argv,返回局部未完成或取消退出码。 |
| 12 | +pub fn run(args: &[String]) -> ExitCode { |
| 13 | + let arguments = match PythonCommentsArguments::parse(args) { |
| 14 | + Ok(value) => value, |
| 15 | + Err(reason) => { |
| 16 | + eprintln!("{reason}"); |
| 17 | + return ExitCode::from(2); |
| 18 | + } |
| 19 | + }; |
| 20 | + let (timeout, source) = match select_check_timeout(arguments.timeout) |
| 21 | + .and_then(|(value, source)| resolve_project_default(&arguments.root, value, source)) |
| 22 | + { |
| 23 | + Ok(value) => value, |
| 24 | + Err(reason) => { |
| 25 | + eprintln!("{reason}"); |
| 26 | + return ExitCode::from(2); |
| 27 | + } |
| 28 | + }; |
| 29 | + let deadline = Instant::now() + Duration::from_millis(timeout); |
| 30 | + let mut native = Value::Null; |
| 31 | + let mut next = Value::Null; |
| 32 | + let mut reason = "project_root_unavailable"; |
| 33 | + if let Some(root) = arguments |
| 34 | + .root |
| 35 | + .canonicalize() |
| 36 | + .ok() |
| 37 | + .filter(|root| root.is_dir()) |
| 38 | + { |
| 39 | + match crate::python_lint_command::scan_and_sync_report_with_deadline( |
| 40 | + &root, |
| 41 | + arguments.tool.as_deref(), |
| 42 | + deadline, |
| 43 | + &AtomicBool::new(false), |
| 44 | + ) { |
| 45 | + Ok(mut report) => { |
| 46 | + // 工作台已经保存原 0.9 扫描;这里仅引用已有 0.12 对话协议,不改写原始事实。 |
| 47 | + crate::python_lint_command::annotate_conversation_budget( |
| 48 | + &mut report, |
| 49 | + timeout, |
| 50 | + source, |
| 51 | + ); |
| 52 | + native = report; |
| 53 | + reason = "native_documentation_coverage_unverified"; |
| 54 | + if native["workspace_binding"] == "bound" { |
| 55 | + next = crate::next_command::read_python_documentation_brief(&root) |
| 56 | + .unwrap_or(Value::Null); |
| 57 | + } |
| 58 | + } |
| 59 | + Err(_) => reason = "language_registry_invalid", |
| 60 | + } |
| 61 | + } |
| 62 | + let interrupted = codeguard_runtime::sigint_cancellation_requested() |
| 63 | + || native["incomplete_reasons"] |
| 64 | + .as_array() |
| 65 | + .is_some_and(|reasons| reasons.iter().any(|reason| reason == "request_cancelled")); |
| 66 | + if interrupted && native.is_object() { |
| 67 | + native["command_status"] = json!("cancelled"); |
| 68 | + native["exit_code"] = json!(130); |
| 69 | + } |
| 70 | + let findings: Vec<_> = native["files"] |
| 71 | + .as_array() |
| 72 | + .into_iter() |
| 73 | + .flatten() |
| 74 | + .flat_map(|file| file["findings"].as_array().into_iter().flatten()) |
| 75 | + .filter(|finding| { |
| 76 | + finding["rule_id"] |
| 77 | + .as_str() |
| 78 | + .is_some_and(codeguard_adapters::is_ruff_documentation_rule) |
| 79 | + }) |
| 80 | + .cloned() |
| 81 | + .collect(); |
| 82 | + let complete = !interrupted && native["local_scan_complete"] == true; |
| 83 | + let exit = if interrupted { 130 } else { 3 }; |
| 84 | + let report = json!({"schema_version":"0.1.0","report_type":"python_comments_feedback","operation":"comments","language":"python", |
| 85 | + "command_status":if interrupted{"cancelled"}else{"incomplete"},"exit_code":exit,"authority":"local_unverified","coverage_proven":false, |
| 86 | + "delivery_decision":"not_evaluated","local_scan_complete":complete,"reason":reason,"execution_budget":budget_record(timeout,source), |
| 87 | + "native_report":native,"documentation_findings":findings,"next":next,"documentation_rule_coverage":"unverified","detailed_contract_qualification":"not_granted", |
| 88 | + "next_actions":["核对原项目 Ruff 文档规则和配置;不会隐式启用 preview 或改写规则选择。", |
| 89 | + "按稳定任务使用 task verify 原工具复检;零诊断不证明文档规则完整启用、详细内容准确或任务已关闭。"]}); |
| 90 | + if arguments.json { |
| 91 | + println!("{report}"); |
| 92 | + } else { |
| 93 | + println!( |
| 94 | + "Python 注释检查:原生局部完成 {};文档规则完整覆盖未验证。", |
| 95 | + complete |
| 96 | + ); |
| 97 | + for finding in report["documentation_findings"] |
| 98 | + .as_array() |
| 99 | + .into_iter() |
| 100 | + .flatten() |
| 101 | + { |
| 102 | + println!( |
| 103 | + "原生规则 {}:{}:{};{};任务 {}", |
| 104 | + finding["rule_id"], |
| 105 | + finding["path"], |
| 106 | + finding["line"], |
| 107 | + finding["rule_summary"], |
| 108 | + finding["finding_id"] |
| 109 | + ); |
| 110 | + } |
| 111 | + println!( |
| 112 | + "原生未完成原因:{};下一步:{}", |
| 113 | + report["native_report"]["incomplete_reasons"], report["next"] |
| 114 | + ); |
| 115 | + } |
| 116 | + ExitCode::from(exit) |
| 117 | +} |
0 commit comments