merge-event-log-tags.py revision 9bd4962af87257c6a97e9026af7e4764394412c2
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 sys
30
31import event_log_tags
32
33by_tagnum = {}
34errors = []
35warnings = []
36
37output_file = None
38
39try:
40  opts, args = getopt.getopt(sys.argv[1:], "ho:")
41except getopt.GetoptError, err:
42  print str(err)
43  print __doc__
44  sys.exit(2)
45
46for o, a in opts:
47  if o == "-h":
48    print __doc__
49    sys.exit(2)
50  elif o == "-o":
51    output_file = a
52  else:
53    print >> sys.stderr, "unhandled option %s" % (o,)
54    sys.exit(1)
55
56for fn in args:
57  tagfile = event_log_tags.TagFile(fn)
58
59  for t in tagfile.tags:
60    tagnum = t.tagnum
61    tagname = t.tagname
62    description = t.description
63
64    if t.tagnum in by_tagnum:
65      orig = by_tagnum[t.tagnum]
66
67      if (t.tagname == orig.tagname and
68          t.description == orig.description):
69        # if the name and description are identical, issue a warning
70        # instead of failing (to make it easier to move tags between
71        # projects without breaking the build).
72        tagfile.AddWarning("tag %d \"%s\" duplicated in %s:%d" %
73                           (t.tagnum, t.tagname, orig.filename, orig.linenum),
74                           linenum=t.linenum)
75      else:
76        tagfile.AddError("tag %d used by conflicting \"%s\" from %s:%d" %
77                         (t.tagnum, orig.tagname, orig.filename, orig.linenum),
78                         linenum=t.linenum)
79      continue
80
81    by_tagnum[t.tagnum] = t
82
83  errors.extend(tagfile.errors)
84  warnings.extend(tagfile.warnings)
85
86if errors:
87  for fn, ln, msg in errors:
88    print >> sys.stderr, "%s:%d: error: %s" % (fn, ln, msg)
89  sys.exit(1)
90
91if warnings:
92  for fn, ln, msg in warnings:
93    print >> sys.stderr, "%s:%d: warning: %s" % (fn, ln, msg)
94
95buffer = cStringIO.StringIO()
96for n in sorted(by_tagnum):
97  t = by_tagnum[n]
98  if t.description:
99    buffer.write("%d %s %s\n" % (t.tagnum, t.tagname, t.description))
100  else:
101    buffer.write("%d %s\n" % (t.tagnum, t.tagname))
102
103event_log_tags.WriteOutput(output_file, buffer)
104