Mercurial > treepkg
changeset 113:312949e7628d
Add a smarter way to load the packager modules: Add the function
treepkg.packager.import_packager_module and use it to load the packager
modules. Also add corresponding tests.
author | Bernhard Herzog <bh@intevation.de> |
---|---|
date | Tue, 01 Apr 2008 12:30:39 +0000 |
parents | cea98d4e4a6a |
children | 02c261e4443f |
files | test/test_packager.py treepkg/packager.py |
diffstat | 2 files changed, 53 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/test/test_packager.py Thu Mar 20 20:21:34 2008 +0000 +++ b/test/test_packager.py Tue Apr 01 12:30:39 2008 +0000 @@ -1,4 +1,4 @@ -# Copyright (C) 2007 by Intevation GmbH +# Copyright (C) 2007, 2008 by Intevation GmbH # Authors: # Bernhard Herzog <bh@intevation.de> # @@ -14,7 +14,7 @@ from treepkg.run import call from treepkg.cmdexpand import cmdexpand from treepkg.util import writefile -from treepkg.packager import PackagerGroup +from treepkg.packager import PackagerGroup, import_packager_module import treepkg.subversion as subversion import treepkg @@ -344,3 +344,32 @@ [os.path.join(srcdir, filename) for filename in ["test_1.0-1_i386.changes", "test_1.0-1_i386.deb"]]) + + +class TestImportPackagerModule(unittest.TestCase, FileTestMixin): + + files = [("treepkg_importtest", + [("__init__.py", ""), + ("withtrack.py", "\n".join(["class PackageTrack:", + " pass", + ""])), + ("notrack.py", "\n".join(["class SourcePackager:", + " pass", + ""]))])] + + def setUp(self): + self.directory = self.create_temp_dir(self.id()) + self.create_files(self.directory, self.files) + self.old_path = sys.path + sys.path = [self.directory] + sys.path + + def tearDown(self): + sys.path = self.old_path + + def test_import_with_track(self): + module = import_packager_module("treepkg_importtest.withtrack") + self.failUnless(hasattr(module, "PackageTrack")) + + def test_import_without_track(self): + module = import_packager_module("treepkg_importtest.notrack") + self.failUnless(hasattr(module, "PackageTrack"))
--- a/treepkg/packager.py Thu Mar 20 20:21:34 2008 +0000 +++ b/treepkg/packager.py Tue Apr 01 12:30:39 2008 +0000 @@ -13,6 +13,7 @@ import logging import shutil import datetime +import new import util import subversion @@ -395,8 +396,28 @@ for revision in self.get_revision_numbers()] +def import_packager_module(packager_class): + """Import the packager module named by packager_class. + + The packager_class must be the full absolute module name for the + packager. The module must either contain a class called + 'PackageTrack' or a class called called 'SourcePackager'. If it + doesn't have a 'PackageTrack' class, a 'PackageTrack' class and a + 'RevisionPackager' class will be created the latter of which will + use the module's 'SourcePackager' class as source_packager_cls. + """ + module = util.import_dotted_name(packager_class) + if not hasattr(module, "PackageTrack"): + module.RevisionPackager \ + = new.classobj("RevisionPackager", (RevisionPackager,), + dict(source_packager_cls=module.SourcePackager)) + module.PackageTrack \ + = new.classobj("PackageTrack", (PackageTrack,), + dict(revision_packager_cls=module.RevisionPackager)) + return module + def create_package_track(packager_class, **kw): - module = util.import_dotted_name(packager_class) + module = import_packager_module(packager_class) return module.PackageTrack(**kw)