Debian Codegraph @

s_tui/sources/hook_script.py

raw symbols SCIP
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2017-2025 Alex Manuskin, Gil Tsuker
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18
19 from __future__ import annotations
20
21 import os
22 import subprocess
23 from typing import Any
24
25 from s_tui.sources.hook import Hook
26
27
28 class ScriptHook:
29 """
30 Runs an arbitrary shell script stored in the filesystem when invoked
31 """
32
33 def __init__(self, path: str, timeout_milliseconds: int = 0) -> None:
34 self.path = path
35 self.hook = self._make_script_hook(path, timeout_milliseconds)
36
37 def is_ready(self) -> bool:
38 return self.hook.is_ready()
39
40 def invoke(self) -> None:
41 self.hook.invoke()
42
43 def _run_script(self, *args: Any) -> None:
44 # Run script in a shell subprocess asynchronously so
45 # as to not block main thread (graphs)
46 # if the script is a long-running task
47 with open(os.devnull, "w") as dev_null:
48 subprocess.Popen(
49 ["/bin/sh", args[0][0]],
50 # TODO -- Could redirect this to a separate log
51 # file
52 # but not a priority just now
53 # Silence hook scripts so that they don't
54 # interfere with the application's tui
55 stdout=dev_null,
56 stderr=dev_null,
57 )
58
59 def _make_script_hook(self, path: str, timeout_milliseconds: int) -> Hook:
60 return Hook(self._run_script, timeout_milliseconds, path)