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