Created
June 11, 2018 18:47
-
-
Save RabidCicada/db4e56d6e96bbdffd91ad18a5b69df0e to your computer and use it in GitHub Desktop.
Recursive enabled directory watcher for directory events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import sys | |
import inotify.adapters | |
_DEFAULT_LOG_FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' | |
_LOGGER = logging.getLogger(__name__) | |
def _configure_logging(): | |
_LOGGER.setLevel(logging.DEBUG) | |
ch = logging.StreamHandler() | |
formatter = logging.Formatter(_DEFAULT_LOG_FORMAT) | |
ch.setFormatter(formatter) | |
_LOGGER.addHandler(ch) | |
def _main(thedir): | |
i = inotify.adapters.InotifyTree(thedir) | |
try: | |
for event in i.event_gen(): | |
if event is not None: | |
(header, type_names, watch_path, filename) = event | |
_LOGGER.info("WD=(%d) MASK=(%d) COOKIE=(%d) LEN=(%d) MASK->NAMES=%s " | |
"WATCH-PATH=[%s] FILENAME=[%s]", | |
header.wd, header.mask, header.cookie, header.len, type_names, | |
watch_path, filename) | |
finally: | |
print("exception received") | |
if __name__ == '__main__': | |
if len(sys.argv) != 2: | |
print("Usage: {} dirpath".format(sys.argv[0])) | |
_configure_logging() | |
_main(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment