Skip to content

Commit

Permalink
pw_watch: Auto rebuild and logging fixes
Browse files Browse the repository at this point in the history
- Add an Auto Rebuild checkbox option. If disabled then builds
  won't trigger on file changes.
- Fix resetting log follow mode on retriggers and manual building.
- Add logging.shutdown() befor os._exit calls. This helps inotify
  error logs surfacing in the terminal before Python shuts down.

Change-Id: I64d6c8fe99f3a00b253d3f4663ae1737d0b38129
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/132050
Reviewed-by: Ted Pudlik <[email protected]>
Commit-Queue: Auto-Submit <[email protected]>
Pigweed-Auto-Submit: Anthony DiGirolamo <[email protected]>
  • Loading branch information
AnthonyDiGirolamo authored and CQ Bot Account committed Mar 6, 2023
1 parent baf04ad commit dd51740
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 11 deletions.
8 changes: 8 additions & 0 deletions pw_build/py/pw_build/project_builder_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,13 +435,21 @@ def exit(
) -> None:
"""Exit function called when the user presses ctrl-c."""

# Note: The correct way to exit Python is via sys.exit() however this
# takes a number of seconds when running pw_watch with multiple parallel
# builds. Instead, this function calls os._exit() to shutdown
# immediately. This is similar to `pw_watch.watch._exit`:
# https://cs.opensource.google/pigweed/pigweed/+/main:pw_watch/py/pw_watch/watch.py?q=_exit.code

if not self.progress_bar:
self.restore_logging_and_shutdown(log_after_shutdown)
logging.shutdown()
os._exit(exit_code) # pylint: disable=protected-access

# Shut everything down after the progress_bar exits.
def _really_exit(future: asyncio.Future) -> NoReturn:
self.restore_logging_and_shutdown(log_after_shutdown)
logging.shutdown()
os._exit(future.result()) # pylint: disable=protected-access

if self.progress_bar.app.future:
Expand Down
2 changes: 1 addition & 1 deletion pw_watch/py/pw_watch/watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def _handle_matched_event(self, matching_path: Path) -> None:
log_message = f'File change detected: {os.path.relpath(matching_path)}'
if self.restart_on_changes:
if self.fullscreen_enabled and self.watch_app:
self.watch_app.rebuild_on_filechange()
self.watch_app.clear_log_panes()
self.debouncer.press(f'{log_message} Triggering build...')
else:
_LOG.info('%s ; not rebuilding', log_message)
Expand Down
64 changes: 54 additions & 10 deletions pw_watch/py/pw_watch/watch_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,33 @@
Balance all window sizes. ------------------------- Ctrl-U


Bottom Toolbar Controls
=======================

Rebuild Enter --------------- Click or press Enter to trigger a rebuild.
[x] Auto Rebuild ------------ Click to globaly enable or disable automatic
rebuilding when files change.
Help F1 --------------------- Click or press F1 to open this help window.
Quit Ctrl-d ----------------- Click or press Ctrl-d to quit pw_watch.
Next Tab Ctrl-Alt-n --------- Switch to the next log tab.
Previous Tab Ctrl-Alt-p ----- Switch to the previous log tab.


Build Status Bar
================

The build status bar shows the current status of all build directories outlined
in a colored frame.

┏━━ BUILDING ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ [✓] out_directory Building Last line of standard out. ┃
┃ [✓] out_dir2 Waiting Last line of standard out. ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

Each checkbox on the far left controls whether that directory is built when
files change and manual builds are run.


Copying Text
============

Expand Down Expand Up @@ -388,6 +415,14 @@ def __init__(
self.help_toolbar.add_button(
ToolbarButton('Enter', 'Rebuild', self.run_build)
)
self.help_toolbar.add_button(
ToolbarButton(
description='Auto Rebuild',
mouse_handler=self.toggle_restart_on_filechange,
is_checkbox=True,
checked=lambda: self.restart_on_changes,
)
)
self.help_toolbar.add_button(
ToolbarButton('F1', 'Help', self.user_guide_window.toggle_display)
)
Expand Down Expand Up @@ -655,25 +690,34 @@ def all_log_panes(self) -> Iterable[LogPane]:
if isinstance(pane, LogPane):
yield pane

def clear_ninja_log(self) -> None:
def clear_log_panes(self) -> None:
"""Erase all log pane content and turn on follow.

This is called whenever rebuilds occur. Either a manual build from
self.run_build or on file changes called from
pw_watch._handle_matched_event."""
for pane in self.all_log_panes():
pane.log_view.clear_visual_selection()
pane.log_view.clear_filters()
pane.log_view.log_store.clear_logs()
pane.log_view._restart_filtering() # pylint: disable=protected-access
pane.log_view.view_mode_changed()
# Re-enable follow if needed
if not pane.log_view.follow:
pane.log_view.toggle_follow()

def run_build(self):
"""Manually trigger a rebuild."""
self.clear_ninja_log()
def run_build(self) -> None:
"""Manually trigger a rebuild from the UI."""
self.clear_log_panes()
self.event_handler.rebuild()

def rebuild_on_filechange(self):
for pane in self.all_log_panes():
pane.log_view.clear_visual_selection()
pane.log_view.log_store.clear_logs()
pane.log_view.view_mode_changed()
@property
def restart_on_changes(self) -> bool:
return self.event_handler.restart_on_changes

def toggle_restart_on_filechange(self) -> None:
self.event_handler.restart_on_changes = (
not self.event_handler.restart_on_changes
)

def get_status_bar_text(self) -> StyleAndTextTuples:
"""Return formatted text for build status bar."""
Expand Down

0 comments on commit dd51740

Please sign in to comment.