FocusFriend is now called aw-focus enforces aw-commit.
This commit is contained in:
40
focusfriend.py → aw-focus.py
Executable file → Normal file
40
focusfriend.py → aw-focus.py
Executable file → Normal file
@@ -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)
|
||||
|
||||
|
||||
@@ -1 +1 @@
|
||||
/home/felixm/.config/focusfriend/config.json
|
||||
/home/felixm/.config/aw-focus/config.json
|
||||
@@ -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'
|
||||
|
||||
70
focus.py
70
focus.py
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user