-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsafer_file_old_watcher.py
86 lines (70 loc) · 2.57 KB
/
safer_file_old_watcher.py
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
80
81
82
83
84
85
86
import os
import time
import random
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
os.popen("git pull")
class Watcher:
DIRECTORY_TO_WATCH = "C:\GFG_DataScience\DSA"
def __init__(self):
self.observer = Observer()
def run(self):
event_handler = Handler()
self.observer.schedule(event_handler, self.DIRECTORY_TO_WATCH, recursive=False)
self.observer.start()
try:
while True:
time.sleep(5)
except:
self.observer.stop()
print("Error")
self.observer.join()
class Handler(FileSystemEventHandler):
@staticmethod
def on_any_event(event):
if event.is_directory:
return None
elif event.event_type == 'created':
# Take any action here when a file is first created.
print("Received created event - %s." % event.src_path)
os.popen('git add .\img')
time.sleep(1)
time.sleep(1)
if '~' in event.src_path:
value = event.src_path.split("~")[-1]
else:
value = event.src_path.split("\\")[-1]
print("created")
os.popen(f"git commit -m Created {value}")
time.sleep(1)
os.popen("git push")
elif event.event_type == 'modified':
# Taken any action here when a file is modified.
print("Received modified event - %s." % event.src_path)
# event_name = event.src_path.split("~")
# print("Event Name",event_name[-1])
# value = "Problem Solving.ipynb"
# value2 = event.src_path.split("~")[-1]
if '~' in event.src_path:
value = event.src_path.split("~")[-1]
else:
value = event.src_path.split("\\")[-1]
print(value)
# print("Experiment name:" , value2)
if value != 'Untitled.ipynb':
print("GIt adding for: ", value)
else:
print(f"{value} is common so it wouldn't be added Rename it to add")
if value != 'Untitled.ipynb':
os.popen(f'git add "{value}"')
time.sleep(1)
time.sleep(1)
s = ["changes", "update", "modification"]
os.popen(f"git commit -m {random.choice(s)}")
time.sleep(1)
os.popen("git push")
# os.popen("^C")
time.sleep(10)
if __name__ == '__main__':
w = Watcher()
w.run()