提交 4b5f3b83 创建 作者: 宋海霞's avatar 宋海霞

提交作业

上级 ad602ec6
#!/bin/env python
# -*- coding: utf-8 -*-
import difflib
import sys
import argparse
import webbrowser
import os
import filecmp
import subprocess
import platform
from comtool import *
if __name__ == "__main__":
dsType, dsFileStr, resultFileType, userresultFileStr, resultFileStr = getProjInfo()
runProgCmdStr = getRunProgCmdStr(dsType, resultFileType, dsFileStr.format(1), userresultFileStr.format(1))
runCommand = "make && valgrind --xml=yes --xml-file=./build/valgrind-report.xml {0}".format(runProgCmdStr)
runCmd(runCommand)
\ No newline at end of file
#!/bin/env python
# -*- coding: utf-8 -*-
import os
import platform
import sys
from enum import Enum
execProgStr = sys.argv[1]
class DataSourceFileType(Enum):
noinput = 0
input = 1
readtxt = 2
readdat = 3
class ResultFileType(Enum):
nooutput = 0
output = 1
writetxt = 2
writedat = 3
class PlatformType(Enum):
other = 0
windows = 1
linux = 2
# 读取文件
def read_file(file_name):
try:
file_desc = open(file_name, 'r')
# 读取后按行分割
text = file_desc.read().splitlines()
file_desc.close()
return text
except IOError as error:
print('Read input file Error: {0}'.format(error))
sys.exit()
# 比较时忽略行末的空格和文件末尾的回车换行
def advanced_file_compare(file1, file2):
f1 = open(file1)
f2 = open(file2)
returnVal = 1
str1 = []
for line1 in f1:
line1 = line1.rstrip()
line1 = line1.replace('\n', '')
if len(line1) != 0:
str1.append(line1)
str2 = []
for line2 in f2:
line2 = line2.rstrip()
line2 = line2.replace('\n', '')
if len(line2) != 0:
str2.append(line2)
count = 0
if len(str1) == len(str2):
for line1 in str1:
if line1 != str2[count]: #文件不同
returnVal = 0
break
else:
count = count + 1
else:
returnVal = 0
f1.close()
f2.close()
return returnVal
def advanced_dat_file_compare(file1, file2):
returnVal = 1
runCommand = "diff {0} {1} -b -B -y -i -W 100".format(file1, file2)
execResult = os.system(runCommand)
if execResult != 0:
returnVal = 0
return returnVal
def getPlatformType():
platformType = PlatformType.other
if platform.system().lower() == 'windows':
platformType = PlatformType.windows
elif platform.system().lower() == 'linux':
platformType = PlatformType.linux
else:
platformType = PlatformType.other
return platformType
def getProjInfo():
dsType = getDataSourceFileType()
dsFileStr = getDataSourceFileStr(dsType)
resultFileType = getResultFileType()
resultFileStr = getResultFileStr(resultFileType)
userresultFileStr = getUserResultFileStr(resultFileType)
return dsType, dsFileStr, resultFileType, userresultFileStr, resultFileStr
def getDataSourceFileType():
dsType = DataSourceFileType.noinput
if os.path.isfile("input1.txt"):
dsType = DataSourceFileType.input
elif os.path.isfile("readfile1.txt"):
dsType = DataSourceFileType.readtxt
elif os.path.isfile("readfile1.dat"):
dsType = DataSourceFileType.readdat
else:
dsType = DataSourceFileType.noinput
return dsType
def getDataSourceFileStr(dataSourceFileType):
dsType = dataSourceFileType
dsFileStr = ""
if dsType == DataSourceFileType.readtxt:
dsFileStr = "readfile{0}.txt"
elif dsType == DataSourceFileType.readdat:
dsFileStr = "readfile{0}.dat"
elif dsType == DataSourceFileType.input:
dsFileStr = "input{0}.txt"
return dsFileStr
def getResultFileType():
resultFileType = 0
if os.path.isfile("output1.txt"):
resultFileType = ResultFileType.output
elif os.path.isfile("writefile1.txt"):
resultFileType = ResultFileType.writetxt
elif os.path.isfile("writefile1.dat"):
resultFileType = ResultFileType.writedat
else:
resultFileType = ResultFileType.nooutput
return resultFileType
def getResultFileStr(resultFileType):
resultFileStr = ""
type = resultFileType
if type == ResultFileType.output:
resultFileStr = "output{0}.txt"
elif type == ResultFileType.writetxt:
resultFileStr = "writefile{0}.txt"
elif type == ResultFileType.writedat:
resultFileStr = "writefile{0}.dat"
return resultFileStr
def getUserResultFileStr(resultFileType):
userresultFileStr = ""
type = resultFileType
if type == ResultFileType.output:
userresultFileStr = "user_output{0}.txt"
elif type == ResultFileType.writetxt:
userresultFileStr = "user_writefile{0}.txt"
elif type == ResultFileType.writedat:
userresultFileStr = "user_writefile{0}.dat"
return userresultFileStr
def getSpecifyfileStr():
specifyFile = ""
if os.path.isfile("file.dat"):
specifyFile = "file.dat"
elif os.path.isfile("file.txt"):
specifyFile = "file.txt"
return specifyFile
def getRunProgCmdStr(dsType, resultFileType, dsFile, userresultFile):
global execProgStr
inresultFileStr = ""
if dsType == DataSourceFileType.input:
if resultFileType == ResultFileType.output:
inresultFileStr = " < {0} > {1} ".format(dsFile, userresultFile)
elif resultFileType == ResultFileType.writedat or resultFileType == ResultFileType.writetxt:
inresultFileStr = " {1} < {0} ".format(dsFile, userresultFile)
elif dsType == DataSourceFileType.readtxt or dsType == DataSourceFileType.readdat:
if resultFileType == ResultFileType.output:
inresultFileStr = " {0} > {1} ".format(dsFile, userresultFile)
elif resultFileType == ResultFileType.writedat or resultFileType == ResultFileType.writetxt:
inresultFileStr = " {0} {1} ".format(dsFile, userresultFile)
else:
if resultFileType == ResultFileType.output:
inresultFileStr = " > {0} ".format(userresultFile)
elif resultFileType == ResultFileType.writedat or resultFileType == ResultFileType.writetxt:
inresultFileStr = " {0} ".format(userresultFile)
runCommandStr = execProgStr
runCommandStr += inresultFileStr
return runCommandStr
def runCmd(cmdStr):
print("正在执行命令: {0}".format(cmdStr))
sys.stdout.flush()
execResult = os.system(cmdStr)
if execResult != 0:
exit(1)
import sys
import argparse
import os
import filecmp
import subprocess
import platform
from comtool import *
if __name__ == "__main__":
dsType, dsFileStr, resultFileType, userresultFileStr, resultFileStr = getProjInfo()
runProgCmdStr = getRunProgCmdStr(dsType, resultFileType, dsFileStr.format(1), userresultFileStr.format(1))
runCommandStr = "valgrind --leak-check=full {0}".format(runProgCmdStr)
if platform.system().lower() == 'windows':
runCommandStr = "make && drmemory -ignore_kernel -leaks_only -brief -batch -- {0}".format(runProgCmdStr)
runCmd(runCommandStr)
......@@ -5,16 +5,15 @@
"label": "生成项目(make)",
"type": "shell",
"windows": {
"command": "cat \"${execPath}.tips\\env_tip.txt\" ; echo VSCode安装路径-${execPath} ; echo 正在使用makefile文件生成项目 ; make"
"command": "cmd.exe /c type \"'${execPath}.tips\\env_tip.txt'\" ; echo \"`\" ; cmd.exe /c echo VSCode安装路径-${execPath} ; echo 正在使用makefile文件生成项目 ; cmd.exe /c type \"'${execPath}.tips\\make_tip.txt'\" ; echo \"`\" ; make"
},
"linux": {
"command": "echo 正在使用makefile文件生成项目 && make && echo 生成项目成功"
"command": "echo 更多帮助请参考 https://www.codecode.net/engintime/codecode/publicmanual/blob/master/VSCodeFAQ.md && echo 正在使用makefile文件生成项目 && echo VSCode安装路径-'${execPath}' && echo 正在使用makefile文件生成项目 && echo \"如果报告 ld.exe: cannot open output file ***.exe: Permission denied 错误,可以 重启 vscode 或者 重启计算机\" && make && echo 生成项目成功"
},
"args": [],
"group": "build",
"presentation": {
"reveal": "always",
"focus": true,
"group": "make"
},
"problemMatcher": "$gcc"
......@@ -23,10 +22,10 @@
"label": "安装python包",
"type": "shell",
"windows": {
"command": "pip install -i https://mirrors.aliyun.com/pypi/simple/ -r requirements.txt"
"command": "echo 更多帮助请参考https://www.codecode.net/engintime/codecode/publicmanual/blob/master/VSCodeFAQ.md ; pip install -i https://mirrors.aliyun.com/pypi/simple/ -r requirements.txt"
},
"linux": {
"command": "pip3 install -i https://mirrors.aliyun.com/pypi/simple/ -r requirements.txt"
"command": "echo 更多帮助请参考 https://www.codecode.net/engintime/codecode/publicmanual/blob/master/VSCodeFAQ.md && pip3 install -i https://mirrors.aliyun.com/pypi/simple/ -r requirements.txt"
},
"args": [],
"group": "build",
......@@ -40,10 +39,10 @@
"label": "测试(test)",
"type": "shell",
"windows": {
"command": "cat \"${execPath}.tips\\python_tip.txt\" ; python .vscode\\test.py"
"command": "cmd.exe /c type \"'${execPath}.tips\\python_tip.txt'\" ; echo \"`\" ; python .vscode\\test.py .\\app.exe"
},
"linux": {
"command": "python3 .vscode/test.py"
"command": "echo 更多帮助请参考 https://www.codecode.net/engintime/codecode/publicmanual/blob/master/VSCodeFAQ.md && python3 .vscode/test.py ./app.exe"
},
"args": [],
"group": "build",
......@@ -57,10 +56,10 @@
"label": "清理项目(make clean)",
"type": "shell",
"windows": {
"command": "cat \"${execPath}.tips\\env_tip.txt\" ; echo VSCode安装路径-${execPath} ; echo 正在使用makefile文件清理项目 ; make clean"
"command": "cmd.exe /c type \"'${execPath}.tips\\env_tip.txt'\" ; echo \"`\" ; cmd.exe /c echo VSCode安装路径-${execPath} ; echo \"`\" ; echo 正在使用makefile文件清理项目 ; make clean"
},
"linux": {
"command": "echo 正在使用makefile文件清理项目 && make clean && echo 清理项目成功"
"command": "echo 更多帮助请参考 https://www.codecode.net/engintime/codecode/publicmanual/blob/master/VSCodeFAQ.md && echo 正在使用makefile文件清理项目 && make clean && echo 清理项目成功"
},
"args": [],
"group": "build",
......@@ -74,10 +73,10 @@
"label": "提交作业(git push)",
"type": "shell",
"windows": {
"command": "cat \"${execPath}.tips\\python_tip.txt\" ; python .vscode\\gitpush.py"
"command": "cmd.exe /c type \"'${execPath}.tips\\python_tip.txt'\" ; echo \"`\" ; python .vscode\\gitpush.py"
},
"linux": {
"command": "python3 .vscode/gitpush.py"
"command": "echo 更多帮助请参考 https://www.codecode.net/engintime/codecode/publicmanual/blob/master/VSCodeFAQ.md && python3 .vscode/gitpush.py"
},
"args": [],
"group": "build",
......@@ -91,11 +90,11 @@
"label": "内存检测",
"type": "shell",
"windows": {
"command": "cat \"${execPath}.tips\\env_tip.txt\" ; echo VSCode安装路径-${execPath} ; echo 正在使用Dr.Memory检测内存 ; make ; cmd.exe /c \"drmemory -ignore_kernel -leaks_only -brief -batch -- .\\app.exe '<' input1.txt\""
"command": "cmd.exe /c type \"'${execPath}.tips\\env_tip.txt'\" ; echo \"`\" ; cmd.exe /c echo VSCode安装路径-${execPath} ; echo \"`\" ; echo 正在使用Dr.Memory检测内存 ; python .vscode/memorycheck.py .\\app.exe"
},
"linux": {
"command": "echo 正在使用 Valgrind 检测内存 && make && echo ***如果提示 /bin/bash: valgrind: command not found 可使用终端命令 sudo apt install valgrind 安装(需要连接互联网)*** && valgrind --leak-check=full ./app.exe < input1.txt"
},
"command": "echo 更多帮助请参考 https://www.codecode.net/engintime/codecode/publicmanual/blob/master/VSCodeFAQ.md && echo 正在使用 Valgrind 检测内存 && make && echo ***如果提示 /bin/bash: valgrind: command not found 可使用终端命令 sudo apt install valgrind 安装(需要连接互联网)*** && python3 .vscode/memorycheck.py ./app.exe"
},
"args": [],
"group": "build",
"presentation": {
......
#!/bin/env python
# -*- coding: utf-8 -*-
import argparse
import difflib
import filecmp
import os
import platform
import subprocess
import sys
import webbrowser
from enum import Enum
from comtool import *
class CmpFileType:
unknown = 0
templatedatasource = 1
templateresult = 2
userprojresult = 3
def getFileCount(fileStr):
fileCount = 1
while 1:
resultFile = fileStr.format(fileCount)
if not os.path.isfile(resultFile):
fileCount -= 1
break
fileCount += 1
return fileCount
# 比较两个文件并把结果生成一份html文本
def compare_file(file1, file2, type):
returnVal = 0
bDat = False
if file1 == "" or file2 == "":
print('文件路径不能为空:第一个文件的路径:{0}, 第二个文件的路径:{1} .'.format(file1, file2))
sys.exit()
else:
resultFileType = getResultFileType()
dsFileType = getDataSourceFileType()
if type == CmpFileType.userprojresult:
promptInfo = "正在比较标准答案结果文件 {0} 和用户编写的应用程序结果文件 {1}"
if resultFileType == ResultFileType.writedat:
bDat = True
elif type == CmpFileType.templateresult:
promptInfo = "正在比较模板中的标准答案结果文件 {0} 和用户程序中的标准答案结果文件 {1}"
if resultFileType == ResultFileType.writedat:
bDat = True
elif type == CmpFileType.templatedatasource:
promptInfo = "正在比较模板中的数据源文件 {0} 和用户程序中的数据源文件 {1}"
if dsFileType == DataSourceFileType.readdat:
bDat = True
print(promptInfo.format(file1, file2), end=': ')
sys.stdout.flush()
if os.path.isfile(file1) and os.path.isfile(file2):
comResult = 0
if bDat:
comResult = advanced_dat_file_compare(file1, file2)
else:
comResult = advanced_file_compare(file1, file2)
if comResult == 1:
print("文件相同")
print()
sys.stdout.flush()
returnVal = 1
else:
print("文件不同")
print()
sys.stdout.flush()
return returnVal
if __name__ == "__main__":
dsType, dsFileStr, resultFileType, userresultFileStr, resultFileStr = getProjInfo()
# 检查数据源文件一致性
dsFileCount = getFileCount(dsFileStr)
for i in range(1, dsFileCount + 1):
dsFile = dsFileStr.format(i)
userresultFile = userresultFileStr.format(i)
runProgCommand = getRunProgCmdStr(dsType, resultFileType, dsFile, userresultFile)
runCmd(runProgCommand)
print("正在检查{0}文件的一致性".format(dsFile))
sys.stdout.flush()
if compare_file("template/{0}".format(dsFile), dsFile, CmpFileType.templatedatasource) == 0:
runCmpCommand = "diff template/{0} {0} -b -B -y -i --suppress-common-lines".format(dsFile)
runCmd(runCmpCommand)
exit(1)
# 检查输出文件的一致性
resultFileCount = getFileCount(resultFileStr)
for i in range(1, resultFileCount + 1):
dsFile = dsFileStr.format(i)
userresultFile = userresultFileStr.format(i)
runProgCommand = getRunProgCmdStr(dsType, resultFileType, dsFile, userresultFile)
runCmd(runProgCommand)
resultFile = resultFileStr.format(i)
print("正在检查{0}文件的一致性".format(resultFile))
sys.stdout.flush()
if compare_file( "template/{0}".format(resultFile), resultFile, CmpFileType.templateresult) == 0:
runCmpCommand = "diff template/{0} {0} -b -B -y -i --suppress-common-lines".format(resultFile)
runCmd(runCmpCommand)
exit(1)
# 执行完成文件完整性检查
os.system("fileidentity.sh")
if sys.argv[2] != "":
# 克隆教师检查项目,参数:${CODECODE_PROTOCOL}gitlab-ci-token:${CI_JOB_TOKEN}@${CODECODE_DOMAIN}/${TEACHERCHECK_REPO}
runCloneCommand = "git clone {0} teacher_check".format(sys.argv[3])
runCmd(runCloneCommand)
teaCheckCount = getFileCount("teacher_check/" + dsFileStr)
for i in range(1, teaCheckCount + 1):
print("正在检查附加算例case{0}".format(i))
sys.stdout.flush()
# runProgCommand = "./app.exe < teacher_check/input{0}.txt > user_output{0}.txt".format(i)
# execResult = os.system(runProgCommand)
dsFile = "teacher_check/{0}".format(dsFileStr.format(i))
userresultFile = userresultFileStr.format(i)
runProgCmdStr = getRunProgCmdStr(dsType, resultFileType, dsFile, userresultFile)
runCmd(runProgCmdStr)
resultFile = resultFileStr.format(i)
if compare_file( "teacher_check/{0}".format(resultFile), userresultFile, CmpFileType.userprojresult) == 0:
runCmpCommand = "diff teacher_check/{0} {1} -b -B -y -i -W 100".format(resultFile, userresultFile)
runCmd(runCmpCommand)
exit(1)
# 处理读取指定文件
if teaCheckCount == 0:
specifyfile = getSpecifyfileStr()
if os.path.isfile("teacher_check/" + specifyfile):
runCpCommand = "cp teacher_check/{0} {0}".format(specifyfile)
runCmd(runCpCommand)
print("正在检查附加算例")
userresultFile = userresultFileStr.format(1)
runProgCmdStr = getRunProgCmdStr(dsType, resultFileType, "", userresultFile)
runCmd(runProgCmdStr)
resultFile = resultFileStr.format(1)
if compare_file( "teacher_check/{0}".format(resultFile), userresultFile, CmpFileType.userprojresult) == 0:
runCmpCommand = "diff teacher_check/{0} {1} -b -B -y -i -W 100".format(resultFile, userresultFile)
runCmd(runCmpCommand)
exit(1)
# 执行完成附加算例检查
os.system("extracase.sh")
\ No newline at end of file
差异被折叠。
......@@ -26,5 +26,6 @@ int main()
}
}
cout << endl;
free(z);
return 0;
}
......@@ -15,7 +15,7 @@ CFLAGS = -g -w -fmax-errors=10 -std=c++11 -fsigned-char -I${DIR_INC}
${BIN_TARGET}:${OBJ}
@echo --- build executable file: $@
$(CC) $(OBJ) -o $@
$(CC) $(OBJ) -o $@ -lm
${DIR_OBJ}/%.o:${DIR_SRC}/%.cpp
@echo --- build object file: $@
......@@ -32,4 +32,4 @@ else
endif
clean:
$(RM) *.exe *.o result_comparation.html user_output*
\ No newline at end of file
$(RM) *.exe *.o result_comparation.html user_*
\ No newline at end of file
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论