Fix compiler warning due to missing function prototype.
[svn.git] / tools / dev / which-error.py
blob6c08746277d8effbe96abec4df448a16d6c2fb7d
1 #!/usr/bin/env python
3 # which-error.py: Print semantic Subversion error code names mapped from
4 # their numeric error code values
6 # ====================================================================
7 # Copyright (c) 2005 CollabNet. All rights reserved.
9 # * This software is licensed as described in the file COPYING, which
10 # you should have received as part of this distribution. The terms
11 # are also available at http://subversion.tigris.org/license-1.html.
12 # If newer versions of this license are posted there, you may use a
13 # newer version instead, at your option.
15 # * This software consists of voluntary contributions made by many
16 # individuals. For exact contribution history, see the revision
17 # history and logs, available at http://subversion.tigris.org/.
18 # ====================================================================
20 # $HeadURL$
21 # $LastChangedDate$
22 # $LastChangedBy$
23 # $LastChangedRevision$
26 import sys
27 import os.path
28 import re
30 try:
31 from svn import core
32 except ImportError, e:
33 print >> sys.stderr, \
34 "ERROR: Unable to import Subversion's Python bindings: '%s'\n" \
35 "Hint: Set your PYTHONPATH environment variable, or adjust your " \
36 "PYTHONSTARTUP\nfile to point to your Subversion install " \
37 "location's svn-python directory." % e
38 sys.exit(1)
41 def usage_and_exit():
42 progname = os.path.basename(sys.argv[0])
43 sys.stderr.write("""Usage: 1. %s ERRNUM [...]
44 2. %s parse
45 3. %s list
47 Print numeric and semantic error code information for Subversion error
48 codes. This can be done in variety of ways:
50 1. For each ERRNUM, list the error code information.
52 2. Parse standard input as if it was error stream from a debug-mode
53 Subversion command-line client, echoing that input to stdout,
54 followed by the error code information for codes found in use in
55 that error stream.
57 3. Simply list the error code information for all known such
58 mappings.
60 """ % (progname, progname, progname))
61 sys.exit(1)
63 def get_errors():
64 errs = {}
65 for key in vars(core):
66 if key.find('SVN_ERR_') == 0:
67 try:
68 val = int(vars(core)[key])
69 errs[val] = key
70 except:
71 pass
72 return errs
74 def print_error(code):
75 try:
76 print '%08d %s' % (code, __svn_error_codes[code])
77 except KeyError:
78 print '%08d *** UNKNOWN ERROR CODE ***' % (code)
80 if __name__ == "__main__":
81 global __svn_error_codes
82 __svn_error_codes = get_errors()
83 codes = []
84 if len(sys.argv) < 2:
85 usage_and_exit()
87 # Get a list of known codes
88 if sys.argv[1] == 'list':
89 if len(sys.argv) > 2:
90 usage_and_exit()
91 codes = __svn_error_codes.keys()
92 codes.sort()
94 # Get a list of code by parsing stdin for apr_err=CODE instances
95 elif sys.argv[1] == 'parse':
96 if len(sys.argv) > 2:
97 usage_and_exit()
98 while 1:
99 line = sys.stdin.readline()
100 if not line:
101 break
102 sys.stdout.write(line)
103 match = re.match(r'^.*apr_err=([0-9]+)[^0-9].*$', line)
104 if match:
105 codes.append(int(match.group(1)))
107 # Get the list of requested codes
108 else:
109 for code in sys.argv[1:]:
110 try:
111 codes.append(int(code))
112 except ValueError:
113 usage_and_exit()
115 # Print the harvest codes
116 for code in codes:
117 print_error(code)