-
Notifications
You must be signed in to change notification settings - Fork 6
增加算子库环境快照 #38
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
Open
ghangz
wants to merge
2
commits into
MetaX-MACA:main
Choose a base branch
from
ghangz:mengz/mcoplib-maca-env-snapshot
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
增加算子库环境快照 #38
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| #!/usr/bin/env python3 | ||
| """Collect a reproducible MACA validation environment snapshot.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import json | ||
| import os | ||
| import platform | ||
| import shutil | ||
| import subprocess | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| def _command_version(command: str, args: list[str]) -> dict[str, object]: | ||
| path = shutil.which(command) | ||
| if not path: | ||
| return {"path": "", "available": False, "version": ""} | ||
| try: | ||
| completed = subprocess.run([path, *args], text=True, capture_output=True, timeout=5) | ||
| version = (completed.stdout or completed.stderr).strip().splitlines()[:5] | ||
| return { | ||
| "path": path, | ||
| "available": True, | ||
| "returncode": completed.returncode, | ||
| "version": version, | ||
| } | ||
| except (subprocess.SubprocessError, OSError) as exc: | ||
| return { | ||
| "path": path, | ||
| "available": True, | ||
| "returncode": -1, | ||
| "version": [f"Error: {exc}"], | ||
| } | ||
|
|
||
|
|
||
| def snapshot() -> dict[str, object]: | ||
| return { | ||
| "python": sys.version.split()[0], | ||
| "platform": platform.platform(), | ||
| "environment": { | ||
| "MACA_PATH": os.environ.get("MACA_PATH", ""), | ||
| "LD_LIBRARY_PATH": os.environ.get("LD_LIBRARY_PATH", ""), | ||
| "PYTHONPATH": os.environ.get("PYTHONPATH", ""), | ||
| }, | ||
| "tools": { | ||
| "mxcc": _command_version("mxcc", ["--version"]), | ||
| "cmake_maca": _command_version("cmake_maca", ["--version"]), | ||
| }, | ||
| } | ||
|
|
||
|
|
||
| def main() -> int: | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument("--output", type=Path) | ||
| args = parser.parse_args() | ||
|
|
||
| text = json.dumps(snapshot(), indent=2, ensure_ascii=False) | ||
| if args.output: | ||
| args.output.parent.mkdir(parents=True, exist_ok=True) | ||
| args.output.write_text(text + "\n", encoding="utf-8") | ||
| else: | ||
| print(text) | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import subprocess | ||
| from unittest.mock import patch | ||
| import unittest | ||
|
|
||
| from tools.maca_env_snapshot import _command_version, snapshot | ||
|
|
||
|
|
||
| class MacaEnvSnapshotTest(unittest.TestCase): | ||
| def test_snapshot_has_expected_sections(self): | ||
| report = snapshot() | ||
|
|
||
| self.assertIn("python", report) | ||
| self.assertIn("environment", report) | ||
| self.assertIn("tools", report) | ||
|
|
||
| @patch("tools.maca_env_snapshot.shutil.which", return_value="/usr/bin/mxcc") | ||
| @patch( | ||
| "tools.maca_env_snapshot.subprocess.run", | ||
| side_effect=subprocess.TimeoutExpired(cmd=["mxcc", "--version"], timeout=5), | ||
| ) | ||
| def test_command_version_handles_timeout(self, _run, _which): | ||
| report = _command_version("mxcc", ["--version"]) | ||
|
|
||
| self.assertTrue(report["available"]) | ||
| self.assertEqual(report["returncode"], -1) | ||
| self.assertIn("Error:", report["version"][0]) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
如果指定的输出路径(
args.output)包含不存在的父目录,直接调用write_text会抛出FileNotFoundError异常。建议在写入前自动创建父目录,以提升用户体验和脚本的容错能力。