forked from avinashkranjan/Amazing-Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilechange.py
65 lines (60 loc) · 2.19 KB
/
filechange.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
import os
from os import listdir
from os.path import isfile, join
import gitcommands as git
import time
import diffcalc
mypath = os.getcwd()
nestfiles = []
# add folders that you don't want to listen to
ignoredirs = ['.git' , '.idea' , '__pycache__' , 'node_modules']
# gets the list of all nested files
def getNestedFiles(rootDir):
for path , subdirs , files in os.walk(rootDir):
if(all(ele not in path for ele in ignoredirs)):
for name in files:
nestfiles.append(join(path , name))
return nestfiles
onlyfiles = getNestedFiles(mypath)
# Reads and appends the contents of each file
def read_file():
filecontent = []
for file in onlyfiles:
with open(onlyfiles[onlyfiles.index(file)], "r") as f:
filecontent.append(f.readlines())
return filecontent
def ischanged(url , branch):
changedfile = []
print('Listening for changes....')
initial = list(read_file())
while True:
current = list(read_file())
changeditem = []
previtem = []
if(current != initial):
# Calculating Previous Version of File
for ele in initial:
if ele not in current:
for item in ele:
previtem.append(item)
# Calculating New Version of File
for ele in current:
if ele not in initial:
changeditem.append(ele)
# calculating changed file's name
for i in range(0 ,len(changeditem)):
print('loop :-' , i)
changedfile.append(onlyfiles[current.index(changeditem[i])])
print('Changed file is:-' , changedfile,'\n')
# Calculating Diff for previous and changed version of file
diffcalc.calcDiff(previtem , changeditem[0])
# Performing Git Commands For Changed File
# Performing Git Add
git.add(changedfile)
# Performing Git Commit
if(git.commit(changedfile) == False):
print('Reverting Push')
# Performing Git Push
elif(len(changedfile) == 0):
git.push(url , branch)
initial = current