1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
4# which-error.py: Print semantic Subversion error code names mapped from
5#                 their numeric error code values
6#
7# ====================================================================
8#    Licensed to the Apache Software Foundation (ASF) under one
9#    or more contributor license agreements.  See the NOTICE file
10#    distributed with this work for additional information
11#    regarding copyright ownership.  The ASF licenses this file
12#    to you under the Apache License, Version 2.0 (the
13#    "License"); you may not use this file except in compliance
14#    with the License.  You may obtain a copy of the License at
15#
16#      http://www.apache.org/licenses/LICENSE-2.0
17#
18#    Unless required by applicable law or agreed to in writing,
19#    software distributed under the License is distributed on an
20#    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21#    KIND, either express or implied.  See the License for the
22#    specific language governing permissions and limitations
23#    under the License.
24# ====================================================================
25#
26# $HeadURL: https://svn.apache.org/repos/asf/subversion/branches/1.14.x/tools/dev/which-error.py $
27# $LastChangedDate: 2016-04-30 08:16:53 +0000 (Sat, 30 Apr 2016) $
28# $LastChangedBy: stefan2 $
29# $LastChangedRevision: 1741723 $
30#
31
32import errno
33import sys
34import os.path
35import re
36
37try:
38  from svn import core
39except ImportError as e:
40  sys.stderr.write("ERROR: Unable to import Subversion's Python bindings: '%s'\n" \
41                   "Hint: Set your PYTHONPATH environment variable, or adjust your " \
42                   "PYTHONSTARTUP\nfile to point to your Subversion install " \
43                   "location's svn-python directory.\n" % e)
44  sys.stderr.flush()
45  sys.exit(1)
46
47
48def usage_and_exit():
49  progname = os.path.basename(sys.argv[0])
50  sys.stderr.write("""Usage: 1. %s ERRNUM [...]
51       2. %s parse
52       3. %s list
53
54Print numeric and semantic error code information for Subversion error
55codes.  This can be done in variety of ways:
56
57   1. For each ERRNUM, list the error code information.
58
59   2. Parse standard input as if it was error stream from a debug-mode
60      Subversion command-line client, echoing that input to stdout,
61      followed by the error code information for codes found in use in
62      that error stream.
63
64   3. Simply list the error code information for all known such
65      mappings.
66
67""" % (progname, progname, progname))
68  sys.exit(1)
69
70def get_errors():
71  errs = {}
72  ## errno values.
73  errs.update(errno.errorcode)
74  ## APR-defined errors, from apr_errno.h.
75  dirname = os.path.dirname(os.path.realpath(__file__))
76  for line in open(os.path.join(dirname, 'aprerr.txt')):
77    # aprerr.txt parsing duplicated in gen_base.py:write_errno_table()
78    if line.startswith('#'):
79       continue
80    key, _, val = line.split()
81    errs[int(val)] = key
82  ## Subversion errors, from svn_error_codes.h.
83  for key in vars(core):
84    if key.find('SVN_ERR_') == 0:
85      try:
86        val = int(vars(core)[key])
87        errs[val] = key
88      except:
89        pass
90  return errs
91
92def print_error(code):
93  try:
94    print('%08d  %s' % (code, __svn_error_codes[code]))
95  except KeyError:
96    if code == -41:
97      print("Sit by a lake.")
98    elif code >= 120100 and code < 121000:
99      print('%08d  <error code from libserf; see serf.h>' % (code))
100    else:
101      print('%08d  *** UNKNOWN ERROR CODE ***' % (code))
102
103if __name__ == "__main__":
104  global __svn_error_codes
105  __svn_error_codes = get_errors()
106  codes = []
107  if len(sys.argv) < 2:
108    usage_and_exit()
109
110  # Get a list of known codes
111  if sys.argv[1] == 'list':
112    if len(sys.argv) > 2:
113      usage_and_exit()
114    codes = sorted(__svn_error_codes.keys())
115
116  # Get a list of code by parsing stdin for apr_err=CODE instances
117  elif sys.argv[1] == 'parse':
118    if len(sys.argv) > 2:
119      usage_and_exit()
120    while True:
121      line = sys.stdin.readline()
122      if not line:
123        break
124      sys.stdout.write(line)
125      match = re.match(r'^.*apr_err=([0-9]+)[^0-9].*$', line)
126      if match:
127        codes.append(int(match.group(1)))
128
129  # Get the list of requested codes
130  else:
131    for code in sys.argv[1:]:
132      try:
133        code = code.lstrip('EW')
134        codes.append(int(code))
135      except ValueError:
136        usage_and_exit()
137
138  # Print the harvest codes
139  for code in codes:
140    print_error(code)
141
142
143