-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcher.py
122 lines (105 loc) · 5.28 KB
/
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import re
import os
import time
import random
import subprocess
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
# for a safer side git pull before any other operation
os.popen("git pull")
class Watcher:
# set the Path of the directory to monitor
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):
counter = 0
@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)
elif event.event_type == 'modified':
GREEN = "\033[1;32m"
RED = "\033[1;31m"
RESET = "\033[0m"
# Taken any action here when a file is modified.
print("Received modified event - %s." % event.src_path)
# to extract specific file name from the event path
if '~' in event.src_path:
value = event.src_path.split("~")[-1]
else:
value = event.src_path.split("\\")[-1]
# to check which value gets printed
print(value)
# if the file is not Untitled then only perform operations
if value != 'Untitled.ipynb':
# get all the possible values such that it can related to changes
s = ["changes", "update", "modification","Made modifications", "Updated code", "Implemented alterations", "Tweaked the code", "Adjusted the program", "Modified the source code", "Refactored the code", "Edited", "Made adjustments"]
# pre_msg for commit messagfe
pre_msg = ["Added Some Code to","Modified","Changes in ", "Added new functionality to", "Introduced code changes to","Integrated new code into","Updated code in","Made changes to","Tweaked code in","Edited code in","Adjusted code in","Refactored code in","Revised code in","Reworked code in","Altered code in","Improved code in"]
# it returns the output of what all changes we have done to the file
diff_output = subprocess.check_output(['git', 'diff', f'{value}']).decode()
# this is the pattern to find def function present in it or not
pattern = r"def\s+(\w+)\("
# Use re.search() to find the first match of the pattern in the input string
match = re.findall(pattern, diff_output)
# set initial value to None
commit_message = None
# if a match is founf then change the commit_message with the function name
if match:
commit_message = f"{random.choice(pre_msg)} `{match[-1]}()` function"
Handler.counter = 0
# else select random from s and change the value of commit_message
else:
# commit_message = random.choice(s)
# check if the counter has reached 10
if Handler.counter == 10:
# reset the counter
Handler.counter = 0
# choose a random commit message from s
commit_message = random.choice(s)
else:
# increment the counter
Handler.counter += 1
# Handler.counter = 10
# print the commit message to see in terminal
k = f"{RED}You haven't made changes in the functions so your commit will be added after {10 - Handler.counter} changes in your file:({RESET}" if (10 - Handler.counter) > 0 else f"{GREEN}Your commit will be added now:){RESET}"
print(f"Counter: {Handler.counter}")
if commit_message != None:
print(f"commit message is {GREEN}'{commit_message}'{RESET}")
# now git add the file
os.popen(f'git add "{value}"')
# time.sleep so that there will be no load at once at cmd
time.sleep(1)
os.popen(f'git commit -m "{commit_message}"')
# finally git push 🥳
time.sleep(1)
os.popen("git push")
# os.popen("^C")
time.sleep(1)
# this timer such that it will again see in after 10 seconds
print(f"{GREEN}Git Pushed Successfully{RESET} 🥳")
else:
print(k)
time.sleep(10)
# if not untitled then send message
else:
print(f"{value} is common so it wouldn't be added Rename it to add")
# print("test")
if __name__ == '__main__':
w = Watcher()
w.run()