-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatch_cpp_folder.py
More file actions
79 lines (63 loc) · 2.22 KB
/
watch_cpp_folder.py
File metadata and controls
79 lines (63 loc) · 2.22 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import time
import subprocess
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import os
import glob
# Absolute or relative path to your folder (adjust as needed for Windows)
WATCHED_FOLDER = os.path.abspath("cpp") # e.g., "C:/cpp"
class ChangeHandler(FileSystemEventHandler):
def __init__(self):
self.last_copy_time = 0
self.copy_cooldown = 1
def on_any_event(self, event):
current_time = time.time()
if current_time - self.last_copy_time < self.copy_cooldown:
return
if event.is_directory:
return
if not event.src_path.startswith(WATCHED_FOLDER):
return
if not event.src_path.endswith(".cpp"):
return
print(f"Detected change: {event.event_type} - {event.src_path}")
try:
self.copy_cpp_files()
print("Docker copy of .cpp files successful.")
self.last_copy_time = current_time
except subprocess.CalledProcessError as e:
print(f"Error during Docker copy: {e}")
try:
self.create_test_file()
print("Test file created.")
except subprocess.CalledProcessError as e:
print(f"Error during test file creation: {e}")
print("\n\n\n")
def copy_cpp_files(self):
cpp_files = glob.glob(os.path.join(WATCHED_FOLDER, "*.cpp"))
for cpp_file in cpp_files:
docker_command = ["docker", "cp", cpp_file, "image-editor-wasm-1:/app/cpp/"]
subprocess.run(docker_command, check=True)
def create_test_file(self):
docker_command = [
"docker",
"exec",
"-it",
"image-editor-wasm-1",
"bash",
"-c",
"touch /app/cpp/test.txt",
]
subprocess.run(docker_command, check=True)
if __name__ == "__main__":
event_handler = ChangeHandler()
observer = Observer()
observer.schedule(event_handler, path=WATCHED_FOLDER, recursive=True)
print(f"Watching folder: {WATCHED_FOLDER} for .cpp files")
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()