Don't mix exceptions and sys.exit based failures
authorPaul Crowley <paul@lshift.net>
Mon, 12 Oct 2009 17:04:26 +0100
changeset 109 72100d3ed1bd
parent 108 00b48d7bdfa0
child 110 69596fffcf7d
Don't mix exceptions and sys.exit based failures
src/hg-ssh
src/mercurialserver/ruleset.py
--- a/src/hg-ssh	Mon Oct 12 16:52:06 2009 +0100
+++ b/src/hg-ssh	Mon Oct 12 17:04:26 2009 +0100
@@ -46,7 +46,7 @@
     if path == "":
         return
     if os.path.exists(path + "/.hg"):
-        raise ruleset.AccessException()
+        fail("Cannot create repo under existing repo")
     checkpath(path)
 
 def getrepo(op, repo):
@@ -59,7 +59,8 @@
         if component.startswith("."):
             fail("paths cannot contain dot file components")
     ruleset.rules.set(repo=repo)
-    ruleset.rules.check(op, branch=None, file=None)
+    if not ruleset.rules.allow(op, branch=None, file=None):
+        fail("access denied")
     checkpath(repo)
     return repo
 
@@ -90,24 +91,21 @@
         ruleset.rules.readfile(f)
 
 cmd = os.environ.get('SSH_ORIGINAL_COMMAND', None)
-try:
-    if cmd is None:
-        fail("direct logins on the hg account prohibited")
-    elif cmd.startswith('hg -R ') and cmd.endswith(' serve --stdio'):
-        repo = getrepo("read", cmd[6:-14])
-        if not os.path.isdir(repo + "/.hg"):
-            fail("no such repository %s" % repo)
-        dispatch.dispatch(['-R', repo, 'serve', '--stdio'])
-    elif cmd.startswith('hg init '):
-        repo = getrepo("init", cmd[8:])
-        if os.path.exists(repo):
-            fail("%s exists" % repo)
-        d = os.path.dirname(repo)
-        if d != "" and not os.path.isdir(d):
-            os.makedirs(d)
-        dispatch.dispatch(['init', repo])
-    else:
-        fail("illegal command %r" % cmd)
-except ruleset.AccessException:
-    fail("access denied")
+if cmd is None:
+    fail("direct logins on the hg account prohibited")
+elif cmd.startswith('hg -R ') and cmd.endswith(' serve --stdio'):
+    repo = getrepo("read", cmd[6:-14])
+    if not os.path.isdir(repo + "/.hg"):
+        fail("no such repository %s" % repo)
+    dispatch.dispatch(['-R', repo, 'serve', '--stdio'])
+elif cmd.startswith('hg init '):
+    repo = getrepo("init", cmd[8:])
+    if os.path.exists(repo):
+        fail("%s exists" % repo)
+    d = os.path.dirname(repo)
+    if d != "" and not os.path.isdir(d):
+        os.makedirs(d)
+    dispatch.dispatch(['init', repo])
+else:
+    fail("illegal command %r" % cmd)
 
--- a/src/mercurialserver/ruleset.py	Mon Oct 12 16:52:06 2009 +0100
+++ b/src/mercurialserver/ruleset.py	Mon Oct 12 17:04:26 2009 +0100
@@ -28,9 +28,6 @@
         return True
     return c
 
-class AccessException(Exception):
-    pass
-
 class Ruleset(object):
     '''Class representing the rules in a rule file'''
     
@@ -61,10 +58,6 @@
         a = self.matchrule(kw)
         return a in self.levels and self.levels.index(a) <= self.levels.index(level)
     
-    def check(self, level, **kw):
-        if not self.allow(level, **kw):
-            raise AccessException()
-    
     def readfile(self, fn):
         try:
             f = open(fn)