Debian Codegraph @

src/fenrirscreenreader/core/remoteInstanceRegistry.py

raw
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 # Fenrir TTY screen reader
5 # By Chrys, Storm Dragon, and contributors.
6
7 import errno
8 import json
9 import os
10 import tempfile
11 import time
12
13
14 INSTANCE_TIMEOUT_SEC = 30.0
15 REGISTRY_DIR_MODE = 0o1777
16
17
18 def get_registry_dir():
19 return os.path.join(tempfile.gettempdir(), "fenrirscreenreader-instances")
20
21
22 def get_instance_file(pid=None):
23 if pid is None:
24 pid = os.getpid()
25 return os.path.join(get_registry_dir(), f"{pid}.json")
26
27
28 def write_instance(instance_data):
29 registry_dir = get_registry_dir()
30 os.makedirs(registry_dir, mode=REGISTRY_DIR_MODE, exist_ok=True)
31 os.chmod(registry_dir, REGISTRY_DIR_MODE)
32 prune_stale_instances()
33 instance_data["updated_at"] = time.time()
34 instance_path = get_instance_file(instance_data.get("pid"))
35 with open(instance_path, "w", encoding="utf-8") as instance_file:
36 json.dump(instance_data, instance_file, sort_keys=True)
37 instance_file.write("\n")
38 os.chmod(instance_path, 0o644)
39
40
41 def remove_instance(pid=None):
42 try:
43 os.unlink(get_instance_file(pid))
44 except FileNotFoundError:
45 pass
46
47
48 def process_exists(pid):
49 try:
50 os.kill(pid, 0)
51 return True
52 except OSError as e:
53 if e.errno == errno.EPERM:
54 return True
55 return False
56
57
58 def prune_stale_instances():
59 registry_dir = get_registry_dir()
60 try:
61 instance_files = os.listdir(registry_dir)
62 except FileNotFoundError:
63 return
64
65 now = time.time()
66 for instance_name in instance_files:
67 instance_path = os.path.join(registry_dir, instance_name)
68 try:
69 with open(instance_path, "r", encoding="utf-8") as instance_file:
70 instance_data = json.load(instance_file)
71 pid = int(instance_data.get("pid", 0))
72 updated_at = float(instance_data.get("updated_at", 0))
73 except (OSError, ValueError, TypeError, json.JSONDecodeError):
74 try:
75 os.unlink(instance_path)
76 except OSError:
77 pass
78 continue
79
80 if (
81 not pid
82 or not process_exists(pid)
83 or now - updated_at > INSTANCE_TIMEOUT_SEC
84 ):
85 try:
86 os.unlink(instance_path)
87 except OSError:
88 pass
89
90
91 def list_instances():
92 registry_dir = get_registry_dir()
93 instances = []
94 try:
95 instance_files = os.listdir(registry_dir)
96 except FileNotFoundError:
97 return instances
98
99 now = time.time()
100 for instance_name in instance_files:
101 instance_path = os.path.join(registry_dir, instance_name)
102 try:
103 with open(instance_path, "r", encoding="utf-8") as instance_file:
104 instance_data = json.load(instance_file)
105 pid = int(instance_data.get("pid", 0))
106 updated_at = float(instance_data.get("updated_at", 0))
107 except (OSError, ValueError, TypeError, json.JSONDecodeError):
108 continue
109
110 if (
111 not pid
112 or not process_exists(pid)
113 or now - updated_at > INSTANCE_TIMEOUT_SEC
114 ):
115 try:
116 os.unlink(instance_path)
117 except OSError:
118 pass
119 continue
120 instances.append(instance_data)
121
122 return sorted(instances, key=lambda instance: int(instance.get("pid", 0)))