提交 284f119e 创建 作者: 宋海霞's avatar 宋海霞

更新 local_test_c.py

上级 60c4a3d3
流水线 #151541 已失败 于阶段
用时 42 秒
#!/bin/env python #!/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# 不要删除或修改此文件
import difflib import difflib
import sys import sys
import argparse import argparse
...@@ -19,8 +17,9 @@ from colorama import Fore, init, AnsiToWin32 ...@@ -19,8 +17,9 @@ from colorama import Fore, init, AnsiToWin32
init(wrap=False) init(wrap=False)
stream = AnsiToWin32(sys.stderr).stream stream = AnsiToWin32(sys.stderr).stream
red = lambda text: '\033[0;31m' + text + '\033[0m' red = lambda text: '\033[0;31;1m' + text + '\033[0m'
green = lambda text: '\033[0;32m' + text + '\033[0m' green = lambda text: '\033[0;32;1m' + text + '\033[0m'
yellow = lambda text: '\033[0;33;1m' + text + '\033[0m'
# 读取文件 # 读取文件
def read_file(file_name): def read_file(file_name):
...@@ -34,6 +33,8 @@ def read_file(file_name): ...@@ -34,6 +33,8 @@ def read_file(file_name):
print('Read input file Error: {0}'.format(error)) print('Read input file Error: {0}'.format(error))
sys.exit() sys.exit()
# 比较时忽略行末的空格和文件末尾的回车换行 # 比较时忽略行末的空格和文件末尾的回车换行
def advanced_file_compare(file1, file2): def advanced_file_compare(file1, file2):
f1 = open(file1) f1 = open(file1)
...@@ -71,25 +72,34 @@ def advanced_file_compare(file1, file2): ...@@ -71,25 +72,34 @@ def advanced_file_compare(file1, file2):
return returnVal return returnVal
# 比较两个文件并把结果生成一份html文本 # 比较两个文件并把结果生成一份html文本
def compare_file(file1, file2, seqNum): def compare_file(file1, file2, seqNum, caseCount):
returnVal = 0 returnVal = 0
if file1 == "" or file2 == "": if file1 == "" or file2 == "":
print('文件路径不能为空:第一个文件的路径:{0}, 第二个文件的路径:{1} .'.format(file1, file2)) print('文件路径不能为空:第一个文件的路径:{0}, 第二个文件的路径:{1} .'.format(file1, file2))
sys.exit() sys.exit()
else: else:
print("正在比较文件 {0} 和 {1}".format(file1, file2), end=': ') print("正在比较文件 {0} 和 {1}".format(file1, file2), end=': ')
# if os.path.isfile(file1) and os.path.isfile(file2) and filecmp.cmp(file1, file2): # if os.path.isfile(file1) and os.path.isfile(file2) and filecmp.cmp(file1, file2):
if os.path.isfile(file1) and os.path.isfile(file2) and advanced_file_compare(file1, file2): if os.path.isfile(file1) and os.path.isfile(file2) and advanced_file_compare(file1, file2):
print("文件相同") print("文件相同")
print(Fore.GREEN + "Case{0} 验证成功".format(seqNum), file = stream, end='') score = 60
print(Fore.WHITE, file = stream) if seqNum == caseCount:
score = 100
else:
score = score + 40 / caseCount * seqNum
promptInfo = "Case{0} 验证成功, 分数: {1}".format(seqNum, round(score, 2))
outputPromptInfo(bIncludeCi, promptInfo, 1)
# print(Fore.GREEN + "Case{0} 验证成功, 分数: {1}".format(seqNum, round(score, 2)), file = stream, end='')
# print(Fore.WHITE, file = stream)
returnVal = 1 returnVal = 1
return returnVal return returnVal
else: else:
print("文件不同") print("文件不同")
print(Fore.RED + "Case{0} 验证失败".format(seqNum), file = stream, end='') promptInfo = "Case{0} 验证失败".format(seqNum)
print(Fore.WHITE, file = stream) outputPromptInfo(bIncludeCi, promptInfo, 2)
# print(Fore.RED + "Case{0} 验证失败".format(seqNum), file = stream, end='')
# print(Fore.WHITE, file = stream)
text1_lines = read_file(file1) text1_lines = read_file(file1)
text2_lines = read_file(file2) text2_lines = read_file(file2)
...@@ -106,60 +116,135 @@ def compare_file(file1, file2, seqNum): ...@@ -106,60 +116,135 @@ def compare_file(file1, file2, seqNum):
finally: finally:
return returnVal return returnVal
# 1表示绿色,2表示红色,3表示黄色
def outputPromptInfo(bIncludeCi, promptInfo, color):
if bIncludeCi :
if color == 1:
print(green(promptInfo))
elif color == 2:
print(red(promptInfo))
elif color == 3:
print(yellow(promptInfo))
else:
if color == 1:
print(Fore.GREEN + promptInfo, file = stream)
elif color == 2:
print(Fore.RED + promptInfo, file = stream)
elif color == 3:
print(Fore.YELLOW + promptInfo, file = stream)
print(Fore.WHITE, file = stream)
if __name__ == "__main__": if __name__ == "__main__":
print()
print(green("start validatehahaha哈哈")) bIncludeCi = False
print(Fore.YELLOW + "提示:\n1.如果验证程序长时间未结束,说明应用程序中可能存在死循环。请停止验证程序,修改应用程序后再验证。\n2.如果提示‘python’不是内部或外部命令,也不是可运行的程序或批处理文件。需要设置 Python 的环境变量并重启开发环境来解决此问题。", file = stream) if len(sys.argv) > 1 and sys.argv[1] == "ci":
print(Fore.WHITE, file = stream) bIncludeCi = True
promptInfo = "提示:\n1.如果验证程序长时间未结束,说明应用程序中可能存在死循环。请停止验证程序,修改应用程序后再验证。\n2.如果提示‘python’不是内部或外部命令,也不是可运行的程序或批处理文件。需要设置 Python 的环境变量并重启开发环境来解决此问题。"
outputPromptInfo(bIncludeCi, promptInfo, 3)
# if bIncludeCi :
# print(yellow(promptInfo))
# else:
# print(Fore.YELLOW + promptInfo, file = stream)
# print(Fore.WHITE, file = stream)
print("开发环境中的 Python 解释器版本号:" + platform.python_version()) print("开发环境中的 Python 解释器版本号:" + platform.python_version())
print("环境变量中的 Python 解释器版本号:", end=' ') print("环境变量中的 Python 解释器版本号:", end=' ')
sys.stdout.flush() sys.stdout.flush()
execResult = os.system("python --version") execResult = os.system("python --version")
if execResult != 0: if execResult != 0:
print(Fore.RED + "应用程序异常,返回值:{0}。".format(execResult), file = stream, end='') errorInfo = "应用程序异常,返回值:{0}。".format(execResult)
print(Fore.WHITE, file = stream) outputPromptInfo(bIncludeCi, errorInfo, 2)
# if bIncludeCi :
# print(red(errorInfo))
# else:
# print(Fore.RED + "应用程序异常,返回值:{0}。".format(execResult), file = stream, end='')
# print(Fore.WHITE, file = stream)
exit(1) exit(1)
print() print()
compResultFile = "result_comparation.html" compResultFile = "result_comparation.html"
if os.path.isfile(compResultFile): if os.path.isfile(compResultFile):
os.remove(compResultFile) os.remove(compResultFile)
execFile = "app.exe"
if os.path.isfile(execFile):
os.remove(execFile)
execFile = "*.o"
if os.path.isfile(execFile):
os.remove(execFile)
execResult = os.system("make")
if execResult != 0:
errorInfo = "应用程序异常,返回值:{0}。".format(execResult)
outputPromptInfo(bIncludeCi, errorInfo, 2)
# if bIncludeCi :
# print(red(errorInfo))
# else:
# print(Fore.RED + "应用程序异常,返回值:{0}。".format(execResult), file = stream, end='')
# print(Fore.WHITE, file = stream)
exit(1)
else:
promptInfo = "项目生成成功,分数:{0}。".format(60)
outputPromptInfo(bIncludeCi, promptInfo, 1)
# if bIncludeCi :
# print(green("项目生成成功,分数:{0}。".format(60)))
# else:
# print(Fore.GREEN + "项目生成成功,分数:{0}。".format(60), file = stream, end='')
# print(Fore.WHITE, file = stream)
# 获取case的数量
caseCount = 1
while 1:
outputFile = "output{0}.txt".format(caseCount)
if not os.path.isfile(outputFile):
caseCount -= 1
break
caseCount += 1
seqNum = 1 seqNum = 1
while 1: while 1:
inputFile = "input{0}.txt".format(seqNum) inputFile = "input{0}.txt".format(seqNum)
outputFile = "output{0}.txt".format(seqNum) outputFile = "output{0}.txt".format(seqNum)
useroutputFile = "user_output{0}.txt".format(seqNum) useroutputFile = "user_output{0}.txt".format(seqNum)
if seqNum == 1 and not os.path.isfile(outputFile): if seqNum == 1 and not os.path.isfile(outputFile):
print(Fore.RED + "该项目未提供自动化验证功能", file = stream, end='') promptInfo = "该项目未提供自动化验证功能"
print(Fore.WHITE, file = stream) if bIncludeCi :
print(red(promptInfo))
else:
print(Fore.RED + "该项目未提供自动化验证功能", file = stream, end='')
print(Fore.WHITE, file = stream)
break break
if not os.path.isfile(outputFile): if not os.path.isfile(outputFile):
break break
print("正在使用文件 {0} 验证 case{1}".format(inputFile, seqNum)) print("正在使用文件 {0} 验证 case{1}".format(inputFile, seqNum))
runCommand = "./app.exe < {0} > {1}".format(inputFile, useroutputFile)
if platform.system().lower() == 'windows': if platform.system().lower() == 'windows':
runCommand = "make && app.exe < {0} > {1}".format(inputFile, useroutputFile) runCommand = "app.exe < {0} > {1}".format(inputFile, useroutputFile)
elif platform.system().lower() == 'linux':
runCommand = "make && ./app.exe < {0} > {1}".format(inputFile, useroutputFile)
execResult = os.system(runCommand) execResult = os.system(runCommand)
if execResult != 0: if execResult != 0:
print(Fore.RED + "应用程序异常,返回值:{0}。".format(execResult), file = stream, end='') errorInfo = "应用程序异常,返回值:{0}。".format(execResult)
print(Fore.WHITE, file = stream) outputPromptInfo(bIncludeCi, errorInfo, 2)
break # if bIncludeCi :
# print(red(errorInfo))
# else:
# print(Fore.RED + "应用程序异常,返回值:{0}。".format(execResult), file = stream, end='')
# print(Fore.WHITE, file = stream)
exit(1)
if os.path.isfile(outputFile) and os.path.isfile(useroutputFile): if os.path.isfile(outputFile) and os.path.isfile(useroutputFile):
if compare_file(outputFile, useroutputFile, seqNum) == 0: if compare_file(outputFile, useroutputFile, seqNum, caseCount) == 0:
print("正在使用浏览器打开文件比较结果,可帮助用户查找验证失败的原因。") runCommand = "code --diff {0} {1}".format(outputFile, useroutputFile)
print(Fore.RED + "注意:如果读者是在jupyterlab中完成实验,请使用以下方式打开文件比较结果:\n选择View菜单中的Explorer菜单项,在打开的文件列表中,右击result_comparation.html文件,\n在弹出的菜单中选择“Open Preview”菜单项,可以打开文件比较结果,可帮助用户查找验证失败的原因。") if bIncludeCi :
runCommand = "diff {0} {1} -b -B -y -i -W 100".format(outputFile, useroutputFile)
execResult = os.system(runCommand)
# print("正在使用浏览器打开文件比较结果,可帮助用户查找验证失败的原因。")
# 使用浏览器打开比较结果页面 # 使用浏览器打开比较结果页面
webbrowser.open('file://' + os.path.realpath(compResultFile)) # webbrowser.open('file://' + os.path.realpath(compResultFile))
break exit(1)
seqNum = seqNum + 1 seqNum = seqNum + 1
\ No newline at end of file
\ No newline at end of file
  • Developer

    代码质量分析发现了 3 个问题。

    • 0 blocker
    • 🚫 0 critical
    • 0 major
    • 🔽 1 minor
    • 2 info

    注意: 存在下列问题的代码行在本次提交中没有发生变更,无法使用代码行评论的方式进行报告。所以将下列问题汇总显示在这里(点击问题链接可以转到对应的源代码行):

    1. 🔽 Cppcheck cannot find all the include files. Cppcheck can check the code without the include files found. But the results will probably be more accurate if all the include files are found. Please check your project's include directories and add all of them as include directories for Cppcheck. To see what files Cppcheck cannot find use --check-config. (ProjectKey-169403) 📘
    2. keyword 'if' not followed by a single space 📘
    3. keyword 'if' not followed by a single space 📘
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论