我正在比较下面程序中的两个文件。如果它是相同的那么我打印成功就像失败一样。我正在使用一个名为jenkins的集成工具,在比较文件失败时发送电子邮件,这样做 - 我必须正确处理错误。有人能告诉我如何处理错误吗?
Error_Status=0
def compare_files(file1, file2):
try:
with open(file1, 'rb') as f_file1, open(file2, 'rb') as f_file2:
if f_file1.read() == f_file2.read():
print 'SUCCESS \n'
#print 'SUCESS:\n {}\n {}'.format(file1, file2)
else:
print 'FAILURE \n'
Error_Status=1
except IOError:
print "File is NOT compared"
Error_Status = 1
jenkins控制台输出:
E:\Projekte\Audi\Cloud_SOP17_TTS>rem !BUILD step: Execute test: tts.py
E:\Projekte\Audi\Cloud_SOP17_TTS>call python tts.py file1 file2 || echo failed
INPUT ENG: I am tired
Latency: 114msec
[ERROR] Can't Create Reference PCM or Response JSON files!
INPUT GED: facebook nachricht schönes wetter heute
Latency: 67msec
INPUT GED: erinnere mich an den termin heute abend
Latency: 113msec
E:\Projekte\Audi\Cloud_SOP17_TTS>echo Started at: 15:51:25.37
Started at: 15:51:25.37
E:\Projekte\Audi\Cloud_SOP17_TTS>exit 0
Archiving artifacts
Recording plot data
Saving plot series data from: E:\Projekte\Audi\Cloud_SOP17_TTS\Backups\tts_2016_02_04.py
Not creating point with null values: y=null label= url=
No emails were triggered.
Finished: SUCCESS
答案 0 :(得分:2)
没有必要编写自己的代码来执行此操作,因为如果您使用的是Windows,则只需重新实现现有的cmp(1)
Unix命令或fc
命令。
您可以在Jenkins工作区中执行以下操作之一:
# UNIX shell
cmp file1 file2 || send email
我不会使用Windows脚本,但这样的事情应该有效:
rem Windows batch file
FC /B file1 file2
IF %ERRORLEVEL% NEQ 0 SEND_EMAIL_COMMAND
如果你真的想要自己的Python脚本来做这个......
Jenkins将在shell(或类似的命令解释器)中执行您的脚本。要传达比较结果,您可以设置流程'使用sys.exit()
退出状态。惯例是,如果命令的退出状态为0,则命令成功,否则失败,因此当文件相同时可以使用0,而当文件不相符时使用1(或者有错误)。 / p>
import sys
def compare_files(file1, file2):
try:
with open(file1, 'rb') as f_file1, open(file2, 'rb') as f_file2:
return f_file1.read() == f_file2.read()
except Exception as exc:
print 'compare_files(): failed to compare file {} to {}: {}'.format(file1, file2, exc)
return False
if __name__ == '__main__':
if len(sys.argv) >= 3:
if not compare_files(sys.argv[1], sys.argv[2]):
sys.exit(1)
else:
print >>sys.stderr, 'Usage: {} file1 file2'.format(sys.argv[0])
sys.exit(2)
然后在你的Jenkins工作区:
python compare_files.py file1 file2 || send email
或
call python compare_files.py file1 file2
IF %ERRORLEVEL% NEQ 0 SEND_EMAIL_COMMAND
答案 1 :(得分:0)
您可以使用class AuthController extends Controller
{
public function __construct()
{
parent::__construct();
}
// other methods here...
}
和all
逐行比较,这样您就不会在内存中同时拥有两个完整文件,并在发生错误时返回任何errno
:
izip_longest
任何数字条0或1都表示引发了错误。
from itertools import izip_longest
def compare_files(file1, file2):
try:
with open(file1, 'rb') as f_file1, open(file2, 'rb') as f_file2:
return all(l1 == l2 for l1, l2 in izip_longest(f_file1, f_file2, fillvalue=""))
except EnvironmentError as e:
print("File is NOT compared, error message {}".format(e))
return e.errno
答案 2 :(得分:-1)
使用assert
。它将退出抛出异常,因此您将获得将回溯写入输出并且Jenkins任务将失败。
def compare_files(file1, file2):
with open(file1, 'rb') as f_file1, open(file2, 'rb') as f_file2:
assert f_file1.read() == f_file2.read()
如果目标恰好是看错了什么并让詹金斯的工作失败,我就没有看到捕捉异常的重点。
编辑:如果您真的想明确打印失败的成功:
def compare_files(file1, file2):
try:
with open(file1, 'rb') as f_file1, open(file2, 'rb') as f_file2:
assert f_file1.read() == f_file2.read()
except:
'''
I know, I know. Normally we should catch specific exceptions.
But OP wants to print SUCCESS or FAILURE and fail the Jenkins job
in case of error.
'''
print 'FAILURE'
raise
else:
print 'SUCCESS'