chpwd, config.py: Don't fail if there's no configuration file.
authorMark Wooding <mdw@distorted.org.uk>
Sun, 5 Apr 2015 21:40:15 +0000 (22:40 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Tue, 7 Apr 2015 18:54:14 +0000 (19:54 +0100)
Things won't work well, but at least the initial make won't fail.

chpwd
config.py

diff --git a/chpwd b/chpwd
index 07fe830..dc054b4 100755 (executable)
--- a/chpwd
+++ b/chpwd
@@ -229,13 +229,13 @@ if __name__ == '__main__':
     ## `USERV_USER' environment variable.
 
     with cli_errors():
-      args = parse_options()
-      if not args or args[0] != 'userv':
-        raise U.ExpectedError, (500, 'missing userv token')
-      CONF.loadconfig(OPTS.config)
-      try: CU.set_user(ENV['USERV_USER'])
-      except KeyError: raise ExpectedError, (500, 'USERV_USER unset')
       with OUT.redirect_to(O.FileOutput()):
+        args = parse_options()
+        if not args or args[0] != 'userv':
+          raise U.ExpectedError, (500, 'missing userv token')
+        CONF.loadconfig(OPTS.config)
+        try: CU.set_user(ENV['USERV_USER'])
+        except KeyError: raise ExpectedError, (500, 'USERV_USER unset')
         OPTPARSE.dispatch('userv', [ENV['USERV_SERVICE']] + args[1:])
 
   elif 'SSH_ORIGINAL_COMMAND' in ENV:
@@ -256,9 +256,9 @@ if __name__ == '__main__':
       ## of telling us that this is a user request, so treat it like Userv.
 
       with cli_errors():
-        cmd = ssh_setup()
-        CU.set_user(ENV['CHPWD_SSH_USER'])
         with OUT.redirect_to(O.FileOutput()):
+          cmd = ssh_setup()
+          CU.set_user(ENV['CHPWD_SSH_USER'])
           OPTPARSE.dispatch('userv', cmd)
 
     elif 'CHPWD_SSH_MASTER' in ENV:
@@ -268,8 +268,8 @@ if __name__ == '__main__':
       ## a user.
 
       try:
-        cmd = ssh_setup()
         with OUT.redirect_to(O.RemoteOutput()):
+          cmd = ssh_setup()
           OPTPARSE.dispatch('remote', map(CGI.urldecode, cmd))
       except U.ExpectedError, e:
         print 'ERR', e.code, e.msg
@@ -290,20 +290,20 @@ if __name__ == '__main__':
     ## as we are.
 
     with cli_errors():
-      args = parse_options()
-      CONF.loadconfig(OPTS.config)
-      CGI.SSLP = OPTS.sslp
-      ctx = OPTS.context
-      if OPTS.user:
-        CU.set_user(OPTS.user)
-        CGI.STATE.kw['user'] = OPTS.user
-        if ctx is None: ctx = 'userv'
-      else:
-        D.opendb()
-        if ctx is None:
-          ctx = 'admin'
-          OPTPARSE.show_global_opts = True
       with OUT.redirect_to(O.FileOutput()):
+        args = parse_options()
+        CONF.loadconfig(OPTS.config)
+        CGI.SSLP = OPTS.sslp
+        ctx = OPTS.context
+        if OPTS.user:
+          CU.set_user(OPTS.user)
+          CGI.STATE.kw['user'] = OPTS.user
+          if ctx is None: ctx = 'userv'
+        else:
+          D.opendb()
+          if ctx is None:
+            ctx = 'admin'
+            OPTPARSE.show_global_opts = True
         OPTPARSE.dispatch(ctx, args)
 
 ###----- That's all, folks --------------------------------------------------
index 2c13232..bb7b32b 100644 (file)
--- a/config.py
+++ b/config.py
@@ -26,6 +26,7 @@
 from __future__ import with_statement
 
 import os as OS; ENV = OS.environ
+import output as O; OUT = O.OUT
 import sys as SYS
 import types as TY
 
@@ -81,8 +82,12 @@ def loadconfig(config):
   d.update(DEFAULTS)
 
   ## And run the configuration code.
-  with open(config) as f:
-    exec f in d
+  try:
+    with open(config) as f:
+      exec f in d
+  except IOError, e:
+    OUT.warn("couldn't open configuration file `%s': %s" %
+             (config, e.strerror))
 
   ## Run the hooks.
   for func in _HOOKS: