Ticket #8285: patchtest.py

File patchtest.py, 1.2 KB (added by sukhbir, 7 years ago)

Test script for the patch

Line 
1#! /usr/bin/env python
2
3# For the patch for ticket 8285.
4
5import email
6import datetime
7
8from dateutil import parser
9from dateutil import tz
10
11REGISTRATION_LIMIT = 30
12
13
14def accept_message(header):
15    """Check if a message is newer than REGISTRATION_LIMIT."""
16    # Convert the header to UTC for comparison.
17    message_time = parser.parse(header.splitlines()[-1]).astimezone(tz.tzutc())
18    # Get the current time, also in UTC.
19    current_time = datetime.datetime.now(tz.tzutc())
20
21    time_offset = (current_time - message_time).seconds / 60
22    # If the message is newer than the limit, return True.
23    if time_offset < REGISTRATION_LIMIT:
24        return True
25    else:
26        return False
27
28
29def main():
30    print "Paste the message headers ('.' on a new line to quit):"
31    headers = []
32    while True:
33        headers_input = raw_input()
34        if headers_input == '.':
35            break
36        headers.append(headers_input)
37
38    headers = '\n'.join(headers)
39    msg = email.message_from_string(headers)['X-Received']
40
41    print '-' * 4
42    if accept_message(msg):
43        print 'Message is NEW'
44    else:
45        print 'Message is OLD'
46    print '-' * 4
47
48
49if __name__ == '__main__':
50    main()