bh: thuban/test postgissupport.py,1.10,1.11

cvs@intevation.de cvs at intevation.de
Wed Dec 15 15:01:06 CET 2004


Author: bh

Update of /thubanrepository/thuban/test
In directory doto:/tmp/cvs-serv25951/test

Modified Files:
	postgissupport.py 
Log Message:
(PostgreSQLServer.execute_sql): Extend to
so that it returns a result for select statements.
(PostgreSQLServer.server_version): New.  Return the version of the
server software.
(PostgreSQLServer.require_authentication): The format of
pg_hba.conf has changed between PostgrSQL 7.2 and 7.3.  Check the
server version and generate the file in the correct format


Index: postgissupport.py
===================================================================
RCS file: /thubanrepository/thuban/test/postgissupport.py,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- postgissupport.py	15 Dec 2004 11:12:11 -0000	1.10
+++ postgissupport.py	15 Dec 2004 14:01:04 -0000	1.11
@@ -17,6 +17,7 @@
 import popen2
 import shutil
 import traceback
+import re
 
 import support
 
@@ -303,17 +304,39 @@
         return " ".join(params)
 
     def execute_sql(self, dbname, user, sql):
-        """Execute the sql statament
+        """Execute the sql statament and return a result for SELECT statements
 
         The user parameter us used as in connection_params. The dbname
-        parameter must be the name of a database in the cluster.
+        parameter must be the name of a database in the cluster.  The
+        sql parameter is the SQL statement to execute as a string.  If
+        the string starts with 'select' (matched case insensitively) the
+        first row of the result will be returned.  Otherwise the return
+        value is None.
         """
         conn = psycopg.connect("dbname=%s " % dbname
                                + self.connection_string(user))
         cursor = conn.cursor()
         cursor.execute(sql)
+        if sql.lower().startswith("select"):
+            row = cursor.fetchone()
+        else:
+            row = None
         conn.commit()
         conn.close()
+        return row
+
+    def server_version(self):
+        """Return the server version as a tuple (major, minor, patch)
+
+        Each item in the tuple is an int.
+        """
+        result = self.execute_sql("template1", "admin", "SELECT version();")[0]
+        match = re.match(r"PostgreSQL (\d+\.\d+\.\d+)", result)
+        if match:
+            return tuple(map(int, match.group(1).split(".")))
+        else:
+            raise RutimeError("Cannot determine PostgreSQL server version"
+                              " from %r" % result)
 
     def require_authentication(self, required):
         """Switch authentication requirements on or off
@@ -325,10 +348,17 @@
         corresponding call to switch it off again in the test case'
         tearDown method or in a finally: block.
         """
+        # Starting with PostgreSQL 7.3 the pg_hba.conf file has an
+        # additional column with a username.  Query the server version
+        # and generate a file in the correct format.
+        if self.server_version() >= (7, 3):
+            user = "all"
+        else:
+            user = ""
         if required:
-            contents = "local all password\n"
+            contents = "local all %s password\n" % user
         else:
-            contents = "local all trust\n"
+            contents = "local all %s trust\n" % user
         f = open(os.path.join(self.dbdir, "pg_hba.conf"), "w")
         f.write(contents)
         f.close()





More information about the Thuban-devel mailing list

This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)