[csw-devel] SF.net SVN: gar:[12976] csw/mgar/gar/v2/lib/python/configuration.py

wahwah at users.sourceforge.net wahwah at users.sourceforge.net
Mon Jan 17 10:28:44 CET 2011


Revision: 12976
          http://gar.svn.sourceforge.net/gar/?rev=12976&view=rev
Author:   wahwah
Date:     2011-01-17 09:28:43 +0000 (Mon, 17 Jan 2011)

Log Message:
-----------
pkgdb: Cope with missing HOME

In some environments, such as CGI scripts, the HOME variable is unset.  pkgdb
needs to cope with it, by not trying to look for configuration there.

Modified Paths:
--------------
    csw/mgar/gar/v2/lib/python/configuration.py

Modified: csw/mgar/gar/v2/lib/python/configuration.py
===================================================================
--- csw/mgar/gar/v2/lib/python/configuration.py	2011-01-17 09:28:24 UTC (rev 12975)
+++ csw/mgar/gar/v2/lib/python/configuration.py	2011-01-17 09:28:43 UTC (rev 12976)
@@ -59,36 +59,47 @@
     else: raise
 
 
+def HomeExists():
+  if "HOME" not in os.environ:
+    return False
+  return True
+
+
 def GetConfig():
   config = ConfigParser.SafeConfigParser()
   file_was_found = False
   for file_name_tmpl, default_file in CONFIGURATION_FILE_LOCATIONS:
-    filename = file_name_tmpl % os.environ
-    if os.path.exists(filename):
-      if not default_file:
-        file_was_found = True
-      config.read(file_name_tmpl % os.environ)
+    filename = None
+    try:
+      filename = file_name_tmpl % os.environ
+      if os.path.exists(filename):
+        if not default_file:
+          file_was_found = True
+        config.read(file_name_tmpl % os.environ)
+    except KeyError, e:
+      logging.warn(e)
   if not file_was_found:
-    db_file = "%(HOME)s/.checkpkg/checkpkg.db" % os.environ
-    checkpkg_dir = CHECKPKG_DIR % os.environ
-    MkdirP(checkpkg_dir)
-    config_file = AUTO_CONFIG_FILE_TMPL % os.environ
-    logging.warning(
-        "No configuration file found.  Will attempt to create "
-        "an sane default configuration in %s."
-        % repr(config_file))
-    if not config.has_section("database"):
-      config.add_section("database")
-    config.set("database", "type", "sqlite")
-    config.set("database", "name", db_file)
-    config.set("database", "host", "")
-    config.set("database", "user", "")
-    config.set("database", "password", "")
-    config.set("database", "auto_manage", "yes")
-    fd = open(config_file, "w")
-    config.write(fd)
-    fd.close()
-    logging.debug("Configuration has been written.")
+    if HomeExists():
+      db_file = os.path.join(CHECKPKG_DIR % os.environ, "checkpkg.db")
+      checkpkg_dir = CHECKPKG_DIR % os.environ
+      MkdirP(checkpkg_dir)
+      config_file = AUTO_CONFIG_FILE_TMPL % os.environ
+      logging.warning(
+          "No configuration file found.  Will attempt to create "
+          "an sane default configuration in %s."
+          % repr(config_file))
+      if not config.has_section("database"):
+        config.add_section("database")
+      config.set("database", "type", "sqlite")
+      config.set("database", "name", db_file)
+      config.set("database", "host", "")
+      config.set("database", "user", "")
+      config.set("database", "password", "")
+      config.set("database", "auto_manage", "yes")
+      fd = open(config_file, "w")
+      config.write(fd)
+      fd.close()
+      logging.debug("Configuration has been written.")
   if not config.has_section("database"):
     logging.fatal("Section 'database' not found in the config file. "
         "Please refer to the documentation: "


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.


More information about the devel mailing list