It’s not really necessary to write your own code, because you simply override the existing cmp(1) Unix command, or fc if you are using Windows.
You can do one of the following in the Jenkins workspace:
I'm not sure about Windows scripts, but something like this should work:
rem Windows batch file FC /B file1 file2 IF %ERRORLEVEL% NEQ 0 SEND_EMAIL_COMMAND
If you really want your own Python script to do this .....
Jenkins will execute your script from within the shell (or a similar command interpreter). To report the comparison result, you can set the exit status of the process using sys.exit() . The convention is that the command was successful if it completed status 0, otherwise it failed, so you could use 0 when the files match, and 1 when they are not (or there was an error).
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)
Then in the Jenkins workspace:
python compare_files.py file1 file2 || send email
or
call python compare_files.py file1 file2 IF %ERRORLEVEL% NEQ 0 SEND_EMAIL_COMMAND
source share