Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Github-Automation/colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class logcolors:
HEADER = '\033[95m'
BLUE = '\033[94m'
CYAN = '\033[96m'
SUCCESS = '\033[92m'
WARNING = '\033[93m'
ERROR = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
30 changes: 16 additions & 14 deletions Github-Automation/diffcalc.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
from colors import logcolors
from difflib import ndiff
from utils import getMaxSpaces

import difflib

def getMaxSpaces(file):
max = float('-inf')
for ele in file:
ele = ele.strip()
if(len(ele) > max):
max = len(ele)
return max

def calcDiff(firstFile , secondFile):
diff = difflib.ndiff(firstFile , secondFile)
def calcDiff(firstFile, secondFile):
# calculate raw diff
diff = ndiff(firstFile, secondFile)
# calculate unique lines in secondfile
deltainit = ''.join(x[2:] for x in diff if x.startswith('+ '))
# reformat the lines
deltainit = deltainit.split('\n')
maxspacesinit = getMaxSpaces(deltainit)
print('CHANGED LINES ARE:-\n' , '-' * maxspacesinit)
print(f'{logcolors.BOLD}CHANGED LINES ARE{logcolors.ENDC}\n',
f'{logcolors.BOLD}-{logcolors.ENDC}' * maxspacesinit)

for ele in deltainit:
print(str(ele.strip()) , ' ' * (maxspacesinit - len(ele.strip())), '+|')
print('' , '-' * maxspacesinit)

print(f'{logcolors.SUCCESS} {str(ele.strip())} {logcolors.ENDC}',
' ' * (maxspacesinit - len(ele.strip())), '+')

print('', f'{logcolors.BOLD}-{logcolors.ENDC}' * maxspacesinit)
return deltainit
75 changes: 37 additions & 38 deletions Github-Automation/filechange.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
import os
from os import listdir
from os.path import isfile, join
import gitcommands as git
import time
import diffcalc
from ignore import getIgnoreFiles
import logger
from utils import getNestedFiles,read_file,commitAndUpdate
from colors import logcolors
mypath = os.getcwd()
nestfiles = []
# add folders that you don't want to listen to
ignoredirs = ['.git' , '.idea' , '__pycache__' , 'node_modules']

ignoredirs = getIgnoreFiles()
print(ignoredirs)

# 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)
onlyfiles = getNestedFiles(mypath,ignoredirs)

# 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):

def ischanged(url, branch,*args,**kwargs):
changedfile = []
diffarr = []
# if uncommited data found perform git commands on them
initbuffer = kwargs.get('initbuffer' , -1)
if(initbuffer != -1):
for obj in initbuffer:
file = obj['path']
diff = obj['changes']
diffarr.append(diff)
changedfile.append(file)

# Performing Git Commands for changed files
commitAndUpdate(changedfile,diffarr,url,branch)
print('Listening for changes....')
initial = list(read_file())
initial = list(read_file(onlyfiles))
while True:
current = list(read_file())
current = list(read_file(onlyfiles))
changeditem = []
previtem = []
if(current != initial):
Expand All @@ -45,21 +46,19 @@ def ischanged(url , branch):
if ele not in initial:
changeditem.append(ele)
# calculating changed file's name
for i in range(0 ,len(changeditem)):
print('loop :-' , i)
for i in range(0, len(changeditem)):
print('loop :-', i)
changedfile.append(onlyfiles[current.index(changeditem[i])])
print('Changed file is:-' , changedfile,'\n')
print(f"Changed file is {logcolors.BOLD}{changedfile}{logcolors.ENDC}\n")

# Calculating Diff for previous and changed version of file
diffcalc.calcDiff(previtem , changeditem[0])
diff = diffcalc.calcDiff(previtem, changeditem[0])
diffarr.append(diff)
for file in changedfile:
logger.writedata(path=file, diff=diff)

# 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
# Performing Git Commands for changed files
commitAndUpdate(changedfile,diffarr,url,branch)

initial = current
# time.sleep(5)
44 changes: 30 additions & 14 deletions Github-Automation/gitcommands.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from subprocess import call
from sys import platform as _platform
from colors import logcolors


def init():
call('git init')
# git add <filename>


def createReadme():
if _platform == "linux" or _platform == "linux2":
call('touch README.md')
Expand All @@ -12,38 +16,50 @@ def createReadme():
call('type nul>README.md')


# Windows
def add(filelist):
for file in filelist:
# perform git add on file
print("Adding" , file)
print(f"{logcolors.SUCCESS}Adding{logcolors.ENDC}",
file.split('\\')[-1])
call(('git add ' + file))

# git commit -m "passed message"
def commit(filelist):


def commit(filelist,*args,**kwargs):
diffarr = kwargs.get('diffarr' , -1)
for file in filelist:
# ask user for commit message
msg = str(input('Enter the commit message for ' + file + ' or enter -r to reject commit'))
msg = str(input(f'{logcolors.BOLD}Enter the commit message for{logcolors.ENDC} ' +
file.split('\\')[-1] + f' {logcolors.BOLD}or enter {logcolors.ERROR}-r{logcolors.ENDC} to reject commit{logcolors.ENDC}'))
# if msg == -r reject commit
if(msg == '-r'):
print(f'{logcolors.ERROR}commit rejected{logcolors.ENDC}')
if(diffarr != -1):
diffarr.remove(diffarr[filelist.index(file)])
filelist.remove(file)
print('commit rejected')
call('cls', shell=True)
return False
# else execute git commit for the file
#added a comment
# added a comment
else:
filelist.remove(file)
call('git commit -m "' + msg + '"')
call('cls' , shell=True)

