root/livinglogic.python.tipimaid/liaalh_mergelogs.py @ 76:c243ae2f22ce

Revision 76:c243ae2f22ce, 1.3 KB (checked in by Nikolas Tautenhahn <nik@…>, 11 years ago)

added encoding/shebang

Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4import heapq, re, sys, datetime, time
5
6re_find_date = re.compile(" \[(.*?)\] ")
7
8
9def apachedate2utc(line):
10    d = re_find_date.findall(line)[0]
11    temp = d.split()
12    utcdate = datetime.datetime(*(time.strptime(temp[0], "%d/%b/%Y:%H:%M:%S")[0:6])) # support ancient distributions with python < 2.5
13    minsoff = int("%s%s" % (temp[1][0], temp[1][-2:]))
14    hrsoff = int("%s%s" % (temp[1][0], temp[1][1:3]))
15    utcdate -= datetime.timedelta(hours=hrsoff, minutes=minsoff)
16    return utcdate
17
18
19def reallines(iter, key):
20    for item in iter:
21        try:
22            keyitem = key(item)
23        except Exception:
24            sys.stderr.write("Skipping malformed line %s" % item)
25            sys.stderr.flush()
26        else:
27            yield item
28
29
30def mergesort(filelist, key):
31    heap = []
32    for (i, itr) in enumerate(iter(pl) for pl in filelist):
33        try:
34            item = itr.next()
35            heap.append((key(item), i, item, itr))
36        except StopIteration:
37            pass
38    heapq.heapify(heap)
39
40    while heap:
41        (_, idx, item, itr) = heap[0]
42        yield item
43        try:
44            item = itr.next()
45            heapq.heapreplace(heap, (key(item), idx, item, itr) )
46        except StopIteration:
47            heapq.heappop(heap)
48
49
50openedfiles = [reallines(open(filename), apachedate2utc) for filename in sys.argv[1:]]
51
52for line in mergesort(openedfiles, apachedate2utc):
53    sys.stdout.write(line)
54sys.stdout.flush()
Note: See TracBrowser for help on using the browser.