90 lines
3.1 KiB
Python
90 lines
3.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Self-contained Python environment validator using only stdlib."""
|
|
|
|
import sys
|
|
import json
|
|
import os
|
|
import hashlib
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
|
|
def main():
|
|
results = {
|
|
"timestamp": datetime.utcnow().isoformat() + "Z",
|
|
"python_version": sys.version,
|
|
"python_version_info": {
|
|
"major": sys.version_info.major,
|
|
"minor": sys.version_info.minor,
|
|
"micro": sys.version_info.micro,
|
|
"releaselevel": sys.version_info.releaselevel,
|
|
"serial": sys.version_info.serial
|
|
},
|
|
"platform": sys.platform,
|
|
"tests": {}
|
|
}
|
|
|
|
# Test 1: Basic arithmetic
|
|
results["tests"]["arithmetic"] = {
|
|
"passed": (2 + 2 == 4) and (10 * 5 == 50) and (100 / 4 == 25.0),
|
|
"values": {"two_plus_two": 2 + 2, "ten_times_five": 10 * 5, "hundred_div_four": 100 / 4}
|
|
}
|
|
|
|
# Test 2: String operations
|
|
results["tests"]["string_ops"] = {
|
|
"passed": ("hello".upper() == "HELLO") and ("WORLD".lower() == "world"),
|
|
"values": {"hello_upper": "hello".upper(), "world_lower": "WORLD".lower()}
|
|
}
|
|
|
|
# Test 3: List comprehension
|
|
results["tests"]["list_comprehension"] = {
|
|
"passed": [x * 2 for x in range(5)] == [0, 2, 4, 6, 8],
|
|
"values": {"squares": [x * 2 for x in range(5)]}
|
|
}
|
|
|
|
# Test 4: File I/O
|
|
test_file = "/tmp/python_validation_test.txt"
|
|
test_content = "validation test content"
|
|
try:
|
|
with open(test_file, "w") as f:
|
|
f.write(test_content)
|
|
with open(test_file, "r") as f:
|
|
read_content = f.read()
|
|
results["tests"]["file_io"] = {
|
|
"passed": read_content == test_content,
|
|
"values": {"file_exists": os.path.exists(test_file), "content_match": read_content == test_content}
|
|
}
|
|
os.remove(test_file)
|
|
except Exception as e:
|
|
results["tests"]["file_io"] = {"passed": False, "error": str(e)}
|
|
|
|
# Test 5: Hash computation
|
|
results["tests"]["hashlib"] = {
|
|
"passed": hashlib.sha256(b"test").hexdigest() == "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08",
|
|
"values": {"expected": "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08",
|
|
"computed": hashlib.sha256(b"test").hexdigest()}
|
|
}
|
|
|
|
# Test 6: DateTime operations
|
|
results["tests"]["datetime"] = {
|
|
"passed": isinstance(datetime.utcnow(), datetime),
|
|
"values": {"current_utc": datetime.utcnow().isoformat()}
|
|
}
|
|
|
|
# Overall pass/fail
|
|
results["all_tests_passed"] = all(t.get("passed", False) for t in results["tests"].values())
|
|
|
|
# Write results
|
|
output_dir = Path("/root/.hermes/wizard-outputs/spark")
|
|
output_dir.mkdir(parents=True, exist_ok=True)
|
|
output_file = output_dir / "validation_results.json"
|
|
with open(output_file, "w") as f:
|
|
json.dump(results, f, indent=2)
|
|
|
|
print(f"Validation results written to {output_file}")
|
|
print(f"All tests passed: {results['all_tests_passed']}")
|
|
|
|
# Return exit code
|
|
return 0 if results["all_tests_passed"] else 1
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main()) |