|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import os, sys, re, json, datetime, optparse |
| 4 | +import syslog |
| 5 | + |
| 6 | +# boilerplate |
| 7 | +basedir = os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")) |
| 8 | +sys.path = [ basedir ] + sys.path |
| 9 | + |
| 10 | +from ietf import settings |
| 11 | +from django.core import management |
| 12 | +management.setup_environ(settings) |
| 13 | + |
| 14 | + |
| 15 | +from optparse import OptionParser |
| 16 | + |
| 17 | +parser = OptionParser() |
| 18 | +parser.add_option("-f", "--from", dest="start", |
| 19 | + help="Start time, defaults to a little less than 23 hours ago", metavar="YYYY-MM-DD HH:MM:SS") |
| 20 | +parser.add_option("-t", "--to", dest="end", |
| 21 | + help="End time, defaults to 23 hours later than from", metavar="YYYY-MM-DD HH:MM:SS") |
| 22 | +parser.add_option("", "--no-email", dest="send_email", default=True, action="store_false", |
| 23 | + help="Skip sending emails") |
| 24 | + |
| 25 | +options, args = parser.parse_args() |
| 26 | + |
| 27 | +# compensate to avoid we ask for something that happened now and then |
| 28 | +# don't get it back because our request interval is slightly off |
| 29 | +CLOCK_SKEW_COMPENSATION = 5 # seconds |
| 30 | + |
| 31 | +# actually the interface accepts 24 hours, but then we get into |
| 32 | +# trouble with daylights savings - meh |
| 33 | +MAX_INTERVAL_ACCEPTED_BY_IANA = datetime.timedelta(hours=23) |
| 34 | + |
| 35 | + |
| 36 | +start = datetime.datetime.now() - datetime.timedelta(hours=23) + datetime.timedelta(seconds=CLOCK_SKEW_COMPENSATION) |
| 37 | +if options.start: |
| 38 | + start = datetime.datetime.strptime(options.start, "%Y-%m-%d %H:%M:%S") |
| 39 | + |
| 40 | +end = start + datetime.timedelta(hours=23) |
| 41 | +if options.end: |
| 42 | + end = datetime.datetime.strptime(options.end, "%Y-%m-%d %H:%M:%S") |
| 43 | + |
| 44 | +syslog.openlog(os.path.basename(__file__), syslog.LOG_PID, syslog.LOG_LOCAL0) |
| 45 | + |
| 46 | + |
| 47 | +from ietf.sync.iana import * |
| 48 | + |
| 49 | +syslog.syslog("Updating history log with new changes from IANA from %s, period %s - %s" % (CHANGES_URL, start, end)) |
| 50 | + |
| 51 | +t = start |
| 52 | +while t < end: |
| 53 | + # the IANA server doesn't allow us to fetch more than a certain |
| 54 | + # period, so loop over the requested period and make multiple |
| 55 | + # requests if necessary |
| 56 | + |
| 57 | + text = fetch_changes_json(CHANGES_URL, t, min(end, t + MAX_INTERVAL_ACCEPTED_BY_IANA)) |
| 58 | + changes = parse_changes_json(text) |
| 59 | + added_events, warnings = update_history_with_changes(changes, send_email=options.send_email) |
| 60 | + |
| 61 | + for e in added_events: |
| 62 | + syslog.syslog("Added event for %s %s: %s" % (e.doc_id, e.time, e.desc)) |
| 63 | + |
| 64 | + for w in warnings: |
| 65 | + syslog.syslog("WARNING: %s" % w) |
| 66 | + |
| 67 | + t += MAX_INTERVAL_ACCEPTED_BY_IANA |
0 commit comments