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: java-event-log-tags.py [-o output_file] <input_file> |
| 19 | |
| 20 | Generate a java class containing constants for each of the event log |
| 21 | tags in the given input file. |
| 22 | |
| 23 | -h to display this usage message and exit. |
| 24 | """ |
| 25 | |
| 26 | import cStringIO |
| 27 | import getopt |
| 28 | import os |
| 29 | import sys |
| 30 | |
| 31 | import event_log_tags |
| 32 | |
| 33 | output_file = None |
| 34 | |
| 35 | try: |
| 36 | opts, args = getopt.getopt(sys.argv[1:], "ho:") |
| 37 | except getopt.GetoptError, err: |
| 38 | print str(err) |
| 39 | print __doc__ |
| 40 | sys.exit(2) |
| 41 | |
| 42 | for o, a in opts: |
| 43 | if o == "-h": |
| 44 | print __doc__ |
| 45 | sys.exit(2) |
| 46 | elif o == "-o": |
| 47 | output_file = a |
| 48 | else: |
| 49 | print >> sys.stderr, "unhandled option %s" % (o,) |
| 50 | sys.exit(1) |
| 51 | |
| 52 | if len(args) != 1: |
| 53 | print "need exactly one input file, not %d" % (len(args),) |
| 54 | print __doc__ |
| 55 | sys.exit(1) |
| 56 | |
| 57 | fn = args[0] |
| 58 | tagfile = event_log_tags.TagFile(fn) |
| 59 | |
| 60 | if "java_package" not in tagfile.options: |
| 61 | tagfile.AddError("java_package option not specified", linenum=0) |
| 62 | |
| 63 | if tagfile.errors: |
| 64 | for fn, ln, msg in tagfile.errors: |
| 65 | print >> sys.stderr, "%s:%d: error: %s" % (fn, ln, msg) |
| 66 | sys.exit(1) |
| 67 | |
| 68 | buffer = cStringIO.StringIO() |
| 69 | buffer.write("/* This file is auto-generated. DO NOT MODIFY.\n" |
| 70 | " * Source file: %s\n" |
| 71 | " */\n\n" % (fn,)) |
| 72 | |
| 73 | buffer.write("package %s;\n\n" % (tagfile.options["java_package"][0],)) |
| 74 | |
| 75 | basename, _ = os.path.splitext(os.path.basename(fn)) |
| 76 | buffer.write("public class %s {\n" % (basename,)) |
| 77 | buffer.write(" private %s() { } // don't instantiate\n" % (basename,)) |
| 78 | |
| 79 | for t in tagfile.tags: |
| 80 | if t.description: |
| 81 | buffer.write("\n /** %d %s %s */\n" % (t.tagnum, t.tagname, t.description)) |
| 82 | else: |
| 83 | buffer.write("\n /** %d %s */\n" % (t.tagnum, t.tagname)) |
| 84 | |
| 85 | buffer.write(" public static final int %s = %d;\n" % |
| 86 | (t.tagname.upper(), t.tagnum)) |
| 87 | buffer.write("}\n"); |
| 88 | |
| 89 | event_log_tags.WriteOutput(output_file, buffer) |