You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

run_task_in_pkgs_if_exist.py 1.9 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import glob
  2. import sys
  3. from pathlib import Path
  4. from typing import List
  5. import tomli
  6. from poethepoet.app import PoeThePoet
  7. from rich import print
  8. def discover_projects(workspace_pyproject_file: Path) -> List[Path]:
  9. with workspace_pyproject_file.open("rb") as f:
  10. data = tomli.load(f)
  11. projects = data["tool"]["uv"]["workspace"]["members"]
  12. all_projects: List[Path] = []
  13. for project in projects:
  14. if "*" in project:
  15. globbed = glob.glob(str(project), root_dir=workspace_pyproject_file.parent)
  16. globbed_paths = [Path(p) for p in globbed]
  17. all_projects.extend(globbed_paths)
  18. else:
  19. all_projects.append(Path(project))
  20. return all_projects
  21. def extract_poe_tasks(file: Path) -> set[str]:
  22. with file.open("rb") as f:
  23. data = tomli.load(f)
  24. tasks = set(data.get("tool", {}).get("poe", {}).get("tasks", {}).keys())
  25. # Check if there is an include too
  26. include: str | None = data.get("tool", {}).get("poe", {}).get("include", None)
  27. if include:
  28. include_file = file.parent / include
  29. if include_file.exists():
  30. tasks = tasks.union(extract_poe_tasks(include_file))
  31. return tasks
  32. def main() -> None:
  33. pyproject_file = Path(__file__).parent / "pyproject.toml"
  34. projects = discover_projects(pyproject_file)
  35. if len(sys.argv) < 2:
  36. print("Please provide a task name")
  37. sys.exit(1)
  38. task_name = sys.argv[1]
  39. for project in projects:
  40. tasks = extract_poe_tasks(project / "pyproject.toml")
  41. if task_name in tasks:
  42. print(f"Running task {task_name} in {project}")
  43. app = PoeThePoet(cwd=project)
  44. result = app(cli_args=sys.argv[1:])
  45. if result:
  46. sys.exit(result)
  47. else:
  48. print(f"Task {task_name} not found in {project}")
  49. if __name__ == "__main__":
  50. main()