java-event-log-tags.py revision 5ae770fc0e43172e1efc5146a38e7d1f452143e2
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"""
18Usage: java-event-log-tags.py [-o output_file] <input_file>
19
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
29import sys
30
31import event_log_tags
32
33output_file = None
34
35try:
36  opts, args = getopt.getopt(sys.argv[1:], "ho:")
37except getopt.GetoptError, err:
38  print str(err)
39  print __doc__
40  sys.exit(2)
41
42for 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
52if len(args) != 1:
53  print "need exactly one input file, not %d" % (len(args),)
54  print __doc__
55  sys.exit(1)
56
57fn = args[0]
58tagfile = event_log_tags.TagFile(fn)
59
60if "java_package" not in tagfile.options:
61  tagfile.AddError("java_package option not specified", linenum=0)
62
63hide = True
64if "javadoc_hide" in tagfile.options:
65  hide = event_log_tags.BooleanFromString(tagfile.options["javadoc_hide"][0])
66
67if tagfile.errors:
68  for fn, ln, msg in tagfile.errors:
69    print >> sys.stderr, "%s:%d: error: %s" % (fn, ln, msg)
70  sys.exit(1)
71
72buffer = cStringIO.StringIO()
73buffer.write("/* This file is auto-generated.  DO NOT MODIFY.\n"
74             " * Source file: %s\n"
75             " */\n\n" % (fn,))
76
77buffer.write("package %s;\n\n" % (tagfile.options["java_package"][0],))
78
79basename, _ = os.path.splitext(os.path.basename(fn))
80
81if hide:
82  buffer.write("/**\n"
83               " * @hide\n"
84               " */\n")
85buffer.write("public class %s {\n" % (basename,))
86buffer.write("  private %s() { }  // don't instantiate\n" % (basename,))
87
88for t in tagfile.tags:
89  if t.description:
90    buffer.write("\n  /** %d %s %s */\n" % (t.tagnum, t.tagname, t.description))
91  else:
92    buffer.write("\n  /** %d %s */\n" % (t.tagnum, t.tagname))
93
94  buffer.write("  public static final int %s = %d;\n" %
95               (t.tagname.upper(), t.tagnum))
96buffer.write("}\n");
97
98event_log_tags.WriteOutput(output_file, buffer)
99