Tuesday, 26 June 2018

python - Split the file name from git diff


Program:

import sys   # For command line argument
import os.path # To check file existing

def generate_writefile(file_name):
    count = 1
    while True:                      # in True T must be capital
        file_name_new = file_name
        file_name_new += "_" + str(count)
        count += 1
        if not os.path.isfile(file_name_new):
            return file_name_new
        if count >=10 :
            print "Error while creating file"
            return None


def main():
    cmd_len = len(sys.argv)
    if(cmd_len <2):
        print "\t\tVel Error, provide the git file name\n\n"
        return

    name =  sys.argv[1]
    file_name = generate_writefile(sys.argv[1])
    if file_name == None:
        return
    file_write = open(file_name, 'w')
    if  not os.path.isfile(sys.argv[1]):
        print "Vel Error, provided file is not existing\n\n"
        return

    count_file = 0
    with open(sys.argv[1]) as openfileobject:
        for line in openfileobject:
            spl_for_com = line.split()      # convert the list of array into an separated string
            length = len(spl_for_com)
            if((length >=4) and (spl_for_com[0] == "diff") and (spl_for_com[1] == "--git")):
#        print "Got the match velraj ", spl_for_com[2][1:], spl_for_com[3]
                file_write.write(spl_for_com[2][1:])
                file_write.write("\n")
                count_file += 1
    print "Number of file = ", str(count_file), "Output file name is = ", file_name

    if count_file == 0:
        os.remove(file_name)

main()


# API to read from file.
    #    message = file_read.read()   # It read a whole file in  1 time
    #    message = file_read.readline()  # Its read only single line

No comments:

Post a Comment