summaryrefslogtreecommitdiff
path: root/python/vyos/util.py
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@vyos.io>2021-07-08 08:48:15 -0500
committerDaniil Baturin <daniil@vyos.io>2021-07-08 08:48:15 -0500
commite6bce67f2ab25541f4a20e3147ea32ff0464aa9d (patch)
treebeb0d6f4feeb5b5cbe5bf16f17febeeb1bd4cbe5 /python/vyos/util.py
parent63713fc60c0fff8a6c6eabcba21191b347bfb8b4 (diff)
downloadvyos-1x-e6bce67f2ab25541f4a20e3147ea32ff0464aa9d.tar.gz
vyos-1x-e6bce67f2ab25541f4a20e3147ea32ff0464aa9d.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.
Diffstat (limited to 'python/vyos/util.py')
-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 553e995df..5a96013ea 100644
--- a/python/vyos/util.py
+++ b/python/vyos/util.py
@@ -504,7 +504,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(
@@ -521,6 +521,9 @@ def wait_for_inotify(file_path, event_type=None, timeout=None, sleep_interval=0.
i = 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,
@@ -533,10 +536,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! """