diff -Naur smart-0.51/smart/backends/rpm/pm.py smart-0.51.tpg/smart/backends/rpm/pm.py --- smart-0.51/smart/backends/rpm/pm.py 2006-08-31 17:59:46.000000000 +0000 +++ smart-0.51.tpg/smart/backends/rpm/pm.py 2007-08-28 09:08:05.000000000 +0000 @@ -38,7 +38,7 @@ try: ENCODING = locale.getpreferredencoding() except locale.Error: - ENCODING = "C" + ENCODING = "ascii" class RPMPackageManager(PackageManager): diff -Naur smart-0.51/smart/cache.py smart-0.51.tpg/smart/cache.py --- smart-0.51/smart/cache.py 2005-06-08 17:41:27.000000000 +0000 +++ smart-0.51.tpg/smart/cache.py 2007-08-28 09:08:05.000000000 +0000 @@ -641,11 +641,13 @@ if not loader._packages: loader.load() self.loadFileProvides() + hooks.call("cache-loaded-pre-link", self) self._objmap.clear() self.linkDeps() prog.setDone() prog.show() prog.stop() + hooks.call("cache-loaded", self) def unload(self): self.reset() diff -Naur smart-0.51/smart/ccache.c smart-0.51.tpg/smart/ccache.c --- smart-0.51/smart/ccache.c 2007-01-22 12:23:56.000000000 +0000 +++ smart-0.51.tpg/smart/ccache.c 2007-08-28 09:08:05.000000000 +0000 @@ -128,21 +128,19 @@ PyObject *_objmap; } CacheObject; -/* static PyObject * -getSysConf(void) +getHooks(void) { - static PyObject *sysconf = NULL; - if (sysconf == NULL) { + static PyObject *hooks = NULL; + if (hooks == NULL) { PyObject *module = PyImport_ImportModule("smart"); if (module) { - sysconf = PyObject_GetAttrString(module, "sysconf"); + hooks = PyObject_GetAttrString(module, "hooks"); Py_DECREF(module); } } - return sysconf; + return hooks; } -*/ static PyObject * getPkgConf(void) @@ -2766,6 +2764,7 @@ { int i, len; int total = 1; + PyObject *hooks; PyObject *prog; PyObject *ret; @@ -2802,12 +2801,15 @@ CALLMETHOD(loader, "load", NULL); } CALLMETHOD(self, "loadFileProvides", NULL); + hooks = getHooks(); + CALLMETHOD(hooks, "call", "sO", "cache-loaded-pre-link", self); PyDict_Clear(self->_objmap); CALLMETHOD(self, "linkDeps", NULL); CALLMETHOD(prog, "setDone", NULL); CALLMETHOD(prog, "show", NULL); CALLMETHOD(prog, "stop", NULL); Py_DECREF(prog); + CALLMETHOD(hooks, "call", "sO", "cache-loaded", self); Py_RETURN_NONE; } diff -Naur smart-0.51/tests/load-hooks.txt smart-0.51.tpg/tests/load-hooks.txt --- smart-0.51/tests/load-hooks.txt 1970-01-01 00:00:00.000000000 +0000 +++ smart-0.51.tpg/tests/load-hooks.txt 2007-08-28 09:08:05.000000000 +0000 @@ -0,0 +1,85 @@ + +In that test we'll add a couple of hooks that should be called by the +cache when loading packages. + +We want everything from the cache. + + >>> from smart.cache import * + + >>> class TestPackage(Package): pass + >>> class TestProvides(Provides): pass + >>> class TestDepends(Depends): + ... def matches(self, prv): + ... return prv.name == self.name and prv.version == self.version + >>> class TestUpgrades(Requires, TestDepends): pass + + >>> class TestLoader(Loader): + ... def load(self): + ... pkg1 = self.buildPackage( + ... (TestPackage, "name1", "version1"), + ... [(TestProvides, "name1", "version1")], [], [], []) + ... pkg1.loaders[self] = 1 + ... pkg2 = self.buildPackage( + ... (TestPackage, "name2", "version2"), + ... [], [], [], []) + ... pkg2.loaders[self] = 2 + + +Then, we create an instance of it. + + >>> loader = TestLoader() + + +We'll also create a cache, to include the loader into. + + >>> cache = Cache() + >>> cache.addLoader(loader) + + +Now we create our hooks, and plug them into the specific places. + +The first hook will add an artificial upgrades relation between +package name2 and name1. + + >>> verify_data = [] + + >>> def add_upgrade(cache): + ... # First, check the current state. + ... pkg1 = cache.getPackages("name1")[0] + ... verify_data.append(pkg1.provides[0].upgradedby) + ... + ... # Then, stick an artificial upgrades relation. + ... upg = TestUpgrades("name1", "=", "version1") + ... cache._upgrades.append(upg) + ... pkg2 = cache.getPackages("name2")[0] + ... pkg2.upgrades += (upg,) + + +Our second hook will just verify that the relation has been linked. + + >>> def check_upgrade(cache): + ... pkg1 = cache.getPackages("name1")[0] + ... verify_data.append(pkg1.provides[0].upgradedby) + + +Let's link them effectively. + + >>> hooks.register("cache-loaded-pre-link", add_upgrade) + >>> hooks.register("cache-loaded", check_upgrade) + + +Loading the cache should run these hooks. + + >>> cache.load() + Updating cache... ######################################## [100%] + <BLANKLINE> + + +Let's see if our hooks got called correctly. + + >>> import pprint + >>> pprint.pprint(verify_data) + [(), [name1 = version1]] + + +vim:ft=doctest