OSDN Git Service

Merge CTS-related change
[android-x86/build.git] / tools / java-event-log-tags.py
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> <merged_tags_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 re
30 import sys
31
32 import event_log_tags
33
34 output_file = None
35
36 try:
37   opts, args = getopt.getopt(sys.argv[1:], "ho:")
38 except getopt.GetoptError, err:
39   print str(err)
40   print __doc__
41   sys.exit(2)
42
43 for o, a in opts:
44   if o == "-h":
45     print __doc__
46     sys.exit(2)
47   elif o == "-o":
48     output_file = a
49   else:
50     print >> sys.stderr, "unhandled option %s" % (o,)
51     sys.exit(1)
52
53 if len(args) != 2:
54   print "need exactly two input files, not %d" % (len(args),)
55   print __doc__
56   sys.exit(1)
57
58 fn = args[0]
59 tagfile = event_log_tags.TagFile(fn)
60
61 # Load the merged tag file (which should have numbers assigned for all
62 # tags.  Use the numbers from the merged file to fill in any missing
63 # numbers from the input file.
64 merged_fn = args[1]
65 merged_tagfile = event_log_tags.TagFile(merged_fn)
66 merged_by_name = dict([(t.tagname, t) for t in merged_tagfile.tags])
67 for t in tagfile.tags:
68   if t.tagnum is None:
69     if t.tagname in merged_by_name:
70       t.tagnum = merged_by_name[t.tagname].tagnum
71     else:
72       # We're building something that's not being included in the
73       # product, so its tags don't appear in the merged file.  Assign
74       # them all an arbitrary number so we can emit the java and
75       # compile the (unused) package.
76       t.tagnum = 999999
77
78 if "java_package" not in tagfile.options:
79   tagfile.AddError("java_package option not specified", linenum=0)
80
81 hide = True
82 if "javadoc_hide" in tagfile.options:
83   hide = event_log_tags.BooleanFromString(tagfile.options["javadoc_hide"][0])
84
85 if tagfile.errors:
86   for fn, ln, msg in tagfile.errors:
87     print >> sys.stderr, "%s:%d: error: %s" % (fn, ln, msg)
88   sys.exit(1)
89
90 buffer = cStringIO.StringIO()
91 buffer.write("/* This file is auto-generated.  DO NOT MODIFY.\n"
92              " * Source file: %s\n"
93              " */\n\n" % (fn,))
94
95 buffer.write("package %s;\n\n" % (tagfile.options["java_package"][0],))
96
97 basename, _ = os.path.splitext(os.path.basename(fn))
98
99 if hide:
100   buffer.write("/**\n"
101                " * @hide\n"
102                " */\n")
103 buffer.write("public class %s {\n" % (basename,))
104 buffer.write("  private %s() { }  // don't instantiate\n" % (basename,))
105
106 for t in tagfile.tags:
107   if t.description:
108     buffer.write("\n  /** %d %s %s */\n" % (t.tagnum, t.tagname, t.description))
109   else:
110     buffer.write("\n  /** %d %s */\n" % (t.tagnum, t.tagname))
111
112   buffer.write("  public static final int %s = %d;\n" %
113                (t.tagname.upper(), t.tagnum))
114
115 keywords = frozenset(["abstract", "continue", "for", "new", "switch", "assert",
116                       "default", "goto", "package", "synchronized", "boolean",
117                       "do", "if", "private", "this", "break", "double",
118                       "implements", "protected", "throw", "byte", "else",
119                       "import", "public", "throws", "case", "enum",
120                       "instanceof", "return", "transient", "catch", "extends",
121                       "int", "short", "try", "char", "final", "interface",
122                       "static", "void", "class", "finally", "long", "strictfp",
123                       "volatile", "const", "float", "native", "super", "while"])
124
125 def javaName(name):
126   out = name[0].lower() + re.sub(r"[^A-Za-z0-9]", "", name.title())[1:]
127   if out in keywords:
128     out += "_"
129   return out
130
131 javaTypes = ["ERROR", "int", "long", "String", "Object[]"]
132 for t in tagfile.tags:
133   methodName = javaName("write_" + t.tagname)
134   if t.description:
135     args = [arg.strip("() ").split("|") for arg in t.description.split(",")]
136   else:
137     args = []
138   argTypesNames = ", ".join([javaTypes[int(arg[1])] + " " + javaName(arg[0]) for arg in args])
139   argNames = "".join([", " + javaName(arg[0]) for arg in args])
140   buffer.write("\n  public static void %s(%s) {" % (methodName, argTypesNames))
141   buffer.write("\n    android.util.EventLog.writeEvent(%s%s);" % (t.tagname.upper(), argNames))
142   buffer.write("\n  }\n")
143
144
145 buffer.write("}\n");
146
147 event_log_tags.WriteOutput(output_file, buffer)