Skip to content
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

Use watchdog #91

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ else
endif

LIBEXEC ?= $(PREFIX)/libexec
PYTHON ?= python3

.PHONY: all
all:
Expand All @@ -26,11 +27,14 @@ native/textern.json:

.PHONY: native-install
native-install: native/textern.json
@if ! test -f native/inotify_simple/.git; then echo "Missing inotify_simple submodule! Try 'git submodule update --init'."; false; fi
mkdir -p $(DESTDIR)$(MOZILLA_NATIVE)
cp -f native/textern.json $(DESTDIR)$(MOZILLA_NATIVE)
mkdir -p $(DESTDIR)$(LIBEXEC)/textern
cp -rf native/textern.py native/inotify_simple $(DESTDIR)$(LIBEXEC)/textern
$(PYTHON) -m venv $(DESTDIR)$(LIBEXEC)/textern/venv
$(DESTDIR)$(LIBEXEC)/textern/venv/bin/python -m pip install watchdog
sed "s@#!/usr/bin/env python3@#!$(DESTDIR)$(LIBEXEC)/textern/venv/bin/python@" \
< native/textern.py > $(DESTDIR)$(LIBEXEC)/textern/textern.py
chmod a+x $(DESTDIR)$(LIBEXEC)/textern/textern.py

.PHONY: native-uninstall
native-uninstall:
Expand Down
1 change: 0 additions & 1 deletion native/inotify_simple
Submodule inotify_simple deleted from 979648
47 changes: 28 additions & 19 deletions native/textern.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,11 @@
import struct
import sys
import tempfile
import urllib.parse
import time
import urllib.parse

try:
from inotify_simple import INotify, flags
except ImportError:
# fall-back to the old import pattern for git-submodule use-case (PR#57)
from inotify_simple.inotify_simple import INotify, flags

from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer

BACKUP_RETENTION_SECS = 24 * 60 * 60

Expand Down Expand Up @@ -97,14 +93,28 @@ def get(self, relfn):
return f.read(), self._tmpfiles[relfn]


class CloseEventHandler(FileSystemEventHandler):
def __init__(self, tmp_mgr):
super(CloseEventHandler, self).__init__()
self.tmp_mgr = tmp_mgr

def on_closed(self, event):
super(CloseEventHandler, self).on_closed(event)
handle_close_event(os.path.basename(event.src_path), self.tmp_mgr)


def main():
with INotify() as ino, TmpManager() as tmp_mgr:
ino.add_watch(tmp_mgr.tmpdir, flags.CLOSE_WRITE)
with TmpManager() as tmp_mgr:
event_handler = CloseEventHandler(tmp_mgr)
observer = Observer()
observer.schedule(event_handler, tmp_mgr.tmpdir)
observer.start()
loop = asyncio.get_event_loop()
loop.add_reader(sys.stdin.buffer, handle_stdin, tmp_mgr)
loop.add_reader(ino.fd, handle_inotify_event, ino, tmp_mgr)
loop.run_forever()
loop.close()
observer.stop()
observer.join()


def handle_stdin(tmp_mgr):
Expand Down Expand Up @@ -201,15 +211,14 @@ async def handle_message_new_text(tmp_mgr, msg):
}


def handle_inotify_event(ino, tmp_mgr):
for event in ino.read():
# this check is relevant in the case where we're handling the inotify
# event caused by tmp_mgr.new(), but then an exception occurred in
# handle_message() which caused the tmpfile to already be deleted
if event.name in tmp_mgr:
text, id = tmp_mgr.get(event.name)
send_text_update(id, text)
tmp_mgr.backup(event.name)
def handle_close_event(relfn, tmp_mgr):
# this check is relevant in the case where we're handling the inotify
# event caused by tmp_mgr.new(), but then an exception occurred in
# handle_message() which caused the tmpfile to already be deleted
if relfn in tmp_mgr:
text, id = tmp_mgr.get(relfn)
send_text_update(id, text)
tmp_mgr.backup(relfn)


def send_text_update(id, text):
Expand Down