g3w-admin-plugins 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,100 @@
1
+ import contextlib
2
+ import ctypes
3
+ from ctypes import wintypes
4
+ import errno
5
+ import hashlib
6
+ import os
7
+ import subprocess
8
+ import time
9
+
10
+
11
+ kernel = ctypes.WinDLL("kernel32", use_last_error=True)
12
+ kernel.CreateSemaphoreW.argtypes = [ctypes.c_void_p, wintypes.LONG, wintypes.LONG, wintypes.LPCWSTR]
13
+ kernel.CreateSemaphoreW.restype = wintypes.HANDLE
14
+ kernel.CreateMutexW.argtypes = [ctypes.c_void_p, wintypes.BOOL, wintypes.LPCWSTR]
15
+ kernel.CreateMutexW.restype = wintypes.HANDLE
16
+ kernel.WaitForSingleObject.argtypes = [wintypes.HANDLE, wintypes.DWORD]
17
+ kernel.WaitForSingleObject.restype = wintypes.DWORD
18
+ kernel.ReleaseSemaphore.argtypes = [wintypes.HANDLE, wintypes.LONG, ctypes.c_void_p]
19
+ kernel.ReleaseSemaphore.restype = wintypes.BOOL
20
+ kernel.ReleaseMutex.argtypes = [wintypes.HANDLE]
21
+ kernel.ReleaseMutex.restype = wintypes.BOOL
22
+ kernel.CloseHandle.argtypes = [wintypes.HANDLE]
23
+ kernel.CloseHandle.restype = wintypes.BOOL
24
+
25
+
26
+ def close(handle, release=False):
27
+ try:
28
+ if release and not kernel.ReleaseSemaphore(handle, 1, None):
29
+ raise ctypes.WinError(ctypes.get_last_error())
30
+ finally:
31
+ if not kernel.CloseHandle(handle):
32
+ raise ctypes.WinError(ctypes.get_last_error())
33
+
34
+
35
+ @contextlib.contextmanager
36
+ def probe_gate(name):
37
+ handle = kernel.CreateMutexW(None, False, name + "-gate")
38
+ if not handle:
39
+ raise ctypes.WinError(ctypes.get_last_error())
40
+ try:
41
+ result = kernel.WaitForSingleObject(handle, 0xFFFFFFFF)
42
+ if result not in (0, 0x80):
43
+ raise ctypes.WinError(ctypes.get_last_error())
44
+ try:
45
+ yield
46
+ finally:
47
+ if not kernel.ReleaseMutex(handle):
48
+ raise ctypes.WinError(ctypes.get_last_error())
49
+ finally:
50
+ close(handle)
51
+
52
+
53
+ class Lock:
54
+ def __init__(self, root, name, blocking):
55
+ # ponytail: host-wide lock; distributed Windows deployments need a shared lock service.
56
+ identity = os.path.normcase(str(root)).encode("utf-8")
57
+ key = hashlib.sha256(identity).hexdigest()
58
+ name = f"Global\\g3w-plugins-{name}-{key}"
59
+ while True:
60
+ # Serialize probes so the last abandoned semaphore handle can close.
61
+ with probe_gate(name):
62
+ self.handle = kernel.CreateSemaphoreW(None, 1, 1, name)
63
+ if not self.handle:
64
+ raise ctypes.WinError(ctypes.get_last_error())
65
+ result = kernel.WaitForSingleObject(self.handle, 0)
66
+ if result == 0:
67
+ break
68
+ error = ctypes.get_last_error()
69
+ close(self.handle)
70
+ if result != 258:
71
+ raise ctypes.WinError(error)
72
+ if not blocking:
73
+ raise BlockingIOError(errno.EAGAIN, "Plugin operation is already running")
74
+ # Closing the handle lets an abandoned semaphore disappear after its last child exits.
75
+ time.sleep(0.1)
76
+
77
+ def __enter__(self):
78
+ return self
79
+
80
+ def __exit__(self, exc_type, exc_value, traceback):
81
+ if self.handle is not None:
82
+ close(self.handle, release=True)
83
+
84
+ def detach(self):
85
+ close(self.handle)
86
+ self.handle = None
87
+
88
+
89
+ @contextlib.contextmanager
90
+ def inherited(handles):
91
+ old = [(handle, os.get_handle_inheritable(handle)) for handle in handles]
92
+ try:
93
+ for handle, _ in old:
94
+ os.set_handle_inheritable(handle, True)
95
+ info = subprocess.STARTUPINFO()
96
+ info.lpAttributeList = {"handle_list": handles}
97
+ yield {"startupinfo": info, "close_fds": True}
98
+ finally:
99
+ for handle, inheritable in old:
100
+ os.set_handle_inheritable(handle, inheritable)