[PATCH 1 of 2 v2] hg-ssh: refactor to have main() method

David Schleimer dschleimer at fb.com
Mon May 21 18:29:24 CDT 2012


# HG changeset patch
# User David Schleimer <dschleimer at fb.com>
# Date 1337642370 25200
# Node ID b52b7fe0dd08b257dfc69c8a5de503cec94f4b76
# Parent  d0b9ebba41e9a1733294d5fa1b497ada5eda93c8
hg-ssh: refactor to have main() method

Refactor hg-ssh to have a main() function instead of a bunch of
top-level statements.

diff --git a/contrib/hg-ssh b/contrib/hg-ssh
--- a/contrib/hg-ssh
+++ b/contrib/hg-ssh
@@ -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()


More information about the Mercurial-devel mailing list