c1f2dd563bf328e5b50e07e2effccf678de6fda6
[scons.git] / bin / objcounts.py
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2005 The SCons Foundation
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24 import re
25 import sys
26
27 filenames = sys.argv[1:]
28
29 if len(sys.argv) != 3:
30     print """Usage:  objcounts.py file1 file2
31
32 Compare the --debug=object counts from two build logs.
33 """
34     sys.exit(0)
35
36 def fetch_counts(fname):
37     contents = open(fname).read()
38     m = re.search('\nObject counts:(\n\s[^\n]*)*', contents, re.S)
39     lines = m.group().split('\n')
40     list = [l.split() for l in lines if re.match('\s+\d', l)]
41     d = {}
42     for l in list:
43         d[l[-1]] = list(map(int, l[:-1]))
44     return d
45
46 c1 = fetch_counts(sys.argv[1])
47 c2 = fetch_counts(sys.argv[2])
48
49 common = {}
50 for k in c1.keys():
51     try:
52         common[k] = (c1[k], c2[k])
53     except KeyError:
54         # Transition:  we added the module to the names of a bunch of
55         # the logged objects.  Assume that c1 might be from an older log
56         # without the modules in the names, and look for an equivalent
57         # in c2.
58         if not '.' in k:
59             s = '.'+k
60             l = len(s)
61             for k2 in c2.keys():
62                 if k2[-l:] == s:
63                     common[k2] = (c1[k], c2[k2])
64                     del c1[k]
65                     del c2[k2]
66                     break
67     else:
68         del c1[k]
69         del c2[k]
70
71 def diffstr(c1, c2):
72     try:
73         d = c2 - c1
74     except TypeError:
75         d = ''
76     else:
77         if d:
78             d = '[%+d]' % d
79         else:
80             d = ''
81     return " %5s/%-5s %-8s" % (c1, c2, d)
82
83 def printline(c1, c2, classname):
84     print \
85           diffstr(c1[2], c2[2]) + \
86           diffstr(c1[3], c2[3]) + \
87           ' ' + classname
88
89 keys = common.keys()
90 keys.sort()
91 for k in keys:
92     c = common[k]
93     printline(c[0], c[1], k)
94
95 keys = c1.keys()
96 keys.sort()
97 for k in keys:
98     printline(c1[k], ['--']*4, k)
99
100 keys = c2.keys()
101 keys.sort()
102 for k in keys:
103     printline(['--']*4, c2[k], k)
104
105 # Local Variables:
106 # tab-width:4
107 # indent-tabs-mode:nil
108 # End:
109 # vim: set expandtab tabstop=4 shiftwidth=4: