diff options
Diffstat (limited to 'lib/bb/cache.py')
-rw-r--r-- | lib/bb/cache.py | 212 |
1 files changed, 212 insertions, 0 deletions
diff --git a/lib/bb/cache.py b/lib/bb/cache.py new file mode 100644 index 000000000..e5c5c8ef7 --- /dev/null +++ b/lib/bb/cache.py @@ -0,0 +1,212 @@ +#!/usr/bin/env python +# ex:ts=4:sw=4:sts=4:et +# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- +""" +BitBake 'Event' implementation + +Caching of bitbake variables before task execution + +# Copyright (C) 2006 Richard Purdie + +# but small sections based on code from bin/bitbake: +# Copyright (C) 2003, 2004 Chris Larson +# Copyright (C) 2003, 2004 Phil Blundell +# Copyright (C) 2003 - 2005 Michael 'Mickey' Lauer +# Copyright (C) 2005 Holger Hans Peter Freyther +# Copyright (C) 2005 ROAD GmbH + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 2 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with +this program; if not, write to the Free Software Foundation, Inc., 59 Temple +Place, Suite 330, Boston, MA 02111-1307 USA. + +""" + +import os, re +import bb.data +import bb.utils + +try: + import cPickle as pickle +except ImportError: + import pickle + print "NOTE: Importing cPickle failed. Falling back to a very slow implementation." + +class Cache: + + 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, '']: + if cooker.cb is not None: + print "NOTE: Not using a cache. Set CACHE = <directory> to enable." + else: + if cooker.cb is not None: + print "NOTE: Using cache in '%s'" % self.cachedir + try: + os.stat( self.cachedir ) + 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.depends_cache: + for fn in self.depends_cache.keys(): + self.clean[fn] = "" + self.cacheValidUpdate(fn) + + def getVar(self, var, fn, exp = 0): + if fn in self.clean: + return self.depends_cache[fn][var] + + if not fn in self.depends_cache: + self.depends_cache[fn] = {} + + if fn != self.data_fn: + bb.fatal("Parsing error data_fn %s and fn %s don't match" % (self.data_fn, fn)) + + result = bb.data.getVar(var, self.data, exp) + self.depends_cache[fn][var] = result + return result + + def setData(self, fn, data): + self.data_fn = fn + self.data = data + + # Make sure __depends makes the depends_cache + self.getVar("__depends", fn, True) + self.depends_cache[fn]["CACHETIMESTAMP"] = bb.parse.cached_mtime(fn) + + def loadDataFull(self, fn, cooker): + + bb_data, skipped = self.load_bbfile(fn, cooker) + return bb_data, False + + def loadData(self, fn, cooker): + if self.cacheValid(fn): + if "SKIPPED" in self.depends_cache[fn]: + return True, True + return True, False + + bb_data, skipped = self.load_bbfile(fn, cooker) + self.setData(fn, bb_data) + return False, skipped + + def cacheValid(self, fn): + # Is cache enabled? + if self.cachedir in [None, '']: + return False + if fn in self.clean: + return True + return False + + def cacheValidUpdate(self, fn): + # Is cache enabled? + if self.cachedir in [None, '']: + return False + + # File isn't in depends_cache + if not fn in self.depends_cache: + bb.note("Cache: %s is not cached" % fn) + if fn in self.clean: + del self.clean[fn] + return False + + # Check the file's timestamp + if bb.parse.cached_mtime(fn) > self.getVar("CACHETIMESTAMP", fn, True): + bb.note("Cache: %s changed" % fn) + if fn in self.clean: + del self.clean[fn] + return False + + # Check dependencies are still valid + depends = self.getVar("__depends", fn, True) + if depends: + deps = depends.split(" ") + for dep in deps: + (f,old_mtime_s) = dep.split("@") + old_mtime = int(old_mtime_s) + new_mtime = bb.parse.cached_mtime(f) + if (new_mtime > old_mtime): + bb.note("Cache: %s's dependency %s changed" % (fn, f)) + if fn in self.clean: + del self.clean[fn] + return False + + #bb.note("Depends Cache: %s is clean" % fn) + if not fn in self.clean: + self.clean[fn] = "" + + return True + + def skip(self, fn): + if not fn in self.depends_cache: + self.depends_cache[fn] = {} + self.depends_cache[fn]["SKIPPED"] = "1" + + def remove(self, fn): + bb.note("Removing %s from cache" % fn) + if fn in self.depends_cache: + del self.depends_cache[fn] + + def sync(self): + p = pickle.Pickler(file(self.cachefile, "wb" ), -1 ) + p.dump(self.depends_cache) + + def mtime(self, cachefile): + try: + return os.stat(cachefile)[8] + except OSError: + return 0 + + def load_bbfile( self, bbfile , cooker): + """Load and parse one .bb build file""" + + import bb + from bb import utils, data, parse, debug, event, fatal + + topdir = data.getVar('TOPDIR', cooker.configuration.data) + if not topdir: + topdir = os.path.abspath(os.getcwd()) + # set topdir to here + data.setVar('TOPDIR', topdir, cooker.configuration) + bbfile = os.path.abspath(bbfile) + bbfile_loc = os.path.abspath(os.path.dirname(bbfile)) + # expand tmpdir to include this topdir + data.setVar('TMPDIR', data.getVar('TMPDIR', cooker.configuration.data, 1) or "", cooker.configuration.data) + # set topdir to location of .bb file + topdir = bbfile_loc + #data.setVar('TOPDIR', topdir, cfg) + # go there + oldpath = os.path.abspath(os.getcwd()) + os.chdir(topdir) + bb_data = data.init_db(self.cachedir,bbfile, True, cooker.configuration.data) + try: + parse.handle(bbfile, bb_data) # read .bb data + os.chdir(oldpath) + return bb_data, False + except bb.parse.SkipPackage: + os.chdir(oldpath) + return bb_data, True + except: + os.chdir(oldpath) + raise + +def init(cooker): + return Cache(cooker) + |