[ci skip] Add note that this change may break SetOption() + ninja usage with fix
[scons.git] / bin / objcounts.py
blobf387ad52cec0ea794c099e606e3ffdc8f229a250
1 #!/usr/bin/env python
3 # Copyright (c) 2005 The SCons Foundation
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:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
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 import re
24 import sys
26 filenames = sys.argv[1:]
28 if len(sys.argv) != 3:
29 print("""Usage: objcounts.py file1 file2
31 Compare the --debug=object counts from two build logs.
32 """)
33 sys.exit(0)
35 def fetch_counts(fname):
36 contents = open(fname).read()
37 m = re.search('\nObject counts:(\n\s[^\n]*)*', contents, re.S)
38 lines = m.group().split('\n')
39 list = [l.split() for l in lines if re.match('\s+\d', l)]
40 d = {}
41 for l in list:
42 d[l[-1]] = list(map(int, l[:-1]))
43 return d
45 c1 = fetch_counts(sys.argv[1])
46 c2 = fetch_counts(sys.argv[2])
48 common = {}
49 for k in list(c1.keys()):
50 try:
51 common[k] = (c1[k], c2[k])
52 except KeyError:
53 # Transition: we added the module to the names of a bunch of
54 # the logged objects. Assume that c1 might be from an older log
55 # without the modules in the names, and look for an equivalent
56 # in c2.
57 if not '.' in k:
58 s = '.'+k
59 l = len(s)
60 for k2 in list(c2.keys()):
61 if k2[-l:] == s:
62 common[k2] = (c1[k], c2[k2])
63 del c1[k]
64 del c2[k2]
65 break
66 else:
67 del c1[k]
68 del c2[k]
70 def diffstr(c1, c2):
71 try:
72 d = c2 - c1
73 except TypeError:
74 d = ''
75 else:
76 if d:
77 d = '[%+d]' % d
78 else:
79 d = ''
80 return " %5s/%-5s %-8s" % (c1, c2, d)
82 def printline(c1, c2, classname):
83 print(diffstr(c1[2], c2[2]) + \
84 diffstr(c1[3], c2[3]) + \
85 ' ' + classname)
87 for k in sorted(common.keys()):
88 c = common[k]
89 printline(c[0], c[1], k)
91 for k in sorted(list(c1.keys())):
92 printline(c1[k], ['--']*4, k)
94 for k in sorted(list(c2.keys())):
95 printline(['--']*4, c2[k], k)
97 # Local Variables:
98 # tab-width:4
99 # indent-tabs-mode:nil
100 # End:
101 # vim: set expandtab tabstop=4 shiftwidth=4: