Skip to content

Commit

Permalink
Enable user to ignore file change events in files matching given patt…
Browse files Browse the repository at this point in the history
…erns.
  • Loading branch information
merofeev committed Oct 5, 2018
1 parent 6b927df commit 04f6a22
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
7 changes: 6 additions & 1 deletion docker_volume_watcher/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 5 additions & 2 deletions docker_volume_watcher/container_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions docker_volume_watcher/container_notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,24 @@ 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
Args:
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
Expand Down

0 comments on commit 04f6a22

Please sign in to comment.