Doug Zongker | 9bd4962 | 2009-11-30 14:28:59 -0800 | [diff] [blame] | 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 | """ |
| 18 | Usage: merge-event-log-tags.py [-o output_file] [input_files...] |
| 19 | |
| 20 | Merge together zero or more event-logs-tags files to produce a single |
| 21 | output file, stripped of comments. Checks that no tag numbers conflict |
| 22 | and fails if they do. |
| 23 | |
| 24 | -h to display this usage message and exit. |
| 25 | """ |
| 26 | |
| 27 | import cStringIO |
| 28 | import getopt |
Doug Zongker | 6ce87a1 | 2012-09-20 11:50:34 -0700 | [diff] [blame] | 29 | try: |
| 30 | import hashlib |
| 31 | except ImportError: |
| 32 | import md5 as hashlib |
Doug Zongker | abfbbe2 | 2010-02-16 14:32:08 -0800 | [diff] [blame] | 33 | import struct |
Doug Zongker | 9bd4962 | 2009-11-30 14:28:59 -0800 | [diff] [blame] | 34 | import sys |
| 35 | |
| 36 | import event_log_tags |
| 37 | |
Doug Zongker | 9bd4962 | 2009-11-30 14:28:59 -0800 | [diff] [blame] | 38 | errors = [] |
| 39 | warnings = [] |
| 40 | |
| 41 | output_file = None |
Joe Onorato | b751053 | 2010-07-14 10:22:54 -0700 | [diff] [blame] | 42 | pre_merged_file = None |
Doug Zongker | 9bd4962 | 2009-11-30 14:28:59 -0800 | [diff] [blame] | 43 | |
Doug Zongker | abfbbe2 | 2010-02-16 14:32:08 -0800 | [diff] [blame] | 44 | # Tags with a tag number of ? are assigned a tag in the range |
| 45 | # [ASSIGN_START, ASSIGN_LIMIT). |
| 46 | ASSIGN_START = 900000 |
| 47 | ASSIGN_LIMIT = 1000000 |
| 48 | |
Doug Zongker | 9bd4962 | 2009-11-30 14:28:59 -0800 | [diff] [blame] | 49 | try: |
Joe Onorato | b751053 | 2010-07-14 10:22:54 -0700 | [diff] [blame] | 50 | opts, args = getopt.getopt(sys.argv[1:], "ho:m:") |
Doug Zongker | 9bd4962 | 2009-11-30 14:28:59 -0800 | [diff] [blame] | 51 | except getopt.GetoptError, err: |
| 52 | print str(err) |
| 53 | print __doc__ |
| 54 | sys.exit(2) |
| 55 | |
| 56 | for o, a in opts: |
| 57 | if o == "-h": |
| 58 | print __doc__ |
| 59 | sys.exit(2) |
| 60 | elif o == "-o": |
| 61 | output_file = a |
Joe Onorato | b751053 | 2010-07-14 10:22:54 -0700 | [diff] [blame] | 62 | elif o == "-m": |
| 63 | pre_merged_file = a |
Doug Zongker | 9bd4962 | 2009-11-30 14:28:59 -0800 | [diff] [blame] | 64 | else: |
| 65 | print >> sys.stderr, "unhandled option %s" % (o,) |
| 66 | sys.exit(1) |
| 67 | |
Doug Zongker | abfbbe2 | 2010-02-16 14:32:08 -0800 | [diff] [blame] | 68 | # Restrictions on tags: |
| 69 | # |
| 70 | # Tag names must be unique. (If the tag number and description are |
| 71 | # also the same, a warning is issued instead of an error.) |
| 72 | # |
| 73 | # Explicit tag numbers must be unique. (If the tag name is also the |
| 74 | # same, no error is issued because the above rule will issue a |
| 75 | # warning or error.) |
| 76 | |
| 77 | by_tagname = {} |
| 78 | by_tagnum = {} |
| 79 | |
Joe Onorato | b751053 | 2010-07-14 10:22:54 -0700 | [diff] [blame] | 80 | pre_merged_tags = {} |
| 81 | if pre_merged_file: |
| 82 | for t in event_log_tags.TagFile(pre_merged_file).tags: |
| 83 | pre_merged_tags[t.tagname] = t |
| 84 | |
Doug Zongker | 9bd4962 | 2009-11-30 14:28:59 -0800 | [diff] [blame] | 85 | for fn in args: |
| 86 | tagfile = event_log_tags.TagFile(fn) |
| 87 | |
| 88 | for t in tagfile.tags: |
| 89 | tagnum = t.tagnum |
| 90 | tagname = t.tagname |
| 91 | description = t.description |
| 92 | |
Doug Zongker | abfbbe2 | 2010-02-16 14:32:08 -0800 | [diff] [blame] | 93 | if t.tagname in by_tagname: |
| 94 | orig = by_tagname[t.tagname] |
Doug Zongker | 9bd4962 | 2009-11-30 14:28:59 -0800 | [diff] [blame] | 95 | |
Dan Egnor | 5fe3b35 | 2010-03-17 16:59:12 -0700 | [diff] [blame] | 96 | # Allow an explicit tag number to define an implicit tag number |
| 97 | if orig.tagnum is None: |
| 98 | orig.tagnum = t.tagnum |
| 99 | elif t.tagnum is None: |
| 100 | t.tagnum = orig.tagnum |
| 101 | |
Doug Zongker | abfbbe2 | 2010-02-16 14:32:08 -0800 | [diff] [blame] | 102 | if (t.tagnum == orig.tagnum and |
Doug Zongker | 9bd4962 | 2009-11-30 14:28:59 -0800 | [diff] [blame] | 103 | t.description == orig.description): |
| 104 | # if the name and description are identical, issue a warning |
| 105 | # instead of failing (to make it easier to move tags between |
| 106 | # projects without breaking the build). |
Doug Zongker | abfbbe2 | 2010-02-16 14:32:08 -0800 | [diff] [blame] | 107 | tagfile.AddWarning("tag \"%s\" (%s) duplicated in %s:%d" % |
| 108 | (t.tagname, t.tagnum, orig.filename, orig.linenum), |
Doug Zongker | 9bd4962 | 2009-11-30 14:28:59 -0800 | [diff] [blame] | 109 | linenum=t.linenum) |
| 110 | else: |
Doug Zongker | abfbbe2 | 2010-02-16 14:32:08 -0800 | [diff] [blame] | 111 | tagfile.AddError( |
| 112 | "tag name \"%s\" used by conflicting tag %s from %s:%d" % |
| 113 | (t.tagname, orig.tagnum, orig.filename, orig.linenum), |
| 114 | linenum=t.linenum) |
Doug Zongker | 9bd4962 | 2009-11-30 14:28:59 -0800 | [diff] [blame] | 115 | continue |
| 116 | |
Doug Zongker | abfbbe2 | 2010-02-16 14:32:08 -0800 | [diff] [blame] | 117 | if t.tagnum is not None and t.tagnum in by_tagnum: |
| 118 | orig = by_tagnum[t.tagnum] |
| 119 | |
| 120 | if t.tagname != orig.tagname: |
| 121 | tagfile.AddError( |
| 122 | "tag number %d used by conflicting tag \"%s\" from %s:%d" % |
| 123 | (t.tagnum, orig.tagname, orig.filename, orig.linenum), |
| 124 | linenum=t.linenum) |
| 125 | continue |
| 126 | |
| 127 | by_tagname[t.tagname] = t |
| 128 | if t.tagnum is not None: |
| 129 | by_tagnum[t.tagnum] = t |
Doug Zongker | 9bd4962 | 2009-11-30 14:28:59 -0800 | [diff] [blame] | 130 | |
| 131 | errors.extend(tagfile.errors) |
| 132 | warnings.extend(tagfile.warnings) |
| 133 | |
| 134 | if errors: |
| 135 | for fn, ln, msg in errors: |
| 136 | print >> sys.stderr, "%s:%d: error: %s" % (fn, ln, msg) |
| 137 | sys.exit(1) |
| 138 | |
| 139 | if warnings: |
| 140 | for fn, ln, msg in warnings: |
| 141 | print >> sys.stderr, "%s:%d: warning: %s" % (fn, ln, msg) |
| 142 | |
Doug Zongker | abfbbe2 | 2010-02-16 14:32:08 -0800 | [diff] [blame] | 143 | # Python's hash function (a) isn't great and (b) varies between |
| 144 | # versions of python. Using md5 is overkill here but is the same from |
| 145 | # platform to platform and speed shouldn't matter in practice. |
| 146 | def hashname(str): |
Doug Zongker | 6ce87a1 | 2012-09-20 11:50:34 -0700 | [diff] [blame] | 147 | d = hashlib.md5(str).digest()[:4] |
Doug Zongker | abfbbe2 | 2010-02-16 14:32:08 -0800 | [diff] [blame] | 148 | return struct.unpack("!I", d)[0] |
| 149 | |
| 150 | # Assign a tag number to all the entries that say they want one |
| 151 | # assigned. We do this based on a hash of the tag name so that the |
| 152 | # numbers should stay relatively stable as tags are added. |
| 153 | |
Joe Onorato | b751053 | 2010-07-14 10:22:54 -0700 | [diff] [blame] | 154 | # If we were provided pre-merged tags (w/ the -m option), then don't |
| 155 | # ever try to allocate one, just fail if we don't have a number |
| 156 | |
Doug Zongker | abfbbe2 | 2010-02-16 14:32:08 -0800 | [diff] [blame] | 157 | for name, t in sorted(by_tagname.iteritems()): |
| 158 | if t.tagnum is None: |
Joe Onorato | b751053 | 2010-07-14 10:22:54 -0700 | [diff] [blame] | 159 | if pre_merged_tags: |
| 160 | try: |
| 161 | t.tagnum = pre_merged_tags[t.tagname] |
| 162 | except KeyError: |
| 163 | print >> sys.stderr, ("Error: Tag number not defined for tag `%s'." |
| 164 | +" Have you done a full build?") % t.tagname |
| 165 | sys.exit(1) |
| 166 | else: |
| 167 | while True: |
| 168 | x = (hashname(name) % (ASSIGN_LIMIT - ASSIGN_START - 1)) + ASSIGN_START |
| 169 | if x not in by_tagnum: |
| 170 | t.tagnum = x |
| 171 | by_tagnum[x] = t |
| 172 | break |
| 173 | name = "_" + name |
Doug Zongker | abfbbe2 | 2010-02-16 14:32:08 -0800 | [diff] [blame] | 174 | |
| 175 | # by_tagnum should be complete now; we've assigned numbers to all tags. |
| 176 | |
Doug Zongker | 9bd4962 | 2009-11-30 14:28:59 -0800 | [diff] [blame] | 177 | buffer = cStringIO.StringIO() |
Doug Zongker | abfbbe2 | 2010-02-16 14:32:08 -0800 | [diff] [blame] | 178 | for n, t in sorted(by_tagnum.iteritems()): |
Doug Zongker | 9bd4962 | 2009-11-30 14:28:59 -0800 | [diff] [blame] | 179 | if t.description: |
| 180 | buffer.write("%d %s %s\n" % (t.tagnum, t.tagname, t.description)) |
| 181 | else: |
| 182 | buffer.write("%d %s\n" % (t.tagnum, t.tagname)) |
| 183 | |
| 184 | event_log_tags.WriteOutput(output_file, buffer) |