-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adding peru override watch #163
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,9 @@ | |
import os | ||
from pathlib import Path | ||
import tempfile | ||
import logging | ||
import sys | ||
import time | ||
|
||
from . import cache | ||
from . import compat | ||
|
@@ -11,6 +14,8 @@ | |
from .keyval import KeyVal | ||
from . import parser | ||
from . import plugin | ||
from watchdog.observers import Observer | ||
from watchdog.events import LoggingEventHandler | ||
|
||
|
||
@asyncio.coroutine | ||
|
@@ -139,6 +144,67 @@ def print_overrides(self): | |
self.display.print(' {}: {}'.format( | ||
name, self.get_override(name))) | ||
|
||
def watch_overrides(self): | ||
class WatchHandler(LoggingEventHandler): | ||
logging.basicConfig(level=logging.INFO, | ||
format='%(asctime)s - %(message)s', | ||
datefmt='%Y-%m-%d %H:%M:%S') | ||
|
||
def on_moved(self, event): | ||
super(LoggingEventHandler, self).on_moved(event) | ||
what = 'directory' if event.is_directory else 'file' | ||
logging.info('moved %s: from %s to %s', what, event.src_path, | ||
event.dest_path) | ||
logging.info('running peru sync --force') | ||
os.system('peru sync --force') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'd probably want some way to control whether syncs are done with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than shelling out with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Peru doesn't currently do any kind of filesystem locking to prevent running multiple commands in parallel in the same project. But if you do that, it could cause weird conflicts and errors. We should definitely add proper locking at some point, but it still also probably makes sense for a watcher to avoid queueing up several waiting instances at once (or running commands that will error out, depending on how we want to implement lock contention). |
||
logging.info('overrides synced') | ||
|
||
def on_created(self, event): | ||
super(LoggingEventHandler, self).on_created(event) | ||
what = 'directory' if event.is_directory else 'file' | ||
logging.info('Created %s: %s', what, event.src_path) | ||
logging.info('running peru sync --force') | ||
os.system('peru sync --force') | ||
logging.info('overrides synced') | ||
|
||
def on_deleted(self, event): | ||
super(LoggingEventHandler, self).on_deleted(event) | ||
what = 'directory' if event.is_directory else 'file' | ||
logging.info('Deleted %s: %s', what, event.src_path) | ||
logging.info('running peru sync --force') | ||
os.system('peru sync --force') | ||
logging.info('overrides synced') | ||
|
||
def on_modified(self, event): | ||
super(LoggingEventHandler, self).on_modified(event) | ||
what = 'directory' if event.is_directory else 'file' | ||
logging.info('Modified %s: %s', what, event.src_path) | ||
logging.info('running peru sync --force') | ||
os.system('peru sync --force') | ||
logging.info('overrides synced') | ||
|
||
event_handler = WatchHandler() | ||
observer = Observer() | ||
|
||
if not self.overrides: | ||
print('WARNING: no overrides found') | ||
return | ||
|
||
print('watching overrides:') | ||
for module in self.overrides: | ||
observer.schedule(event_handler, self.get_override(module), recursive=True) | ||
print(' ' + module) | ||
|
||
observer.start() | ||
|
||
try: | ||
while True: | ||
time.sleep(1) | ||
except KeyboardInterrupt: | ||
observer.stop() | ||
|
||
observer.join() | ||
|
||
def warn_unused_overrides(self): | ||
if self.quiet or self.no_overrides: | ||
return | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
sync
andreup
commands use a fancy display to draw to the terminal and clean things up as jobs finish. This conflicts with regular printing. The display object (usuallyruntime.display
) has aprint
method that lets us do regular printing in a way that doesn't screw up the redrawing.