-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Copy pathrun_task_in_pkgs_if_exist.py
67 lines (50 loc) · 1.86 KB
/
run_task_in_pkgs_if_exist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import glob
import sys
from pathlib import Path
from typing import List
import tomli
from poethepoet.app import PoeThePoet
from rich import print
def discover_projects(workspace_pyproject_file: Path) -> List[Path]:
with workspace_pyproject_file.open("rb") as f:
data = tomli.load(f)
projects = data["tool"]["uv"]["workspace"]["members"]
all_projects: List[Path] = []
for project in projects:
if "*" in project:
globbed = glob.glob(str(project), root_dir=workspace_pyproject_file.parent)
globbed_paths = [Path(p) for p in globbed]
all_projects.extend(globbed_paths)
else:
all_projects.append(Path(project))
return all_projects
def extract_poe_tasks(file: Path) -> set[str]:
with file.open("rb") as f:
data = tomli.load(f)
tasks = set(data.get("tool", {}).get("poe", {}).get("tasks", {}).keys())
# Check if there is an include too
include: str | None = data.get("tool", {}).get("poe", {}).get("include", None)
if include:
include_file = file.parent / include
if include_file.exists():
tasks = tasks.union(extract_poe_tasks(include_file))
return tasks
def main() -> None:
pyproject_file = Path(__file__).parent / "pyproject.toml"
projects = discover_projects(pyproject_file)
if len(sys.argv) < 2:
print("Please provide a task name")
sys.exit(1)
task_name = sys.argv[1]
for project in projects:
tasks = extract_poe_tasks(project / "pyproject.toml")
if task_name in tasks:
print(f"Running task {task_name} in {project}")
app = PoeThePoet(cwd=project)
result = app(cli_args=sys.argv[1:])
if result:
sys.exit(result)
else:
print(f"Task {task_name} not found in {project}")
if __name__ == "__main__":
main()