From 04f6a222016fad6ebca9152fbc4ccfab93bbe09e Mon Sep 17 00:00:00 2001 From: Mikhail Erofeev Date: Fri, 5 Oct 2018 16:47:45 +0300 Subject: [PATCH] Enable user to ignore file change events in files matching given patterns. --- README.rst | 5 +++++ docker_volume_watcher/cli.py | 7 ++++++- docker_volume_watcher/container_monitor.py | 7 +++++-- docker_volume_watcher/container_notifier.py | 8 ++++++-- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index b4cbb6f..6d5a3d7 100644 --- a/README.rst +++ b/README.rst @@ -43,6 +43,11 @@ You can also specify wildcards with ``*`` and ``?`` characters. For example: mon docker-volume-watcher *myproject* C:\project\folder\* +Do not monitor files/directories matching ``*.git*`` and ``*build*`` patterns: + +.. code:: bat + + docker-volume-watcher -e "*.git*" "*build*" Use flag ``-v`` to enable verbose output: the script will report start/stop events of eligible containers and print all detected file changes. diff --git a/docker_volume_watcher/cli.py b/docker_volume_watcher/cli.py index 62ac7d4..0f9e780 100644 --- a/docker_volume_watcher/cli.py +++ b/docker_volume_watcher/cli.py @@ -24,12 +24,17 @@ def main(): parser.add_argument("-v", "--verbose", help="increase output verbosity", action="store_true") + parser.add_argument('-e', '--exclude', + help='ignore changes in files/directories matching given patterns', + nargs='+') + args = parser.parse_args() if args.verbose: logging.basicConfig(level=logging.INFO) - monitor = ContainerMonitor(args.container_pattern, args.host_dir_pattern) + monitor = ContainerMonitor(args.container_pattern, args.host_dir_pattern, + exclude_patterns=args.exclude) try: monitor.find_containers() monitor.monitor() diff --git a/docker_volume_watcher/container_monitor.py b/docker_volume_watcher/container_monitor.py index f708b3c..38f871e 100644 --- a/docker_volume_watcher/container_monitor.py +++ b/docker_volume_watcher/container_monitor.py @@ -35,18 +35,20 @@ class ContainerMonitor(object): """ Monitors container start/stop events and creates notifiers for mounts matching patterns. """ - def __init__(self, container_name_pattern, host_dir_pattern): + def __init__(self, container_name_pattern, host_dir_pattern, exclude_patterns=None): """ Initialize new instance of ContainerMonitor Args: container_name_pattern (str): Container name pattern host_dir_pattern (str): Host directory pattern + exclude_patterns (list): List of file name patterns for which changes should be ignored """ self.client = docker.from_env() self.container_name_pattern = container_name_pattern self.host_dir_pattern = host_dir_pattern self.notifiers = {} + self.exclude_patterns = exclude_patterns if exclude_patterns else [] def __handle_event(self, event): container_name = event['Actor']['Attributes']['name'] @@ -109,7 +111,8 @@ def watch_container(self, container_name): 'Bind of container %s was skipped for path %s as it\'s not a directory', container_name, mount['Source']) continue - notifier = ContainerNotifier(container, host_directory, mount['Destination']) + notifier = ContainerNotifier( + container, host_directory, mount['Destination'], self.exclude_patterns) notifiers.append(notifier) logging.info('Notifier %s created.', notifier) return notifiers diff --git a/docker_volume_watcher/container_notifier.py b/docker_volume_watcher/container_notifier.py index 41f7256..c335560 100644 --- a/docker_volume_watcher/container_notifier.py +++ b/docker_volume_watcher/container_notifier.py @@ -24,7 +24,7 @@ class ContainerNotifier(object): Notifies container about file changes in binded host-directory. """ - def __init__(self, container, host_dir, container_dir): + def __init__(self, container, host_dir, container_dir, exclude_patterns=None): """ Initialize a new instance of ContainerNotifier @@ -32,12 +32,16 @@ def __init__(self, container, host_dir, container_dir): container: Container host_dir (str): Host directory container_dir (str): Container directory + exclude_patterns (list): List of file name patterns for which changes should be ignored """ self.container = container self.host_dir = host_dir self.container_dir = container_dir + exclude_patterns = exclude_patterns if exclude_patterns else [] + + event_handler = PatternMatchingEventHandler( + ignore_patterns=exclude_patterns, ignore_directories=False) - event_handler = PatternMatchingEventHandler(ignore_directories=False) handler = self.__change_handler event_handler.on_created = handler event_handler.on_moved = handler