1#!/usr/bin/env python3 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""" 18Merge together zero or more event-logs-tags files to produce a single 19output file, stripped of comments. Checks that no tag numbers conflict 20and fails if they do. 21""" 22 23from io import StringIO 24import argparse 25import sys 26 27import event_log_tags 28 29errors = [] 30warnings = [] 31 32parser = argparse.ArgumentParser(description=__doc__) 33parser.add_argument('-o', dest='output_file') 34parser.add_argument('files', nargs='*') 35args = parser.parse_args() 36 37# Restrictions on tags: 38# 39# Tag names must be unique. (If the tag number and description are 40# also the same, a warning is issued instead of an error.) 41# 42# Explicit tag numbers must be unique. (If the tag name is also the 43# same, no error is issued because the above rule will issue a 44# warning or error.) 45 46by_tagname = {} 47by_tagnum = {} 48 49for fn in args.files: 50 tagfile = event_log_tags.TagFile(fn) 51 52 for t in tagfile.tags: 53 tagnum = t.tagnum 54 tagname = t.tagname 55 description = t.description 56 57 if t.tagname in by_tagname: 58 orig = by_tagname[t.tagname] 59 60 if (t.tagnum == orig.tagnum and 61 t.description == orig.description): 62 # if the name and description are identical, issue a warning 63 # instead of failing (to make it easier to move tags between 64 # projects without breaking the build). 65 tagfile.AddWarning("tag \"%s\" (%s) duplicated in %s:%d" % 66 (t.tagname, t.tagnum, orig.filename, orig.linenum), 67 linenum=t.linenum) 68 else: 69 tagfile.AddError( 70 "tag name \"%s\" used by conflicting tag %s from %s:%d" % 71 (t.tagname, orig.tagnum, orig.filename, orig.linenum), 72 linenum=t.linenum) 73 continue 74 75 if t.tagnum in by_tagnum: 76 orig = by_tagnum[t.tagnum] 77 78 if t.tagname != orig.tagname: 79 tagfile.AddError( 80 "tag number %d used by conflicting tag \"%s\" from %s:%d" % 81 (t.tagnum, orig.tagname, orig.filename, orig.linenum), 82 linenum=t.linenum) 83 continue 84 85 by_tagname[t.tagname] = t 86 by_tagnum[t.tagnum] = t 87 88 errors.extend(tagfile.errors) 89 warnings.extend(tagfile.warnings) 90 91if errors: 92 for fn, ln, msg in errors: 93 print("%s:%d: error: %s" % (fn, ln, msg), file=sys.stderr) 94 sys.exit(1) 95 96if warnings: 97 for fn, ln, msg in warnings: 98 print("%s:%d: warning: %s" % (fn, ln, msg), file=sys.stderr) 99 100# by_tagnum should be complete now; we've assigned numbers to all tags. 101 102buffer = StringIO() 103for n, t in sorted(by_tagnum.items()): 104 if t.description: 105 buffer.write("%d %s %s\n" % (t.tagnum, t.tagname, t.description)) 106 else: 107 buffer.write("%d %s\n" % (t.tagnum, t.tagname)) 108 109event_log_tags.WriteOutput(args.output_file, buffer) 110