diff --git a/heroku_deploy/api/Dockerfile.api b/heroku_deploy/api/Dockerfile.api index d3be497..29bae4c 100644 --- a/heroku_deploy/api/Dockerfile.api +++ b/heroku_deploy/api/Dockerfile.api @@ -38,8 +38,11 @@ WORKDIR /usr/src/app COPY --from=builder /usr/src/app/package*.json /usr/src/app/ COPY --from=builder /usr/src/app/dist/ /usr/src/app/ -ENV LIBS_DIR=/usr/src/app/libs +ENV LIBS_DIR=/usr/src/app/ +ENV ALGO_DIR=/usr/src/app/evaluation_code + COPY ./libs ${LIBS_DIR} +COPY ./evaluation_code ${ALGO_DIR} COPY ./conf /usr/src/app/conf diff --git a/heroku_deploy/api/evaluation_code/README.md b/heroku_deploy/api/evaluation_code/README.md new file mode 100644 index 0000000..3188577 --- /dev/null +++ b/heroku_deploy/api/evaluation_code/README.md @@ -0,0 +1 @@ +"# Algo-valuation-code" diff --git a/heroku_deploy/api/evaluation_code/cpp/__pycache__/evalCommentaireCpp.cpython-39.pyc b/heroku_deploy/api/evaluation_code/cpp/__pycache__/evalCommentaireCpp.cpython-39.pyc new file mode 100644 index 0000000..774ddc4 Binary files /dev/null and b/heroku_deploy/api/evaluation_code/cpp/__pycache__/evalCommentaireCpp.cpython-39.pyc differ diff --git a/heroku_deploy/api/evaluation_code/cpp/__pycache__/evalNbLigneFonctionCpp.cpython-39.pyc b/heroku_deploy/api/evaluation_code/cpp/__pycache__/evalNbLigneFonctionCpp.cpython-39.pyc new file mode 100644 index 0000000..dd3566a Binary files /dev/null and b/heroku_deploy/api/evaluation_code/cpp/__pycache__/evalNbLigneFonctionCpp.cpython-39.pyc differ diff --git a/heroku_deploy/api/evaluation_code/cpp/__pycache__/evalRedondanceCpp.cpython-39.pyc b/heroku_deploy/api/evaluation_code/cpp/__pycache__/evalRedondanceCpp.cpython-39.pyc new file mode 100644 index 0000000..7d5fb62 Binary files /dev/null and b/heroku_deploy/api/evaluation_code/cpp/__pycache__/evalRedondanceCpp.cpython-39.pyc differ diff --git a/heroku_deploy/api/evaluation_code/cpp/__pycache__/evalVariableNameCpp.cpython-39.pyc b/heroku_deploy/api/evaluation_code/cpp/__pycache__/evalVariableNameCpp.cpython-39.pyc new file mode 100644 index 0000000..189bc65 Binary files /dev/null and b/heroku_deploy/api/evaluation_code/cpp/__pycache__/evalVariableNameCpp.cpython-39.pyc differ diff --git a/heroku_deploy/api/evaluation_code/cpp/evalCommentaire.py b/heroku_deploy/api/evaluation_code/cpp/evalCommentaire.py new file mode 100644 index 0000000..5f13b8d --- /dev/null +++ b/heroku_deploy/api/evaluation_code/cpp/evalCommentaire.py @@ -0,0 +1,38 @@ +def excecEvalCommentaire(lignes): + + scopeCodeUser = False + + nbLigne = 0 + nbComment = 0 + longComment = False + + for ligne in lignes: + + + if ligne != "": + nbLigne += 1 + + if '//' in ligne and not longComment: + nbComment +=1 + + if '/*' in ligne: + longComment = True + + if '*/' in ligne: + longComment = False + + if longComment: + nbComment += 1 + + + res = "" + if nbLigne >0: + + if (nbComment/nbLigne)*100 >= 10: + res = "ok" + else: + res = "error" + else: + res = "ok" + + return res diff --git a/heroku_deploy/api/evaluation_code/cpp/evalNbLigneFonction.py b/heroku_deploy/api/evaluation_code/cpp/evalNbLigneFonction.py new file mode 100644 index 0000000..eb733a4 --- /dev/null +++ b/heroku_deploy/api/evaluation_code/cpp/evalNbLigneFonction.py @@ -0,0 +1,149 @@ +import re + +PATERN_VARIABLE = [ + r'[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,}\s*', + r'[A-Za-z0-9_]{1,}\s{0,}<[A-Za-z0-9_]{1,}>\s{0,}[A-Za-z0-9_]{1,}', + r'[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,}', + + r'[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,}\s{0,};', + r'[A-Za-z0-9_]{1,}\s*<[A-Za-z0-9_]{1,}>\s*[A-Za-z0-9_]{0,};', + r'[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{0,};', +] + + + +def find_function(line): + """ + :param: line: représente le code à analyser + :return retourne les différents blocks représentant ce code (fonction while for if ..) + """ + + blockCodes = [] + i = 0 + cptAcollade = 0 + newBlock="" + addAblock = False + + while i < len(line): + + if line[i] == "{": + + cptAcollade += 1 + newBlock += line[i] + addAblock = True + i +=1 + + while cptAcollade != 0 and newBlock != "" and i < len(line): + + if line[i] == "{": + cptAcollade += 1 + + elif line[i] == "}": + cptAcollade -= 1 + + if addAblock: + newBlock += line[i] + + if cptAcollade == 0: + + blockCodes.append(newBlock) + newBlock = "" + i +=1 + + i +=1 + + return blockCodes + + + + + + + + + + + +def remove_comentary(lignes): + """ + :param liste_variable: représente la liste des variables du code + :return: retourne la liste des variables après avoir ajouté un espace après le type de la variable. Permet de différencier les types Matrice et collection + """ + long_comment = False + code_without_comentary = [] + + for ligne in lignes: + + if "//" in ligne: + tab_line = ligne.split("//") + code_without_comentary.append(tab_line[0]) + + elif "/*" in ligne and "*/" in ligne: + tab_line1 = ligne.split("/*") + tab_line2 = ligne.split("*/") + new_line = tab_line1[0] + tab_line2[len(tab_line2)-1] + code_without_comentary.append(new_line) + + elif "/*" in ligne: + tab_line = ligne.split("/*") + code_without_comentary.append(tab_line[0]) + long_comment = True + + elif "*/" in ligne: + tab_line = ligne.split("*/") + code_without_comentary.append(tab_line[len(tab_line)-1]) + long_comment = False + + + elif not long_comment: + code_without_comentary.append(ligne) + + + + return code_without_comentary + + +def excecEvalNbLigneFonction(lignes): + + listFunction = [] + listVariableRename = [] + lastListVariableRename = [] + + listVarBlock = [] + + scopeCodeUser = False + firstInsert = False + long_comment = False + + lignesCompacte = "" + newBlock = [] + functionCode = "" + + lignes = remove_comentary(lignes) + + for ligne in lignes: + + if "#include" not in ligne and "using namespace" not in ligne: + lignesCompacte +=ligne + + + listFunctionCode = find_function(lignesCompacte) + + cpt_error = 0 + for function in listFunctionCode: + function = function.replace(" ","") + cpt_ligne = function.count('\n')-1 + + + table_rm = function.split('\n') + cpt_rm = 0 + + for ligne in table_rm: + ligne = ligne.replace(" ","") + if len(ligne) ==0: + cpt_rm +=1 + + if cpt_ligne-cpt_rm > 30: + cpt_error+=1 + + return cpt_error diff --git a/heroku_deploy/api/evaluation_code/cpp/evalPlagiat.py b/heroku_deploy/api/evaluation_code/cpp/evalPlagiat.py new file mode 100644 index 0000000..7192f37 --- /dev/null +++ b/heroku_deploy/api/evaluation_code/cpp/evalPlagiat.py @@ -0,0 +1,699 @@ +import re + +PATERN_VARIABLE = [ + r'[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,}\s*', + r'[A-Za-z0-9_]{1,}\s{0,}<[A-Za-z0-9_]{1,}>\s{0,}[A-Za-z0-9_]{1,}', + r'[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,}', + + r'[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,}\s{0,};', + r'[A-Za-z0-9_]{1,}\s*<[A-Za-z0-9_]{1,}>\s*[A-Za-z0-9_]{0,};', + r'[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{0,};', +] + + + + + + +dico_stemming = { + "def": "d", + "while": "w", + "for": "f", + "elif": "ei", + "else": "e", + "<=": "c", + ">=": "c", + "<": "c", + ">": "c", + "!=": "!", + "==": "-", + "and": "&", + "or": "|", + "int": "i", + "bool": "bo", + "string": "s", + "long ": "l", + "char":"ch", + "float ": "fl", + "void": "v", + "double":"db", + "break":"b", + "case":"cs", + "try":"t", + "catch":"ct", + "class":"cl", + "continue":"cn", + "default":"df", + "const": "cst", + "delete": "dl" , + "enum":"en", + "private":"pri", + "protected":"pro", + "public":"pub", + "extern": "ex", + "friend": "fr", + "goto": "got", + "inline": "inl", + "new": "nw", + "operator": "op", + "register": "rg", + "return": "rt", + "short": "sh", + "signed": "sg", + "sizeof": "sz", + "static": "stc", + "struct": "str", + "switch": "sw", + "typedef": "td", + "template": "tmp", + "throw": "th", + "union": "un", + "unsigned": "uns", + "virtual": "vr", + "volatile": "vl", + + "alignas": "ag", + "alignof": "agn", + "and_eq": "&e", + "auto": "au", + "bitand": "btd", + "bitor": "bt", + "char16_t": "c16", + "char32_t": "c32", + + "constexpr": "cx", + "decltype": "dt", + "const_cast": "ca", + "export": "ep", + "thread_local": "tl", + "static_assert": "ss", + "reinterpret_cast": "ra", + "this": "ti", + "mutable": "mt", + "xor_eq": "xe", + "using,": "us", + "noexcept, ": "nx", + "not": "n", + "not_eq": "!p", + "nullptr": "nr", + "or_eq": "|p", + "true": "tr", + "false": "fs", + "typeid": "tp", + "xor": "xr", + ",": "", + +} + + +dico_supression = { + " ": "", + "{": "", + "}": "", + "(": "", + ")": "", + ";": "", + ":": "" +} + + +def delete_string(ligne): + delete = False + list_chain_to_remove = [] + + if ligne.count("\"") > 0: + + i = 0 + chain_to_remove = "" + + while i < len(ligne): + + if ligne[i] == '\"': + list_chain_to_remove.append(chain_to_remove) + chain_to_remove = "" + delete = not delete + + if delete and ligne[i] != '"': + chain_to_remove = chain_to_remove + ligne[i] + + i += 1 + + for chain in list_chain_to_remove: + ligne = ligne.replace(chain, "") + + ligne = ligne.replace("\"\"", "\"") + + return ligne + + +def replace_by_new_content(ligne): + for key in dico_stemming: + ligne = ligne.replace(key, dico_stemming[key]) + return ligne + + +def delete_unuse_content(ligne): + for key in dico_supression: + ligne = ligne.replace(key, dico_supression[key]) + + return ligne + + +def sanitize_content(ligne): + ligne = delete_string(ligne) + filtered_content =replace_by_new_content(ligne) + filtered_content = delete_unuse_content(filtered_content) + filtered_content = re.sub('[0-9]{1,}', '*', filtered_content) + + return filtered_content + + + +def findVariableInFuction(line): + listVariable = [] + cpt = 0 + inFunction = False + + listContentinit = [] + listVarInFunction = "" + + for signe in line: + + if signe == "(": + cpt +=1 + inFunction = True + + elif signe == ")": + cpt -=1 + + if inFunction: + if True and cpt > 0 and signe != "(": + listVarInFunction += signe + + else: + listContentinit.append(listVarInFunction) + listVarInFunction = "" + + + listContentSplit = [] + listContentSplit2 = [] + + for functionContent in listContentinit: + functionContent = functionContent.replace(',', ', ') + x = functionContent.split(", ") + + for content in x: + listContentSplit2.append(content) + if not set('~!@#$%^&*()+.{}":;\'+$').intersection(content): + + if "[" in content: + newContent = "" + id=0 + finishExtractVarFromTable = False + + while id < len(content) and not finishExtractVarFromTable: + if content[id] == "[": + finishExtractVarFromTable = True + + else: + newContent += content[id] + id +=1 + + content = newContent + + if re.search(PATERN_VARIABLE[0], content) != None or re.search(PATERN_VARIABLE[1], content) != None or re.search(PATERN_VARIABLE[2], content) != None: + + listContentSplit.append(content) + + + for varaiblePart in listContentSplit: + + if ">" in varaiblePart: + parts = varaiblePart.split(">") + parts[len(parts)-2] +=">" + else: + parts = varaiblePart.split(" ") + + i = 0 + type="" + + while i < len(parts) -1: + type = type+parts[i] + i +=1 + + type = re.sub(' ', '', type) + variable = re.sub(' ', '', parts[len(parts)-1]) + listVariable.append((type, variable)) + + + return listVariable + + +def findVariableDeclare(ligne): + """ + :param ligne: représente une ligne de code + :return: retour le la liste des variable au seins d'une fonction + """ + + listVariable = [] + type = "" + list = [] + + if True: + i = 0 + find = False + + while i < len(ligne) and not find: + + if ligne[i] == "=": + + notFind = True + findSeparator = True + permissionParcourtWord = False + + k = i + var = "" + typeVar = "" + listVariableTransition = [] + inTab = False + + while k > 0 and notFind: + + if ligne[k] == " ": + + permissionParcourtWord = False + + if var != "": + listVariableTransition.append(var) + + var = "" + + if ligne[k] == ",": + + findSeparator = True + permissionParcourtWord = False + + if var != "": + listVariableTransition.append(var) + var = "" + + if ligne[k] == "]": + inTab= True + + if ligne[k] == "/": + if len(ligne) > k+1: + if ligne[k+1] == "/" or ligne[k+1] == "*": + + notFind = False + + #Si on trouve un signe arpès avoir trouvé un séparateur ou qu'on est en train de parcourir un mot + if ligne[k] != "," and ligne[k] != " " and k != i and not inTab: + + if findSeparator or permissionParcourtWord: + findSeparator = False + permissionParcourtWord = True + var = ligne[k]+var + + else: + notFind = False + + + if ligne[k] == "[": + inTab = False + + + if not notFind: + type = "" + while k > 0 and ligne[k] != " ": + type = ligne[k]+type + k -=1 + + + for variable in listVariableTransition: + if not set('[~!@#$%^&*()+.{}":;\']+$').intersection(type) and not set('[~!@#$%^&*()+.{}":;\']+$').intersection(variable) : + type = re.sub(' ', '', type) + variable = re.sub(' ', '', variable) + typeVar = (type, variable) + + if typeVar not in listVariable: + listVariable.append(typeVar) + + k -= 1 + + elif ligne[i] == ";" and "=" not in ligne: + line="" + line = re.findall(r'\s{0,}[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,};', ligne) + + if line != [] and "return" not in line[0]: + + line = re.sub(';', '', line[0]) + ligneTab = line.split(" ") + + u = 0 + type = "" + var = "" + while u 75: + blockCodes.append(sanitize_code) + newBlock = "" + k +=1 + + i +=1 + + return blockCodes + + + + +def find_function(line): + """ + :param: line: représente le code à analyser + :return retourne les différents blocks représentant ce code (fonction while for if ..) + """ + + blockCodes = [] + i = 0 + cptAcollade = 0 + newBlock="" + addAblock = False + + while i < len(line): + + if line[i] == "{": + + cptAcollade += 1 + newBlock += line[i] + addAblock = True + i +=1 + + while cptAcollade != 0 and newBlock != "" and i < len(line): + + if line[i] == "{": + cptAcollade += 1 + + elif line[i] == "}": + cptAcollade -= 1 + + if addAblock: + newBlock += line[i] + + if cptAcollade == 0: + + blockCodes.append(newBlock) + newBlock = "" + i +=1 + + i +=1 + + return blockCodes + + + +def rename_variable(line, listVariableRename): + + listType = [] + i = 0 + + while i < len(listVariableRename): + + type = listVariableRename[i][0] + if type not in listType: + listType.append(type) + + listCurrentVar = [] + + for variable in listVariableRename: + + if variable[0] == type: + listCurrentVar.append(variable[1]) + + + line = line.replace(type, "") + + for variable in listCurrentVar: + + ###gestion var++ et var-- + line = re.sub(r'\s{0,}'+variable+r'\s{0,}\+\+', type+"="+type+"+1", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}\-\-', type+"="+type+"-1", line) + + line = re.sub(r'\s{1,}'+variable+r'\s{1,}', type, line) + + ###gestion de opérateur mathématique + line = re.sub(r'\s{0,}'+variable+r'\s{0,}-', type+"-", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}\+', type+"+", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}\*', type+"*", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}\\', type+"|divide|", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}=', type+"=", line) + + ###gestion des symbile [] + line = re.sub(r'\[\s{0,}'+variable+r'\s{0,}\]', "["+type+"]", line) + + ###gestion de symbole ; + line = re.sub(r'\s{0,}'+variable+r'\s{0,};', type+";", line) + + ###gestion de symbole . + line = re.sub(r'\s{0,}'+variable+r'\s{0,}\.', type+".", line) + + ###gestion de symbole [ + line = re.sub(r'\s{0,}'+variable+r'\s{0,}\[', type+"[", line) + + ###gestion des symboles < > + line = re.sub(r'\s{0,}'+variable+r'\s{0,}<', type+"<", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}>', type+">", line) + line = re.sub(r'>\s{0,}'+variable+r'\s{0,}>', ">"+type+">", line) + line = re.sub(r'<\s{0,}'+variable+r'\s{0,}<', "<"+type+"<", line) + line = re.sub(r'>\s{0,}'+variable+r'\s{0,}<', ">"+type+"<", line) + line = re.sub(r'<\s{0,}'+variable+r'\s{0,}>', "<"+type+">", line) + + ###gestion du séparateur "," + line = re.sub(r'\s{0,}'+variable+r'\s{0,},', type+",", line) + line = re.sub(r',\s{0,}'+variable+r'\s{0,},', ","+type+",", line) + line = re.sub(r',\s{0,}'+variable+r'\s{0,}=', ","+type+"=", line) + line = re.sub(r',\s{0,}'+variable+r'\s{0,}\)', ","+type+")", line) + + ###gestion des symbole ( ) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}\)', type+")", line) + line = re.sub(r'\(\s{0,}'+variable+r'\s{0,},', "("+type+",", line) + line = re.sub(r'\(\s{0,}'+variable+r'\s{0,}\)', "("+type+")", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}!', type+"!", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}<', type+"<", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}>', type+">", line) + + i += 1 + + return line + + + + + + +def sanitize_list(liste_variable): + """ + :param liste_variable: représente la liste des variables du code + :return: retourne la liste des variables après avoir ajouté un espace après le type de la variable. Permet de différencier les types Matrice et collection + """ + + id = 0 + for elt in liste_variable: + cpt = elt[0].count('>') + + if cpt == 0: + newElt = (elt[0] + " ", elt[1]) + elt = newElt + liste_variable[id] = newElt + + id += 1 + + return liste_variable + + + +def remove_comentary(lignes): + """ + :param liste_variable: représente la liste des variables du code + :return: retourne la liste des variables après avoir ajouté un espace après le type de la variable. Permet de différencier les types Matrice et collection + """ + long_comment = False + code_without_comentary = [] + + for ligne in lignes: + + if "//" in ligne: + tab_line = ligne.split("//") + code_without_comentary.append(tab_line[0]) + + elif "/*" in ligne and "*/" in ligne: + tab_line1 = ligne.split("/*") + tab_line2 = ligne.split("*/") + new_line = tab_line1[0] + tab_line2[len(tab_line2)-1] + code_without_comentary.append(new_line) + + elif "/*" in ligne: + tab_line = ligne.split("/*") + code_without_comentary.append(tab_line[0]) + long_comment = True + + elif "*/" in ligne: + tab_line = ligne.split("*/") + code_without_comentary.append(tab_line[len(tab_line)-1]) + long_comment = False + + + elif not long_comment: + code_without_comentary.append(ligne) + + + + return code_without_comentary + + +def excecEvalPlagiat(code): + + listFunction = [] + listVariableRename = [] + lastListVariableRename = [] + + listVarBlock = [] + + scopeCodeUser = False + firstInsert = False + long_comment = False + + lignesCompacte = "" + newBlock = [] + functionCode = "" + + lignes = remove_comentary(lignes) + + for ligne in lignes: + + if "#include" not in ligne and "using namespace" not in ligne: + + ligne = ligne.replace('\n', '') + + listeVarInitFunction = findVariableInFuction(ligne) + + if listeVarInitFunction != []: + + if listVariableRename != []: + + listFunction.append(listVariableRename) + functionCode= "" + listVariableRename=[] + listVarToRenameFunction = [] + lastListVariableRename = [] + + i = 0 + while i < len(listeVarInitFunction): + if listeVarInitFunction[i] not in listVariableRename and listeVarInitFunction[i] != "": + listVariableRename.append(listeVarInitFunction[i]) + i += 1 + + else: + listVarToRenameFunction = listeVarInitFunction + + i = 0 + while i < len(listVarToRenameFunction): + if listVarToRenameFunction[i] not in listVariableRename and listVarToRenameFunction[i] != "": + listVariableRename.append(listVarToRenameFunction[i]) + i += 1 + + + else: + listVarToRenameFunction = listeVarInitFunction + findVariableDeclare(ligne) + + i = 0 + while i < len(listVarToRenameFunction): + if listVarToRenameFunction[i] not in listVariableRename and listVarToRenameFunction[i] != "": + listVariableRename.append(listVarToRenameFunction[i]) + i += 1 + + + + lignesCompacte +=ligne + + listFunction.append(listVariableRename) + + blockCodesWithRenameVariable = [] + listFunctionCode = find_function(lignesCompacte) + + + for function in listFunctionCode: + blockCodesWithRenameVariable.append(function) + + codeRename = "" + i=0 + for elt in blockCodesWithRenameVariable: + codeRename += rename_variable(elt, listFunction[i]) + i +=1 + + blockCodes = find_block(codeRename) + + return blockCodes \ No newline at end of file diff --git a/heroku_deploy/api/evaluation_code/cpp/evalRedondance.py b/heroku_deploy/api/evaluation_code/cpp/evalRedondance.py new file mode 100644 index 0000000..d4b4dcc --- /dev/null +++ b/heroku_deploy/api/evaluation_code/cpp/evalRedondance.py @@ -0,0 +1,548 @@ +import re + +PATERN_VARIABLE = [ + r'[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,}\s*', + r'[A-Za-z0-9_]{1,}\s{0,}<[A-Za-z0-9_]{1,}>\s{0,}[A-Za-z0-9_]{1,}', + r'[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,}', + + r'[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,}\s{0,};', + r'[A-Za-z0-9_]{1,}\s*<[A-Za-z0-9_]{1,}>\s*[A-Za-z0-9_]{0,};', + r'[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{0,};', +] + + + +def findVariableInFuction(line): + listVariable = [] + cpt = 0 + inFunction = False + + listContentinit = [] + listVarInFunction = "" + + for signe in line: + + if signe == "(": + cpt +=1 + inFunction = True + + elif signe == ")": + cpt -=1 + + if inFunction: + if True and cpt > 0 and signe != "(": + listVarInFunction += signe + + else: + listContentinit.append(listVarInFunction) + listVarInFunction = "" + + + listContentSplit = [] + listContentSplit2 = [] + + for functionContent in listContentinit: + functionContent = functionContent.replace(',', ', ') + x = functionContent.split(", ") + + for content in x: + listContentSplit2.append(content) + if not set('~!@#$%^&*()+.{}":;\'+$').intersection(content): + + if "[" in content: + newContent = "" + id=0 + finishExtractVarFromTable = False + + while id < len(content) and not finishExtractVarFromTable: + if content[id] == "[": + finishExtractVarFromTable = True + + else: + newContent += content[id] + id +=1 + + content = newContent + + if re.search(PATERN_VARIABLE[0], content) != None or re.search(PATERN_VARIABLE[1], content) != None or re.search(PATERN_VARIABLE[2], content) != None: + + listContentSplit.append(content) + + + for varaiblePart in listContentSplit: + + if ">" in varaiblePart: + parts = varaiblePart.split(">") + parts[len(parts)-2] +=">" + else: + parts = varaiblePart.split(" ") + + i = 0 + type="" + + while i < len(parts) -1: + type = type+parts[i] + i +=1 + + type = re.sub(' ', '', type) + variable = re.sub(' ', '', parts[len(parts)-1]) + listVariable.append((type, variable)) + + + return listVariable + + + +def findVariableDeclare(ligne): + """ + :param ligne: représente une ligne de code + :return: retour le la liste des variable au seins d'une fonction + """ + + listVariable = [] + type = "" + list = [] + + if True: + i = 0 + find = False + + while i < len(ligne) and not find: + + if ligne[i] == "=": + + notFind = True + findSeparator = True + permissionParcourtWord = False + + k = i + var = "" + typeVar = "" + listVariableTransition = [] + inTab = False + + while k > 0 and notFind: + + if ligne[k] == " ": + + permissionParcourtWord = False + + if var != "": + listVariableTransition.append(var) + + var = "" + + if ligne[k] == ",": + + findSeparator = True + permissionParcourtWord = False + + if var != "": + listVariableTransition.append(var) + var = "" + + if ligne[k] == "]": + inTab= True + + if ligne[k] == "/": + if len(ligne) > k+1: + if ligne[k+1] == "/" or ligne[k+1] == "*": + + notFind = False + + #Si on trouve un signe arpès avoir trouvé un séparateur ou qu'on est en train de parcourir un mot + if ligne[k] != "," and ligne[k] != " " and k != i and not inTab: + + if findSeparator or permissionParcourtWord: + findSeparator = False + permissionParcourtWord = True + var = ligne[k]+var + + else: + notFind = False + + + if ligne[k] == "[": + inTab = False + + + if not notFind: + type = "" + while k > 0 and ligne[k] != " ": + type = ligne[k]+type + k -=1 + + + for variable in listVariableTransition: + if not set('[~!@#$%^&*()+.{}":;\']+$').intersection(type) and not set('[~!@#$%^&*()+.{}":;\']+$').intersection(variable) : + type = re.sub(' ', '', type) + variable = re.sub(' ', '', variable) + typeVar = (type, variable) + + if typeVar not in listVariable: + listVariable.append(typeVar) + + k -= 1 + + elif ligne[i] == ";" and "=" not in ligne: + line="" + line = re.findall(r'\s{0,}[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,};', ligne) + + if line != [] and "return" not in line[0]: + + line = re.sub(';', '', line[0]) + ligneTab = line.split(" ") + + u = 0 + type = "" + var = "" + while u + line = re.sub(r'\s{0,}'+variable+r'\s{0,}<', type+"<", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}>', type+">", line) + line = re.sub(r'>\s{0,}'+variable+r'\s{0,}>', ">"+type+">", line) + line = re.sub(r'<\s{0,}'+variable+r'\s{0,}<', "<"+type+"<", line) + line = re.sub(r'>\s{0,}'+variable+r'\s{0,}<', ">"+type+"<", line) + line = re.sub(r'<\s{0,}'+variable+r'\s{0,}>', "<"+type+">", line) + + ###gestion du séparateur "," + line = re.sub(r'\s{0,}'+variable+r'\s{0,},', type+",", line) + line = re.sub(r',\s{0,}'+variable+r'\s{0,},', ","+type+",", line) + line = re.sub(r',\s{0,}'+variable+r'\s{0,}=', ","+type+"=", line) + line = re.sub(r',\s{0,}'+variable+r'\s{0,}\)', ","+type+")", line) + + ###gestion des symbole ( ) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}\)', type+")", line) + line = re.sub(r'\(\s{0,}'+variable+r'\s{0,},', "("+type+",", line) + line = re.sub(r'\(\s{0,}'+variable+r'\s{0,}\)', "("+type+")", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}!', type+"!", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}<', type+"<", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}>', type+">", line) + + i += 1 + + return line + + + + + + +def sanitize_list(liste_variable): + """ + :param liste_variable: représente la liste des variables du code + :return: retourne la liste des variables après avoir ajouté un espace après le type de la variable. Permet de différencier les types Matrice et collection + """ + + id = 0 + for elt in liste_variable: + cpt = elt[0].count('>') + + if cpt == 0: + newElt = (elt[0] + " ", elt[1]) + elt = newElt + liste_variable[id] = newElt + + id += 1 + + return liste_variable + + + +def remove_comentary(lignes): + """ + :param liste_variable: représente la liste des variables du code + :return: retourne la liste des variables après avoir ajouté un espace après le type de la variable. Permet de différencier les types Matrice et collection + """ + long_comment = False + code_without_comentary = [] + + for ligne in lignes: + + if "//" in ligne: + tab_line = ligne.split("//") + code_without_comentary.append(tab_line[0]) + + elif "/*" in ligne and "*/" in ligne: + tab_line1 = ligne.split("/*") + tab_line2 = ligne.split("*/") + new_line = tab_line1[0] + tab_line2[len(tab_line2)-1] + code_without_comentary.append(new_line) + + elif "/*" in ligne: + tab_line = ligne.split("/*") + code_without_comentary.append(tab_line[0]) + long_comment = True + + elif "*/" in ligne: + tab_line = ligne.split("*/") + code_without_comentary.append(tab_line[len(tab_line)-1]) + long_comment = False + + + elif not long_comment: + code_without_comentary.append(ligne) + + + + return code_without_comentary + + +def excecEvalRedondance(lignes): + + listFunction = [] + listVariableRename = [] + lastListVariableRename = [] + + listVarBlock = [] + + scopeCodeUser = False + firstInsert = False + long_comment = False + + lignesCompacte = "" + newBlock = [] + functionCode = "" + + lignes = remove_comentary(lignes) + + for ligne in lignes: + + if "#include" not in ligne and "using namespace" not in ligne: + + ligne = ligne.replace('\n', '') + + listeVarInitFunction = findVariableInFuction(ligne) + + if listeVarInitFunction != []: + + if listVariableRename != []: + + listFunction.append(listVariableRename) + functionCode= "" + listVariableRename=[] + listVarToRenameFunction = [] + lastListVariableRename = [] + + i = 0 + while i < len(listeVarInitFunction): + if listeVarInitFunction[i] not in listVariableRename and listeVarInitFunction[i] != "": + listVariableRename.append(listeVarInitFunction[i]) + i += 1 + + else: + listVarToRenameFunction = listeVarInitFunction + + i = 0 + while i < len(listVarToRenameFunction): + if listVarToRenameFunction[i] not in listVariableRename and listVarToRenameFunction[i] != "": + listVariableRename.append(listVarToRenameFunction[i]) + i += 1 + + + else: + listVarToRenameFunction = listeVarInitFunction + findVariableDeclare(ligne) + + i = 0 + while i < len(listVarToRenameFunction): + if listVarToRenameFunction[i] not in listVariableRename and listVarToRenameFunction[i] != "": + listVariableRename.append(listVarToRenameFunction[i]) + i += 1 + + + + lignesCompacte +=ligne + + listFunction.append(listVariableRename) + + blockCodesWithRenameVariable = [] + listFunctionCode = find_function(lignesCompacte) + + + for function in listFunctionCode: + blockCodesWithRenameVariable.append(function) + + codeRename = "" + i=0 + for elt in blockCodesWithRenameVariable: + codeRename += rename_variable(elt, listFunction[i]) + i +=1 + + blockCodes = find_block(codeRename) + + cptRedondance = 0 + + for block in blockCodes: + if blockCodes[block] > 1: + cptRedondance += blockCodes[block]-1 + + return cptRedondance + diff --git a/heroku_deploy/api/evaluation_code/cpp/evalVariableName.py b/heroku_deploy/api/evaluation_code/cpp/evalVariableName.py new file mode 100644 index 0000000..7afa079 --- /dev/null +++ b/heroku_deploy/api/evaluation_code/cpp/evalVariableName.py @@ -0,0 +1,307 @@ +import re + + +PATERN_VARIABLE = [ + r'[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,}\s*', + r'[A-Za-z0-9_]{1,}\s*<[A-Za-z0-9_]{1,}>\s*[A-Za-z0-9_]{1,}', + r'[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,}', +] + + +def findVariableInFuction(line): + listVariable = [] + cpt = 0 + inFunction = False + + listContentinit = [] + listVarInFunction = "" + + for signe in line: + + if signe == "(": + cpt +=1 + inFunction = True + + elif signe == ")": + cpt -=1 + + if inFunction: + if True and cpt > 0 and signe != "(": + listVarInFunction += signe + + else: + listContentinit.append(listVarInFunction) + listVarInFunction = "" + + + listContentSplit = [] + listContentSplit2 = [] + + for functionContent in listContentinit: + functionContent = functionContent.replace(',', ', ') + x = functionContent.split(", ") + + for content in x: + + listContentSplit2.append(content) + if not set('~!@#$%^&*()+.{}":;\'+$').intersection(content): + + if "[" in content: + newContent = "" + id=0 + finishExtractVarFromTable = False + + while id < len(content) and not finishExtractVarFromTable: + if content[id] == "[": + finishExtractVarFromTable = True + + else: + newContent += content[id] + id +=1 + + content = newContent+"[]" + + if re.search(PATERN_VARIABLE[0], content) != None or re.search(PATERN_VARIABLE[1], content) != None or re.search(PATERN_VARIABLE[2], content) != None: + + listContentSplit.append(content) + + + for varaiblePart in listContentSplit: + + if ">" in varaiblePart: + parts = varaiblePart.split(">") + parts[len(parts)-2] +=">" + else: + parts = varaiblePart.split(" ") + + i = 0 + type="" + + while i < len(parts) -1: + type = type+parts[i] + i +=1 + + type = re.sub(' ', '', type) + variable = re.sub(' ', '', parts[len(parts)-1]) + + listVariable.append(variable) + + + return listVariable + + + +def findVariableDeclare(ligne): + """ + :param ligne: représente une ligne de code + :return: retour le la liste des variable au seins d'une fonction + """ + + listVariable = [] + type = "" + list = [] + + if True: + i = 0 + find = False + + while i < len(ligne) and not find: + + if ligne[i] == "=": + + notFind = True + findSeparator = True + permissionParcourtWord = False + + k = i + var = "" + typeVar = "" + listVariableTransition = [] + inTab = False + + while k > 0 and notFind: + + if ligne[k] == " ": + permissionParcourtWord = False + + if var != "": + listVariableTransition.append(var) + + var = "" + + if ligne[k] == ",": + + findSeparator = True + permissionParcourtWord = False + + if var != "": + listVariableTransition.append(var) + + var = "" + + + if ligne[k] == "[": + inTab = False + + + #Si on trouve un signe arpès avoir trouvé un séparateur ou qu'on est en train de parcourir un mot + if ligne[k] != "," and ligne[k] != " " and k != i and not inTab: + + if findSeparator or permissionParcourtWord: + findSeparator = False + permissionParcourtWord = True + var = ligne[k]+var + + else: + notFind = False + + if ligne[k] == "]": + inTab= True + + if not notFind: + type = "" + while k > 0 and ligne[k] != " ": + type = ligne[k]+type + k -=1 + + + for variable in listVariableTransition: + if not set('[~!@#$%^&*()+.{}":;\']+$').intersection(type) and not set('~!@#$%^&*()+.{}":;\'+$').intersection(variable) : + type = re.sub(' ', '', type) + variable = re.sub(' ', '', variable) + typeVar = variable + + if typeVar not in listVariable: + listVariable.append(typeVar) + + k -= 1 + + elif ligne[i] == ";" and "=" not in ligne: + line="" + line = re.findall(r'\s{0,}[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,};', ligne) + + if line != [] and "return" not in line[0]: + + line = re.sub(';', '', line[0]) + ligneTab = line.split(" ") + + u = 0 + type = "" + var = "" + while u + """ + long_comment = False + code_without_comentary = [] + + for ligne in lignes: + + if "//" in ligne: + tab_line = ligne.split("//") + code_without_comentary.append(tab_line[0]) + + elif "/*" in ligne and "*/" in ligne: + tab_line1 = ligne.split("/*") + tab_line2 = ligne.split("*/") + new_line = tab_line1[0] + tab_line2[len(tab_line2)-1] + code_without_comentary.append(new_line) + + elif "/*" in ligne: + tab_line = ligne.split("/*") + code_without_comentary.append(tab_line[0]) + long_comment = True + + elif "*/" in ligne: + tab_line = ligne.split("*/") + code_without_comentary.append(tab_line[len(tab_line)-1]) + long_comment = False + + elif not long_comment: + code_without_comentary.append(ligne) + + return code_without_comentary + + +def excecEvalVariableName(lignes): + + scopeCodeUser = False + cpt = 0 + scopeCodeUser = False + cpt = 0 + + lignesCompacte = "" + listVariable = [] + lignes = remove_comentary(lignes) + + for ligne in lignes: + + # ligne = ligne.replace('\\n', '') + + variable = "" + variableDeclare = findVariableDeclare(ligne) + + for variable in variableDeclare: + if variable != "" and variable not in listVariable: + if not set('<>+-*~!@#$%^&*().{}":;\'+$').intersection(variable): + listVariable.append(variable) + + variable = "" + functionListVariable = findVariableInFuction(ligne) + + i = 0 + while i < len(functionListVariable): + if functionListVariable[i] not in listVariable and functionListVariable[i] != "": + if not set('<>+-*~!@#$%^&*().{}":;\'+$').intersection(functionListVariable[i]): + listVariable.append(functionListVariable[i]) + i += 1 + + + lignesCompacte +=ligne + + + error = 0 + for var in listVariable: + error = error + switch(var) + + + ###RESULTAT + return error + diff --git a/heroku_deploy/api/evaluation_code/cpp/main.py b/heroku_deploy/api/evaluation_code/cpp/main.py new file mode 100644 index 0000000..2f58f08 --- /dev/null +++ b/heroku_deploy/api/evaluation_code/cpp/main.py @@ -0,0 +1,13 @@ +from evaluation_code.evalNbLigneFonctionCpp import excecEvalNbLigneFonction +from evaluation_code.evalCommentaireCpp import excecEvalCommentaire +from evaluation_code.evalRedondanceCpp import excecEvalRedondance +from evaluation_code.evalVariableNameCpp import excecEvalVariableName + +if __name__ == '__main__': + payload = { + "eval_variable_name\":excecEvalVariableName(), + "eval_redondance\": excecEvalRedondance(), + "eval_nb ligne_fonction\": excecEvalNbLigneFonction(), + "eval_commentaire\": excecEvalCommentaire() + } + print(payload) \ No newline at end of file diff --git a/heroku_deploy/api/evaluation_code/python/__pycache__/evalCommentairePy.cpython-39.pyc b/heroku_deploy/api/evaluation_code/python/__pycache__/evalCommentairePy.cpython-39.pyc new file mode 100644 index 0000000..851693b Binary files /dev/null and b/heroku_deploy/api/evaluation_code/python/__pycache__/evalCommentairePy.cpython-39.pyc differ diff --git a/heroku_deploy/api/evaluation_code/python/__pycache__/evalNbLigneFonctionPy.cpython-39.pyc b/heroku_deploy/api/evaluation_code/python/__pycache__/evalNbLigneFonctionPy.cpython-39.pyc new file mode 100644 index 0000000..4e619b0 Binary files /dev/null and b/heroku_deploy/api/evaluation_code/python/__pycache__/evalNbLigneFonctionPy.cpython-39.pyc differ diff --git a/heroku_deploy/api/evaluation_code/python/__pycache__/evalRedondancePy.cpython-39.pyc b/heroku_deploy/api/evaluation_code/python/__pycache__/evalRedondancePy.cpython-39.pyc new file mode 100644 index 0000000..664c415 Binary files /dev/null and b/heroku_deploy/api/evaluation_code/python/__pycache__/evalRedondancePy.cpython-39.pyc differ diff --git a/heroku_deploy/api/evaluation_code/python/__pycache__/evalVariableNamePython.cpython-39.pyc b/heroku_deploy/api/evaluation_code/python/__pycache__/evalVariableNamePython.cpython-39.pyc new file mode 100644 index 0000000..07f4cc9 Binary files /dev/null and b/heroku_deploy/api/evaluation_code/python/__pycache__/evalVariableNamePython.cpython-39.pyc differ diff --git a/heroku_deploy/api/evaluation_code/python/evalCommentaire.py b/heroku_deploy/api/evaluation_code/python/evalCommentaire.py new file mode 100644 index 0000000..cd9244b --- /dev/null +++ b/heroku_deploy/api/evaluation_code/python/evalCommentaire.py @@ -0,0 +1,41 @@ +def excecEvalCommentaire(lignes): + + scopeCodeUser = False + + nbligne = 0 + nbligneComment = 0 + longComment = False + + for ligne in lignes: + + if ligne != "": + nbligne += 1 + + if '#' in ligne: + nbligneComment +=1 + + if '"""' in ligne: + if ligne.count('"""') > 1: + nbligneComment +=1 + else: + if longComment: + longComment = False + else: + longComment = True + + if longComment: + nbligneComment +=1 + + + + res = "" + + if nbligne >0: + if (nbligneComment/nbligne)*100 >= 10: + res = "ok" + else: + res = "pas assez de commentaires" + else: + res = "ok" + + return res diff --git a/heroku_deploy/api/evaluation_code/python/evalNbLigneFonction.py b/heroku_deploy/api/evaluation_code/python/evalNbLigneFonction.py new file mode 100644 index 0000000..9722f01 --- /dev/null +++ b/heroku_deploy/api/evaluation_code/python/evalNbLigneFonction.py @@ -0,0 +1,123 @@ +import re + + +def find_function(lines): + """ + :param: line: représente le code à analyser + :return retourne les différents blocks représentant ce code (fonction while for if ..) + """ + + blockCodes = [] + i = 0 + newBlock="" + addAblock = False + + for line in lines: + i = 0 + while i < len(line): + + if len(line)>i+4: + if line[i] == "d": + if line[i+1] == "e": + if line[i + 2] == "f": + if line[i + 3] == " ": + + if newBlock != "": + blockCodes.append(newBlock) + newBlock="" + + newBlock += line[i] + i +=1 + + return blockCodes + + + + + + + + +def remove_comentary(lignes): + """ + :param liste_variable: représente la liste des variables du code + :return: retourne la liste des variables après avoir ajouté un espace après le type de la variable. Permet de différencier les types Matrice et collection + """ + long_comment = False + code_without_comentary = [] + + for ligne in lignes: + + if "#" in ligne: + tab_line = ligne.split("#") + + if "\n" not in tab_line[0]: + tab_line[0] = tab_line[0] + "\n" + + code_without_comentary.append(tab_line[0]) + + elif ligne.count("\"\"\"") == 2: + tab_line = ligne.split("\"\"\"") + new_line = tab_line[0] + tab_line[len(tab_line)-1] + code_without_comentary.append(new_line) + + elif "\"\"\"" in ligne: + tab_line = ligne.split("\"\"\"" ) + code_without_comentary.append(tab_line[0]) + long_comment = not long_comment + + elif "\"\"\"" in ligne: + tab_line = ligne.split("\"\"\"" ) + code_without_comentary.append(tab_line[len(tab_line)-1]) + long_comment = not long_comment + + elif not long_comment: + code_without_comentary.append(ligne) + + return code_without_comentary + + + + +def excecEvalNbLigneFonction(lignes): + + listFunction = [] + listVariableRename = [] + lastListVariableRename = [] + + listVarBlock = [] + + scopeCodeUser = False + firstInsert = False + + lignesCompacte = [] + newBlock = [] + functionCode = "" + listVarToRename = [] + + lignes = remove_comentary(lignes) + + for ligne in lignes: + + lignesCompacte.append(ligne) + + functionCodes = find_function(lignesCompacte) + + + cpt_error = 0 + for function in functionCodes: + + cpt_ligne = 0 + cpt_ligne = function.count("\n") + + table_rm = function.split('\n') + cpt_rm = 0 + for ligne in table_rm: + ligne = ligne.replace(" ", "") + if len(ligne) == 0: + cpt_rm += 1 + + if cpt_ligne-cpt_rm > 30: + cpt_error +=1 + + return cpt_error diff --git a/heroku_deploy/api/evaluation_code/python/evalPlagiat.py b/heroku_deploy/api/evaluation_code/python/evalPlagiat.py new file mode 100644 index 0000000..1fdb0d7 --- /dev/null +++ b/heroku_deploy/api/evaluation_code/python/evalPlagiat.py @@ -0,0 +1,583 @@ +import re + +PATERN_VARIABLE = [ + r'[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,}\s*', + r'[A-Za-z0-9_]{1,}\s{0,}<[A-Za-z0-9_]{1,}>\s{0,}[A-Za-z0-9_]{1,}', + r'[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,}', + + r'[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,}\s{0,};', + r'[A-Za-z0-9_]{1,}\s*<[A-Za-z0-9_]{1,}>\s*[A-Za-z0-9_]{0,};', + r'[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{0,};', +] + + + + + + +dico_stemming = { + "def": "d", + "while": "w", + "for": "f", + "if": "i", + "elif": "ei", + "else": "e", + "<=": "c", + ">=": "c", + "<": "c", + ">": "c", + "def": "d", + "!=": "!", + "==": "-", + "and": "&", + "or": "|", + "var": "v", + "del": "d", + "from": "w", + "not": "n", + "while": "w", + "assert": "as", + "global": "gl", + "as": "as", + "break": "bk", + "continue": "ct", + "lambda": "lb", + "try": "t", + "with": "wh", + "except": "ex", + "pass": "ps", + "yield": "y", + "import": "ip", + "finally": "fn", + "class": "cl", + "raise": "rs", + "exec": "ex", + "is": "is", + "in": "in", + "print": "", + "True": "tr", + "False": "fl", + ",": "", + +} + +dico_supression = { + " ": "", + "{": "", + "}": "", + "(": "", + ")": "", + ";": "", + ":": "" + +} + + +def delete_string(ligne): + delete = False + list_chain_to_remove = [] + + if ligne.count("\"") > 0: + + i = 0 + chain_to_remove = "" + + while i < len(ligne): + + if ligne[i] == '\"': + list_chain_to_remove.append(chain_to_remove) + chain_to_remove = "" + delete = not delete + + if delete and ligne[i] != '"': + chain_to_remove = chain_to_remove + ligne[i] + + i += 1 + + for chain in list_chain_to_remove: + ligne = ligne.replace(chain, "") + + ligne = ligne.replace("\"\"", "\"") + + return ligne + + +def replace_by_new_content(ligne): + for key in dico_stemming: + ligne = ligne.replace(key, dico_stemming[key]) + + return ligne + + +def delete_unuse_content(ligne): + for key in dico_supression: + ligne = ligne.replace(key, dico_supression[key]) + + return ligne + + +def sanitize_content(ligne): + filtered_content = "" + + ligne = delete_string(ligne) + filtered_content = filtered_content + (replace_by_new_content(ligne)) + filtered_content = delete_unuse_content(filtered_content) + + return filtered_content + + + +def findVariableInFuction(line): + listVariable = [] + variable = "" + + i = 0 + if line.count('def') > 0: + start = False + end = False + + while i < len(line): + + if line[i] == "(": + start = True + i=i+1 + + + if start and not end: + + if line[i] != "," and line[i] != ")": + + if line[i] != " ": + variable += line[i] + + else: + listVariable.append(variable) + variable = "" + + if line[i] == ")": + end = True + + i +=1 + + return listVariable + + + + +def findVariableDeclare(ligne): + listVariable = [] + + if "=" in ligne: + i = 0 + find = False + + while i < len(ligne) and not find: + if ligne[i] == "=": + + notFind = True + findSeparator = True + permissionParcourtWord = False + + k = i + var = "" + while k > 0 and notFind: + + if ligne[k] == " ": + permissionParcourtWord = False + + if var != "": + listVariable.append(var) + var = "" + + if ligne[k] == ",": + + findSeparator = True + permissionParcourtWord = False + + if var != "": + listVariable.append(var) + var = "" + + + if ligne[k] != "," and ligne[k] != " " and k != i: + + if findSeparator or permissionParcourtWord: + findSeparator = False + permissionParcourtWord = True + var = ligne[k]+var + + else: + notFind = False + + k -= 1 + + i += 1 + + if " in " in ligne: + findIn = False + notFind = True + permissionParcourtWord = False + + p = 0 + while p < len(ligne) and notFind: + if len(ligne) > p + 3: + if ligne[p] == " ": + if ligne[p+1] == "i": + if ligne[p + 2] == "n": + if ligne[p + 3] == " ": + findIn = True + + if findIn: + var = "" + m=p + while m > 0: + + if ligne[m] != " ": + if not permissionParcourtWord: + permissionParcourtWord = True + + elif permissionParcourtWord: + notFind = False + permissionParcourtWord = False + + if var != "" and var not in listVariable: + listVariable.append(var) + + if permissionParcourtWord and notFind: + var = ligne[m]+var + + + + m -=1 + + p +=1 + + + + return listVariable + + +def update_block(newBlock, blockCodes): + if newBlock in blockCodes: + blockCodes[newBlock] += 1 + + else: + blockCodes.update({newBlock: 1}) + + return blockCodes + + +def find_indentation(line): + + cpt = 0 + while cpt < len(line): + + symbole = line[cpt] + + if symbole == " ": + cpt +=1 + + elif symbole == "": + return 0 + + elif symbole == "\n": + return 0 + + elif symbole == "#": + return 0 + + else: + return cpt + + + return cpt + + +def find_block(code): + """ + :param: line: représente le code à analyser + :return retourne les différents blocks représentant ce code (fonction while for if ..) + """ + listBlock = [] + i = 0 + lastIndentationValue=0 + currentIndentationValue = 0 + blockCodes = "" + save=0 + + for line in code: + + sanitize_line = line.replace(" ", "") + sanitize_line = sanitize_line.replace("\n", "") + + if len(sanitize_line) > 0: + + lastIndentationValue = save + currentIndentationValue = find_indentation(line) + + if currentIndentationValue > lastIndentationValue : + + k=i + + + blockCreate = False + blockCodes = "" + save = find_indentation(line) + + while not blockCreate and k < len(code): + + sanitize_line = code[k].replace(" ", "") + sanitize_line = sanitize_line.replace("\n", "") + + if len(sanitize_line) > 0: + + linebis = code[k] + newVal = find_indentation(linebis) + + if newVal != -1: + currentIndentationValue = newVal + + + if currentIndentationValue > lastIndentationValue: + blockCodes = blockCodes+linebis + + + else: + blockCreate = True + + k +=1 + + listBlock.append(blockCodes) + + else: + if find_indentation(line) != -1: + save = find_indentation(line) + + i +=1 + + + return listBlock + + + + +def find_function(line): + """ + :param: line: représente le code à analyser + :return retourne les différents blocks représentant ce code (fonction while for if ..) + """ + + blockCodes = [] + i = 0 + newBlock="" + addAblock = False + + while i < len(line): + + if len(line)>i+4: + if line[i] == "d": + if line[i+1] == "e": + if line[i + 1] == "f": + if line[i + 1] == " ": + + if newBlock != "": + blockCodes.append(newBlock) + newBlock="" + + newBlock += line[i] + i +=1 + + return blockCodes + +def line_is_comment(line): + line.replace(" ","") + + if len(line)>0: + if line[0] == "#": + return True + else: + return False + + + + + +def rename_variable(line, listVariableRename): + + i = 0 + var = "var" + lineIndentation = find_indentation(line) + + a = 0 + espace = "" + while a < lineIndentation: + espace += " " + a +=1 + + for variable in listVariableRename: + + ###gestion var++ et var-- + line = re.sub(r'\s{0,}'+variable+r'\s{0,}\+\+', var+"="+var+"+1", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}\-\-', var+"="+var+"-1", line) + + line = re.sub(r'\s{1,}'+variable+r'\s{1,}', var, line) + + ###gestion de opérateur mathématique + line = re.sub(r'\s{0,}'+variable+r'\s{0,}-', var+"-", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}\+', var+"+", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}\*', var+"*", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}\\', var+"|divide|", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}=', var+"=", line) + + ###gestion des symbile [] + line = re.sub(r'\[\s{0,}'+variable+r'\s{0,}\]', "["+var+"]", line) + + ###gestion de symbole ; + # line = re.sub(r'\s{0,}'+variable+r'\s{0,};', var+";", line) + + ###gestion de symbole . + line = re.sub(r'\s{0,}'+variable+r'\s{0,}\.', var+".", line) + + ###gestion de symbole [ + line = re.sub(r'\s{0,}'+variable+r'\s{0,}\[', var+"[", line) + # line = re.sub(r'=\s{0,}'+variable+r'\s{0,}\[int\]', type+"[", line) + + ###gestion des symboles < > + line = re.sub(r'\s{0,}'+variable+r'\s{0,}<', var+"<", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}>', var+">", line) + line = re.sub(r'>\s{0,}'+variable+r'\s{0,}>', ">"+var+">", line) + line = re.sub(r'<\s{0,}'+variable+r'\s{0,}<', "<"+var+"<", line) + # line = re.sub(r'>\s{0,}'+variable+r'\s{0,}<', ">"+var+"<", line) + # line = re.sub(r'<\s{0,}'+variable+r'\s{0,}>', "<"+var+">", line) + + ###gestion du séparateur "," + line = re.sub(r'\s{0,}'+variable+r'\s{0,},', var+",", line) + line = re.sub(r',\s{0,}'+variable+r'\s{0,},', ","+var+",", line) + line = re.sub(r',\s{0,}'+variable+r'\s{0,}=', ","+var+"=", line) + line = re.sub(r',\s{0,}'+variable+r'\s{0,}\)', ","+var+")", line) + + ###gestion des symbole ( ) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}\)', var+")", line) + line = re.sub(r'\(\s{0,}'+variable+r'\s{0,},', "("+var+",", line) + line = re.sub(r'\(\s{0,}'+variable+r'\s{0,}\)', "("+var+")", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}!', var+"!", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}<', var+"<", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}>', var+">", line) + + line = re.sub(r'\s{0,}'+variable+r'\s{0,}:', var+":", line) + + + return line + + + +def sanitize_dict_(dict): + """ + :param liste_variable: représente la liste des variables du code + :return: retourne la liste des variables après avoir ajouté un espace après le type de la variable. Permet de différencier les types Matrice et collection + """ + + sanitize_dict = [] + + for block in dict: + sanitizeBlock = sanitize_content(block) + sanitizeBlock = re.sub('[0-9]{1,}', '*', sanitizeBlock) + + if len(sanitizeBlock) > 75: + sanitize_dict.append(sanitizeBlock) + + return sanitize_dict + + + +def remove_comentary(lignes): + """ + :param liste_variable: représente la liste des variables du code + :return: retourne la liste des variables après avoir ajouté un espace après le type de la variable. Permet de différencier les types Matrice et collection + """ + long_comment = False + code_without_comentary = [] + + for ligne in lignes: + + if "#" in ligne: + tab_line = ligne.split("#") + + if "\n" not in tab_line[0]: + tab_line[0] = tab_line[0] + "\n" + + code_without_comentary.append(tab_line[0]) + + elif ligne.count("\"\"\"") == 2: + tab_line = ligne.split("\"\"\"") + new_line = tab_line[0] + tab_line[len(tab_line)-1] + code_without_comentary.append(new_line) + + elif "\"\"\"" in ligne: + tab_line = ligne.split("\"\"\"" ) + code_without_comentary.append(tab_line[0]) + long_comment = not long_comment + + elif "\"\"\"" in ligne: + tab_line = ligne.split("\"\"\"" ) + code_without_comentary.append(tab_line[len(tab_line)-1]) + long_comment = not long_comment + + elif not long_comment: + code_without_comentary.append(ligne) + + return code_without_comentary + + +def excecEvalPlagiat(code): + + listFunction = [] + listVariableRename = [] + lastListVariableRename = [] + + listVarBlock = [] + + scopeCodeUser = False + firstInsert = False + + lignesCompacte = [] + newBlock = [] + functionCode = "" + listVarToRename = [] + + lignes = remove_comentary(code) + + for ligne in lignes: + + if not line_is_comment(ligne): + ligneBis = ligne + + listeVarInitFunction = findVariableInFuction(ligne) + listeVarContentFunction = [] + + if listeVarInitFunction == []: + listeVarContentFunction = findVariableDeclare(ligne) + + listVarToRename = listeVarContentFunction + listeVarInitFunction + + i = 0 + while i < len(listVarToRename): + + if listVarToRename[i] not in listVariableRename and listVarToRename[i] != "": + listVariableRename.append(listVarToRename[i]) + i += 1 + + lignesCompacte.append(ligneBis) + + + + blockCodes = find_block(lignesCompacte) + + + renameBlock = [] + for block in blockCodes: + renameBlock.append(rename_variable(block, listVariableRename)) + + sanitize_code_dict = sanitize_dict_(renameBlock) + + return sanitize_code_dict \ No newline at end of file diff --git a/heroku_deploy/api/evaluation_code/python/evalRedondance.py b/heroku_deploy/api/evaluation_code/python/evalRedondance.py new file mode 100644 index 0000000..f5b531a --- /dev/null +++ b/heroku_deploy/api/evaluation_code/python/evalRedondance.py @@ -0,0 +1,477 @@ +import re + +PATERN_VARIABLE = [ + r'[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,}\s*', + r'[A-Za-z0-9_]{1,}\s{0,}<[A-Za-z0-9_]{1,}>\s{0,}[A-Za-z0-9_]{1,}', + r'[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,}', + + r'[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,}\s{0,};', + r'[A-Za-z0-9_]{1,}\s*<[A-Za-z0-9_]{1,}>\s*[A-Za-z0-9_]{0,};', + r'[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{1,}\s{1,}[A-Za-z0-9_]{0,};', +] + + + +def findVariableInFuction(line): + listVariable = [] + variable = "" + + i = 0 + if line.count('def') > 0: + start = False + end = False + + while i < len(line): + + if line[i] == "(": + start = True + i=i+1 + + + if start and not end: + + if line[i] != "," and line[i] != ")": + + if line[i] != " ": + variable += line[i] + + else: + listVariable.append(variable) + variable = "" + + if line[i] == ")": + end = True + + i +=1 + + return listVariable + + + + +def findVariableDeclare(ligne): + listVariable = [] + + if "=" in ligne: + i = 0 + find = False + + while i < len(ligne) and not find: + if ligne[i] == "=": + + notFind = True + findSeparator = True + permissionParcourtWord = False + + k = i + var = "" + while k > 0 and notFind: + + if ligne[k] == " ": + permissionParcourtWord = False + + if var != "": + listVariable.append(var) + var = "" + + if ligne[k] == ",": + + findSeparator = True + permissionParcourtWord = False + + if var != "": + listVariable.append(var) + var = "" + + + if ligne[k] != "," and ligne[k] != " " and k != i: + + if findSeparator or permissionParcourtWord: + findSeparator = False + permissionParcourtWord = True + var = ligne[k]+var + + else: + notFind = False + + k -= 1 + + i += 1 + + if " in " in ligne: + findIn = False + notFind = True + permissionParcourtWord = False + + p = 0 + while p < len(ligne) and notFind: + if len(ligne) > p + 3: + if ligne[p] == " ": + if ligne[p+1] == "i": + if ligne[p + 2] == "n": + if ligne[p + 3] == " ": + findIn = True + + if findIn: + var = "" + m=p + while m > 0: + + if ligne[m] != " ": + if not permissionParcourtWord: + permissionParcourtWord = True + + elif permissionParcourtWord: + notFind = False + permissionParcourtWord = False + + if var != "" and var not in listVariable: + listVariable.append(var) + + if permissionParcourtWord and notFind: + var = ligne[m]+var + + + + m -=1 + + p +=1 + + + + return listVariable + + +def update_block(newBlock, blockCodes): + if newBlock in blockCodes: + blockCodes[newBlock] += 1 + + else: + blockCodes.update({newBlock: 1}) + + return blockCodes + + +def find_indentation(line): + + cpt = 0 + while cpt < len(line): + + symbole = line[cpt] + + if symbole == " ": + cpt +=1 + + elif symbole == "": + return 0 + + elif symbole == "\n": + return 0 + + elif symbole == "#": + return 0 + + else: + return cpt + + + return cpt + + +def find_block(code): + """ + :param: line: représente le code à analyser + :return retourne les différents blocks représentant ce code (fonction while for if ..) + """ + listBlock = [] + i = 0 + lastIndentationValue=0 + currentIndentationValue = 0 + blockCodes = "" + save=0 + + for line in code: + + sanitize_line = line.replace(" ", "") + sanitize_line = sanitize_line.replace("\n", "") + + if len(sanitize_line) > 0: + + lastIndentationValue = save + currentIndentationValue = find_indentation(line) + + if currentIndentationValue > lastIndentationValue : + + k=i + + + blockCreate = False + blockCodes = "" + save = find_indentation(line) + + while not blockCreate and k < len(code): + + sanitize_line = code[k].replace(" ", "") + sanitize_line = sanitize_line.replace("\n", "") + + if len(sanitize_line) > 0: + + + linebis = code[k] + newVal = find_indentation(linebis) + + if newVal != -1: + currentIndentationValue = newVal + + + if currentIndentationValue > lastIndentationValue: + blockCodes = blockCodes+linebis + + + else: + blockCreate = True + + k +=1 + + listBlock.append(blockCodes) + + else: + if find_indentation(line) != -1: + save = find_indentation(line) + + i +=1 + + + return listBlock + + + + +def find_function(line): + """ + :param: line: représente le code à analyser + :return retourne les différents blocks représentant ce code (fonction while for if ..) + """ + + blockCodes = [] + i = 0 + newBlock="" + addAblock = False + + while i < len(line): + + if len(line)>i+4: + if line[i] == "d": + if line[i+1] == "e": + if line[i + 1] == "f": + if line[i + 1] == " ": + + if newBlock != "": + blockCodes.append(newBlock) + newBlock="" + + newBlock += line[i] + i +=1 + + return blockCodes + +def line_is_comment(line): + line.replace(" ","") + + if len(line)>0: + if line[0] == "#": + return True + else: + return False + + + + + +def rename_variable(line, listVariableRename): + + i = 0 + var = "var" + lineIndentation = find_indentation(line) + + a = 0 + espace = "" + while a < lineIndentation: + espace += " " + a +=1 + + for variable in listVariableRename: + + ###gestion var++ et var-- + line = re.sub(r'\s{0,}'+variable+r'\s{0,}\+\+', var+"="+var+"+1", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}\-\-', var+"="+var+"-1", line) + + line = re.sub(r'\s{1,}'+variable+r'\s{1,}', var, line) + + ###gestion de opérateur mathématique + line = re.sub(r'\s{0,}'+variable+r'\s{0,}-', var+"-", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}\+', var+"+", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}\*', var+"*", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}\\', var+"|divide|", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}=', var+"=", line) + + ###gestion des symbile [] + line = re.sub(r'\[\s{0,}'+variable+r'\s{0,}\]', "["+var+"]", line) + + ###gestion de symbole ; + # line = re.sub(r'\s{0,}'+variable+r'\s{0,};', var+";", line) + + ###gestion de symbole . + line = re.sub(r'\s{0,}'+variable+r'\s{0,}\.', var+".", line) + + ###gestion de symbole [ + line = re.sub(r'\s{0,}'+variable+r'\s{0,}\[', var+"[", line) + # line = re.sub(r'=\s{0,}'+variable+r'\s{0,}\[int\]', type+"[", line) + + ###gestion des symboles < > + line = re.sub(r'\s{0,}'+variable+r'\s{0,}<', var+"<", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}>', var+">", line) + line = re.sub(r'>\s{0,}'+variable+r'\s{0,}>', ">"+var+">", line) + line = re.sub(r'<\s{0,}'+variable+r'\s{0,}<', "<"+var+"<", line) + # line = re.sub(r'>\s{0,}'+variable+r'\s{0,}<', ">"+var+"<", line) + # line = re.sub(r'<\s{0,}'+variable+r'\s{0,}>', "<"+var+">", line) + + ###gestion du séparateur "," + line = re.sub(r'\s{0,}'+variable+r'\s{0,},', var+",", line) + line = re.sub(r',\s{0,}'+variable+r'\s{0,},', ","+var+",", line) + line = re.sub(r',\s{0,}'+variable+r'\s{0,}=', ","+var+"=", line) + line = re.sub(r',\s{0,}'+variable+r'\s{0,}\)', ","+var+")", line) + + ###gestion des symbole ( ) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}\)', var+")", line) + line = re.sub(r'\(\s{0,}'+variable+r'\s{0,},', "("+var+",", line) + line = re.sub(r'\(\s{0,}'+variable+r'\s{0,}\)', "("+var+")", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}!', var+"!", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}<', var+"<", line) + line = re.sub(r'\s{0,}'+variable+r'\s{0,}>', var+">", line) + + line = re.sub(r'\s{0,}'+variable+r'\s{0,}:', var+":", line) + + + return line + + + +def sanitize_dict_(dict): + """ + :param liste_variable: représente la liste des variables du code + :return: retourne la liste des variables après avoir ajouté un espace après le type de la variable. Permet de différencier les types Matrice et collection + """ + + sanitize_dict = {} + + for block in dict: + sanitizeBlock = block.replace('\n', ';@;') + sanitizeBlock = sanitizeBlock.replace(' ', '') + sanitize_dict = update_block(sanitizeBlock, sanitize_dict) + + return sanitize_dict + + + +def remove_comentary(lignes): + """ + :param liste_variable: représente la liste des variables du code + :return: retourne la liste des variables après avoir ajouté un espace après le type de la variable. Permet de différencier les types Matrice et collection + """ + long_comment = False + code_without_comentary = [] + + for ligne in lignes: + + if "#" in ligne: + tab_line = ligne.split("#") + + if "\n" not in tab_line[0]: + tab_line[0] = tab_line[0] + "\n" + + code_without_comentary.append(tab_line[0]) + + elif ligne.count("\"\"\"") == 2: + tab_line = ligne.split("\"\"\"") + new_line = tab_line[0] + tab_line[len(tab_line)-1] + code_without_comentary.append(new_line) + + elif "\"\"\"" in ligne: + tab_line = ligne.split("\"\"\"" ) + code_without_comentary.append(tab_line[0]) + long_comment = not long_comment + + elif "\"\"\"" in ligne: + tab_line = ligne.split("\"\"\"" ) + code_without_comentary.append(tab_line[len(tab_line)-1]) + long_comment = not long_comment + + elif not long_comment: + code_without_comentary.append(ligne) + + return code_without_comentary + + +def excecEvalRedondance(lignes): + + listFunction = [] + listVariableRename = [] + lastListVariableRename = [] + + listVarBlock = [] + + scopeCodeUser = False + firstInsert = False + + lignesCompacte = [] + newBlock = [] + functionCode = "" + listVarToRename = [] + + lignes = remove_comentary(lignes) + + for ligne in lignes: + + if not line_is_comment(ligne): + ligneBis = ligne + + listeVarInitFunction = findVariableInFuction(ligne) + listeVarContentFunction = [] + + if listeVarInitFunction == []: + listeVarContentFunction = findVariableDeclare(ligne) + + listVarToRename = listeVarContentFunction + listeVarInitFunction + + i = 0 + while i < len(listVarToRename): + + if listVarToRename[i] not in listVariableRename and listVarToRename[i] != "": + listVariableRename.append(listVarToRename[i]) + i += 1 + + lignesCompacte.append(ligneBis) + + + + blockCodes = find_block(lignesCompacte) + + + renameBlock = [] + for block in blockCodes: + renameBlock.append(rename_variable(block, listVariableRename)) + + + sanitize_dict = sanitize_dict_(renameBlock) + + + cptRedondance = 0 + + for block in sanitize_dict: + + if sanitize_dict[block] > 1: + cptRedondance += sanitize_dict[block]-1 + + return cptRedondance + diff --git a/heroku_deploy/api/evaluation_code/python/evalVariableName.py b/heroku_deploy/api/evaluation_code/python/evalVariableName.py new file mode 100644 index 0000000..00746c1 --- /dev/null +++ b/heroku_deploy/api/evaluation_code/python/evalVariableName.py @@ -0,0 +1,263 @@ +import re + + + +def findVariableInFuction(line): + listVariable = [] + variable = "" + + i = 0 + if line.count('def') > 0: + start = False + end = False + + while i < len(line): + + if line[i] == "(": + start = True + i=i+1 + + + if start and not end: + + if line[i] != "," and line[i] != ")": + + if line[i] != " ": + variable += line[i] + + elif variable not in listVariable: + listVariable.append(variable) + variable = "" + + if line[i] == ")": + end = True + + i +=1 + + return listVariable + + + + +def findVariableDeclare(ligne): + listVariable = [] + + if "=" in ligne: + i = 0 + find = False + + while i < len(ligne) and not find: + if ligne[i] == "=": + + notFind = True + findSeparator = True + permissionParcourtWord = False + + k = i + var = "" + while k > 0 and notFind: + + if ligne[k] == " ": + permissionParcourtWord = False + + if var != "" and var not in listVariable: + listVariable.append(var) + var = "" + + if ligne[k] == ",": + + findSeparator = True + permissionParcourtWord = False + + if var != "" and var not in listVariable: + listVariable.append(var) + var = "" + + + if ligne[k] != "," and ligne[k] != " " and k != i: + + if findSeparator or permissionParcourtWord: + findSeparator = False + permissionParcourtWord = True + var = ligne[k]+var + + else: + notFind = False + + k -= 1 + + i += 1 + + if " in " in ligne: + findIn = False + notFind = True + permissionParcourtWord = False + + p = 0 + while p < len(ligne) and notFind: + if len(ligne) > p + 3: + if ligne[p] == " ": + if ligne[p+1] == "i": + if ligne[p + 2] == "n": + if ligne[p + 3] == " ": + findIn = True + + if findIn: + var = "" + m=p + while m > 0: + + if ligne[m] != " ": + if not permissionParcourtWord: + permissionParcourtWord = True + + elif permissionParcourtWord: + notFind = False + permissionParcourtWord = False + + if var != "" and var not in listVariable: + listVariable.append(var) + + if permissionParcourtWord and notFind: + var = ligne[m]+var + + m -=1 + + p +=1 + + return listVariable + +def findVariableFromList(line): + + i = 0 + if line.count(' in ') > 0: + variable = "" + find = False + findSpace = False + + while i < len(line): + + if line[i] == "i" and i+4 <= len(line): + + if i-1 > 0: + + if line[i-1] == " ": + + if line[i+1] == "n" and line[i+2] == " ": + find = True + + if not find: + + if line[i] != " ": + + if findSpace: + findSpace = False + variable = "" + + variable += line[i] + + else: + findSpace = True + + else: + return variable + + i +=1 + + return "" + + + +def switch(variable): + + switcher = { + len(variable) == 1: 1, #teste si une varaible contient plus d'une lettre + variable.lower() != variable and re.search(r'[A-Z]{' + str(len(variable)) + ',}',variable) == None : 1, #teste si une varaible commence par une majuscule + + } + + return switcher.get(True, 0) + + + + +def remove_comentary(lignes): + + + long_comment = False + code_without_comentary = [] + + for ligne in lignes: + + if "#" in ligne: + tab_line = ligne.split("#") + code_without_comentary.append(tab_line[0]) + + elif ligne.count("\"\"\"") == 2: + tab_line = ligne.split("\"\"\"") + new_line = tab_line[0] + tab_line[len(tab_line)-1] + code_without_comentary.append(new_line) + + elif "\"\"\"" in ligne: + tab_line = ligne.split("\"\"\"" ) + code_without_comentary.append(tab_line[0]) + long_comment = not long_comment + + elif "\"\"\"" in ligne: + tab_line = ligne.split("\"\"\"" ) + code_without_comentary.append(tab_line[len(tab_line)-1]) + long_comment = not long_comment + + elif not long_comment: + code_without_comentary.append(ligne) + + return code_without_comentary + + + +def excecEvalVariableName(lignes): + + scopeCodeUser = False + cpt = 0 + + listVariable = [] + + lignes = remove_comentary(lignes) + + for ligne in lignes: + + variables = "" + variables = findVariableDeclare(ligne) + + + for variable in variables: + + if variables != "" and variable not in listVariable: + if not set('[=<>+-*~!@#$%^&*().{}":;\']+$').intersection(variable): + listVariable.append(variable) + + + + + variable = "" + functionListVariable = findVariableInFuction(ligne) + + i = 0 + while i < len(functionListVariable): + if functionListVariable[i] not in listVariable: + if not set('[=<>+-*~!@#$%^&*().{}":;\']+$').intersection(functionListVariable[i]): + listVariable.append(functionListVariable[i]) + i +=1 + + variableFromList = findVariableFromList(ligne) + + if variableFromList != "" and variableFromList not in listVariable: + if not set('[=~!@#$%^&*()+.{}":;\']+$').intersection(variableFromList): + listVariable.append(variableFromList) + + + error = 0 + for var in listVariable: + error = error + switch(var) + + return error \ No newline at end of file diff --git a/heroku_deploy/api/evaluation_code/python/main.py b/heroku_deploy/api/evaluation_code/python/main.py new file mode 100644 index 0000000..2f58f08 --- /dev/null +++ b/heroku_deploy/api/evaluation_code/python/main.py @@ -0,0 +1,13 @@ +from evaluation_code.evalNbLigneFonctionCpp import excecEvalNbLigneFonction +from evaluation_code.evalCommentaireCpp import excecEvalCommentaire +from evaluation_code.evalRedondanceCpp import excecEvalRedondance +from evaluation_code.evalVariableNameCpp import excecEvalVariableName + +if __name__ == '__main__': + payload = { + "eval_variable_name\":excecEvalVariableName(), + "eval_redondance\": excecEvalRedondance(), + "eval_nb ligne_fonction\": excecEvalNbLigneFonction(), + "eval_commentaire\": excecEvalCommentaire() + } + print(payload) \ No newline at end of file diff --git a/heroku_deploy/api/libs/cpp/Exercice.h b/heroku_deploy/api/libs/cpp/Exercice.h index bfa7356..9171fb0 100644 --- a/heroku_deploy/api/libs/cpp/Exercice.h +++ b/heroku_deploy/api/libs/cpp/Exercice.h @@ -13,7 +13,7 @@ class Exercice std::string assertRes(int solution_user, int resultat); std::string assertResMult(std::vector solution_user, std::vector resultat); std::string assertResMultInt(std::vector solution_user, std::vector resultat); - std::string assertResMultDict(const std::map solution_user, const std::map resultat); + std::string assertResMultDict(const std::map solution_user, const std::map resultat); }; #endif /* MY_CLASS_H */ diff --git a/heroku_deploy/api/libs/cpp/bib.cpp b/heroku_deploy/api/libs/cpp/bib.cpp index 9024afc..96f0d33 100644 --- a/heroku_deploy/api/libs/cpp/bib.cpp +++ b/heroku_deploy/api/libs/cpp/bib.cpp @@ -4,7 +4,7 @@ #include #include #include - +#include #include "Matrice.h" #include "Opencv.h" #include "Exercice.h" @@ -104,13 +104,13 @@ std::string Exercice::assertResMult(std::vector solution_user, std::vect } } -std::string Exercice::assertResMultDict(const map solution_user,const map resultat){ +std::string Exercice::assertResMultDict(const map solution_user,const map resultat){ if(solution_user.size() != resultat.size()) return "ERROR"; - typename map::const_iterator i, j; + typename map::const_iterator i, j; for(i = solution_user.begin(), j = resultat.begin(); i != solution_user.end(); ++i, ++j) { if(*i != *j) diff --git a/heroku_deploy/api/src/app.module.ts b/heroku_deploy/api/src/app.module.ts index 4093a9a..781b64f 100644 --- a/heroku_deploy/api/src/app.module.ts +++ b/heroku_deploy/api/src/app.module.ts @@ -12,6 +12,8 @@ import { LanguagesModule } from './languages/languages.module'; import { ExecBootstrapsModule } from './exec-bootstrap/exec-bootstraps.module'; import { PicturesModule } from './pictures/pictures.module'; import { UserStatsModule } from './user-stats/user-stats.module'; +import { CatsModule } from 'src/evalPlagiat/evalPlagiat.module'; + @Module({ imports: [ MongooseModule.forRoot(dbConfig.url), @@ -24,6 +26,7 @@ import { UserStatsModule } from './user-stats/user-stats.module'; ExecBootstrapsModule, PicturesModule, UserStatsModule, + CatsModule, ], controllers: [AppController], providers: [AppService], diff --git a/heroku_deploy/api/src/attempts/attempt.schema.ts b/heroku_deploy/api/src/attempts/attempt.schema.ts index 4964d03..4a220de 100644 --- a/heroku_deploy/api/src/attempts/attempt.schema.ts +++ b/heroku_deploy/api/src/attempts/attempt.schema.ts @@ -21,6 +21,9 @@ export class Attempt { @Prop() phase: string; + @Prop() + result: boolean; + @Prop() status: number; diff --git a/heroku_deploy/api/src/attempts/attempts.module.ts b/heroku_deploy/api/src/attempts/attempts.module.ts index 4219849..f476396 100644 --- a/heroku_deploy/api/src/attempts/attempts.module.ts +++ b/heroku_deploy/api/src/attempts/attempts.module.ts @@ -4,6 +4,7 @@ import { AttemptsController } from './attempts.controller'; import { Attempt, AttemptSchema } from './attempt.schema'; import { AttemptsService } from './attempts.service'; import { ChallengesModule } from 'src/challenges/challenges.module'; +import { CatsModule } from 'src/evalPlagiat/evalPlagiat.module'; import { ExecServerModule } from 'src/exec-server/exec-server.module'; import { LanguagesModule } from 'src/languages/languages.module'; import { ExecBootstrapsModule } from 'src/exec-bootstrap/exec-bootstraps.module'; @@ -15,6 +16,7 @@ import { ExecBootstrapsModule } from 'src/exec-bootstrap/exec-bootstraps.module' ExecServerModule, LanguagesModule, ExecBootstrapsModule, + CatsModule, ], providers: [AttemptsService], exports: [AttemptsService], diff --git a/heroku_deploy/api/src/attempts/attempts.service.ts b/heroku_deploy/api/src/attempts/attempts.service.ts index 91783b5..f7c1897 100644 --- a/heroku_deploy/api/src/attempts/attempts.service.ts +++ b/heroku_deploy/api/src/attempts/attempts.service.ts @@ -2,6 +2,7 @@ import { Injectable, NotFoundException } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { ChallengesService } from 'src/challenges/challenges.service'; +import { EvalPlagiatService } from 'src/evalPlagiat/evalPlagiat.service'; import { FindByIdDTO } from 'src/common/dto/find-by-id.dto'; import { ExecBootstrapsService } from 'src/exec-bootstrap/exec-bootstraps.service'; import { GodBoxRepository } from 'src/exec-server/godbox.repository'; @@ -9,6 +10,7 @@ import { Attempt, AttemptDocument } from './attempt.schema'; import { ExecutionResultsDTO } from './dto/execution-results.dto'; import { FindByUserAndBootstrapDTO } from './dto/find-by-user-and-bootstrap.dto'; import { InsertAttemptDTO } from './dto/insert-attempt.dto'; +import { CreateCatDto } from 'src/evalPlagiat/dto/create-evalPlagiat.dto'; @Injectable() export class AttemptsService { @@ -18,6 +20,8 @@ export class AttemptsService { private readonly execServerService: GodBoxRepository, private readonly execBootstrapService: ExecBootstrapsService, private readonly challengesService: ChallengesService, + private readonly evalPlagiatService: EvalPlagiatService, + ) {} async create( @@ -38,6 +42,65 @@ export class AttemptsService { execBootstrap, ); + + const execResultsAlgoEvaluation = await this.execServerService.executeAlgoEvaluation( + insertAttemptDTO.code, + execBootstrap, + ); + + + const evaluation = execResultsAlgoEvaluation['stdout'] + const string = JSON.stringify(evaluation) + const list_content_start = string.split('['); + const list_content_stop = list_content_start[1].split(']'); + const list_content = list_content_stop[0]; + const elt = list_content.split(",") + + + + const challenge = await this.challengesService.findOne({ + id: execBootstrap.challenge, + }); + + + let plagiaStringSize = 0 + let stringSize = 0 + + + let n = 0; + while(n 0){ + plagiaStringSize = plagiaStringSize + element.length + }else{ + const res = await this.evalPlagiatService.create(plagiatCodeDto); + + } + n = n+1; + } + + + if ((plagiaStringSize*100)/(stringSize) >= 80){ + execResults["stdout"] = "PALGIAT" + }else{ + execResults["stdout"]=execResults["stdout"]+execResultsAlgoEvaluation["stdout"] + } + + + console.log("aaaa") + console.log((plagiaStringSize*100)/(stringSize)) + console.log("aaaa") + const attempt = ( await new this.attemptModel({ ...execResults, @@ -50,6 +113,8 @@ export class AttemptsService { return { ...execResults, id: attempt.id }; } + + async findOne(findAttemptDTO: FindByIdDTO): Promise { const attempt: Attempt = ( await this.attemptModel.findById(findAttemptDTO.id).exec() diff --git a/heroku_deploy/api/src/attempts/dto/evaluate-plagia.dto.ts b/heroku_deploy/api/src/attempts/dto/evaluate-plagia.dto.ts new file mode 100644 index 0000000..d565711 --- /dev/null +++ b/heroku_deploy/api/src/attempts/dto/evaluate-plagia.dto.ts @@ -0,0 +1,13 @@ +import { IsMongoId, IsNotEmpty, IsString } from 'class-validator'; + +export class EvaluatePlagiaDTO { + + @IsNotEmpty() + codeToken: string; + + @IsNotEmpty() + user: string; + + @IsNotEmpty() + exoName: string; +} diff --git a/heroku_deploy/api/src/evalPlagiat/dto/create-evalPlagiat.dto.ts b/heroku_deploy/api/src/evalPlagiat/dto/create-evalPlagiat.dto.ts new file mode 100644 index 0000000..b04162f --- /dev/null +++ b/heroku_deploy/api/src/evalPlagiat/dto/create-evalPlagiat.dto.ts @@ -0,0 +1,9 @@ + +export class CreateCatDto { + tokenCode: string; + + nameExo: string; + + userId: string; +} + \ No newline at end of file diff --git a/heroku_deploy/api/src/evalPlagiat/evalPlagiat.module.ts b/heroku_deploy/api/src/evalPlagiat/evalPlagiat.module.ts new file mode 100644 index 0000000..ec6d677 --- /dev/null +++ b/heroku_deploy/api/src/evalPlagiat/evalPlagiat.module.ts @@ -0,0 +1,12 @@ + +import { Module } from '@nestjs/common'; +import { MongooseModule } from '@nestjs/mongoose'; +import { EvalPlagiatService } from './evalPlagiat.service'; +import { EvalPlagiat, EvalPlagiatSchema } from './evalPlagiat.schema'; + +@Module({ + imports: [MongooseModule.forFeature([{ name: EvalPlagiat.name, schema: EvalPlagiatSchema }])], + providers: [EvalPlagiatService], + exports: [EvalPlagiatService], +}) +export class CatsModule {} diff --git a/heroku_deploy/api/src/evalPlagiat/evalPlagiat.schema.ts b/heroku_deploy/api/src/evalPlagiat/evalPlagiat.schema.ts new file mode 100644 index 0000000..48e3acb --- /dev/null +++ b/heroku_deploy/api/src/evalPlagiat/evalPlagiat.schema.ts @@ -0,0 +1,18 @@ +import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; +import { Document } from 'mongoose'; + +export type EvalPlagiatDocument = EvalPlagiat & Document; + +@Schema() +export class EvalPlagiat { + @Prop() + tokenCode: string; + + @Prop() + nameExo: string; + + @Prop() + userId: string; +} + +export const EvalPlagiatSchema = SchemaFactory.createForClass(EvalPlagiat); diff --git a/heroku_deploy/api/src/evalPlagiat/evalPlagiat.service.ts b/heroku_deploy/api/src/evalPlagiat/evalPlagiat.service.ts new file mode 100644 index 0000000..3e51fc8 --- /dev/null +++ b/heroku_deploy/api/src/evalPlagiat/evalPlagiat.service.ts @@ -0,0 +1,31 @@ + +import { Model } from 'mongoose'; +import { Injectable } from '@nestjs/common'; +import { InjectModel } from '@nestjs/mongoose'; +import { EvalPlagiat, EvalPlagiatDocument } from './evalPlagiat.schema'; +import { CreateCatDto } from './dto/create-evalPlagiat.dto'; + +@Injectable() +export class EvalPlagiatService { + + constructor(@InjectModel(EvalPlagiat.name) private evalPlagiatModel: Model) {} + + async create(createCatDto: CreateCatDto): Promise { + const createdCat = new this.evalPlagiatModel(createCatDto); + return createdCat.save(); + } + + async findAll(): Promise { + return this.evalPlagiatModel.find().exec(); + } + + async find(plagiatCodeDto : CreateCatDto): Promise { + return this.evalPlagiatModel.find({ + userId: {$ne: plagiatCodeDto.userId}, + tokenCode:plagiatCodeDto.tokenCode, + nameExo:plagiatCodeDto.nameExo, + + }) + } +} + diff --git a/heroku_deploy/api/src/exec-server/godbox.repository.ts b/heroku_deploy/api/src/exec-server/godbox.repository.ts index 1522906..f6e28b3 100644 --- a/heroku_deploy/api/src/exec-server/godbox.repository.ts +++ b/heroku_deploy/api/src/exec-server/godbox.repository.ts @@ -10,6 +10,8 @@ import { ChallengesService } from 'src/challenges/challenges.service'; import { Language } from 'src/languages/language.schema'; const LIBS_DIR = process.env.LIBS_DIR || '/usr/src/app/libs'; +const ALGO_DIR = process.env.ALGO_DIR || '/usr/src/app/evaluation_code'; + @Injectable() export class GodBoxRepository { constructor( @@ -75,6 +77,52 @@ export class GodBoxRepository { return zip.toBuffer().toString('base64'); } + + + private async bundleExecEvaluationCode( + code: string, + language: Language + ) { + const zip = new AdmZip(); + + const exec_algo = + "from evaluation_code.evalNbLigneFonction import excecEvalNbLigneFonction \n"+ + "from evaluation_code.evalCommentaire import excecEvalCommentaire \n"+ + "from evaluation_code.evalRedondance import excecEvalRedondance \n"+ + "from evaluation_code.evalVariableName import excecEvalVariableName \n"+ + "from evaluation_code.evalPlagiat import excecEvalPlagiat \n"+ + "if __name__ == '__main__': \n"+ + " import os \n"+ + " import re \n"+ + " filin = open(\"userCode.py\", \"r\") \n"+ + " lignes = filin.readlines()\n"+ + " new_lignes = []\n"+ + " for ligne in lignes:\n"+ + " ligne.replace(\"\\n\", \"\")\n"+ + " new_lignes.append(ligne)\n"+ + " lignes = new_lignes\n"+ + " payload = { \n"+ + " \"eval_variable_name\":excecEvalVariableName(lignes), \n"+ + " \"eval_redondance\": excecEvalRedondance(lignes), \n"+ + " \"eval_nb ligne_fonction\": excecEvalNbLigneFonction(lignes), \n"+ + " \"eval_commentaire\": excecEvalCommentaire(lignes), \n"+ + " \"eval_plagiat\": excecEvalPlagiat(lignes) \n"+ + + " } \n"+ + " filin.close() \n"+ + " print(payload) \n" + + + zip.addLocalFolder(`${ALGO_DIR}/${language.name}`, 'evaluation_code'); + zip.addFile(`userCode.py`, Buffer.from(code)); + zip.addFile(`main.py`, Buffer.from(exec_algo)); + + + return zip.toBuffer().toString('base64'); + } + + + formatPhasesResults(phases: GodboxPhaseOutputDTO[]): GodboxPhaseOutputDTO { const res = phases[phases.length - 1]; return { @@ -108,4 +156,31 @@ export class GodBoxRepository { ); } } + + + async executeAlgoEvaluation( + code: string, + bootstrap: ExecBootstrap, + ): Promise { + const language = await this.languagesService.findByName("python"); + + + const payload = { + phases: language.phases, + files: await this.bundleExecEvaluationCode(code, language), + }; + + + try { + const { data }: { data: { phases: GodboxPhaseOutputDTO[] } } = + await axios.post(`${godboxConfig.baseUrl}/run`, payload); + return this.formatPhasesResults(data.phases); + } catch (err) { + throw new InternalServerErrorException( + err?.response?.data?.message || err?.message || 'Unkown reason.', + ); + } + } + + } diff --git a/heroku_deploy/api/src/object-storage/object-storage.service.ts b/heroku_deploy/api/src/object-storage/object-storage.service.ts index a7b5ab0..9695187 100644 --- a/heroku_deploy/api/src/object-storage/object-storage.service.ts +++ b/heroku_deploy/api/src/object-storage/object-storage.service.ts @@ -16,7 +16,8 @@ export default class ObjectStorageService { constructor(private readonly minioService: MinioService) {} public generateInternalServerAddress(endOfLink: string) { - console.log("COUCOUTUTUINT") + + return `http${config.MINIO_USESSL ? 's' : ''}://${ config.MINIO_INTERNAL_ENDPOINT @@ -24,7 +25,7 @@ export default class ObjectStorageService { } public generateExternalServerAddress(endOfLink: string) { - console.log("COUCOUTUTUEXT") + // https://minio.picspy.vagahbond.com/minio/picspy-challenges/AyoubTest/c5b3f69c-5222-4623-9993-792f7d8487491627296856152.PNG return `http${config.MINIO_USESSL ? 's' : ''}://${ diff --git a/heroku_deploy/api/src/seed/seeds.module.ts b/heroku_deploy/api/src/seed/seeds.module.ts index b13f1ee..b354594 100644 --- a/heroku_deploy/api/src/seed/seeds.module.ts +++ b/heroku_deploy/api/src/seed/seeds.module.ts @@ -20,6 +20,7 @@ import { LanguagesCommand } from './languages.command'; forwardRef(() => ExecBootstrapsModule), forwardRef(() => SeriesModule), forwardRef(() => UsersModule), + ], providers: [LanguagesCommand], exports: [LanguagesCommand], diff --git a/heroku_deploy/api/src/series/series.service.ts b/heroku_deploy/api/src/series/series.service.ts index 0adb0b7..b7cdc21 100644 --- a/heroku_deploy/api/src/series/series.service.ts +++ b/heroku_deploy/api/src/series/series.service.ts @@ -49,13 +49,18 @@ export class SeriesService { } async findDefaultSeries(): Promise { - return (await this.seriesModel.find({ isCourse: true }).exec()).map((a) => + return (await this.seriesModel.find({ name: "default" }).exec()).map((a) => a.toObject(), ); } async create(createSerieDTO: InsertSerieDTO): Promise { + + console.log("createSerieDTO") + console.log(createSerieDTO) + if (await this.findByName(createSerieDTO.name)) { + throw new UnprocessableEntityException( `Serie name ${createSerieDTO.name} has already been taken.`, ); diff --git a/heroku_deploy/api/src/user-stats/user-stats.controller.ts b/heroku_deploy/api/src/user-stats/user-stats.controller.ts index 31f9000..f467cfb 100644 --- a/heroku_deploy/api/src/user-stats/user-stats.controller.ts +++ b/heroku_deploy/api/src/user-stats/user-stats.controller.ts @@ -8,6 +8,8 @@ export class UserStatsController { @Get('default_series/:id') async completion(@Param() userIdObject: FindByIdDTO) { + console.log("userIdObject") + console.log(userIdObject) return await this.userStatsService.getCompletion(userIdObject); } diff --git a/heroku_deploy/api/src/user-stats/user-stats.service.ts b/heroku_deploy/api/src/user-stats/user-stats.service.ts index 9763e29..a68d254 100644 --- a/heroku_deploy/api/src/user-stats/user-stats.service.ts +++ b/heroku_deploy/api/src/user-stats/user-stats.service.ts @@ -23,6 +23,11 @@ export class UserStatsService { ): Promise { const defaultSeries = await this.seriesService.findDefaultSeries(); + console.log("defaultSeries") + console.log(defaultSeries) + console.log("userIdDTO") + console.log(userIdDTO) + const attemptsToStats = async ( challengeId: string, userId: string, @@ -31,12 +36,18 @@ export class UserStatsService { id: challengeId, }); + const attempts = await this.attemptsService.findValidatedByUserAndChallenge( userId, challengeId, ); + + + console.log("attempts") + console.log(attempts) + let bestTime: number; if (attempts.length > 0) { bestTime = attempts.reduce((prev, next) =>