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 2.3 kB

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