hg-ssh: refactor to have main() method
Refactor hg-ssh to have a main() function instead of a bunch of
top-level statements.
--- a/contrib/hg-ssh Tue May 22 14:37:20 2012 -0500
+++ b/contrib/hg-ssh Mon May 21 16:19:30 2012 -0700
@@ -33,25 +33,31 @@
import sys, os, shlex
-cwd = os.getcwd()
-allowed_paths = [os.path.normpath(os.path.join(cwd, os.path.expanduser(path)))
- for path in sys.argv[1:]]
-orig_cmd = os.getenv('SSH_ORIGINAL_COMMAND', '?')
-try:
- cmdargv = shlex.split(orig_cmd)
-except ValueError, e:
- sys.stderr.write('Illegal command "%s": %s\n' % (orig_cmd, e))
- sys.exit(255)
+def main():
+ cwd = os.getcwd()
+ allowed_paths = [os.path.normpath(os.path.join(cwd,
+ os.path.expanduser(path)))
+ for path in sys.argv[1:]]
+ orig_cmd = os.getenv('SSH_ORIGINAL_COMMAND', '?')
+ try:
+ cmdargv = shlex.split(orig_cmd)
+ except ValueError, e:
+ sys.stderr.write('Illegal command "%s": %s\n' % (orig_cmd, e))
+ sys.exit(255)
-if cmdargv[:2] == ['hg', '-R'] and cmdargv[3:] == ['serve', '--stdio']:
- path = cmdargv[2]
- repo = os.path.normpath(os.path.join(cwd, os.path.expanduser(path)))
- if repo in allowed_paths:
- dispatch.dispatch(dispatch.request(['-R', repo, 'serve', '--stdio']))
+ if cmdargv[:2] == ['hg', '-R'] and cmdargv[3:] == ['serve', '--stdio']:
+ path = cmdargv[2]
+ repo = os.path.normpath(os.path.join(cwd, os.path.expanduser(path)))
+ if repo in allowed_paths:
+ dispatch.dispatch(dispatch.request(['-R', repo,
+ 'serve',
+ '--stdio']))
+ else:
+ sys.stderr.write('Illegal repository "%s"\n' % repo)
+ sys.exit(255)
else:
- sys.stderr.write('Illegal repository "%s"\n' % repo)
+ sys.stderr.write('Illegal command "%s"\n' % orig_cmd)
sys.exit(255)
-else:
- sys.stderr.write('Illegal command "%s"\n' % orig_cmd)
- sys.exit(255)
+if __name__ == '__main__':
+ main()