1#!/usr/bin/env python
2#
3# gen-javahl-errors.py: Generate a Java class containing an enum for the
4#                       C error codes
5#
6# ====================================================================
7#    Licensed to the Apache Software Foundation (ASF) under one
8#    or more contributor license agreements.  See the NOTICE file
9#    distributed with this work for additional information
10#    regarding copyright ownership.  The ASF licenses this file
11#    to you under the Apache License, Version 2.0 (the
12#    "License"); you may not use this file except in compliance
13#    with the License.  You may obtain a copy of the License at
14#
15#      http://www.apache.org/licenses/LICENSE-2.0
16#
17#    Unless required by applicable law or agreed to in writing,
18#    software distributed under the License is distributed on an
19#    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20#    KIND, either express or implied.  See the License for the
21#    specific language governing permissions and limitations
22#    under the License.
23# ====================================================================
24#
25
26import sys, os
27
28try:
29  from svn import core
30except ImportError as e:
31  sys.stderr.write("ERROR: Unable to import Subversion's Python bindings: '%s'\n" \
32                   "Hint: Set your PYTHONPATH environment variable, or adjust your " \
33                   "PYTHONSTARTUP\nfile to point to your Subversion install " \
34                   "location's svn-python directory.\n" % e)
35  sys.stderr.flush()
36  sys.exit(1)
37
38def get_errors():
39  errs = {}
40  for key in vars(core):
41    if key.find('SVN_ERR_') == 0:
42      try:
43        val = int(vars(core)[key])
44        errs[val] = key
45      except:
46        pass
47  return errs
48
49def gen_javahl_class(error_codes, output_filename):
50  jfile = open(output_filename, 'w')
51  jfile.write(
52"""/** ErrorCodes.java - This file is autogenerated by gen-javahl-errors.py
53 */
54
55package org.tigris.subversion.javahl;
56
57/**
58 * Provide mappings from error codes generated by the C runtime to meaningful
59 * Java values.  For a better description of each error, please see
60 * svn_error_codes.h in the C source.
61 */
62public class ErrorCodes
63{
64""")
65
66  keys = sorted(error_codes.keys())
67
68  for key in keys:
69    # Format the code name to be more Java-esque
70    code_name = error_codes[key][8:].replace('_', ' ').title().replace(' ', '')
71    code_name = code_name[0].lower() + code_name[1:]
72
73    jfile.write("    public static final int %s = %d;\n" % (code_name, key))
74
75  jfile.write("}\n")
76  jfile.close()
77
78if __name__ == "__main__":
79  if len(sys.argv) > 1:
80    output_filename = sys.argv[1]
81  else:
82    output_filename = os.path.join('..', '..', 'subversion', 'bindings',
83                                   'javahl', 'src', 'org', 'tigris',
84                                   'subversion', 'javahl', 'ErrorCodes.java')
85
86  gen_javahl_class(get_errors(), output_filename)
87