##// END OF EJS Templates
misc: use modern exception syntax...
FUJIWARA Katsunori -
r28047:863075fd default
parent child Browse files
Show More
@@ -1,86 +1,86 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 #
2 #
3 # Copyright 2005-2007 by Intevation GmbH <intevation@intevation.de>
3 # Copyright 2005-2007 by Intevation GmbH <intevation@intevation.de>
4 #
4 #
5 # Author(s):
5 # Author(s):
6 # Thomas Arendsen Hein <thomas@intevation.de>
6 # Thomas Arendsen Hein <thomas@intevation.de>
7 #
7 #
8 # This software may be used and distributed according to the terms of the
8 # This software may be used and distributed according to the terms of the
9 # GNU General Public License version 2 or any later version.
9 # GNU General Public License version 2 or any later version.
10
10
11 """
11 """
12 hg-ssh - a wrapper for ssh access to a limited set of mercurial repos
12 hg-ssh - a wrapper for ssh access to a limited set of mercurial repos
13
13
14 To be used in ~/.ssh/authorized_keys with the "command" option, see sshd(8):
14 To be used in ~/.ssh/authorized_keys with the "command" option, see sshd(8):
15 command="hg-ssh path/to/repo1 /path/to/repo2 ~/repo3 ~user/repo4" ssh-dss ...
15 command="hg-ssh path/to/repo1 /path/to/repo2 ~/repo3 ~user/repo4" ssh-dss ...
16 (probably together with these other useful options:
16 (probably together with these other useful options:
17 no-port-forwarding,no-X11-forwarding,no-agent-forwarding)
17 no-port-forwarding,no-X11-forwarding,no-agent-forwarding)
18
18
19 This allows pull/push over ssh from/to the repositories given as arguments.
19 This allows pull/push over ssh from/to the repositories given as arguments.
20
20
21 If all your repositories are subdirectories of a common directory, you can
21 If all your repositories are subdirectories of a common directory, you can
22 allow shorter paths with:
22 allow shorter paths with:
23 command="cd path/to/my/repositories && hg-ssh repo1 subdir/repo2"
23 command="cd path/to/my/repositories && hg-ssh repo1 subdir/repo2"
24
24
25 You can use pattern matching of your normal shell, e.g.:
25 You can use pattern matching of your normal shell, e.g.:
26 command="cd repos && hg-ssh user/thomas/* projects/{mercurial,foo}"
26 command="cd repos && hg-ssh user/thomas/* projects/{mercurial,foo}"
27
27
28 You can also add a --read-only flag to allow read-only access to a key, e.g.:
28 You can also add a --read-only flag to allow read-only access to a key, e.g.:
29 command="hg-ssh --read-only repos/*"
29 command="hg-ssh --read-only repos/*"
30 """
30 """
31
31
32 # enable importing on demand to reduce startup time
32 # enable importing on demand to reduce startup time
33 from mercurial import demandimport; demandimport.enable()
33 from mercurial import demandimport; demandimport.enable()
34
34
35 from mercurial import dispatch
35 from mercurial import dispatch
36
36
37 import sys, os, shlex
37 import sys, os, shlex
38
38
39 def main():
39 def main():
40 cwd = os.getcwd()
40 cwd = os.getcwd()
41 readonly = False
41 readonly = False
42 args = sys.argv[1:]
42 args = sys.argv[1:]
43 while len(args):
43 while len(args):
44 if args[0] == '--read-only':
44 if args[0] == '--read-only':
45 readonly = True
45 readonly = True
46 args.pop(0)
46 args.pop(0)
47 else:
47 else:
48 break
48 break
49 allowed_paths = [os.path.normpath(os.path.join(cwd,
49 allowed_paths = [os.path.normpath(os.path.join(cwd,
50 os.path.expanduser(path)))
50 os.path.expanduser(path)))
51 for path in args]
51 for path in args]
52 orig_cmd = os.getenv('SSH_ORIGINAL_COMMAND', '?')
52 orig_cmd = os.getenv('SSH_ORIGINAL_COMMAND', '?')
53 try:
53 try:
54 cmdargv = shlex.split(orig_cmd)
54 cmdargv = shlex.split(orig_cmd)
55 except ValueError, e:
55 except ValueError as e:
56 sys.stderr.write('Illegal command "%s": %s\n' % (orig_cmd, e))
56 sys.stderr.write('Illegal command "%s": %s\n' % (orig_cmd, e))
57 sys.exit(255)
57 sys.exit(255)
58
58
59 if cmdargv[:2] == ['hg', '-R'] and cmdargv[3:] == ['serve', '--stdio']:
59 if cmdargv[:2] == ['hg', '-R'] and cmdargv[3:] == ['serve', '--stdio']:
60 path = cmdargv[2]
60 path = cmdargv[2]
61 repo = os.path.normpath(os.path.join(cwd, os.path.expanduser(path)))
61 repo = os.path.normpath(os.path.join(cwd, os.path.expanduser(path)))
62 if repo in allowed_paths:
62 if repo in allowed_paths:
63 cmd = ['-R', repo, 'serve', '--stdio']
63 cmd = ['-R', repo, 'serve', '--stdio']
64 if readonly:
64 if readonly:
65 cmd += [
65 cmd += [
66 '--config',
66 '--config',
67 'hooks.pretxnopen.hg-ssh=python:__main__.rejectpush',
67 'hooks.pretxnopen.hg-ssh=python:__main__.rejectpush',
68 '--config',
68 '--config',
69 'hooks.prepushkey.hg-ssh=python:__main__.rejectpush'
69 'hooks.prepushkey.hg-ssh=python:__main__.rejectpush'
70 ]
70 ]
71 dispatch.dispatch(dispatch.request(cmd))
71 dispatch.dispatch(dispatch.request(cmd))
72 else:
72 else:
73 sys.stderr.write('Illegal repository "%s"\n' % repo)
73 sys.stderr.write('Illegal repository "%s"\n' % repo)
74 sys.exit(255)
74 sys.exit(255)
75 else:
75 else:
76 sys.stderr.write('Illegal command "%s"\n' % orig_cmd)
76 sys.stderr.write('Illegal command "%s"\n' % orig_cmd)
77 sys.exit(255)
77 sys.exit(255)
78
78
79 def rejectpush(ui, **kwargs):
79 def rejectpush(ui, **kwargs):
80 ui.warn(("Permission denied\n"))
80 ui.warn(("Permission denied\n"))
81 # mercurial hooks use unix process conventions for hook return values
81 # mercurial hooks use unix process conventions for hook return values
82 # so a truthy return means failure
82 # so a truthy return means failure
83 return True
83 return True
84
84
85 if __name__ == '__main__':
85 if __name__ == '__main__':
86 main()
86 main()
@@ -1,66 +1,66 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2
2
3 from mercurial import demandimport
3 from mercurial import demandimport
4 demandimport.enable()
4 demandimport.enable()
5
5
6 import sys
6 import sys
7 from mercurial.i18n import _
7 from mercurial.i18n import _
8 from mercurial import error, simplemerge, fancyopts, util, ui
8 from mercurial import error, simplemerge, fancyopts, util, ui
9
9
10 options = [('L', 'label', [], _('labels to use on conflict markers')),
10 options = [('L', 'label', [], _('labels to use on conflict markers')),
11 ('a', 'text', None, _('treat all files as text')),
11 ('a', 'text', None, _('treat all files as text')),
12 ('p', 'print', None,
12 ('p', 'print', None,
13 _('print results instead of overwriting LOCAL')),
13 _('print results instead of overwriting LOCAL')),
14 ('', 'no-minimal', None, _('no effect (DEPRECATED)')),
14 ('', 'no-minimal', None, _('no effect (DEPRECATED)')),
15 ('h', 'help', None, _('display help and exit')),
15 ('h', 'help', None, _('display help and exit')),
16 ('q', 'quiet', None, _('suppress output'))]
16 ('q', 'quiet', None, _('suppress output'))]
17
17
18 usage = _('''simplemerge [OPTS] LOCAL BASE OTHER
18 usage = _('''simplemerge [OPTS] LOCAL BASE OTHER
19
19
20 Simple three-way file merge utility with a minimal feature set.
20 Simple three-way file merge utility with a minimal feature set.
21
21
22 Apply to LOCAL the changes necessary to go from BASE to OTHER.
22 Apply to LOCAL the changes necessary to go from BASE to OTHER.
23
23
24 By default, LOCAL is overwritten with the results of this operation.
24 By default, LOCAL is overwritten with the results of this operation.
25 ''')
25 ''')
26
26
27 class ParseError(Exception):
27 class ParseError(Exception):
28 """Exception raised on errors in parsing the command line."""
28 """Exception raised on errors in parsing the command line."""
29
29
30 def showhelp():
30 def showhelp():
31 sys.stdout.write(usage)
31 sys.stdout.write(usage)
32 sys.stdout.write('\noptions:\n')
32 sys.stdout.write('\noptions:\n')
33
33
34 out_opts = []
34 out_opts = []
35 for shortopt, longopt, default, desc in options:
35 for shortopt, longopt, default, desc in options:
36 out_opts.append(('%2s%s' % (shortopt and '-%s' % shortopt,
36 out_opts.append(('%2s%s' % (shortopt and '-%s' % shortopt,
37 longopt and ' --%s' % longopt),
37 longopt and ' --%s' % longopt),
38 '%s' % desc))
38 '%s' % desc))
39 opts_len = max([len(opt[0]) for opt in out_opts])
39 opts_len = max([len(opt[0]) for opt in out_opts])
40 for first, second in out_opts:
40 for first, second in out_opts:
41 sys.stdout.write(' %-*s %s\n' % (opts_len, first, second))
41 sys.stdout.write(' %-*s %s\n' % (opts_len, first, second))
42
42
43 try:
43 try:
44 for fp in (sys.stdin, sys.stdout, sys.stderr):
44 for fp in (sys.stdin, sys.stdout, sys.stderr):
45 util.setbinary(fp)
45 util.setbinary(fp)
46
46
47 opts = {}
47 opts = {}
48 try:
48 try:
49 args = fancyopts.fancyopts(sys.argv[1:], options, opts)
49 args = fancyopts.fancyopts(sys.argv[1:], options, opts)
50 except fancyopts.getopt.GetoptError, e:
50 except fancyopts.getopt.GetoptError as e:
51 raise ParseError(e)
51 raise ParseError(e)
52 if opts['help']:
52 if opts['help']:
53 showhelp()
53 showhelp()
54 sys.exit(0)
54 sys.exit(0)
55 if len(args) != 3:
55 if len(args) != 3:
56 raise ParseError(_('wrong number of arguments'))
56 raise ParseError(_('wrong number of arguments'))
57 sys.exit(simplemerge.simplemerge(ui.ui(), *args, **opts))
57 sys.exit(simplemerge.simplemerge(ui.ui(), *args, **opts))
58 except ParseError, e:
58 except ParseError as e:
59 sys.stdout.write("%s: %s\n" % (sys.argv[0], e))
59 sys.stdout.write("%s: %s\n" % (sys.argv[0], e))
60 showhelp()
60 showhelp()
61 sys.exit(1)
61 sys.exit(1)
62 except error.Abort, e:
62 except error.Abort as e:
63 sys.stderr.write("abort: %s\n" % e)
63 sys.stderr.write("abort: %s\n" % e)
64 sys.exit(255)
64 sys.exit(255)
65 except KeyboardInterrupt:
65 except KeyboardInterrupt:
66 sys.exit(255)
66 sys.exit(255)
@@ -1,64 +1,64 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 """Test the running system for features availability. Exit with zero
2 """Test the running system for features availability. Exit with zero
3 if all features are there, non-zero otherwise. If a feature name is
3 if all features are there, non-zero otherwise. If a feature name is
4 prefixed with "no-", the absence of feature is tested.
4 prefixed with "no-", the absence of feature is tested.
5 """
5 """
6 import optparse
6 import optparse
7 import os, sys
7 import os, sys
8 import hghave
8 import hghave
9
9
10 checks = hghave.checks
10 checks = hghave.checks
11
11
12 def list_features():
12 def list_features():
13 for name, feature in sorted(checks.iteritems()):
13 for name, feature in sorted(checks.iteritems()):
14 desc = feature[1]
14 desc = feature[1]
15 print name + ':', desc
15 print name + ':', desc
16
16
17 def test_features():
17 def test_features():
18 failed = 0
18 failed = 0
19 for name, feature in checks.iteritems():
19 for name, feature in checks.iteritems():
20 check, _ = feature
20 check, _ = feature
21 try:
21 try:
22 check()
22 check()
23 except Exception, e:
23 except Exception as e:
24 print "feature %s failed: %s" % (name, e)
24 print "feature %s failed: %s" % (name, e)
25 failed += 1
25 failed += 1
26 return failed
26 return failed
27
27
28 parser = optparse.OptionParser("%prog [options] [features]")
28 parser = optparse.OptionParser("%prog [options] [features]")
29 parser.add_option("--test-features", action="store_true",
29 parser.add_option("--test-features", action="store_true",
30 help="test available features")
30 help="test available features")
31 parser.add_option("--list-features", action="store_true",
31 parser.add_option("--list-features", action="store_true",
32 help="list available features")
32 help="list available features")
33
33
34 def _loadaddon():
34 def _loadaddon():
35 if 'TESTDIR' in os.environ:
35 if 'TESTDIR' in os.environ:
36 # loading from '.' isn't needed, because `hghave` should be
36 # loading from '.' isn't needed, because `hghave` should be
37 # running at TESTTMP in this case
37 # running at TESTTMP in this case
38 path = os.environ['TESTDIR']
38 path = os.environ['TESTDIR']
39 else:
39 else:
40 path = '.'
40 path = '.'
41
41
42 if not os.path.exists(os.path.join(path, 'hghaveaddon.py')):
42 if not os.path.exists(os.path.join(path, 'hghaveaddon.py')):
43 return
43 return
44
44
45 sys.path.insert(0, path)
45 sys.path.insert(0, path)
46 try:
46 try:
47 import hghaveaddon
47 import hghaveaddon
48 except BaseException, inst:
48 except BaseException as inst:
49 sys.stderr.write('failed to import hghaveaddon.py from %r: %s\n'
49 sys.stderr.write('failed to import hghaveaddon.py from %r: %s\n'
50 % (path, inst))
50 % (path, inst))
51 sys.exit(2)
51 sys.exit(2)
52 sys.path.pop(0)
52 sys.path.pop(0)
53
53
54 if __name__ == '__main__':
54 if __name__ == '__main__':
55 options, args = parser.parse_args()
55 options, args = parser.parse_args()
56 _loadaddon()
56 _loadaddon()
57 if options.list_features:
57 if options.list_features:
58 list_features()
58 list_features()
59 sys.exit(0)
59 sys.exit(0)
60
60
61 if options.test_features:
61 if options.test_features:
62 sys.exit(test_features())
62 sys.exit(test_features())
63
63
64 hghave.require(args)
64 hghave.require(args)
General Comments 0
You need to be logged in to leave comments. Login now