2025-02-09 23:01:53 -07:00
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
from pathlib import Path
|
|
|
|
|
2025-04-04 16:50:46 -06:00
|
|
|
import tomli
|
|
|
|
|
|
|
|
|
2025-02-09 23:01:53 -07:00
|
|
|
def extract_dependency_info():
|
2025-04-04 16:49:10 -06:00
|
|
|
"""Extract version for kokoro and misaki from pyproject.toml"""
|
2025-02-09 23:01:53 -07:00
|
|
|
with open("pyproject.toml", "rb") as f:
|
|
|
|
pyproject = tomli.load(f)
|
2025-04-04 16:58:07 -06:00
|
|
|
|
2025-02-09 23:01:53 -07:00
|
|
|
deps = pyproject["project"]["dependencies"]
|
|
|
|
info = {}
|
2025-04-04 16:49:10 -06:00
|
|
|
kokoro_found = False
|
|
|
|
misaki_found = False
|
2025-04-04 16:58:07 -06:00
|
|
|
|
2025-02-09 23:01:53 -07:00
|
|
|
for dep in deps:
|
2025-04-04 16:49:10 -06:00
|
|
|
# Match kokoro==version
|
|
|
|
kokoro_match = re.match(r"^kokoro==(.+)$", dep)
|
|
|
|
if kokoro_match:
|
|
|
|
info["kokoro"] = {"version": kokoro_match.group(1)}
|
|
|
|
kokoro_found = True
|
2025-04-04 16:58:07 -06:00
|
|
|
|
2025-04-04 16:49:10 -06:00
|
|
|
# Match misaki[...] ==version or misaki==version
|
|
|
|
misaki_match = re.match(r"^misaki(?:\[.*?\])?==(.+)$", dep)
|
|
|
|
if misaki_match:
|
|
|
|
info["misaki"] = {"version": misaki_match.group(1)}
|
|
|
|
misaki_found = True
|
|
|
|
|
|
|
|
# Stop if both found
|
|
|
|
if kokoro_found and misaki_found:
|
|
|
|
break
|
2025-04-04 16:58:07 -06:00
|
|
|
|
2025-04-04 16:49:10 -06:00
|
|
|
if not kokoro_found:
|
|
|
|
raise ValueError("Kokoro version not found in pyproject.toml dependencies")
|
|
|
|
if not misaki_found:
|
|
|
|
raise ValueError("Misaki version not found in pyproject.toml dependencies")
|
2025-04-04 16:58:07 -06:00
|
|
|
|
2025-02-09 23:01:53 -07:00
|
|
|
return info
|
|
|
|
|
2025-04-04 16:58:07 -06:00
|
|
|
|
2025-02-09 23:01:53 -07:00
|
|
|
def run_pytest_with_coverage():
|
|
|
|
"""Run pytest with coverage and return the results"""
|
|
|
|
try:
|
|
|
|
# Run pytest with coverage
|
|
|
|
result = subprocess.run(
|
2025-04-04 16:58:07 -06:00
|
|
|
["pytest", "--cov=api", "-v"], capture_output=True, text=True, check=True
|
2025-02-09 23:01:53 -07:00
|
|
|
)
|
2025-04-04 16:58:07 -06:00
|
|
|
|
2025-02-09 23:01:53 -07:00
|
|
|
# Extract test results
|
|
|
|
test_output = result.stdout
|
|
|
|
passed_tests = len(re.findall(r"PASSED", test_output))
|
2025-04-04 16:58:07 -06:00
|
|
|
|
2025-02-09 23:01:53 -07:00
|
|
|
# Extract coverage from .coverage file
|
|
|
|
coverage_output = subprocess.run(
|
2025-04-04 16:58:07 -06:00
|
|
|
["coverage", "report"], capture_output=True, text=True, check=True
|
2025-02-09 23:01:53 -07:00
|
|
|
).stdout
|
2025-04-04 16:58:07 -06:00
|
|
|
|
2025-02-09 23:01:53 -07:00
|
|
|
# Extract total coverage percentage
|
|
|
|
coverage_match = re.search(r"TOTAL\s+\d+\s+\d+\s+(\d+)%", coverage_output)
|
|
|
|
coverage_percentage = coverage_match.group(1) if coverage_match else "0"
|
2025-04-04 16:58:07 -06:00
|
|
|
|
2025-02-09 23:01:53 -07:00
|
|
|
return passed_tests, coverage_percentage
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
print(f"Error running tests: {e}")
|
|
|
|
print(f"Output: {e.output}")
|
|
|
|
return 0, "0"
|
|
|
|
|
2025-04-04 16:58:07 -06:00
|
|
|
|
2025-02-09 23:01:53 -07:00
|
|
|
def update_readme_badges(passed_tests, coverage_percentage, dep_info):
|
|
|
|
"""Update the badges in the README file"""
|
|
|
|
readme_path = Path("README.md")
|
|
|
|
if not readme_path.exists():
|
|
|
|
print("README.md not found")
|
|
|
|
return False
|
2025-04-04 16:58:07 -06:00
|
|
|
|
2025-02-09 23:01:53 -07:00
|
|
|
content = readme_path.read_text()
|
2025-04-04 16:58:07 -06:00
|
|
|
|
2025-02-09 23:01:53 -07:00
|
|
|
# Update tests badge
|
|
|
|
content = re.sub(
|
2025-04-04 16:58:07 -06:00
|
|
|
r"!\[Tests\]\(https://img\.shields\.io/badge/tests-\d+%20passed-[a-zA-Z]+\)",
|
|
|
|
f"",
|
|
|
|
content,
|
2025-02-09 23:01:53 -07:00
|
|
|
)
|
2025-04-04 16:58:07 -06:00
|
|
|
|
2025-02-09 23:01:53 -07:00
|
|
|
# Update coverage badge
|
|
|
|
content = re.sub(
|
2025-04-04 16:58:07 -06:00
|
|
|
r"!\[Coverage\]\(https://img\.shields\.io/badge/coverage-\d+%25-[a-zA-Z]+\)",
|
|
|
|
f"",
|
|
|
|
content,
|
2025-02-09 23:01:53 -07:00
|
|
|
)
|
2025-04-04 16:58:07 -06:00
|
|
|
|
2025-02-09 23:01:53 -07:00
|
|
|
# Update kokoro badge
|
|
|
|
if "kokoro" in dep_info:
|
2025-04-04 16:49:10 -06:00
|
|
|
# Find badge like kokoro-v0.9.2::abcdefg-BB5420 or kokoro-v0.9.2-BB5420
|
|
|
|
kokoro_version = dep_info["kokoro"]["version"]
|
2025-02-09 23:01:53 -07:00
|
|
|
content = re.sub(
|
2025-04-04 16:58:07 -06:00
|
|
|
r"(!\[Kokoro\]\(https://img\.shields\.io/badge/kokoro-)[^)-]+(-BB5420\))",
|
2025-04-04 16:49:10 -06:00
|
|
|
lambda m: f"{m.group(1)}{kokoro_version}{m.group(2)}",
|
2025-04-04 16:58:07 -06:00
|
|
|
content,
|
2025-02-09 23:01:53 -07:00
|
|
|
)
|
2025-04-04 16:58:07 -06:00
|
|
|
|
2025-02-09 23:01:53 -07:00
|
|
|
# Update misaki badge
|
|
|
|
if "misaki" in dep_info:
|
2025-04-04 16:49:10 -06:00
|
|
|
# Find badge like misaki-v0.9.3::abcdefg-B8860B or misaki-v0.9.3-B8860B
|
|
|
|
misaki_version = dep_info["misaki"]["version"]
|
2025-02-09 23:01:53 -07:00
|
|
|
content = re.sub(
|
2025-04-04 16:58:07 -06:00
|
|
|
r"(!\[Misaki\]\(https://img\.shields\.io/badge/misaki-)[^)-]+(-B8860B\))",
|
2025-04-04 16:49:10 -06:00
|
|
|
lambda m: f"{m.group(1)}{misaki_version}{m.group(2)}",
|
2025-04-04 16:58:07 -06:00
|
|
|
content,
|
2025-02-09 23:01:53 -07:00
|
|
|
)
|
2025-04-04 16:58:07 -06:00
|
|
|
|
2025-02-09 23:01:53 -07:00
|
|
|
readme_path.write_text(content)
|
|
|
|
return True
|
|
|
|
|
2025-04-04 16:58:07 -06:00
|
|
|
|
2025-02-09 23:01:53 -07:00
|
|
|
def main():
|
|
|
|
# Get dependency info
|
|
|
|
dep_info = extract_dependency_info()
|
2025-04-04 16:58:07 -06:00
|
|
|
|
2025-02-09 23:01:53 -07:00
|
|
|
# Run tests and get coverage
|
|
|
|
passed_tests, coverage_percentage = run_pytest_with_coverage()
|
2025-04-04 16:58:07 -06:00
|
|
|
|
2025-02-09 23:01:53 -07:00
|
|
|
# Update badges
|
|
|
|
if update_readme_badges(passed_tests, coverage_percentage, dep_info):
|
|
|
|
print(f"Updated badges:")
|
|
|
|
print(f"- Tests: {passed_tests} passed")
|
|
|
|
print(f"- Coverage: {coverage_percentage}%")
|
|
|
|
if "kokoro" in dep_info:
|
2025-04-04 16:49:10 -06:00
|
|
|
print(f"- Kokoro: {dep_info['kokoro']['version']}")
|
2025-02-09 23:01:53 -07:00
|
|
|
if "misaki" in dep_info:
|
2025-04-04 16:49:10 -06:00
|
|
|
print(f"- Misaki: {dep_info['misaki']['version']}")
|
2025-02-09 23:01:53 -07:00
|
|
|
else:
|
|
|
|
print("Failed to update badges")
|
|
|
|
|
2025-04-04 16:58:07 -06:00
|
|
|
|
2025-02-09 23:01:53 -07:00
|
|
|
if __name__ == "__main__":
|
2025-04-04 16:49:10 -06:00
|
|
|
main()
|