Skip to content

Commit

Permalink
Implement hashing for tool conf watcher
Browse files Browse the repository at this point in the history
If modification time of watched files changes, only trigger event if
hashes differ as well
  • Loading branch information
mvdbeek committed Aug 30, 2016
1 parent f6fc5ed commit abd4fa6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/galaxy/tools/toolbox/watcher.py
Expand Up @@ -2,6 +2,7 @@
import os.path
import threading
import time
from galaxy.util.hash_util import md5_hash_file

try:
from watchdog.events import FileSystemEventHandler
Expand Down Expand Up @@ -82,6 +83,7 @@ def shutdown(self):
self.thread.join()

def check(self):
hashes = { key: None for key in self.paths.keys() }
while self._active:
do_reload = False
with self._lock:
Expand All @@ -90,12 +92,15 @@ def check(self):
if not os.path.exists(path):
continue
mod_time = self.paths[path]
if not hashes.get(path, None):
hashes[path] = md5_hash_file(path)
new_mod_time = None
if os.path.exists(path):
new_mod_time = time.ctime(os.path.getmtime(path))
if new_mod_time != mod_time:
self.paths[path] = new_mod_time
do_reload = True
if hashes[path] != md5_hash_file(path):
self.paths[path] = new_mod_time
do_reload = True

if do_reload:
t = threading.Thread(target=lambda: self.event_handler.on_any_event(None))
Expand Down
11 changes: 11 additions & 0 deletions lib/galaxy/util/hash_util.py
Expand Up @@ -14,6 +14,17 @@
log = logging.getLogger( __name__ )


def md5_hash_file(path):
"""
Return a md5 hashdigest for a file.
"""
hasher = hashlib.md5()
with open(path, 'rb') as afile:
buf = afile.read()
hasher.update(buf)
return hasher.hexdigest()


def new_secure_hash( text_type=None ):
"""
Returns either a sha1 hash object (if called with no arguments), or a
Expand Down

0 comments on commit abd4fa6

Please sign in to comment.