summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@vyos.io>2021-07-08 08:48:15 -0500
committerDaniil Baturin <daniil@vyos.io>2021-07-13 06:58:40 -0500
commit597e3b47d6e976854f1221078553d9dc2db032c0 (patch)
tree6f4410a422f7692ad1d4705e4476de5a2cee3208
parente7f89d4d45fedca6d1df0c15a8bce79c82b76aa8 (diff)
downloadvyos-1x-597e3b47d6e976854f1221078553d9dc2db032c0.tar.gz
vyos-1x-597e3b47d6e976854f1221078553d9dc2db032c0.zip
T3663: add pre_hook argument to util.wait_for_inotify
When waiting for processes that don't take long, we need add an inotify watcher _before_ starting that process. The pre-hook arguments allows the user to pass a () -> () anonymous function to be called before adding a watch.
-rw-r--r--python/vyos/util.py9
1 files changed, 6 insertions, 3 deletions
diff --git a/python/vyos/util.py b/python/vyos/util.py
index d2758aff3..b611dea4c 100644
--- a/python/vyos/util.py
+++ b/python/vyos/util.py
@@ -510,7 +510,7 @@ def file_is_persistent(path):
absolute = os.path.abspath(os.path.dirname(path))
return re.match(location,absolute)
-def wait_for_inotify(file_path, event_type=None, timeout=None, sleep_interval=0.1):
+def wait_for_inotify(file_path, pre_hook=None, event_type=None, timeout=None, sleep_interval=0.1):
""" Waits for an inotify event to occur """
if not os.path.dirname(file_path):
raise ValueError(
@@ -524,6 +524,9 @@ def wait_for_inotify(file_path, event_type=None, timeout=None, sleep_interval=0.
i = inotify.adapters.Inotify()
i.add_watch(os.path.dirname(file_path))
+ if pre_hook:
+ pre_hook()
+
for event in i.event_gen(yield_nones=True):
if (timeout is not None) and ((time() - time_start) > timeout):
# If the function didn't return until this point,
@@ -536,10 +539,10 @@ def wait_for_inotify(file_path, event_type=None, timeout=None, sleep_interval=0.
if event_type in type_names:
return
-def wait_for_file_write_complete(file_path, timeout=None, sleep_interval=0.1):
+def wait_for_file_write_complete(file_path, pre_hook=None, timeout=None, sleep_interval=0.1):
""" Waits for a process to close a file after opening it in write mode. """
wait_for_inotify(file_path,
- event_type='IN_CLOSE_WRITE', timeout=timeout, sleep_interval=sleep_interval)
+ event_type='IN_CLOSE_WRITE', pre_hook=pre_hook, timeout=timeout, sleep_interval=sleep_interval)
def commit_in_progress():
""" Not to be used in normal op mode scripts! """