-
Notifications
You must be signed in to change notification settings - Fork 1
增加监控导出启动脚本选项audit #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import sys | ||
| import tempfile | ||
| import unittest | ||
| from pathlib import Path | ||
|
|
||
| sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tools")) | ||
|
|
||
| from start_script_option_audit import build | ||
|
|
||
|
|
||
| class StartScriptOptionAuditTest(unittest.TestCase): | ||
| def test_detects_undocumented_option(self): | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| root = Path(tmpdir) | ||
| (root / "start_mxexporter.sh").write_text( | ||
| " --port|-p=<port> Specific port, default: non-standard\n" | ||
| " --help|-h Display help-message\n" | ||
| " --help|-h)\n" | ||
| " --port=*|-p=*)\n" | ||
| " --pid=*)\n", | ||
| encoding="utf-8", | ||
| ) | ||
|
|
||
| report = build(root) | ||
|
|
||
| self.assertEqual(report["undocumented_options"], ["--pid"]) | ||
| self.assertEqual(report["stale_documentation"], []) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,63 @@ | ||||||||||||||||||||||||||||||||||||
| #!/usr/bin/env python3 | ||||||||||||||||||||||||||||||||||||
| """Compare documented and implemented start_mxexporter.sh options.""" | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| from __future__ import annotations | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| import argparse | ||||||||||||||||||||||||||||||||||||
| import json | ||||||||||||||||||||||||||||||||||||
| import re | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+6
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 根据 PEP 8 编码规范,标准库导入应当归为一组,且组内不应有空行。建议将
Suggested change
References
|
||||||||||||||||||||||||||||||||||||
| from pathlib import Path | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| TOKEN_RE = re.compile(r"--[A-Za-z0-9-]+|-[A-Za-z0-9]+") | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def parse_documented(text: str) -> set[str]: | ||||||||||||||||||||||||||||||||||||
| items = set() | ||||||||||||||||||||||||||||||||||||
| for raw_line in text.splitlines(): | ||||||||||||||||||||||||||||||||||||
| line = raw_line.strip() | ||||||||||||||||||||||||||||||||||||
| if not line.startswith("--") or line.endswith(")") or ":)" in line: | ||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||
| definition = line.split(maxsplit=1)[0] | ||||||||||||||||||||||||||||||||||||
| items.update(TOKEN_RE.findall(definition)) | ||||||||||||||||||||||||||||||||||||
| return items | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+15
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 在 建议只对每行开头的第一个空格分隔的标记(即选项定义部分)进行正则匹配,以完全避免描述文本的干扰。
Suggested change
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def parse_implemented(text: str) -> set[str]: | ||||||||||||||||||||||||||||||||||||
| items = set() | ||||||||||||||||||||||||||||||||||||
| for raw_line in text.splitlines(): | ||||||||||||||||||||||||||||||||||||
| line = raw_line.strip() | ||||||||||||||||||||||||||||||||||||
| if not line.endswith(")") or not line.startswith("-"): | ||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||
| items.update(TOKEN_RE.findall(line)) | ||||||||||||||||||||||||||||||||||||
| return items | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def build(repo_root: Path) -> dict[str, object]: | ||||||||||||||||||||||||||||||||||||
| script_text = (repo_root / "start_mxexporter.sh").read_text(encoding="utf-8") | ||||||||||||||||||||||||||||||||||||
| documented = parse_documented(script_text) | ||||||||||||||||||||||||||||||||||||
| implemented = parse_implemented(script_text) | ||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||
| "documented_count": len(documented), | ||||||||||||||||||||||||||||||||||||
| "implemented_count": len(implemented), | ||||||||||||||||||||||||||||||||||||
| "undocumented_options": sorted(implemented - documented), | ||||||||||||||||||||||||||||||||||||
| "stale_documentation": sorted(documented - implemented), | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def main() -> int: | ||||||||||||||||||||||||||||||||||||
| parser = argparse.ArgumentParser(description=__doc__) | ||||||||||||||||||||||||||||||||||||
| parser.add_argument("--repo-root", type=Path, default=Path(".")) | ||||||||||||||||||||||||||||||||||||
| parser.add_argument("--output", type=Path) | ||||||||||||||||||||||||||||||||||||
| args = parser.parse_args() | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| text = json.dumps(build(args.repo_root), indent=2, ensure_ascii=False) | ||||||||||||||||||||||||||||||||||||
| if args.output: | ||||||||||||||||||||||||||||||||||||
| args.output.write_text(text + "\n", encoding="utf-8") | ||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||
| print(text) | ||||||||||||||||||||||||||||||||||||
| return 0 | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| if __name__ == "__main__": | ||||||||||||||||||||||||||||||||||||
| raise SystemExit(main()) | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
建议在测试用例中加入包含连字符的描述文本(例如
non-standard和help-message),以验证并确保审计工具不会将描述中的连字符单词误识别为命令行选项。