merge-event-log-tags.py revision 5fe3b35846bd1a0abfee68091daf58d7d92e51ba
1#!/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
29import md5
30import struct
31import sys
32
33import event_log_tags
34
35errors = []
36warnings = []
37
38output_file = None
39
40# Tags with a tag number of ? are assigned a tag in the range
41# [ASSIGN_START, ASSIGN_LIMIT).
42ASSIGN_START = 900000
43ASSIGN_LIMIT = 1000000
44
45try:
46  opts, args = getopt.getopt(sys.argv[1:], "ho:")
47except getopt.GetoptError, err:
48  print str(err)
49  print __doc__
50  sys.exit(2)
51
52for o, a in opts:
53  if o == "-h":
54    print __doc__
55    sys.exit(2)
56  elif o == "-o":
57    output_file = a
58  else:
59    print >> sys.stderr, "unhandled option %s" % (o,)
60    sys.exit(1)
61
62# Restrictions on tags:
63#
64#   Tag names must be unique.  (If the tag number and description are
65#   also the same, a warning is issued instead of an error.)
66#
67#   Explicit tag numbers must be unique.  (If the tag name is also the
68#   same, no error is issued because the above rule will issue a
69#   warning or error.)
70
71by_tagname = {}
72by_tagnum = {}
73
74for fn in args:
75  tagfile = event_log_tags.TagFile(fn)
76
77  for t in tagfile.tags:
78    tagnum = t.tagnum
79    tagname = t.tagname
80    description = t.description
81
82    if t.tagname in by_tagname:
83      orig = by_tagname[t.tagname]
84
85      # Allow an explicit tag number to define an implicit tag number
86      if orig.tagnum is None:
87        orig.tagnum = t.tagnum
88      elif t.tagnum is None:
89        t.tagnum = orig.tagnum
90
91      if (t.tagnum == orig.tagnum and
92          t.description == orig.description):
93        # if the name and description are identical, issue a warning
94        # instead of failing (to make it easier to move tags between
95        # projects without breaking the build).
96        tagfile.AddWarning("tag \"%s\" (%s) duplicated in %s:%d" %
97                           (t.tagname, t.tagnum, orig.filename, orig.linenum),
98                           linenum=t.linenum)
99      else:
100        tagfile.AddError(
101            "tag name \"%s\" used by conflicting tag %s from %s:%d" %
102            (t.tagname, orig.tagnum, orig.filename, orig.linenum),
103            linenum=t.linenum)
104      continue
105
106    if t.tagnum is not None and t.tagnum in by_tagnum:
107      orig = by_tagnum[t.tagnum]
108
109      if t.tagname != orig.tagname:
110        tagfile.AddError(
111            "tag number %d used by conflicting tag \"%s\" from %s:%d" %
112            (t.tagnum, orig.tagname, orig.filename, orig.linenum),
113            linenum=t.linenum)
114        continue
115
116    by_tagname[t.tagname] = t
117    if t.tagnum is not None:
118      by_tagnum[t.tagnum] = t
119
120  errors.extend(tagfile.errors)
121  warnings.extend(tagfile.warnings)
122
123if errors:
124  for fn, ln, msg in errors:
125    print >> sys.stderr, "%s:%d: error: %s" % (fn, ln, msg)
126  sys.exit(1)
127
128if warnings:
129  for fn, ln, msg in warnings:
130    print >> sys.stderr, "%s:%d: warning: %s" % (fn, ln, msg)
131
132# Python's hash function (a) isn't great and (b) varies between
133# versions of python.  Using md5 is overkill here but is the same from
134# platform to platform and speed shouldn't matter in practice.
135def hashname(str):
136  d = md5.md5(str).digest()[:4]
137  return struct.unpack("!I", d)[0]
138
139# Assign a tag number to all the entries that say they want one
140# assigned.  We do this based on a hash of the tag name so that the
141# numbers should stay relatively stable as tags are added.
142
143for name, t in sorted(by_tagname.iteritems()):
144  if t.tagnum is None:
145    while True:
146      x = (hashname(name) % (ASSIGN_LIMIT - ASSIGN_START - 1)) + ASSIGN_START
147      if x not in by_tagnum:
148        t.tagnum = x
149        by_tagnum[x] = t
150        break
151      name = "_" + name
152
153# by_tagnum should be complete now; we've assigned numbers to all tags.
154
155buffer = cStringIO.StringIO()
156for n, t in sorted(by_tagnum.iteritems()):
157  if t.description:
158    buffer.write("%d %s %s\n" % (t.tagnum, t.tagname, t.description))
159  else:
160    buffer.write("%d %s\n" % (t.tagnum, t.tagname))
161
162event_log_tags.WriteOutput(output_file, buffer)
163