set_version.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env python3
  2. from __future__ import annotations
  3. import argparse
  4. import re
  5. from pathlib import Path
  6. VERSION_FILE = Path(__file__).resolve().parents[2] / "lightrag" / "_version.py"
  7. def normalize_core_version(raw_version: str) -> str:
  8. if raw_version.startswith("v") and len(raw_version) > 1:
  9. return raw_version[1:]
  10. return raw_version
  11. def update_assignment(content: str, name: str, value: str) -> str:
  12. pattern = rf'^{name}\s*=\s*"[^"]*"$'
  13. updated, count = re.subn(
  14. pattern,
  15. f'{name} = "{value}"',
  16. content,
  17. count=1,
  18. flags=re.MULTILINE,
  19. )
  20. if count != 1:
  21. raise ValueError(f"Could not update {name} in {VERSION_FILE}")
  22. return updated
  23. def main() -> int:
  24. parser = argparse.ArgumentParser(description="Update LightRAG version constants.")
  25. parser.add_argument(
  26. "--core-version",
  27. required=True,
  28. help="Core package version. A leading 'v' is stripped automatically.",
  29. )
  30. parser.add_argument(
  31. "--api-version",
  32. help="Optional API compatibility version override.",
  33. )
  34. args = parser.parse_args()
  35. core_version = normalize_core_version(args.core_version)
  36. content = VERSION_FILE.read_text(encoding="utf-8")
  37. content = update_assignment(content, "__version__", core_version)
  38. if args.api_version is not None:
  39. content = update_assignment(content, "__api_version__", args.api_version)
  40. VERSION_FILE.write_text(content, encoding="utf-8")
  41. print(f"Updated {VERSION_FILE}")
  42. print(f"__version__={core_version}")
  43. if args.api_version is not None:
  44. print(f"__api_version__={args.api_version}")
  45. return 0
  46. if __name__ == "__main__":
  47. raise SystemExit(main())