diff options
author | Doug Zongker <dougz@android.com> | 2009-11-30 14:28:59 -0800 |
---|---|---|
committer | Doug Zongker <dougz@android.com> | 2009-12-03 16:47:52 -0800 |
commit | 9bd4962af87257c6a97e9026af7e4764394412c2 (patch) | |
tree | 240a6d2622cc50e649296cdba5739eb5fdebbd59 /tools | |
parent | 8c1eeffe4339530cb76572588330a698bccb622c (diff) | |
download | build-9bd4962af87257c6a97e9026af7e4764394412c2.zip build-9bd4962af87257c6a97e9026af7e4764394412c2.tar.gz build-9bd4962af87257c6a97e9026af7e4764394412c2.tar.bz2 |
break up event-log-tags; generate java source files with constants
Construct the /system/etc/event-log-tags file by unioning together any
*.logtags files included in LOCAL_SRC_FILES throughout the system (with
appropriate error checking for dup tag numbers, etc.)
For java packages, generate a java source file from the logtags file for
that package that contains static integer constants for each tag name.
Diffstat (limited to 'tools')
-rw-r--r-- | tools/event_log_tags.py | 112 | ||||
-rwxr-xr-x | tools/java-event-log-tags.py | 89 | ||||
-rwxr-xr-x | tools/merge-event-log-tags.py | 103 |
3 files changed, 304 insertions, 0 deletions
diff --git a/tools/event_log_tags.py b/tools/event_log_tags.py new file mode 100644 index 0000000..fc05aff --- /dev/null +++ b/tools/event_log_tags.py @@ -0,0 +1,112 @@ +# Copyright (C) 2009 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""A module for reading and parsing event-log-tags files.""" + +import re +import sys + +class Tag(object): + __slots__ = ["tagnum", "tagname", "description", "filename", "linenum"] + + def __init__(self, tagnum, tagname, description, filename, linenum): + self.tagnum = tagnum + self.tagname = tagname + self.description = description + self.filename = filename + self.linenum = linenum + + +class TagFile(object): + """Read an input event-log-tags file.""" + def AddError(self, msg, linenum=None): + if linenum is None: + linenum = self.linenum + self.errors.append((self.filename, linenum, msg)) + + def AddWarning(self, msg, linenum=None): + if linenum is None: + linenum = self.linenum + self.warnings.append((self.filename, linenum, msg)) + + def __init__(self, filename, file_object=None): + """'filename' is the name of the file (included in any error + messages). If 'file_object' is None, 'filename' will be opened + for reading.""" + self.errors = [] + self.warnings = [] + self.tags = [] + self.options = {} + + self.filename = filename + self.linenum = 0 + + if file_object is None: + try: + file_object = open(filename, "rb") + except (IOError, OSError), e: + self.AddError(str(e)) + return + + try: + for self.linenum, line in enumerate(file_object): + self.linenum += 1 + + line = line.strip() + if not line or line[0] == '#': continue + parts = re.split(r"\s+", line, 2) + + if len(parts) < 2: + self.AddError("failed to parse \"%s\"" % (line,)) + continue + + if parts[0] == "option": + self.options[parts[1]] = parts[2:] + continue + + try: + tag = int(parts[0]) + except ValueError: + self.AddError("\"%s\" isn't an integer tag" % (parts[0],)) + continue + + tagname = parts[1] + if len(parts) == 3: + description = parts[2] + else: + description = None + + self.tags.append(Tag(tag, tagname, description, + self.filename, self.linenum)) + except (IOError, OSError), e: + self.AddError(str(e)) + + +def WriteOutput(output_file, data): + """Write 'data' to the given output filename (which may be None to + indicate stdout). Emit an error message and die on any failure. + 'data' may be a string or a StringIO object.""" + if not isinstance(data, str): + data = data.getvalue() + try: + if output_file is None: + out = sys.stdout + output_file = "<stdout>" + else: + out = open(output_file, "wb") + out.write(data) + out.close() + except (IOError, OSError), e: + print >> sys.stderr, "failed to write %s: %s" % (output_file, e) + sys.exit(1) diff --git a/tools/java-event-log-tags.py b/tools/java-event-log-tags.py new file mode 100755 index 0000000..f8374b0 --- /dev/null +++ b/tools/java-event-log-tags.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python +# +# Copyright (C) 2009 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +""" +Usage: java-event-log-tags.py [-o output_file] <input_file> + +Generate a java class containing constants for each of the event log +tags in the given input file. + +-h to display this usage message and exit. +""" + +import cStringIO +import getopt +import os +import sys + +import event_log_tags + +output_file = None + +try: + opts, args = getopt.getopt(sys.argv[1:], "ho:") +except getopt.GetoptError, err: + print str(err) + print __doc__ + sys.exit(2) + +for o, a in opts: + if o == "-h": + print __doc__ + sys.exit(2) + elif o == "-o": + output_file = a + else: + print >> sys.stderr, "unhandled option %s" % (o,) + sys.exit(1) + +if len(args) != 1: + print "need exactly one input file, not %d" % (len(args),) + print __doc__ + sys.exit(1) + +fn = args[0] +tagfile = event_log_tags.TagFile(fn) + +if "java_package" not in tagfile.options: + tagfile.AddError("java_package option not specified", linenum=0) + +if tagfile.errors: + for fn, ln, msg in tagfile.errors: + print >> sys.stderr, "%s:%d: error: %s" % (fn, ln, msg) + sys.exit(1) + +buffer = cStringIO.StringIO() +buffer.write("/* This file is auto-generated. DO NOT MODIFY.\n" + " * Source file: %s\n" + " */\n\n" % (fn,)) + +buffer.write("package %s;\n\n" % (tagfile.options["java_package"][0],)) + +basename, _ = os.path.splitext(os.path.basename(fn)) +buffer.write("public class %s {\n" % (basename,)) +buffer.write(" private %s() { } // don't instantiate\n" % (basename,)) + +for t in tagfile.tags: + if t.description: + buffer.write("\n /** %d %s %s */\n" % (t.tagnum, t.tagname, t.description)) + else: + buffer.write("\n /** %d %s */\n" % (t.tagnum, t.tagname)) + + buffer.write(" public static final int %s = %d;\n" % + (t.tagname.upper(), t.tagnum)) +buffer.write("}\n"); + +event_log_tags.WriteOutput(output_file, buffer) diff --git a/tools/merge-event-log-tags.py b/tools/merge-event-log-tags.py new file mode 100755 index 0000000..2852612 --- /dev/null +++ b/tools/merge-event-log-tags.py @@ -0,0 +1,103 @@ +#!/usr/bin/env python +# +# Copyright (C) 2009 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +""" +Usage: merge-event-log-tags.py [-o output_file] [input_files...] + +Merge together zero or more event-logs-tags files to produce a single +output file, stripped of comments. Checks that no tag numbers conflict +and fails if they do. + +-h to display this usage message and exit. +""" + +import cStringIO +import getopt +import sys + +import event_log_tags + +by_tagnum = {} +errors = [] +warnings = [] + +output_file = None + +try: + opts, args = getopt.getopt(sys.argv[1:], "ho:") +except getopt.GetoptError, err: + print str(err) + print __doc__ + sys.exit(2) + +for o, a in opts: + if o == "-h": + print __doc__ + sys.exit(2) + elif o == "-o": + output_file = a + else: + print >> sys.stderr, "unhandled option %s" % (o,) + sys.exit(1) + +for fn in args: + tagfile = event_log_tags.TagFile(fn) + + for t in tagfile.tags: + tagnum = t.tagnum + tagname = t.tagname + description = t.description + + if t.tagnum in by_tagnum: + orig = by_tagnum[t.tagnum] + + if (t.tagname == orig.tagname and + t.description == orig.description): + # if the name and description are identical, issue a warning + # instead of failing (to make it easier to move tags between + # projects without breaking the build). + tagfile.AddWarning("tag %d \"%s\" duplicated in %s:%d" % + (t.tagnum, t.tagname, orig.filename, orig.linenum), + linenum=t.linenum) + else: + tagfile.AddError("tag %d used by conflicting \"%s\" from %s:%d" % + (t.tagnum, orig.tagname, orig.filename, orig.linenum), + linenum=t.linenum) + continue + + by_tagnum[t.tagnum] = t + + errors.extend(tagfile.errors) + warnings.extend(tagfile.warnings) + +if errors: + for fn, ln, msg in errors: + print >> sys.stderr, "%s:%d: error: %s" % (fn, ln, msg) + sys.exit(1) + +if warnings: + for fn, ln, msg in warnings: + print >> sys.stderr, "%s:%d: warning: %s" % (fn, ln, msg) + +buffer = cStringIO.StringIO() +for n in sorted(by_tagnum): + t = by_tagnum[n] + if t.description: + buffer.write("%d %s %s\n" % (t.tagnum, t.tagname, t.description)) + else: + buffer.write("%d %s\n" % (t.tagnum, t.tagname)) + +event_log_tags.WriteOutput(output_file, buffer) |