cjson.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. from __future__ import print_function
  2. import sys
  3. import os
  4. sys.path.extend(['.', '..'])
  5. from pycparser import parse_file, c_ast
  6. from Queue import Queue
  7. from threading import Thread
  8. import re
  9. import files
  10. import analyser
  11. COMMENT_REGEX = r"(//.*)|(/\*[\w\W\n\r]*?\*/)"
  12. USEFUL_REGEX = r"(//.*)|(/\*[\w\W\n\r]*?\*/)|(^\s*$)|(\{\s*\})|(^\s*\{\s*$)|(^\s*\}\s*$)"
  13. CSV_HEADER = ['assignment_id', 'student_id', 'total_submissions', 'code_lines','total_lines','comments']
  14. CSV_HEADER.extend(['!', '<=', '%', '>=', '++', '+', '*', '-', '/', '<', '--', '&&', 'p++', '\'==\'', 'p--', '!=', '||', '>'])
  15. COMMANDS = ['Return', 'For', 'FuncCall', 'Assignment', 'Switch', 'DoWhile', 'While', 'FuncDef', 'If']
  16. CSV_HEADER.extend(COMMANDS)
  17. DECLARATIONS = ['vector_int', 'vector_float', 'matrix_string', 'string', 'int', 'pointer_int', 'float', 'matrix_int', 'pointer_float', 'matrix_float']
  18. CSV_HEADER.extend(DECLARATIONS)
  19. finalDataList = list()
  20. class Worker (Thread):
  21. """Thread executing tasks from a given tasks queue"""
  22. def __init__ (self, tasks):
  23. Thread.__init__(self)
  24. self.tasks = tasks
  25. self.daemon = True
  26. self.start()
  27. def run (self):
  28. while True:
  29. func, args, kargs = self.tasks.get()
  30. try: func(*args, **kargs)
  31. except Exception as e:
  32. print(e)
  33. self.tasks.task_done()
  34. class ThreadPool:
  35. """Pool of threads consuming tasks from a queue"""
  36. def __init__ (self, num_threads):
  37. self.tasks = Queue(num_threads)
  38. for _ in range(num_threads): Worker(self.tasks)
  39. def add_task (self, func, *args, **kargs):
  40. """Add a task to the queue"""
  41. self.tasks.put((func, args, kargs))
  42. def wait_completion (self):
  43. """Wait for completion of all the tasks in the queue"""
  44. self.tasks.join()
  45. def processFile (filename):
  46. if len(filename) == 0:
  47. return list()
  48. ast = parse_file(filename, use_cpp=True,
  49. cpp_path='gcc',
  50. cpp_args=['-E', r'-Iutils/fake_libc_include'])
  51. nodeList = [node for (_, node) in ast.children() if node.__class__.__name__ is 'FuncDef' or node.__class__.__name__ is 'Decl']
  52. return nodeList
  53. def processStudentData (studentData, assignment):
  54. try:
  55. result = processFile(studentData[1])
  56. if len(result) == 0:
  57. return
  58. data = analyser.ASTAnalyser(assignment, studentData[0], result)
  59. data.beginAnalysis()
  60. fileText = open(studentData[1], 'r').read()
  61. totalLines = fileText.count("\n")
  62. totalComments = len(re.findall(COMMENT_REGEX, fileText, re.MULTILINE))
  63. usefulText = re.sub(USEFUL_REGEX, "", fileText, flags=re.MULTILINE)
  64. usefulText = os.linesep.join([s for s in usefulText.splitlines() if s])
  65. usefulLines = usefulText.count("\n")
  66. finalDataList.append((data, studentData[2], totalLines, totalComments, usefulLines))
  67. print("Processing data from student %s at assignment %s \n Commands: %s \n Declarations:\n\tBasic:%s\n\tMatrix:%s\n\tPointers:%s\n\tVectors:%s \n Operators: %s \n Constants: %s \n For data: %s \n Condition data: %s \n Lines:%s, Comments:%s, Useful:%s" % (studentData[0], assignment, data.commandCount, data.declarations, data.declarationsMatrixes, data.declarationsPointers, data.declarationsVectors, data.operatorsCount, data.constantInitCount, data.forCommandStr(), data.conditionCommandStr(), totalLines, totalComments, usefulLines))
  68. except Exception as e:
  69. print("%s! Failed to process file: %s" % (e, studentData[1]))
  70. def loadStudentsFolders (raiz, folder):
  71. userDataPath = os.path.join(raiz, folder)
  72. return files.filesFromFolder(userDataPath, "usersdata")
  73. def loadStudentFiles (raiz, folder, studentsFolders):
  74. studentsData = []
  75. for s in studentsFolders:
  76. path = os.path.join(raiz, folder, "usersdata", s)
  77. totalSub = files.countFolders(path)
  78. finalFolder = files.highestFileName(path)
  79. cFileFolder = os.path.join(path, finalFolder, "submittedfiles")
  80. cFiles = files.getFilesInFolder(cFileFolder, "*[cC]*")
  81. if len(cFiles) == 0:
  82. studentsData.append((s, "", totalSub))
  83. else:
  84. studentsData.append((s, cFiles[0], totalSub))
  85. return studentsData
  86. def loadAssignments (raiz):
  87. assignmentsFolders = files.filesFromFolder(raiz, "")
  88. assignments = {}
  89. for a in assignmentsFolders:
  90. studentsFolders = loadStudentsFolders(raiz, a)
  91. if(len(studentsFolders) == 0):
  92. assignments[a] = []
  93. continue
  94. studentsData = loadStudentFiles(raiz, a, studentsFolders)
  95. assignments[a] = studentsData
  96. return assignments
  97. def initEmptyDict (list):
  98. result = dict()
  99. for k in list:
  100. result[k] = 0
  101. return result
  102. #--- run ---#
  103. if __name__ == "__main__":
  104. if len(sys.argv) > 1:
  105. file = sys.argv[1]
  106. if file == "-f" and len(sys.argv) > 2:
  107. print(processFile(sys.argv[2]))
  108. elif file != "-f":
  109. raiz = "./" + sys.argv[1]
  110. data = loadAssignments(raiz)
  111. pool = ThreadPool(10)
  112. for a in data:
  113. for studentData in data[a]:
  114. pool.add_task(processStudentData, studentData, a)
  115. pool.wait_completion()
  116. csvfile = ""
  117. assignmentList = dict()
  118. for data in finalDataList:
  119. if data[0].assignment in assignmentList:
  120. assignmentList[data[0].assignment].append(data)
  121. else:
  122. assignmentList[data[0].assignment] = list()
  123. assignmentList[data[0].assignment].append(data)
  124. for assignmentKey in assignmentList:
  125. for studentData in assignmentList[assignmentKey]:
  126. astInfo = studentData[0]
  127. studentOpData = initEmptyDict(analyser.VALID_OPS)
  128. for key in astInfo.operatorsCount:
  129. studentOpData[key] = astInfo.operatorsCount[key]
  130. studentCommandData = initEmptyDict(COMMANDS)
  131. for key in astInfo.commandCount:
  132. studentCommandData[key] = astInfo.commandCount[key]
  133. studentDeclarationData = initEmptyDict(DECLARATIONS)
  134. for key in astInfo.declarations:
  135. studentDeclarationData[key] = astInfo.declarations[key]
  136. for key in astInfo.declarationsPointers:
  137. studentDeclarationData["pointer_" + key] = astInfo.declarationsPointers[key]
  138. for key in astInfo.declarationsVectors:
  139. studentDeclarationData["vector_" + key] = astInfo.declarationsVectors[key]
  140. for key in astInfo.declarationsMatrixes:
  141. studentDeclarationData["matrix_" + key] = astInfo.declarationsMatrixes[key]
  142. csvfile += "%s,%s,%s,%s,%s,%s" % (assignmentKey, astInfo.student, studentData[1], studentData[4], studentData[2], studentData[3])
  143. csvfile += "," + ','.join([str(v) for v in studentOpData.values()])
  144. csvfile += "," + ",".join([str(v) for v in studentCommandData.values()])
  145. csvfile += "," + ",".join([str(v) for v in studentDeclarationData.values()])
  146. csvfile += "\n"
  147. csvfile = ','.join(CSV_HEADER) + '\n' + csvfile
  148. file = open("data.csv", "w+")
  149. file.write(csvfile)
  150. file.close()
  151. else:
  152. print("cjson -f file | cjon folder/")
  153. else:
  154. print("cjson -f file | cjon folder/")