-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfile_unlock.py
More file actions
48 lines (37 loc) · 1.11 KB
/
file_unlock.py
File metadata and controls
48 lines (37 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python3
"""PostToolUse hook: release file lock after Edit/Write completes."""
import json
import os
import sys
from hashlib import md5
from pathlib import Path
LOCK_DIR = Path("/tmp/claude_file_locks")
def main():
try:
input_data = json.load(sys.stdin)
except (json.JSONDecodeError, EOFError):
print("{}")
return
tool_name = input_data.get("tool_name", "")
tool_input = input_data.get("tool_input", {})
if tool_name not in ("Edit", "Write"):
print("{}")
return
file_path = tool_input.get("file_path", "")
if not file_path:
print("{}")
return
lock_key = md5(file_path.encode()).hexdigest()[:12]
lock_file = LOCK_DIR / f"{lock_key}.lock"
# Only release if we own the lock
if lock_file.exists():
try:
content = lock_file.read_text().strip()
locked_pid = int(content.split("|")[0])
if locked_pid == os.getpid():
lock_file.unlink()
except (ValueError, IndexError, OSError):
pass
print("{}")
if __name__ == "__main__":
main()