bitbake-1.4/lib/bb/cache.py:
[vuplus_bitbake] / lib / bb / cache.py
index eb4ae85..d4be9d7 100644 (file)
@@ -40,23 +40,29 @@ except ImportError:
     import pickle
     print "NOTE: Importing cPickle failed. Falling back to a very slow implementation."
 
+__cache_version__ = "123"
+
 class Cache:
     """
     BitBake Cache implementation
     """
     def __init__(self, cooker):
 
+
         self.cachedir = bb.data.getVar("CACHE", cooker.configuration.data, True)
-        self.cachefile = os.path.join(self.cachedir,"bb_cache.dat")
         self.clean = {}
         self.depends_cache = {}
         self.data = None
         self.data_fn = None
 
         if self.cachedir in [None, '']:
+            self.has_cache = False
             if cooker.cb is not None:
                 print "NOTE: Not using a cache. Set CACHE = <directory> to enable."
         else:
+            self.has_cache = True
+            self.cachefile = os.path.join(self.cachedir,"bb_cache.dat")
+            
             if cooker.cb is not None:
                 print "NOTE: Using cache in '%s'" % self.cachedir
             try:
@@ -64,9 +70,18 @@ class Cache:
             except OSError:
                 bb.mkdirhier( self.cachedir )
 
-        if (self.mtime(self.cachefile)):
-            p = pickle.Unpickler( file(self.cachefile,"rb"))
-            self.depends_cache = p.load()
+        if self.has_cache and (self.mtime(self.cachefile)):
+            try:
+                p = pickle.Unpickler( file(self.cachefile,"rb"))
+                self.depends_cache, version_data = p.load()
+                if version_data['CACHE_VER'] != __cache_version__:
+                    raise ValueError, 'Cache Version Mismatch'
+                if version_data['BITBAKE_VER'] != bb.__version__:
+                    raise ValueError, 'Bitbake Version Mismatch'
+            except (ValueError, KeyError):
+                bb.note("Invalid cache found, rebuilding...")
+                self.depends_cache = {}
+
         if self.depends_cache:
             for fn in self.depends_cache.keys():
                 self.clean[fn] = ""
@@ -142,7 +157,7 @@ class Cache:
         Fast version, no timestamps checked.
         """
         # Is cache enabled?
-        if self.cachedir in [None, '']:
+        if not self.has_cache:
             return False
         if fn in self.clean:
             return True
@@ -154,30 +169,25 @@ class Cache:
         Make thorough (slower) checks including timestamps.
         """
         # Is cache enabled?
-        if self.cachedir in [None, '']:
+        if not self.has_cache:
             return False
 
         # Check file still exists
         if self.mtime(fn) == 0:
             bb.debug(2, "Cache: %s not longer exists" % fn)
-            if fn in self.clean:
-                del self.clean[fn]
-            if fn in self.depends_cache:
-                del self.depends_cache[fn]
+            self.remove(fn)
             return False
 
         # File isn't in depends_cache
         if not fn in self.depends_cache:
             bb.debug(2, "Cache: %s is not cached" % fn)
-            if fn in self.clean:
-                del self.clean[fn]
+            self.remove(fn)
             return False
 
         # Check the file's timestamp
         if bb.parse.cached_mtime(fn) > self.getVar("CACHETIMESTAMP", fn, True):
             bb.debug(2, "Cache: %s changed" % fn)
-            if fn in self.clean:
-                del self.clean[fn]
+            self.remove(fn)
             return False
 
         # Check dependencies are still valid
@@ -190,8 +200,7 @@ class Cache:
                 new_mtime = bb.parse.cached_mtime(f)
                 if (new_mtime > old_mtime):
                     bb.debug(2, "Cache: %s's dependency %s changed" % (fn, f))
-                    if fn in self.clean:
-                        del self.clean[fn]
+                    self.remove(fn)
                     return False
 
         bb.debug(2, "Depends Cache: %s is clean" % fn)
@@ -214,17 +223,27 @@ class Cache:
         Remove a fn from the cache
         Called from the parser in error cases
         """
-        bb.note("Removing %s from cache" % fn)
+        bb.debug(1, "Removing %s from cache" % fn)
         if fn in self.depends_cache:
             del self.depends_cache[fn]
+        if fn in self.clean:
+            del self.clean[fn]
 
     def sync(self):
         """
         Save the cache
         Called from the parser when complete (or exitting)
         """
+
+        if not self.has_cache:
+            return
+
+        version_data = {}
+        version_data['CACHE_VER'] = __cache_version__
+        version_data['BITBAKE_VER'] = bb.__version__
+
         p = pickle.Pickler(file(self.cachefile, "wb" ), -1 )
-        p.dump(self.depends_cache)
+        p.dump([self.depends_cache, version_data])
 
     def mtime(self, cachefile):
         try: