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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
#!/usr/bin/env python
# Compile XREF documents
# Copyright (c) 2002 by Stephen Davies
import sys, string, urllib, cPickle
input_files = []
output_file = None
verbose = 0
merge = 0
# Parse command line arguments
i, numargs = 1, len(sys.argv)
while i < numargs:
if sys.argv[i] == '-o':
output_file = sys.argv[i+1]
i = i + 2
elif sys.argv[i] == '-m':
# Means all input files will be pickled data files
merge = 1
i = i + 1
elif sys.argv[i] == '-v':
verbose = 1
i = i + 1
else:
input_files.append(sys.argv[i])
i = i + 1
# Init data structures
data = {}
index = {}
file_data = (data, index)
if not merge:
# Read input files
for file in input_files:
print "Reading",file
f = open(file, 'rt')
lines = f.readlines()
f.close()
for line in lines:
target, file, line, scope, context = string.split(line)
target = tuple(map(intern, string.split(urllib.unquote(target), '\t')))
scope = map(intern, string.split(urllib.unquote(scope), '\t'))
line = int(line)
file = intern(file)
bad = 0
for i in range(len(target)):
if len(target[i]) > 0 and target[i][0] == '`':
# Don't store local function variables
bad = 1
break
if bad: continue
for i in range(len(scope)):
if len(scope[i]) > 0 and scope[i][0] == '`':
# Function scope, truncate here
del scope[i+1:]
scope[i] = scope[i][1:]
break
scope = tuple(scope)
target_data = data.setdefault(target, [[],[],[]])
if context == 'DEF':
target_data[0].append( (file, line, scope) )
elif context == 'CALL':
target_data[1].append( (file, line, scope) )
elif context == 'REF':
target_data[2].append( (file, line, scope) )
else:
print "Warning: Unknown context:",context
else:
# Read input data files
for file in input_files:
print "Reading",file
f = open(file, 'rb')
input_data, input_index = cPickle.load(f)
f.close()
# Ignore index
# Extend data with the read data, one item at a time
for target, itarget_data in input_data.items():
target_data = data.setdefault(target, [[],[],[]])
target_data[0].extend(itarget_data[0])
target_data[1].extend(itarget_data[1])
target_data[2].extend(itarget_data[2])
# Sort the data
for target, target_data in data.items():
target_data[1].sort()
target_data[2].sort()
name = target[-1]
index.setdefault(name,[]).append(target)
# If it's a function name, also add without the parameters
paren = name.find('(')
if paren != -1:
index.setdefault(name[:paren],[]).append(target)
if verbose:
print string.join(target, '::')
if len(target_data[1]):
print " Called from:"
for file, line, scope in target_data[1]:
print " %s:%s: %s"%(file, line, string.join(scope,'::'))
if len(target_data[2]):
print " Referenced from:"
for file, line, scope in target_data[2]:
print " %s:%s: %s"%(file, line, string.join(scope,'::'))
if output_file:
print "Writing",output_file
f = open(output_file, 'wb')
cPickle.dump(file_data, f)
f.close()
|