blob: bddda905e7a929f9c78aaa98f754d23f205ebe98 [file] [log] [blame]
Doug Zongker9bd49622009-11-30 14:28:59 -08001#!/usr/bin/env python
2#
3# Copyright (C) 2009 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""
18Usage: merge-event-log-tags.py [-o output_file] [input_files...]
19
20Merge together zero or more event-logs-tags files to produce a single
21output file, stripped of comments. Checks that no tag numbers conflict
22and fails if they do.
23
24-h to display this usage message and exit.
25"""
26
27import cStringIO
28import getopt
Doug Zongkerabfbbe22010-02-16 14:32:08 -080029import md5
30import struct
Doug Zongker9bd49622009-11-30 14:28:59 -080031import sys
32
33import event_log_tags
34
Doug Zongker9bd49622009-11-30 14:28:59 -080035errors = []
36warnings = []
37
38output_file = None
Joe Onoratob7510532010-07-14 10:22:54 -070039pre_merged_file = None
Doug Zongker9bd49622009-11-30 14:28:59 -080040
Doug Zongkerabfbbe22010-02-16 14:32:08 -080041# Tags with a tag number of ? are assigned a tag in the range
42# [ASSIGN_START, ASSIGN_LIMIT).
43ASSIGN_START = 900000
44ASSIGN_LIMIT = 1000000
45
Doug Zongker9bd49622009-11-30 14:28:59 -080046try:
Joe Onoratob7510532010-07-14 10:22:54 -070047 opts, args = getopt.getopt(sys.argv[1:], "ho:m:")
Doug Zongker9bd49622009-11-30 14:28:59 -080048except getopt.GetoptError, err:
49 print str(err)
50 print __doc__
51 sys.exit(2)
52
53for o, a in opts:
54 if o == "-h":
55 print __doc__
56 sys.exit(2)
57 elif o == "-o":
58 output_file = a
Joe Onoratob7510532010-07-14 10:22:54 -070059 elif o == "-m":
60 pre_merged_file = a
Doug Zongker9bd49622009-11-30 14:28:59 -080061 else:
62 print >> sys.stderr, "unhandled option %s" % (o,)
63 sys.exit(1)
64
Doug Zongkerabfbbe22010-02-16 14:32:08 -080065# Restrictions on tags:
66#
67# Tag names must be unique. (If the tag number and description are
68# also the same, a warning is issued instead of an error.)
69#
70# Explicit tag numbers must be unique. (If the tag name is also the
71# same, no error is issued because the above rule will issue a
72# warning or error.)
73
74by_tagname = {}
75by_tagnum = {}
76
Joe Onoratob7510532010-07-14 10:22:54 -070077pre_merged_tags = {}
78if pre_merged_file:
79 for t in event_log_tags.TagFile(pre_merged_file).tags:
80 pre_merged_tags[t.tagname] = t
81
Doug Zongker9bd49622009-11-30 14:28:59 -080082for fn in args:
83 tagfile = event_log_tags.TagFile(fn)
84
85 for t in tagfile.tags:
86 tagnum = t.tagnum
87 tagname = t.tagname
88 description = t.description
89
Doug Zongkerabfbbe22010-02-16 14:32:08 -080090 if t.tagname in by_tagname:
91 orig = by_tagname[t.tagname]
Doug Zongker9bd49622009-11-30 14:28:59 -080092
Dan Egnor5fe3b352010-03-17 16:59:12 -070093 # Allow an explicit tag number to define an implicit tag number
94 if orig.tagnum is None:
95 orig.tagnum = t.tagnum
96 elif t.tagnum is None:
97 t.tagnum = orig.tagnum
98
Doug Zongkerabfbbe22010-02-16 14:32:08 -080099 if (t.tagnum == orig.tagnum and
Doug Zongker9bd49622009-11-30 14:28:59 -0800100 t.description == orig.description):
101 # if the name and description are identical, issue a warning
102 # instead of failing (to make it easier to move tags between
103 # projects without breaking the build).
Doug Zongkerabfbbe22010-02-16 14:32:08 -0800104 tagfile.AddWarning("tag \"%s\" (%s) duplicated in %s:%d" %
105 (t.tagname, t.tagnum, orig.filename, orig.linenum),
Doug Zongker9bd49622009-11-30 14:28:59 -0800106 linenum=t.linenum)
107 else:
Doug Zongkerabfbbe22010-02-16 14:32:08 -0800108 tagfile.AddError(
109 "tag name \"%s\" used by conflicting tag %s from %s:%d" %
110 (t.tagname, orig.tagnum, orig.filename, orig.linenum),
111 linenum=t.linenum)
Doug Zongker9bd49622009-11-30 14:28:59 -0800112 continue
113
Doug Zongkerabfbbe22010-02-16 14:32:08 -0800114 if t.tagnum is not None and t.tagnum in by_tagnum:
115 orig = by_tagnum[t.tagnum]
116
117 if t.tagname != orig.tagname:
118 tagfile.AddError(
119 "tag number %d used by conflicting tag \"%s\" from %s:%d" %
120 (t.tagnum, orig.tagname, orig.filename, orig.linenum),
121 linenum=t.linenum)
122 continue
123
124 by_tagname[t.tagname] = t
125 if t.tagnum is not None:
126 by_tagnum[t.tagnum] = t
Doug Zongker9bd49622009-11-30 14:28:59 -0800127
128 errors.extend(tagfile.errors)
129 warnings.extend(tagfile.warnings)
130
131if errors:
132 for fn, ln, msg in errors:
133 print >> sys.stderr, "%s:%d: error: %s" % (fn, ln, msg)
134 sys.exit(1)
135
136if warnings:
137 for fn, ln, msg in warnings:
138 print >> sys.stderr, "%s:%d: warning: %s" % (fn, ln, msg)
139
Doug Zongkerabfbbe22010-02-16 14:32:08 -0800140# Python's hash function (a) isn't great and (b) varies between
141# versions of python. Using md5 is overkill here but is the same from
142# platform to platform and speed shouldn't matter in practice.
143def hashname(str):
144 d = md5.md5(str).digest()[:4]
145 return struct.unpack("!I", d)[0]
146
147# Assign a tag number to all the entries that say they want one
148# assigned. We do this based on a hash of the tag name so that the
149# numbers should stay relatively stable as tags are added.
150
Joe Onoratob7510532010-07-14 10:22:54 -0700151# If we were provided pre-merged tags (w/ the -m option), then don't
152# ever try to allocate one, just fail if we don't have a number
153
Doug Zongkerabfbbe22010-02-16 14:32:08 -0800154for name, t in sorted(by_tagname.iteritems()):
155 if t.tagnum is None:
Joe Onoratob7510532010-07-14 10:22:54 -0700156 if pre_merged_tags:
157 try:
158 t.tagnum = pre_merged_tags[t.tagname]
159 except KeyError:
160 print >> sys.stderr, ("Error: Tag number not defined for tag `%s'."
161 +" Have you done a full build?") % t.tagname
162 sys.exit(1)
163 else:
164 while True:
165 x = (hashname(name) % (ASSIGN_LIMIT - ASSIGN_START - 1)) + ASSIGN_START
166 if x not in by_tagnum:
167 t.tagnum = x
168 by_tagnum[x] = t
169 break
170 name = "_" + name
Doug Zongkerabfbbe22010-02-16 14:32:08 -0800171
172# by_tagnum should be complete now; we've assigned numbers to all tags.
173
Doug Zongker9bd49622009-11-30 14:28:59 -0800174buffer = cStringIO.StringIO()
Doug Zongkerabfbbe22010-02-16 14:32:08 -0800175for n, t in sorted(by_tagnum.iteritems()):
Doug Zongker9bd49622009-11-30 14:28:59 -0800176 if t.description:
177 buffer.write("%d %s %s\n" % (t.tagnum, t.tagname, t.description))
178 else:
179 buffer.write("%d %s\n" % (t.tagnum, t.tagname))
180
181event_log_tags.WriteOutput(output_file, buffer)