java-event-log-tags.py revision 9bd4962af87257c6a97e9026af7e4764394412c2
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
63if 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
68buffer = cStringIO.StringIO()
69buffer.write("/* This file is auto-generated.  DO NOT MODIFY.\n"
70             " * Source file: %s\n"
71             " */\n\n" % (fn,))
72
73buffer.write("package %s;\n\n" % (tagfile.options["java_package"][0],))
74
75basename, _ = os.path.splitext(os.path.basename(fn))
76buffer.write("public class %s {\n" % (basename,))
77buffer.write("  private %s() { }  // don't instantiate\n" % (basename,))
78
79for 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))
87buffer.write("}\n");
88
89event_log_tags.WriteOutput(output_file, buffer)
90