diff --git a/Github-Automation/colors.py b/Github-Automation/colors.py new file mode 100644 index 0000000000..c7e7c084f6 --- /dev/null +++ b/Github-Automation/colors.py @@ -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' diff --git a/Github-Automation/diffcalc.py b/Github-Automation/diffcalc.py index 8dce2e7e4a..53fe0c9d99 100644 --- a/Github-Automation/diffcalc.py +++ b/Github-Automation/diffcalc.py @@ -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) - \ No newline at end of file + print(f'{logcolors.SUCCESS} {str(ele.strip())} {logcolors.ENDC}', + ' ' * (maxspacesinit - len(ele.strip())), '+') + + print('', f'{logcolors.BOLD}-{logcolors.ENDC}' * maxspacesinit) + return deltainit diff --git a/Github-Automation/filechange.py b/Github-Automation/filechange.py index ff9700d70b..b41813eb0f 100644 --- a/Github-Automation/filechange.py +++ b/Github-Automation/filechange.py @@ -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): @@ -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) diff --git a/Github-Automation/gitcommands.py b/Github-Automation/gitcommands.py index d1818de574..a1cb7ab891 100644 --- a/Github-Automation/gitcommands.py +++ b/Github-Automation/gitcommands.py @@ -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 + + def createReadme(): if _platform == "linux" or _platform == "linux2": call('touch README.md') @@ -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}') diff --git a/Github-Automation/ignore.py b/Github-Automation/ignore.py new file mode 100644 index 0000000000..46f3d16a26 --- /dev/null +++ b/Github-Automation/ignore.py @@ -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 diff --git a/Github-Automation/install.py b/Github-Automation/install.py index c89b05f5eb..df6153522d 100644 --- a/Github-Automation/install.py +++ b/Github-Automation/install.py @@ -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): @@ -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') diff --git a/Github-Automation/logger.py b/Github-Automation/logger.py new file mode 100644 index 0000000000..8311ba9240 --- /dev/null +++ b/Github-Automation/logger.py @@ -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}') diff --git a/Github-Automation/main.py b/Github-Automation/main.py index f3c38cb6a2..1a50adc98e 100644 --- a/Github-Automation/main.py +++ b/Github-Automation/main.py @@ -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() \ No newline at end of file + f = pyfiglet.figlet_format('G - AUTO', font='5lineoblique') + print(f"{logcolors.BOLD}{f}{logcolors.ENDC}") + init() diff --git a/Github-Automation/repoInfo.py b/Github-Automation/repoInfo.py index a579062864..81c49bfba6 100644 --- a/Github-Automation/repoInfo.py +++ b/Github-Automation/repoInfo.py @@ -1,29 +1,27 @@ +import subprocess import os mypath = os.getcwd() infofile = mypath + '/.git/config' + def takeInfo(): print('No Existing repo info found\n') url = str(input('Enter the Github Repo URL: ')) branch = str(input('Enter the branch: ')) - info = ['n' , url , branch] + info = ['n', url, branch] return info + def checkinfoInDir(): if (os.path.exists(infofile)): - print('Repo Info Found:-') - with open(infofile, "r") as f: - info = f.readlines() - # print(info) - for ele in info: - if('url' in ele): - url = info[info.index(ele)].split()[2] + url = subprocess.Popen('git config --get remote.origin.url', + stdout=subprocess.PIPE).stdout.read().decode('utf-8') - if('branch' in ele): - branch = info[info.index(ele)].split()[1].split('"')[1] - info = [url , branch] + branch = subprocess.Popen('git rev-parse --symbolic-full-name HEAD', + stdout=subprocess.PIPE).stdout.read().decode('utf-8') + + url, branch = url.split('\n')[0], branch.split('\n')[0].split('/')[2] + info = [url, branch] else: info = takeInfo() return info - -print(checkinfoInDir()) \ No newline at end of file diff --git a/Github-Automation/utils.py b/Github-Automation/utils.py new file mode 100644 index 0000000000..d47591279a --- /dev/null +++ b/Github-Automation/utils.py @@ -0,0 +1,54 @@ +def getMaxSpaces(file): + max = float('-inf') + for ele in file: + ele = ele.strip() + if(len(ele) > max): + max = len(ele) + return max + +def getNestedFiles(rootDir,ignoredirs): + from os import walk + from os.path import join + nestfiles = [] + for path, subdirs, files in walk(rootDir): + if(all(ele not in path for ele in ignoredirs)): + for name in files: + nestfiles.append(join(path, name)) + return nestfiles + + +def read_file(onlyfiles): + filecontent = [] + for file in onlyfiles: + with open(onlyfiles[onlyfiles.index(file)], "r") as f: + filecontent.append(f.readlines()) + return filecontent + +def initCommands(info): + import gitcommands as git + import filechange + url,branch = info + info.remove('n') + git.init() + git.createReadme() + git.add(['.']) + git.commit(['README.md']) + git.setBranch(branch) + git.setremote(url) + git.push(url, branch) + print('initial setup done :)') + filechange.ischanged(url, branch) + +def commitAndUpdate(changedfile,diffarr,url,branch): + from gitcommands import commit,push,add + from logger import updatedata + from colors import logcolors + add(changedfile) + if(commit(changedfile,diffarr) == False): + print(f'{logcolors.ERROR}Reverting Push{logcolors.ENDC}') + # updatedata(changedfile, diffarr) + else: + print(f'{logcolors.SUCCESS}Updating Logs{logcolors.ENDC}') + updatedata(changedfile, diffarr) + if(len(changedfile) == 0): + push(url, branch) \ No newline at end of file