diff --git a/focusfriend.code-workspace b/aw-focus.code-workspace similarity index 100% rename from focusfriend.code-workspace rename to aw-focus.code-workspace diff --git a/focusfriend.py b/aw-focus.py old mode 100755 new mode 100644 similarity index 80% rename from focusfriend.py rename to aw-focus.py index 1b04c5b..5da6aa3 --- a/focusfriend.py +++ b/aw-focus.py @@ -112,6 +112,40 @@ def kill_window_if_blocked(blocked: List[re.Pattern]) -> None: kill_sequence(blocked) +def is_process_active(process_name: str) -> bool: + for proc in psutil.process_iter(): + if proc.name() == process_name: + return True + return False + + +def enforce_aw_commit(): + def aw_commit_active(): + return is_process_active("aw-commit") + + def to_display(name: str) -> str: + return name if len(name) < 30 else name[:30] + "..." + + if aw_commit_active(): + return + + for _ in range(10, 0, -1): + notify(f"[warning] aw-commit not running") + time.sleep(1) + if aw_commit_active(): + return + + if aw_commit_active(): + return + + window_name, window_pid = get_active_window_name_and_pid() + if window_name: + notify(f"[kill aw-commit not running] {to_display(window_name)}") + p = psutil.Process(int(window_pid)) + p.terminate() + return enforce_aw_commit() + + def main_root(config: Config) -> None: init(config) blocked = load_blocked_patterns(config) @@ -119,11 +153,13 @@ def main_root(config: Config) -> None: time.sleep(config.sleep_time) run_checks(config) kill_window_if_blocked(blocked) + if config.enforce_aw_commit: + enforce_aw_commit() def main() -> None: """ Run main_root as root except while debugging. """ - config_path = "~/.config/focusfriend/config.json" + config_path = "~/.config/aw-focus/config.json" config = Config.load_config(config_path) if config.start_as_user: @@ -135,7 +171,7 @@ def main() -> None: if newpid == 0: main_root(config) else: - cmd = ["sudo", config.focusfriend_py] + sys.argv[1:] + cmd = ["sudo", config.aw_focus_cmd] + sys.argv[1:] subprocess.Popen(cmd) diff --git a/config.json b/config.json index b59d268..3e9e5ea 120000 --- a/config.json +++ b/config.json @@ -1 +1 @@ -/home/felixm/.config/focusfriend/config.json \ No newline at end of file +/home/felixm/.config/aw-focus/config.json \ No newline at end of file diff --git a/config.py b/config.py index b01361e..013590b 100644 --- a/config.py +++ b/config.py @@ -16,8 +16,9 @@ class Config(BaseModel): blocks: List[Block] start_as_user: bool = True sleep_time: int = 1 - focusfriend_py: str = "focusfriend.py" + aw_focus_cmd: str = "aw-focus" window_names: str = "window_names.txt" + enforce_aw_commit: bool = True class Config: extra = 'forbid' diff --git a/focus.py b/focus.py deleted file mode 100644 index 0bcd3df..0000000 --- a/focus.py +++ /dev/null @@ -1,70 +0,0 @@ -#!/usr/bin/env python3 - -import os -import psutil -import re -import subprocess -import sys -import time - - -XDOTOOL_CMD = ["xdotool", "getactivewindow", "getwindowname", "getwindowpid"] - - -SESSIONS = { - "sicp": [ - "~", - "~/dev/sicp", - "mit-scheme", - "Mozilla Firefox", - "[No Name] - VIM", - re.compile("sicp-ex-"), - re.compile("\.scm"), - re.compile("Structure and Interpretation of Computer Programs"), - re.compile("SICP-Solutions"), - ] -} - - -def is_window_allowed(window_name, allowed): - for a in allowed: - if type(a) is str and a == window_name: - return True - elif type(a) is re.Pattern and a.findall(window_name): - return True - return False - - -def main(session_name): - allowed_window_names = SESSIONS[session_name] - start_time = time.time() - - while time.time() - start_time < 60 * 60: - time.sleep(5) - p = subprocess.run(XDOTOOL_CMD, capture_output=True) - if p.returncode != 0: - continue - window_name, window_pid, _ = p.stdout.decode().split("\n") - if not is_window_allowed(window_name, allowed_window_names): - p = psutil.Process(int(window_pid)) - p.kill() - - -if __name__ == "__main__": - try: - session_name = sys.argv[1] - except IndexError: - print("Provide a session name as the first argument.") - sys.exit(1) - if not session_name in SESSIONS: - print(f"Session with name {session_name} does not exist.") - sys.exit(1) - - if os.geteuid() == 0: - newpid = os.fork() - if newpid == 0: - main(session_name) - else: - cmd = ["sudo"] + sys.argv - subprocess.Popen(cmd, start_new_session=True) -