MOHAN KRISHNA

0 %
Mohan Krishna
Multimedia Professional
Ai & ML Researcher & Enthusiast
  • Residence:
    India
  • City:
    Vijayawada
  • Age:
    46
AI/ML Enthusiast. New Media Trainer, VFX Artist, Non Linear Video Editor, Graphic Designer, Sound Editor and iOS App Designer.
Telugu
English
Hindi
Tamil
Proficiency:
Graphic Design
Web Design
Video & VFX
Machine Learning
Artificial Intelligence
Digital Marketing
Areas of Interest:
Take a look at some of the things I love working on.
  • Non Linear Video Editing
  • Graphic Design
  • Web Design
  • Audio Editing
  • Content Management Systems
  • Python
  • Deep Learning
  • OpenCV
  • Image Classification

Github Automation

September 28, 2022

Installation:

from shutil import copy, copyfile
import os
dir = os.getcwd()
# http://www.pillalamarri.in/python/github-automation/
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('\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\n.idea\n__pycache__\n.git')
    f.close()


def copyfiles(file: str, dst: str):
    copy(file, dst)
    print('Installation Successful\n')


def installfiles():
    location = input('Enter installation directory: ')
    if (checkForIgnore(location)):
        print('.gitignore found')
        addToIgnore(location)
    else:
        print('.gitignore not found, creating one')
        makeIgnore(location)
    os.makedirs(os.path.join(location, 'auto-scripts'))
    location = os.path.join(location, 'auto-scripts')
    print('Installing Files')
    for file in files:
        print('Installing %s' % file)
        copyfiles(file, location)

# http://www.pillalamarri.in/python/github-automation/
installfiles()

Logger:

import json
import os
from colors import logcolors
import filechange
jsonpath = os.path.join(os.getcwd(), 'auto-scripts', 'tmp.json')
buffer = []
# http://www.pillalamarri.in/python/github-automation/

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}'
        )
# http://www.pillalamarri.in/python/github-automation/

Repo Info:

import subprocess
import os
mypath = os.getcwd()
infofile = mypath + '/.git/config'

# http://www.pillalamarri.in/python/github-automation/
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]
    return info


def checkinfoInDir():
    if (os.path.exists(infofile)):
        url = subprocess.Popen(
            'git config --get remote.origin.url',
            stdout=subprocess.PIPE).stdout.read().decode('utf-8')

        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
# http://www.pillalamarri.in/python/github-automation/

Utilities:

def getMaxSpaces(file):
    max = float('-inf')
    for ele in file:
        ele = ele.strip()
        if (len(ele) > max):
            max = len(ele)
    return max
# http://www.pillalamarri.in/python/github-automation/

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)
# http://www.pillalamarri.in/python/github-automation/

Ignore:

import os
# http://www.pillalamarri.in/python/github-automation/
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
# http://www.pillalamarri.in/python/github-automation/

Git Commands:

from subprocess import call
from sys import platform as _platform
from colors import logcolors

# http://www.pillalamarri.in/python/github-automation/
def init():
    call('git init')


def createReadme():
    if _platform == "linux" or _platform == "linux2":
        call('touch README.md')
    elif _platform == "darwin":
        call('touch README.md')
    elif _platform == "win32":
        call('type nul>README.md')


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


# git commit -m "passed message"


def commit(filelist, *args, **kwargs):
    diffarr = kwargs.get('diffarr', -1)
    for file in filelist:
        # ask user for commit message
        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)
            call('cls', shell=True)
            return False
        # else execute git commit for the file
        # added a comment
        else:
            call('git commit -m "' + msg + '"')
            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):
    call('git push -u ' + url + ' ' + branch)
    call('cls', shell=True)
    print(f'{logcolors.SUCCESS}Successfully Pushed Changes{logcolors.ENDC}')
# http://www.pillalamarri.in/python/github-automation/

File Change:

import os
import gitcommands as git
import diffcalc
from ignore import getIgnoreFiles
import logger
from utils import getNestedFiles, read_file, commitAndUpdate
from colors import logcolors
mypath = os.getcwd()
# http://www.pillalamarri.in/python/github-automation/
ignoredirs = getIgnoreFiles()
print(ignoredirs)

# gets the list of all nested files
onlyfiles = getNestedFiles(mypath, ignoredirs)


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(onlyfiles))
    while True:
        current = list(read_file(onlyfiles))
        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(
                f"Changed file is {logcolors.BOLD}{changedfile}{logcolors.ENDC}\n"
            )

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

            # Performing Git Commands for changed files
            commitAndUpdate(changedfile, diffarr, url, branch)

            initial = current
            # time.sleep(5)
# http://www.pillalamarri.in/python/github-automation/

Calculate Difference:

import os
import gitcommands as git
import diffcalc
from ignore import getIgnoreFiles
import logger
from utils import getNestedFiles, read_file, commitAndUpdate
from colors import logcolors
mypath = os.getcwd()
# http://www.pillalamarri.in/python/github-automation/
ignoredirs = getIgnoreFiles()
print(ignoredirs)

# gets the list of all nested files
onlyfiles = getNestedFiles(mypath, ignoredirs)


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(onlyfiles))
    while True:
        current = list(read_file(onlyfiles))
        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(
                f"Changed file is {logcolors.BOLD}{changedfile}{logcolors.ENDC}\n"
            )

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

            # Performing Git Commands for changed files
            commitAndUpdate(changedfile, diffarr, url, branch)

            initial = current
            # time.sleep(5)
# http://www.pillalamarri.in/python/github-automation/

Colors:

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'
# http://www.pillalamarri.in/python/github-automation/

Main Script:

import repoInfo
from filechange import ischanged
from colors import logcolors
import pyfiglet
import logger
from utils import initCommands

# http://www.pillalamarri.in/python/github-automation/
def init():
    info = repoInfo.checkinfoInDir()
    url, branch = info
    logger.checkdata(url, branch)
    if ('n' in info):
        initCommands(info)
    else:
        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__':
    f = pyfiglet.figlet_format('G - AUTO', font='5lineoblique')
    print(f"{logcolors.BOLD}{f}{logcolors.ENDC}")
    init()
# http://www.pillalamarri.in/python/github-automation/
Posted in PythonTags: