From f26ce8c21fa345f6a922aa33c4adb871163ea5aa Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Thu, 20 Jun 2013 11:12:33 +0000 Subject: Remove SystemKindBuilderFactory We now only support one system-kind, so there's no need for the factory stuff, and at this point it only serves to obfuscate and complexify. --- morphlib/app.py | 2 - morphlib/builder2.py | 97 +++++++++++++----------- morphlib/plugins/tarball-systembuilder_plugin.py | 86 --------------------- 3 files changed, 52 insertions(+), 133 deletions(-) delete mode 100644 morphlib/plugins/tarball-systembuilder_plugin.py (limited to 'morphlib') diff --git a/morphlib/app.py b/morphlib/app.py index fcf54118..f82eae33 100644 --- a/morphlib/app.py +++ b/morphlib/app.py @@ -245,8 +245,6 @@ class Morph(cliapp.Application): self.hookmgr = cliapp.HookManager() self.hookmgr.new('new-build-command', cliapp.FilterHook()) - self.system_kind_builder_factory = \ - morphlib.builder2.SystemKindBuilderFactory() def itertriplets(self, args): '''Generate repo, ref, filename triples from args.''' diff --git a/morphlib/builder2.py b/morphlib/builder2.py index 7f400049..abde72ce 100644 --- a/morphlib/builder2.py +++ b/morphlib/builder2.py @@ -19,6 +19,7 @@ import errno import json import logging import os +from os.path import relpath import shutil import stat import time @@ -518,14 +519,60 @@ class StratumBuilder(BuilderBase): return [artifact] -class SystemKindBuilder(BuilderBase): # pragma: no cover +class SystemBuilder(BuilderBase): # pragma: no cover - '''Build a specific kind of a system. + '''Build system image artifacts.''' - Subclasses should set the ``system_kind`` attribute to the kind of - system they build. + def __init__(self, *args, **kwargs): + BuilderBase.__init__(self, *args, **kwargs) + self.args = args + self.kwargs = kwargs - ''' + def build_and_cache(self): + system_kind = self.artifact.source.morphology['system-kind'] + if system_kind != 'rootfs-tarball': + raise morphlib.Error( + 'System kind %s not support (only rootfs-tarball is)') + self.app.status(msg='Building system %(system_name)s', + system_name=self.artifact.source.morphology['name']) + + with self.build_watch('overall-build'): + arch = self.artifact.source.morphology['arch'] + + rootfs_name = self.artifact.source.morphology['name'] + '-rootfs' + rootfs_artifact = self.new_artifact(rootfs_name) + handle = self.local_artifact_cache.put(rootfs_artifact) + + try: + fs_root = self.staging_area.destdir(self.artifact.source) + self.unpack_strata(fs_root) + self.write_metadata(fs_root, rootfs_name) + self.create_fstab(fs_root) + self.copy_kernel_into_artifact_cache(fs_root) + unslashy_root = fs_root[1:] + def uproot_info(info): + info.name = relpath(info.name, unslashy_root) + if info.islnk(): + info.linkname = relpath(info.linkname, + unslashy_root) + return info + artiname = self.artifact.source.morphology['name'] + tar = tarfile.open(fileobj=handle, mode="w", name=artiname) + self.app.status(msg='Constructing tarball of root filesystem', + chatty=True) + tar.add(fs_root, recursive=True, filter=uproot_info) + tar.close() + except BaseException, e: + logging.error(traceback.format_exc()) + self.app.status(msg='Error while building system', + error=True) + handle.abort() + raise + + handle.close() + + self.save_build_times() + return [self.artifact] def unpack_one_stratum(self, stratum_artifact, target): '''Unpack a single stratum into a target directory''' @@ -640,46 +687,6 @@ class SystemKindBuilder(BuilderBase): # pragma: no cover break -class SystemKindBuilderFactory(object): # pragma: no cover - - '''A factory class for SystemKindBuilder objects.''' - - def __init__(self): - self.system_kinds = [] - - def register(self, klass): - self.system_kinds.append(klass) - - def new(self, system_kind, args, kwargs): - for klass in self.system_kinds: - if klass.system_kind == system_kind: - return klass(*args, **kwargs) - raise morphlib.Error("Don't know how to build system kind %s" % - system_kind) - - -class SystemBuilder(BuilderBase): # pragma: no cover - - '''Build system image artifacts.''' - - def __init__(self, *args, **kwargs): - BuilderBase.__init__(self, *args, **kwargs) - self.args = args - self.kwargs = kwargs - - def build_and_cache(self): - system_kind = self.artifact.source.morphology['system-kind'] - if system_kind != 'rootfs-tarball': - raise morphlib.Error( - 'System kind %s not support (only rootfs-tarball is)') - builder = self.app.system_kind_builder_factory.new( - system_kind, self.args, self.kwargs) - logging.debug('Building system with %s' % repr(builder)) - self.app.status(msg='Building system %(system_name)s', - system_name=self.artifact.source.morphology['name']) - return builder.build_and_cache() - - class Builder(object): # pragma: no cover '''Helper class to build with the right BuilderBase subclass.''' diff --git a/morphlib/plugins/tarball-systembuilder_plugin.py b/morphlib/plugins/tarball-systembuilder_plugin.py deleted file mode 100644 index 9f9cf02e..00000000 --- a/morphlib/plugins/tarball-systembuilder_plugin.py +++ /dev/null @@ -1,86 +0,0 @@ -# Copyright (C) 2012,2013 Codethink Limited -# -# 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; version 2 of the License. -# -# 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., -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - -import logging -import os -from os.path import relpath -import shutil -import time -from collections import defaultdict -import tarfile -import traceback -import subprocess - -import cliapp - -import morphlib -from morphlib.artifactcachereference import ArtifactCacheReference -from morphlib.builder2 import (SystemKindBuilder, download_depends, - get_overlaps, log_overlaps, ldconfig, - write_overlap_metadata) - - -class RootfsTarballBuilder(SystemKindBuilder): # pragma: no cover - - system_kind = 'rootfs-tarball' - - def build_and_cache(self): - with self.build_watch('overall-build'): - arch = self.artifact.source.morphology['arch'] - - rootfs_name = self.artifact.source.morphology['name'] + '-rootfs' - rootfs_artifact = self.new_artifact(rootfs_name) - handle = self.local_artifact_cache.put(rootfs_artifact) - - try: - fs_root = self.staging_area.destdir(self.artifact.source) - self.unpack_strata(fs_root) - self.write_metadata(fs_root, rootfs_name) - self.create_fstab(fs_root) - self.copy_kernel_into_artifact_cache(fs_root) - unslashy_root = fs_root[1:] - def uproot_info(info): - info.name = relpath(info.name, unslashy_root) - if info.islnk(): - info.linkname = relpath(info.linkname, - unslashy_root) - return info - artiname = self.artifact.source.morphology['name'] - tar = tarfile.open(fileobj=handle, mode="w", name=artiname) - self.app.status(msg='Constructing tarball of root filesystem', - chatty=True) - tar.add(fs_root, recursive=True, filter=uproot_info) - tar.close() - except BaseException, e: - logging.error(traceback.format_exc()) - self.app.status(msg='Error while building system', - error=True) - handle.abort() - raise - - handle.close() - - self.save_build_times() - return [self.artifact] - - -class RootfsTarballBuilderPlugin(cliapp.Plugin): - - def enable(self): - self.app.system_kind_builder_factory.register(RootfsTarballBuilder) - - def disable(self): - pass -- cgit v1.2.1