From a61b28b7776016e9f1535c9878ac038306701d05 Mon Sep 17 00:00:00 2001
From: Martijn Vermaat <martijn@vermaat.name>
Date: Mon, 25 Jul 2011 15:25:42 +0000
Subject: [PATCH] Some fixes for cache sync.

git-svn-id: https://humgenprojects.lumc.nl/svn/mutalyzer/branches/gbinfo-sync-branch@315 eb6bd6ab-9ccd-42b9-aceb-e2899b4a52f1
---
 mutalyzer/Db.py         |  4 ++--
 mutalyzer/config.py     |  3 +++
 mutalyzer/models.py     |  1 +
 mutalyzer/sync.py       | 32 +++++++++++++++++++++++++-------
 mutalyzer/webservice.py |  3 ++-
 5 files changed, 33 insertions(+), 10 deletions(-)

diff --git a/mutalyzer/Db.py b/mutalyzer/Db.py
index ea559584..bb57f851 100644
--- a/mutalyzer/Db.py
+++ b/mutalyzer/Db.py
@@ -1050,8 +1050,8 @@ class Cache(Db) :
         @rtype: string
         """
         statement = """
-            SELECT
-            (AccNo, GI, hash, ChrAccVer, ChrStart, ChrStop, orientation, url, created)
+            SELECT AccNo, GI, hash, ChrAccVer, ChrStart,
+                   ChrStop, orientation, url, created
             FROM GBInfo
             WHERE created >= %s;
         """, created_since
diff --git a/mutalyzer/config.py b/mutalyzer/config.py
index 363d0a88..0314500a 100644
--- a/mutalyzer/config.py
+++ b/mutalyzer/config.py
@@ -134,6 +134,9 @@ class Config():
             self.GenRecord.spliceAlarm = int(config["spliceAlarm"])
             self.GenRecord.spliceWarn = int(config["spliceWarn"])
 
+            # Set the variables needed by the sync module.
+            self.Sync.cache = config["cache"]
+
             # If we are in a testing environment, use a temporary file for
             # logging and a temporary directory for the cache.
             # We don't remove these after the tests, since they might be
diff --git a/mutalyzer/models.py b/mutalyzer/models.py
index d8b00138..c9e32322 100644
--- a/mutalyzer/models.py
+++ b/mutalyzer/models.py
@@ -233,4 +233,5 @@ class CacheEntry(ClassModel):
     chromosomeOrientation = Integer
     url = String
     created = Mandatory.DateTime
+    cached = Mandatory.Boolean
 #CacheEntry
diff --git a/mutalyzer/sync.py b/mutalyzer/sync.py
index 1f4a8231..d5c7a6b7 100644
--- a/mutalyzer/sync.py
+++ b/mutalyzer/sync.py
@@ -4,8 +4,10 @@ Module for synchronizing the database with other Mutalyzer instances.
 
 
 from mutalyzer.util import monkey_patch_suds; monkey_patch_suds()
-from suds.client import Client
+
+import os
 from datetime import datetime, timedelta
+from suds.client import Client
 
 
 DEFAULT_CREATED_SINCE_DAYS = 7
@@ -29,7 +31,19 @@ class CacheSync(object):
         if not created_since:
             created_since = datetime.today() - \
                             timedelta(days=DEFAULT_CREATED_SINCE_DAYS)
-        return self._database.getGB(created_since)
+        cache = self._database.getGB(created_since)
+
+        entries = []
+
+        # For each entry, check if it is cached on our filesystem.
+        # Todo: refactor
+        for entry in cache:
+            file_path = os.path.join(self._config.cache, '%s.bz2' % entry[0])
+            e = list(entry)
+            e.append(os.path.isfile(file_path))
+            entries.append(e)
+
+        return entries
 
     def remote_cache(self, remote_wsdl, created_since=None):
         """
@@ -47,7 +61,8 @@ class CacheSync(object):
             """
             entry_dict =  {'name':    entry.name,
                            'hash':    entry.hash,
-                           'created': entry.created}
+                           'created': entry.created,
+                           'cached':  bool(entry.cached)}
             for attribute in ('gi', 'chromosomeName', 'chromosomeStart'
                               'chromosomeStop', 'chromosomeOrientation',
                               'url'):
@@ -57,7 +72,7 @@ class CacheSync(object):
 
         return map(cache_entry_from_soap, cache.CacheEntry)
 
-    def sync_with_remote(self, remote_wsdl, created_since=None):
+    def sync_with_remote(self, remote_wsdl, remote_cache, created_since=None):
         """
         Todo.
         """
@@ -74,7 +89,10 @@ class CacheSync(object):
             #                        entry['chromosomeStop'],
             #                        entry['chromosomeOrientation'],
             #                        entry['url'])
-            print 'inserting %s' % entry['name']
-            print entry
+            #print 'inserting %s' % entry['name']
+            #print entry
             if not entry['chromosomeName'] and not entry['url']:
-                print '(downloading file from remote cache...)'
+                if entry['cached']:
+                    print 'downloading file from remote cache: %s' % (remote_cache % entry['name'])
+                #else:
+                    #print 'cannot download this file from remote cache'
diff --git a/mutalyzer/webservice.py b/mutalyzer/webservice.py
index f6401e1d..cd480fda 100644
--- a/mutalyzer/webservice.py
+++ b/mutalyzer/webservice.py
@@ -907,7 +907,8 @@ class MutalyzerService(DefinitionBase):
              e.chromosomeStop,
              e.chromosomeOrientation,
              e.url,
-             e.created) = entry
+             e.created,
+             e.cached) = entry
             return e
 
         output.addMessage(__file__, -1, 'INFO',
-- 
GitLab