Example #1
0
def remove_occurances(path, file_name):
    full_file_name = path + file_name
    if (not filemanagement.fileexist(full_file_name)):
        print 'File ' + full_file_name + ' is not found.'
        print 'Should not happen! Ever!'
        quit()

    full_temporary_file_name = path + temporaryfilename()
    temporary_file = open(full_temporary_file_name, 'w')
    original_file = open(full_file_name, 'r')
    next_occurance_id = 0
    removing_statement = False
    if (len(occurances) == next_occurance_id):
        return
    next_occurance = occurances[next_occurance_id]
    next_occurance_id += 1
    for line_nr, line in enumerate(original_file):
        if (line_nr == next_occurance):
            removing_statement = True
            if (len(occurances) == next_occurance_id):
                next_occurance_id = -1
            else:
                next_occurance = occurances[next_occurance_id]
                next_occurance_id += 1

        if (not removing_statement):
            temporary_file.writelines(line)

        if (endofstatement(line)):
            removing_statement = False

    temporary_file.close()
    original_file.close()
    filemanagement.copyfile(full_file_name, full_temporary_file_name)
    filemanagement.deletefile(full_temporary_file_name)
Example #2
0
def updatefile(path, old_file_name):
    full_old_file_name = path + old_file_name
    if (not filemanagement.fileexist(full_old_file_name)):
        print 'File ' + full_old_file_name + ' is not found.'
        print 'Should not happen! Ever!'
        quit()

    full_temporary_file_name = path + temporaryfilename(old_file_name)

    # Make sure that the files are closed by putting them out of scope
    old_file = open(full_old_file_name,'r')
    temporary_file = open(full_temporary_file_name,'w')

    temporary_file.writelines(fileheaderasstring())
    remove_whitespaces = True
    for line in old_file:
        if (remove_whitespaces and (len(line.split()) == 0)):
            continue
        else:
            remove_whitespaces = False
        temporary_file.writelines(line)
    old_file.close()
    temporary_file.close()

    filemanagement.copyfile(full_old_file_name,full_temporary_file_name)
    filemanagement.deletefile(full_temporary_file_name)
Example #3
0
def would_be_removed_occurances(path, file_name):
    full_file_name = path + file_name
    if (not filemanagement.fileexist(full_file_name)):
        print 'File ' + full_file_name + ' is not found.'
        print 'Should not happen! Ever!'
        quit()

    original_file = open(full_file_name, 'r')
    removing_statement = False
    next_occurance_id = 0
    if (len(occurances) == next_occurance_id):
        return
    next_occurance = occurances[next_occurance_id]
    next_occurance_id += 1
    for line_nr, line in enumerate(original_file):
        if (line_nr == next_occurance):
            removing_statement = True
            if (len(occurances) == next_occurance_id):
                return
            next_occurance = occurances[next_occurance_id]
            next_occurance_id += 1

        if (removing_statement):
            print line_nr

        if (endofstatement(line)):
            removing_statement = False
            if (next_occurance == -1):
                break
    original_file.close()
Example #4
0
def findheader(path, file_name):
    full_file_name = path + file_name
    if (not filemanagement.fileexist(full_file_name)):
        print 'File ' + file_name + ' not found!'
        print 'Unexpected error!'
        quit()
    file_handle = open(full_file_name)
    template_file_content = fileheaderasarray()
    compare_content = []
    # load the same number of lines from file as the fileheader
    for index in range(len(template_file_content)):
        line = file_handle.readline()
        if (line == ''):
            return False
        compare_content.append(line)

    while (True):
        found = True
        for index in range(len(template_file_content)):
            line1 = template_file_content[index]
            line2 = compare_content[index]
            if(line1 != line2):
                found = False
                break
        if (found):
            return True
        compare_content = compare_content[1:len(compare_content)]
        line = file_handle.readline()
        if (line == ''):
            return False
        compare_content.append(line)
    return False
def remove_occurances(path, file_name):
    full_file_name = path + file_name
    if (not filemanagement.fileexist(full_file_name)):
        print 'File ' + full_file_name + ' is not found.'
        print 'Should not happen! Ever!'
        quit()

    full_temporary_file_name = path + temporaryfilename()
    temporary_file = open(full_temporary_file_name,'w')
    original_file = open(full_file_name,'r')
    next_occurance_id = 0;
    removing_statement = False
    if(len(occurances) == next_occurance_id):
        return
    next_occurance = occurances[next_occurance_id]
    next_occurance_id += 1
    for line_nr, line in enumerate(original_file):
        if(line_nr == next_occurance):
            removing_statement = True
            if(len(occurances) == next_occurance_id):
                next_occurance_id = -1
            else:
                next_occurance = occurances[next_occurance_id]
                next_occurance_id += 1

        if (not removing_statement):
            temporary_file.writelines(line)

        if(endofstatement(line)):
            removing_statement = False;

    temporary_file.close()
    original_file.close()
    filemanagement.copyfile(full_file_name,full_temporary_file_name)
    filemanagement.deletefile(full_temporary_file_name)
def would_be_removed_occurances(path, file_name):
    full_file_name = path + file_name
    if (not filemanagement.fileexist(full_file_name)):
        print 'File ' + full_file_name + ' is not found.'
        print 'Should not happen! Ever!'
        quit()

    original_file = open(full_file_name,'r')
    removing_statement = False
    next_occurance_id = 0;
    if(len(occurances) == next_occurance_id):
        return
    next_occurance = occurances[next_occurance_id]
    next_occurance_id += 1
    for line_nr, line in enumerate(original_file):
        if(line_nr == next_occurance):
            removing_statement = True
            if(len(occurances) == next_occurance_id):
                return
            next_occurance = occurances[next_occurance_id]
            next_occurance_id += 1

        if (removing_statement):
            print line_nr

        if(endofstatement(line)):
            removing_statement = False;
            if(next_occurance == -1):
                break
    original_file.close()
Example #7
0
def fileheaderasarray():
    template_file_name = 'license_template.txt'
    if (not filemanagement.fileexist(template_file_name)):
        print 'File ' + template_file_name + ' not found!'
        quit()
    template_file = open(template_file_name,'r')
    return_value = []
    for line in template_file:
        return_value.append(line)
    return return_value
Example #8
0
def fileheaderasstring():
    template_file_name = 'license_template.txt'
    if (not filemanagement.fileexist(template_file_name)):
        print 'File ' + template_file_name + ' not found!'
        quit()
    template_file = open(template_file_name,'r')
    return_string = ''
    for line in template_file:
        return_string += line
    return return_string