All pastes #422874 Raw Edit

Stuff

public text v1 · immutable
·published 2007-04-03 19:26 UTC
rendered paste body
#!/usr/bin/env python
# read the linemarkers of a preprocessed output and print the inclusion tree
# first line is line 1 for compatibility with grep

import sys, re

if ( len( sys.argv ) <= 1 or len( sys.argv ) > 3 ):
	print 'Usage: %s filename [line number]' % ( sys.argv[0] )

filename = sys.argv[1]
linetarget = 0
if ( len( sys.argv ) == 3 ):
	linetarget = int( sys.argv[2] )

includes = []

linemarkpat = re.compile( '^# ([0-9]*) "(.*)" *' )

linecount = 0
f = open( filename )
for l in f.readlines():
	linecount += 1
	if ( linetarget != 0 and linecount >= linetarget ):
		print includes
		break
	if ( linemarkpat.match( l ) ):		
		s = linemarkpat.split( l )
		if ( s[3][0] == '1' ):
			includes.append( '%s' % s[2] )
			if ( linetarget == 0 ):
				print includes
		if ( s[3][0] == '2' ):
			includes.pop()
			if ( linetarget == 0 ):
				print includes

print '%d lines' % linecount