Skip to content

Commit

Permalink
Show which paths changed when rebuilding
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Oct 2, 2024
1 parent 028cf60 commit 1743a43
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion sphinx_autobuild/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def main(argv=()):

if not args.no_initial_build:
show_message("Starting initial build")
builder(rebuild=False)
builder(changed_paths=())

if args.open_browser:
open_browser(url_host, args.delay)
Expand Down
19 changes: 16 additions & 3 deletions sphinx_autobuild/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

from __future__ import annotations

import contextlib
import subprocess
import sys
from collections.abc import Sequence
from pathlib import Path

import sphinx

Expand All @@ -16,10 +19,20 @@ def __init__(self, sphinx_args, *, url_host, pre_build_commands):
self.pre_build_commands = pre_build_commands
self.uri = f"http://{url_host}"

def __call__(self, *, rebuild: bool = True):
def __call__(self, *, changed_paths: Sequence[Path]):
"""Generate the documentation using ``sphinx``."""
if rebuild:
show_message("Detected change. Rebuilding...")
if changed_paths:
cwd = Path.cwd()
rel_paths = []
for changed_path in changed_paths[:5]:
if not changed_path.exists():
continue
with contextlib.suppress(ValueError):
changed_path = changed_path.relative_to(cwd)
rel_paths.append(changed_path.as_posix())
if rel_paths:
show_message(f"Detected changes ({', '.join(rel_paths)})")
show_message("Rebuilding...")

try:
for command in self.pre_build_commands:
Expand Down
9 changes: 5 additions & 4 deletions sphinx_autobuild/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

if TYPE_CHECKING:
import os
from collections.abc import Callable
from collections.abc import Callable, Sequence

from starlette.types import Receive, Scope, Send

Expand All @@ -23,7 +23,7 @@ def __init__(
self,
paths: list[os.PathLike[str]],
ignore_filter: IgnoreFilter,
change_callback: Callable[[], None],
change_callback: Callable[[Sequence[Path]], None],
) -> None:
self.paths = [Path(path).resolve(strict=True) for path in paths]
self.ignore = ignore_filter
Expand All @@ -49,12 +49,13 @@ async def main(self) -> None:
[task.result() for task in done]

async def watch(self) -> None:
async for _changes in watchfiles.awatch(
async for changes in watchfiles.awatch(
*self.paths,
watch_filter=lambda _, path: not self.ignore(path),
):
changed_paths = [Path(path).resolve() for (_, path) in changes]
with ProcessPoolExecutor() as pool:
fut = pool.submit(self.change_callback)
fut = pool.submit(self.change_callback, changed_paths=changed_paths)
await asyncio.wrap_future(fut)
self.flag.set()

Expand Down
2 changes: 1 addition & 1 deletion tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_application(tmp_path):
app = _create_app([src_dir], ignore_handler, builder, out_dir, url_host)
client = TestClient(app)

builder(rebuild=False)
builder(changed_paths=())

response = client.get("/")
assert response.status_code == 200

0 comments on commit 1743a43

Please sign in to comment.