##// END OF EJS Templates
py3: fix exception display encoding in contrib/simplemerge.py...
Emmanuel Leblond -
r43684:3c2799cb stable
parent child Browse files
Show More
@@ -1,87 +1,87 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 from __future__ import absolute_import
2 from __future__ import absolute_import
3
3
4 import getopt
4 import getopt
5 import sys
5 import sys
6
6
7 import hgdemandimport
7 import hgdemandimport
8 hgdemandimport.enable()
8 hgdemandimport.enable()
9
9
10 from mercurial.i18n import _
10 from mercurial.i18n import _
11 from mercurial import (
11 from mercurial import (
12 context,
12 context,
13 error,
13 error,
14 fancyopts,
14 fancyopts,
15 pycompat,
15 pycompat,
16 simplemerge,
16 simplemerge,
17 ui as uimod,
17 ui as uimod,
18 )
18 )
19 from mercurial.utils import (
19 from mercurial.utils import (
20 procutil,
20 procutil,
21 stringutil
21 )
22 )
22
23
23 options = [(b'L', b'label', [], _(b'labels to use on conflict markers')),
24 options = [(b'L', b'label', [], _(b'labels to use on conflict markers')),
24 (b'a', b'text', None, _(b'treat all files as text')),
25 (b'a', b'text', None, _(b'treat all files as text')),
25 (b'p', b'print', None,
26 (b'p', b'print', None,
26 _(b'print results instead of overwriting LOCAL')),
27 _(b'print results instead of overwriting LOCAL')),
27 (b'', b'no-minimal', None, _(b'no effect (DEPRECATED)')),
28 (b'', b'no-minimal', None, _(b'no effect (DEPRECATED)')),
28 (b'h', b'help', None, _(b'display help and exit')),
29 (b'h', b'help', None, _(b'display help and exit')),
29 (b'q', b'quiet', None, _(b'suppress output'))]
30 (b'q', b'quiet', None, _(b'suppress output'))]
30
31
31 usage = _(b'''simplemerge [OPTS] LOCAL BASE OTHER
32 usage = _(b'''simplemerge [OPTS] LOCAL BASE OTHER
32
33
33 Simple three-way file merge utility with a minimal feature set.
34 Simple three-way file merge utility with a minimal feature set.
34
35
35 Apply to LOCAL the changes necessary to go from BASE to OTHER.
36 Apply to LOCAL the changes necessary to go from BASE to OTHER.
36
37
37 By default, LOCAL is overwritten with the results of this operation.
38 By default, LOCAL is overwritten with the results of this operation.
38 ''')
39 ''')
39
40
40 class ParseError(Exception):
41 class ParseError(Exception):
41 """Exception raised on errors in parsing the command line."""
42 """Exception raised on errors in parsing the command line."""
42
43
43 def showhelp():
44 def showhelp():
44 pycompat.stdout.write(usage)
45 pycompat.stdout.write(usage)
45 pycompat.stdout.write(b'\noptions:\n')
46 pycompat.stdout.write(b'\noptions:\n')
46
47
47 out_opts = []
48 out_opts = []
48 for shortopt, longopt, default, desc in options:
49 for shortopt, longopt, default, desc in options:
49 out_opts.append((b'%2s%s' % (shortopt and b'-%s' % shortopt,
50 out_opts.append((b'%2s%s' % (shortopt and b'-%s' % shortopt,
50 longopt and b' --%s' % longopt),
51 longopt and b' --%s' % longopt),
51 b'%s' % desc))
52 b'%s' % desc))
52 opts_len = max([len(opt[0]) for opt in out_opts])
53 opts_len = max([len(opt[0]) for opt in out_opts])
53 for first, second in out_opts:
54 for first, second in out_opts:
54 pycompat.stdout.write(b' %-*s %s\n' % (opts_len, first, second))
55 pycompat.stdout.write(b' %-*s %s\n' % (opts_len, first, second))
55
56
56 try:
57 try:
57 for fp in (sys.stdin, pycompat.stdout, sys.stderr):
58 for fp in (sys.stdin, pycompat.stdout, sys.stderr):
58 procutil.setbinary(fp)
59 procutil.setbinary(fp)
59
60
60 opts = {}
61 opts = {}
61 try:
62 try:
62 bargv = [a.encode('utf8') for a in sys.argv[1:]]
63 bargv = [a.encode('utf8') for a in sys.argv[1:]]
63 args = fancyopts.fancyopts(bargv, options, opts)
64 args = fancyopts.fancyopts(bargv, options, opts)
64 except getopt.GetoptError as e:
65 except getopt.GetoptError as e:
65 raise ParseError(e)
66 raise ParseError(e)
66 if opts[b'help']:
67 if opts[b'help']:
67 showhelp()
68 showhelp()
68 sys.exit(0)
69 sys.exit(0)
69 if len(args) != 3:
70 if len(args) != 3:
70 raise ParseError(_(b'wrong number of arguments').decode('utf8'))
71 raise ParseError(_(b'wrong number of arguments').decode('utf8'))
71 local, base, other = args
72 local, base, other = args
72 sys.exit(simplemerge.simplemerge(uimod.ui.load(),
73 sys.exit(simplemerge.simplemerge(uimod.ui.load(),
73 context.arbitraryfilectx(local),
74 context.arbitraryfilectx(local),
74 context.arbitraryfilectx(base),
75 context.arbitraryfilectx(base),
75 context.arbitraryfilectx(other),
76 context.arbitraryfilectx(other),
76 **pycompat.strkwargs(opts)))
77 **pycompat.strkwargs(opts)))
77 except ParseError as e:
78 except ParseError as e:
78 if pycompat.ispy3:
79 e = stringutil.forcebytestr(e)
79 e = str(e).encode('utf8')
80 pycompat.stdout.write(b"%s: %s\n" % (sys.argv[0].encode('utf8'), e))
80 pycompat.stdout.write(b"%s: %s\n" % (sys.argv[0].encode('utf8'), e))
81 showhelp()
81 showhelp()
82 sys.exit(1)
82 sys.exit(1)
83 except error.Abort as e:
83 except error.Abort as e:
84 pycompat.stderr.write(b"abort: %s\n" % e)
84 pycompat.stderr.write(b"abort: %s\n" % e)
85 sys.exit(255)
85 sys.exit(255)
86 except KeyboardInterrupt:
86 except KeyboardInterrupt:
87 sys.exit(255)
87 sys.exit(255)
General Comments 0
You need to be logged in to leave comments. Login now