def markDifferences(pythonCode, prefix): ''' Esta função com a ajuda do "alter_make_transformations" consegue repro duzir o código de python com as tags de html necessária para identificar onde occoreu uma alteração em comparação com o ficheiro de python original ''' sys.path.append('../qom_questions_transformer') from text_transformer.tt_text_transformer_interface import clear from python_transformer.pt_util.pt_util_file import load_source_code_from_file make_transformations_text = alter_make_transformations() file1 = open('altered_make_transformations.py', 'w+') file1.writelines(make_transformations_text) file1.close() import os clear() import importlib.util spec = importlib.util.spec_from_file_location( "altered_make_transformations", os.getcwd() + "\\altered_make_transformations.py") foo = importlib.util.module_from_spec(spec) spec.loader.exec_module(foo) python_module = load_source_code_from_file(pythonCode) if os.path.isfile(prefix + 'seed.txt'): seedFile = get_file_content(prefix + 'seed.txt') seed(int(seedFile)) foo.make_transformations() return python_module.to_string()
def find_general_files_to_change(): sys.path.append('../qom_questions_transformer') from python_transformer.pt_util.pt_util_file import load_source_code_from_file ldText = load_source_code_from_file('make_random_versions.py') arrLdText = ldText.to_string().split('\n') found = False generalFile = [] general_files_to_change_list = [] for line in arrLdText: if found: if ']' in line: found = False continue else: splittedLine = line.split() for word in splittedLine: if word.strip(): generalFile.append(word) if 'general_files_to_change_list = [' in line: found = True continue for file in generalFile: words = re.split(',', file) for word in words: if word.strip(): general_files_to_change_list.append(get_name_from_str(word)) return general_files_to_change_list
def alter_make_transformations(): ''' Esta função percorre o código de "make_transformations.py" adicionando as tag html nas funções change_all_occurrences e change_token_all_occurrences com a ajuda da função substitute_in_line() ''' sys.path.append('../qom_questions_transformer') from text_transformer.tt_text_transformer_interface import clear from python_transformer.pt_util.pt_util_file import load_source_code_from_file pythonCode = load_source_code_from_file('make_transformations.py') pyLines = pythonCode.to_string().split('\n') newLines = [] for line in pyLines: if 'change_all_occurrences' in line: result = (substitute_in_line(line)) newLines.append(result) elif 'change_token_all_occurrences' in line: result = (substitute_in_line(line)) newLines.append(result) else: newLines.append(line) text = '' for pyLine in newLines: if '' == pyLine: text += '\n' else: text += pyLine + '\n' return text
def alter_full_program(prefix): sys.path.append('../qom_questions_transformer') from text_transformer.tt_text_transformer_interface import clear from python_transformer.pt_util.pt_util_file import load_source_code_from_file clear() import importlib.util spec = importlib.util.spec_from_file_location( "make_transformations", os.getcwd() + "\\make_transformations.py") foo = importlib.util.module_from_spec(spec) spec.loader.exec_module(foo) python_module = load_source_code_from_file('full_program.py') if os.path.isfile(os.getcwd() + "\\" + prefix + 'seed.txt'): seedFile = get_file_content(os.getcwd() + "\\" + prefix + 'seed.txt') seed(int(seedFile)) else: if os.path.isfile('seed.txt'): seedFile = get_file_content('seed.txt') seed(int(seedFile)) else: seed_value = randint(1000, 9999) create_file('seed.txt', str(seed_value)) seed(int(seed_value)) foo.make_transformations() create_file('altered_full_program.py', python_module.to_string())
def simulateTex_values(text, prefix, seed_value): ''' Esta função com a ajuda do "alter_make_transformations" consegue repro duzir o código de python com as tags de html necessária para identificar onde occoreu uma alteração em comparação com o ficheiro de python original ''' sys.path.append('../qom_questions_transformer') from text_transformer.tt_text_transformer_interface import clear, load_text from python_transformer.pt_util.pt_util_file import load_source_code_from_file make_transformations_text = alter_make_transformations_for_code() file1 = open('altered_make_transformations_for_code.py','w+') file1.writelines(make_transformations_text) file1.close() clear() import importlib.util spec = importlib.util.spec_from_file_location("altered_make_transformations_for_code", os.getcwd() + "\\altered_make_transformations_for_code.py") foo = importlib.util.module_from_spec(spec) spec.loader.exec_module(foo) python_module = load_source_code_from_file('full_program.py') text_module = load_text(text) seed(int(seed_value)) foo.make_transformations() python_module.execute() foo.make_transformations_on_results(python_module) final_text = text_module.to_string() create_file('simulated_tex.tex', final_text)
def simulateTex_values(text, prefix, seed_value): ''' Esta função com a ajuda do "alter_make_transformations" consegue repro duzir o código de python com as tags de html necessária para identificar onde occoreu uma alteração em comparação com o ficheiro de python original ''' sys.path.append('../qom_questions_transformer') from text_transformer.tt_text_transformer_interface import clear, load_text from python_transformer.pt_util.pt_util_file import load_source_code_from_file make_transformations_text = alter_make_transformations_for_code() file1 = open('altered_make_transformations_for_code.py','w+') file1.writelines(make_transformations_text) file1.close() clear() import importlib.util spec = importlib.util.spec_from_file_location("altered_make_transformations_for_code", os.getcwd() + "\\altered_make_transformations_for_code.py") foo = importlib.util.module_from_spec(spec) spec.loader.exec_module(foo) python_module = load_source_code_from_file('full_program.py') text_module = load_text(text) seed(int(seed_value)) foo.make_transformations() python_module.execute() foo.make_transformations_on_results(python_module) text3 = text_module.to_string() ## lines1 = re.split('\n|\n\n',text) ## lines2 = re.split('\n|\n\n',text3) ## final_text = '' ## for i in range(len(lines1)): ## text1 = format_string(lines1[i].split(" ")) ## text2 = format_string(lines2[i].split(" ")) ## for i in range(len(text1)): ## if text1[i] != text2[i]: ## if '<b><i>' in text1[i]: ## final_text += text1[i] + " " ## continue ## final_text += '<b><i>' + text2[i] + '</i></b> ' ## else: ## final_text += text2[i] + " " ## final_text+= '\n' final_text = text3 create_file('simulated_tex.tex', final_text)
def make_random_versions(general_files_to_change_list, python_files_list, programs_to_run, seed_filename, make_transformations_script_filename, number_of_versions, version_path_prefix, write_seed_flag, true_or_false_alternative_answers, fixed_files_list): # execution steps # added support for different python program saved in different # files 2017/11/17 # added python stuff 2017/10/27 # new 2017/09/25 # # 0. import the function make_transformations from the # make_transformations_script_filename file # 1. for each text file # 1a. read text file # 1b. load the text file in the text manager storing the returning part # 2. for each python file # 2a. read the python file # 2b. create the correspondent meta python object # 2c. for each configuration program # 2ci. create the corresponding module # 2cii. execute it # 2ciii. saved the program and output # 2civ. confirm the answers output comparing it with the answers tex files # 3. for each version: # 3a. call the make_transformations function # 3b. write the managed texts to the version path # 3c. select true or false answers # 0 cmd = "from " + make_transformations_script_filename \ + " import make_transformations" print(cmd) exec("from " + make_transformations_script_filename + " import make_transformations") exec("from " + make_transformations_script_filename + " import make_transformations_on_results") from make_transformations import make_transformations from make_transformations import make_transformations_on_results # 1. read text files text_parts_list = [] for filename in general_files_to_change_list: text = get_file_content(filename) text_part = load_text(text) text_parts_list.append(text_part) # 2. read python files python_modules_list = [] for filename in python_files_list: python_module = load_source_code_from_file(filename) python_modules_list.append(python_module) print("---------") print(python_module.to_string()) #print("+++++++") #print(type(python_module)) # 2c. for each configured program # 2ci. create the corresponding module for n in range(len(programs_to_run)): program_to_run = programs_to_run[n] program_files_list = program_to_run[0] full_program = PTSourceCode() for python_file in program_files_list: index = python_files_list.index(python_file) python_module = python_modules_list[index] full_program.append(python_module) print("+++++++") print(full_program.to_string()) programs_to_run[n].append(full_program) # 2cii. execute it # 2ciii. saved the program and output version_path = "" # to write in the current (main) directory for program_to_run in programs_to_run: full_program_file = program_to_run[1] output_file = program_to_run[2] full_program = program_to_run[3] write_full_program(full_program.to_string(), version_path, full_program_file) (out, err, excp) = full_program.execute() write_output(out, err, excp, version_path, output_file) # just to change the current 20 in the first version randint(10, 20) # 3. for each version: for version in range(number_of_versions): version_path = version_path_prefix + str(version + 1) if not os.path.exists(version_path): os.makedirs(version_path) # 3a. if write_seed_flag == True: a_seed = randint(1000, 9999) write_seed(a_seed, version_path, seed_filename) else: a_seed = read_seed(version_path, seed_filename) seed(a_seed) make_transformations() for program_to_run in programs_to_run: full_program_file = program_to_run[1] output_file = program_to_run[2] full_program = program_to_run[3] write_full_program(full_program.to_string(), version_path, full_program_file) (out, err, excp) = full_program.execute() print(out + err + excp) write_output(out, err, excp, version_path, output_file) if len(programs_to_run) > 0: make_transformations_on_results(full_program) # 3b. # write the update texts to files write_text_files(version_path, general_files_to_change_list, text_parts_list) write_python_files(python_files_list, python_modules_list, version_path) # to avoid versions dependencies fixed files should be copied # and no linked #link_fixed_files(fixed_files_list, version_path) copy_fixed_files(fixed_files_list, version_path) # 3c. select true or false answers # for answer in range(1, 6): # if choice([True, False]) == True: # command = "cp " + version_path + "/answer_" + str(answer) \ # + "_true.tex " + version_path + "/answer_" \ # + str(answer) + ".tex" # else: # command = "cp " + version_path + "/answer_" + str(answer) \ # + "_false.tex " + version_path + "/answer_" \ # + str(answer) + ".tex" # print(command) # os.system(command) #print('############') #print(os.getcwd()) for (true_file, false_file, destination_file) \ in true_or_false_alternative_answers: if choice([True, False]) == True: source_file = true_file else: source_file = false_file # source_path = os.path.join(os.getcwd(), version_path, # source_file) # destination_path = os.path.join(os.getcwd(), version_path, # destination_file) source_path = Path.cwd() / version_path / source_file destination_path = Path.cwd() / version_path / destination_file #curret_dir_fd = os.open(os.getcwd(), os.O_RDONLY) #print('----------- ', curret_dir_fd) #print('----------- ', source_path) #print('----------- ', destination_path) if destination_path.exists(): destination_path.unlink() # path_descriptor = os.open(version_path, os.O_RDONLY) # os.symlink(source_file, # destination_file, # dir_fd=path_descriptor) # #dir_fd=curret_dir_fd) os.symlink(source_path, destination_path)