summaryrefslogtreecommitdiff
path: root/tools/dev/gen-py-errors.py
blob: 9ca0365f7c4708850a6a3aa46ee8c7aecf55eff1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python
#
# gen-py-errors.py: Generate a python module which maps error names to numbers.
#                   (The purpose being easier writing of the python tests.)
#
# ====================================================================
#    Licensed to the Apache Software Foundation (ASF) under one
#    or more contributor license agreements.  See the NOTICE file
#    distributed with this work for additional information
#    regarding copyright ownership.  The ASF licenses this file
#    to you under the Apache License, Version 2.0 (the
#    "License"); you may not use this file except in compliance
#    with the License.  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing,
#    software distributed under the License is distributed on an
#    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#    KIND, either express or implied.  See the License for the
#    specific language governing permissions and limitations
#    under the License.
# ====================================================================
#
#
# Locates svn_error_codes.h based on its relative location to this script.
#
# Generates to STDOUT. Typically, redirect this into svntest/err.py
#

import sys
import os
import re

HEADER = '''#!/usr/bin/env python
### This file automatically generated by tools/dev/gen-py-errors.py,
### which see for more information
###
### It is versioned for convenience.
'''

# Established by svn 1.0. May as well hard-code these.
APR_OS_START_ERROR = 20000
APR_OS_START_USERERR = APR_OS_START_ERROR + 50000 * 2
SVN_ERR_CATEGORY_SIZE = 5000

RE_CAT_NAME = re.compile(r'SVN_ERR_([A-Z_]+)_CATEG')
RE_CAT_VALUE = re.compile(r'\d+')

RE_DEF_NAME = re.compile(r'SVN_ERRDEF\(([A-Z0-9_]+)')
RE_DEF_VALUE = re.compile(r'SVN_ERR_([A-Z_]+)_CATEG[^0-9]*([0-9]+)')


def write_output(codes):
  print HEADER

  for name, value in codes:
    # skip SVN_ERR_ on the name
    print '%s = %d' % (name[8:], value)


def main(codes_fname):
  categ = { }
  codes = [ ]

  f = open(codes_fname)

  # Parse all the category start values
  while True:
    line = f.next()
    m = RE_CAT_NAME.search(line)
    if m:
      name = m.group(1)
      m = RE_CAT_VALUE.search(f.next())
      assert m
      value = int(m.group(0))
      categ[name] = APR_OS_START_USERERR + value * SVN_ERR_CATEGORY_SIZE

    elif line.strip() == 'SVN_ERROR_START':
      break

  # Parse each of the error values
  while True:
    line = f.next()
    m = RE_DEF_NAME.search(line)
    if m:
      name = m.group(1)
      line = f.next()
      m = RE_DEF_VALUE.search(line)
      if not m:
        # SVN_ERR_WC_NOT_DIRECTORY is defined as equal to NOT_WORKING_COPY
        # rather than relative to SVN_ERR_WC_CATEGORY_START
        #print 'SKIP:', line
        continue
      cat = m.group(1)
      value = int(m.group(2))
      codes.append((name, categ[cat] + value))

    elif line.strip() == 'SVN_ERROR_END':
      break

  write_output(sorted(codes))


if __name__ == '__main__':
  this_dir = os.path.dirname(os.path.abspath(__file__))
  codes_fname = os.path.join(this_dir, os.path.pardir, os.path.pardir,
                             'subversion', 'include', 'svn_error_codes.h')
  main(codes_fname)