call('cls', shell=True)
print(
f'Commited {logcolors.CYAN}{file}{logcolors.ENDC} with msg: {logcolors.BOLD}{msg}{logcolors.ENDC}')


def setremote(url):
call('git remote add origin ' + url)



def setBranch(branch):
call('git branch -M ' + branch)

# git push
def push(url , branch):


def push(url, branch):
call('git push -u ' + url + ' ' + branch)
#added a comment
call('cls', shell=True)
print(f'{logcolors.SUCCESS}Successfully Pushed Changes{logcolors.ENDC}')
17 changes: 17 additions & 0 deletions Github-Automation/ignore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os

cwd = os.getcwd()
ignorepath = os.path.join(cwd, '.gitignore')


def getIgnoreFiles():
ignorefiles = []
with open(ignorepath) as ignore:
files = ignore.readlines()
for file in files:
file = ''.join(file.splitlines())
if(file != ''):
filepath = os.path.join(cwd , file)
if(os.path.isfile(filepath) or os.path.isdir(filepath)):
ignorefiles.append(file)
return ignorefiles
14 changes: 5 additions & 9 deletions Github-Automation/install.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
from shutil import copy , copyfile
import os
dir = os.getcwd()
files = []
# files = ['diffcalc.py' , 'main.py' , 'filechange.py' , 'gitcommands.py' , 'repoInfo.py']
for file in os.listdir(dir):
if(file.endswith('.py') and file != 'install.py'):
files.append(file)
print(files)

files = [file for file in os.listdir(dir) if file.endswith('.py') and file != 'install.py']
files.append('tmp.json')
print(files)
def checkForIgnore(dst):
return os.path.isfile(os.path.join(dst , '.gitignore'))

def addToIgnore(dst):
with open(os.path.join(dst , '.gitignore') , "a") as f:
f.write('\n/auto-scripts')
f.write('\nauto-scripts\n.idea\n__pycache__\n.git')
f.close()

def makeIgnore(dst):
f = open(os.path.join(dst , '.gitignore') , "x")
f.write('/auto-scripts')
f.write('auto-scripts\n.idea\n__pycache__\n.git')
f.close()

def copyfiles(file:str , dst:str):
Expand All @@ -33,7 +30,6 @@ def installfiles():
else:
print('.gitignore not found, creating one')
makeIgnore(location)
addToIgnore(location)
os.makedirs(os.path.join(location , 'auto-scripts'))
location = os.path.join(location , 'auto-scripts')
print('Installing Files')
Expand Down
60 changes: 60 additions & 0 deletions Github-Automation/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import json
import os
from colors import logcolors
import filechange
jsonpath = os.path.join(os.getcwd(), 'auto-scripts' , 'tmp.json')
buffer = []


def writedata(*args, **kwargs):
data = {}
global buffer
updatedbuffer = kwargs.get('buffer', -1)
path = kwargs.get('path', None)
diff = kwargs.get('diff', None)
if(updatedbuffer != -1):
buffer = updatedbuffer
with open(jsonpath, 'w') as file:
json.dump([obj for obj in buffer], file, indent=4)
elif(path and diff):
data['path'] = path
data['changes'] = diff
buffer.append(data)
with open(jsonpath, 'w') as file:
json.dump([obj for obj in buffer], file, indent=4)


def updatedata(filename, diffarr):
if(os.path.getsize(jsonpath) > 0):
with open(jsonpath, 'r') as file:
readdata = json.load(file)
if(len(readdata) == 0):
print('No changed file left')
else:
tmpdata,tmpfile,tmpdiff = readdata.copy(),filename.copy(),diffarr.copy()
print('Found some changed files')
for file,diff in zip(filename,diffarr):
print(f'Removing {str(file)} from json file')
for obj in readdata:
if obj['path'] == file and obj['changes'] == diff:
tmpdata.remove(obj)
tmpfile.remove(file)
tmpdiff.remove(diff)
# make the original lists empty without changing address
del filename[:],diffarr[:]
writedata(buffer=tmpdata)

else:
print('No data to read')


def checkdata(url , branch):
if(os.path.getsize(jsonpath) > 0):
with open(jsonpath, 'r') as file:
initdata = json.load(file)
if(len(initdata) == 0):
print(f'{logcolors.SUCCESS}Change tree clean{logcolors.ENDC}')
else:
filechange.ischanged(url , branch , initbuffer = initdata)
else:
print(f'{logcolors.ERROR}No changes found from previous session{logcolors.ENDC}')
31 changes: 16 additions & 15 deletions Github-Automation/main.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import repoInfo,filechange
import gitcommands as git
import repoInfo
from filechange import ischanged
from colors import logcolors
import pyfiglet
import logger
from utils import initCommands
def init():
info = repoInfo.checkinfoInDir()
url, branch = info
logger.checkdata(url , branch)
if('n' in info):
info.remove('n')
git.init()
git.createReadme()
git.add(['.'])
git.commit(['README.md'])
git.setBranch(info[1])
git.setremote(info[0])
git.push(info[0] , info[1])
print('initial setup done :)')
filechange.ischanged(info[0] , info[1])
initCommands(info)
else:
print('Retrieving info from git directory')
filechange.ischanged(info[0] , info[1])
print(f'{logcolors.BOLD}Retrieving info from git directory{logcolors.ENDC}')
print(f'{logcolors.CYAN}URL:{logcolors.ENDC} {url} , {logcolors.CYAN}Branch:{logcolors.ENDC} {branch}')
ischanged(url,branch)


if __name__ == '__main__':
init()
f = pyfiglet.figlet_format('G - AUTO', font='5lineoblique')
print(f"{logcolors.BOLD}{f}{logcolors.ENDC}")
init()
Loading