blob: 846d9cfd5feaad9f3f30c74e29408ac3b3eff1ab [file] [log] [blame]
Doug Zongker9bd49622009-11-30 14:28:59 -08001#!/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"""
Doug Zongkerabfbbe22010-02-16 14:32:08 -080018Usage: java-event-log-tags.py [-o output_file] <input_file> <merged_tags_file>
Doug Zongker9bd49622009-11-30 14:28:59 -080019
20Generate a java class containing constants for each of the event log
21tags in the given input file.
22
23-h to display this usage message and exit.
24"""
25
26import cStringIO
27import getopt
28import os
Sean Dykeman6da9ef62011-12-12 17:42:55 -080029import os.path
Bjorn Bringert76a72272010-02-01 21:39:22 +000030import re
Doug Zongker9bd49622009-11-30 14:28:59 -080031import sys
32
33import event_log_tags
34
35output_file = None
36
37try:
38 opts, args = getopt.getopt(sys.argv[1:], "ho:")
39except getopt.GetoptError, err:
40 print str(err)
41 print __doc__
42 sys.exit(2)
43
44for o, a in opts:
45 if o == "-h":
46 print __doc__
47 sys.exit(2)
48 elif o == "-o":
49 output_file = a
50 else:
51 print >> sys.stderr, "unhandled option %s" % (o,)
52 sys.exit(1)
53
Doug Zongkerabfbbe22010-02-16 14:32:08 -080054if len(args) != 2:
55 print "need exactly two input files, not %d" % (len(args),)
Doug Zongker9bd49622009-11-30 14:28:59 -080056 print __doc__
57 sys.exit(1)
58
59fn = args[0]
60tagfile = event_log_tags.TagFile(fn)
61
Doug Zongkerabfbbe22010-02-16 14:32:08 -080062# Load the merged tag file (which should have numbers assigned for all
63# tags. Use the numbers from the merged file to fill in any missing
64# numbers from the input file.
65merged_fn = args[1]
66merged_tagfile = event_log_tags.TagFile(merged_fn)
67merged_by_name = dict([(t.tagname, t) for t in merged_tagfile.tags])
68for t in tagfile.tags:
69 if t.tagnum is None:
Doug Zongker7431dac2010-02-17 09:07:55 -080070 if t.tagname in merged_by_name:
71 t.tagnum = merged_by_name[t.tagname].tagnum
72 else:
73 # We're building something that's not being included in the
74 # product, so its tags don't appear in the merged file. Assign
75 # them all an arbitrary number so we can emit the java and
76 # compile the (unused) package.
77 t.tagnum = 999999
Doug Zongkerabfbbe22010-02-16 14:32:08 -080078
Doug Zongker9bd49622009-11-30 14:28:59 -080079if "java_package" not in tagfile.options:
80 tagfile.AddError("java_package option not specified", linenum=0)
81
Doug Zongker5ae770f2009-12-08 12:45:02 -080082hide = True
83if "javadoc_hide" in tagfile.options:
84 hide = event_log_tags.BooleanFromString(tagfile.options["javadoc_hide"][0])
85
Doug Zongker9bd49622009-11-30 14:28:59 -080086if tagfile.errors:
87 for fn, ln, msg in tagfile.errors:
88 print >> sys.stderr, "%s:%d: error: %s" % (fn, ln, msg)
89 sys.exit(1)
90
91buffer = cStringIO.StringIO()
92buffer.write("/* This file is auto-generated. DO NOT MODIFY.\n"
93 " * Source file: %s\n"
94 " */\n\n" % (fn,))
95
96buffer.write("package %s;\n\n" % (tagfile.options["java_package"][0],))
97
98basename, _ = os.path.splitext(os.path.basename(fn))
Doug Zongker5ae770f2009-12-08 12:45:02 -080099
100if hide:
101 buffer.write("/**\n"
102 " * @hide\n"
103 " */\n")
Doug Zongker9bd49622009-11-30 14:28:59 -0800104buffer.write("public class %s {\n" % (basename,))
105buffer.write(" private %s() { } // don't instantiate\n" % (basename,))
106
107for t in tagfile.tags:
108 if t.description:
109 buffer.write("\n /** %d %s %s */\n" % (t.tagnum, t.tagname, t.description))
110 else:
111 buffer.write("\n /** %d %s */\n" % (t.tagnum, t.tagname))
112
113 buffer.write(" public static final int %s = %d;\n" %
114 (t.tagname.upper(), t.tagnum))
Bjorn Bringert76a72272010-02-01 21:39:22 +0000115
116keywords = frozenset(["abstract", "continue", "for", "new", "switch", "assert",
117 "default", "goto", "package", "synchronized", "boolean",
118 "do", "if", "private", "this", "break", "double",
119 "implements", "protected", "throw", "byte", "else",
120 "import", "public", "throws", "case", "enum",
121 "instanceof", "return", "transient", "catch", "extends",
122 "int", "short", "try", "char", "final", "interface",
123 "static", "void", "class", "finally", "long", "strictfp",
124 "volatile", "const", "float", "native", "super", "while"])
125
126def javaName(name):
127 out = name[0].lower() + re.sub(r"[^A-Za-z0-9]", "", name.title())[1:]
128 if out in keywords:
129 out += "_"
130 return out
131
132javaTypes = ["ERROR", "int", "long", "String", "Object[]"]
133for t in tagfile.tags:
134 methodName = javaName("write_" + t.tagname)
135 if t.description:
136 args = [arg.strip("() ").split("|") for arg in t.description.split(",")]
137 else:
138 args = []
139 argTypesNames = ", ".join([javaTypes[int(arg[1])] + " " + javaName(arg[0]) for arg in args])
140 argNames = "".join([", " + javaName(arg[0]) for arg in args])
141 buffer.write("\n public static void %s(%s) {" % (methodName, argTypesNames))
142 buffer.write("\n android.util.EventLog.writeEvent(%s%s);" % (t.tagname.upper(), argNames))
143 buffer.write("\n }\n")
144
145
Doug Zongker9bd49622009-11-30 14:28:59 -0800146buffer.write("}\n");
147
Sean Dykeman6da9ef62011-12-12 17:42:55 -0800148output_dir = os.path.dirname(output_file)
149if not os.path.exists(output_dir):
150 os.makedirs(output_dir)
151
Doug Zongker9bd49622009-11-30 14:28:59 -0800152event_log_tags.WriteOutput(output_file, buffer)