From c2b790456e76ec6e56e1e9f1885cb04ccdbdd5d8 Mon Sep 17 00:00:00 2001
From: Alexey Mints <a.mints@jacobs-university.de>
Date: Mon, 15 Oct 2012 08:23:12 +0000
Subject: [PATCH] Task #2699: GSM database Python 2.6 - Fix

---
 .gitattributes                                |  1 +
 CEP/GSM/bremen/recreate_tables.py             | 37 ++++++++++++++-----
 .../sql/tables/create.table.image_stats.sql   |  8 ++++
 CEP/GSM/bremen/src/sqllist.py                 |  7 +++-
 4 files changed, 41 insertions(+), 12 deletions(-)
 create mode 100644 CEP/GSM/bremen/sql/tables/create.table.image_stats.sql

diff --git a/.gitattributes b/.gitattributes
index 01b4bf73792..db3fac120b9 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -809,6 +809,7 @@ CEP/GSM/bremen/sql/tables/create.table.datasets.sql -text
 CEP/GSM/bremen/sql/tables/create.table.detections.sql -text
 CEP/GSM/bremen/sql/tables/create.table.extractedsources.sql -text
 CEP/GSM/bremen/sql/tables/create.table.frequencybands.sql -text
+CEP/GSM/bremen/sql/tables/create.table.image_stats.sql -text
 CEP/GSM/bremen/sql/tables/create.table.images.sql -text
 CEP/GSM/bremen/sql/tables/create.table.runningcatalog.sql -text
 CEP/GSM/bremen/sql/tables/create.table.runningcatalog_fluxes.sql -text
diff --git a/CEP/GSM/bremen/recreate_tables.py b/CEP/GSM/bremen/recreate_tables.py
index 30feb1014fd..efd1c905919 100755
--- a/CEP/GSM/bremen/recreate_tables.py
+++ b/CEP/GSM/bremen/recreate_tables.py
@@ -7,12 +7,24 @@ import argparse
 import copy
 import re
 from os import path
-import monetdb.sql as db
-import monetdb.monetdb_exceptions as me
-import psycopg2
-from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
+try:
+    import monetdb.sql as db
+    import monetdb.monetdb_exceptions as me
+    HAS_MONET = True
+except ImportError:
+    HAS_MONET =False
+
+try:
+    import psycopg2
+    from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
+    HAS_POSTGRESQL = True
+except ImportError:
+    HAS_POSTGRESQL = False
 import subprocess
 
+def re_sub(regexp, sub_to, sub_from, flags=0):
+    prog = re.compile(regexp, flags)
+    return prog.sub(sub_to, sub_from)
 
 class Recreator(object):
     """
@@ -96,12 +108,12 @@ class Recreator(object):
         Prepare SQL code for MonetDB/PostgreSQL.
         Remove all comments, make necessary substitutions.
         """
-        sql_lines = re.sub(r'/\*.*?\*/', '', sql_lines, flags=re.DOTALL)
-        sql_lines = re.sub(r'--.*$', '', sql_lines, flags=re.MULTILINE)
+        sql_lines = re_sub(r'/\*.*?\*/', '', sql_lines, flags=re.DOTALL)
+        sql_lines = re_sub(r'--.*$', '', sql_lines, flags=re.MULTILINE)
         if not self.monet:
             # Has to apply substitutions for PostgreSQL.
             for from_, to_ in self.PG_SUBSTITUTOR:
-                sql_lines = re.sub(from_, to_, sql_lines,
+                sql_lines = re_sub(from_, to_, sql_lines,
                                    flags=re.MULTILINE | re.IGNORECASE)
         return sql_lines
 
@@ -163,6 +175,12 @@ class Recreator(object):
         print 'Frequencies loaded'
 
     def run(self):
+        error_set = []
+        if HAS_MONET:
+            error_set.append(me.OperationalError)
+        if HAS_POSTGRESQL:
+            error_set.append(psycopg2.ProgrammingError)
+        error_set = tuple(error_set)
         try:
             for procedure in self.PROCEDURES:
                 if self.monet:
@@ -170,13 +188,12 @@ class Recreator(object):
                         self.conn.execute("drop procedure %s;" %
                                           procedure)
                         print "drop procedure %s;" % procedure
-                    except (psycopg2.ProgrammingError,
-                            me.OperationalError):
+                    except error_set:
                         pass
             for view in self.VIEWS:
                 try:
                     self.conn.execute("drop view %s;" % view)
-                except (psycopg2.ProgrammingError, me.OperationalError):
+                except error_set:
                     pass
                 print "drop view %s;" % view
 
diff --git a/CEP/GSM/bremen/sql/tables/create.table.image_stats.sql b/CEP/GSM/bremen/sql/tables/create.table.image_stats.sql
new file mode 100644
index 00000000000..310b69bc405
--- /dev/null
+++ b/CEP/GSM/bremen/sql/tables/create.table.image_stats.sql
@@ -0,0 +1,8 @@
+--table for image statistics
+CREATE TABLE image_stats(
+  image_id int not null,
+  run_id int not null,
+  kind int not null,
+  lr_method int not null,
+  value int not null
+);
diff --git a/CEP/GSM/bremen/src/sqllist.py b/CEP/GSM/bremen/src/sqllist.py
index c8ab93b9a55..37b7c6828ea 100644
--- a/CEP/GSM/bremen/src/sqllist.py
+++ b/CEP/GSM/bremen/src/sqllist.py
@@ -15,6 +15,9 @@ SQL_LIST = {}
 
 GLOBALS = {}
 
+def re_sub(regexp, sub_to, sub_from, count, flags=0):
+    prog = re.compile(regexp, flags)
+    return prog.sub(sub_to, sub_from, count=count)
 
 def _expand_value(value):
     """
@@ -25,7 +28,7 @@ def _expand_value(value):
         Expand $$..$$ by calculating value in $s.
         """
         return str(eval(matchvalues.group(0)[2:-2]))
-    return re.sub(r'\$\$(.*?)\$\$', _expand_formula, value, count=0)
+    return re_sub(r'\$\$(.*?)\$\$', _expand_formula, value, count=0)
 
 
 def _load_from_sql_list(filename):
@@ -57,7 +60,7 @@ def _substitute_globals(sql):
             return str(GLOBALS[matchvalue.group(0)[1:-1]])
         else:
             return ''
-    return re.sub(r'\[(.?)\]', _substitute_global, sql, count=0)
+    return re_sub(r'\[(.?)\]', _substitute_global, sql, count=0)
 
 
 def get_sql(name, *params):
-- 
GitLab