File: mergelog.py

package info (click to toggle)
esys-particle 2.3.4%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 13,036 kB
  • ctags: 10,805
  • sloc: cpp: 80,009; python: 5,872; makefile: 1,243; sh: 313; perl: 225
file content (50 lines) | stat: -rw-r--r-- 1,044 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# ----
#
#  merge console output files according to time stamps
#
# usage:
#	python mergelog.py  INFILENAME N OUTFILENAME
#
#   merges INFILENAME.0 INFILENAME.1 ... INFILENAME.N-1 into OUTFILENAME 
#
#------

import sys
from heapq import *

infilename=sys.argv[1]
nfiles=int(sys.argv[2])
outfilename=sys.argv[3]

iflist=list()
msg_map=list()

# open all files
for i in range(nfiles):
	iflist.append(open(infilename+"."+str(i)))

outfile=open(outfilename,'w')

# initialize time -> message map by reading the 1st message from each file
for i in range(nfiles):
	msg=iflist[i].readline()
	t_msg=float(msg.split()[1])
	heappush(msg_map,(t_msg,msg,i))
	
# merge files
while (len(msg_map)>0):
	# pop msg with lowest time
	t,msg,idx=heappop(msg_map)
	# write it to output file
	outfile.write("<"+str(idx)+">"+msg)
	# replace with message from same file
	msg=iflist[idx].readline()
	if(len(msg)>0):
		t_msg=float(msg.split()[1])
		heappush(msg_map,(t_msg,msg,idx))
	
# close all input files
for i in range(nfiles):
	iflist[i].close()
	
outfile.close()