+++ /dev/null
-#!/usr/bin/python3
-###############################################################################
-# #
-# Pakfire - The IPFire package management system #
-# Copyright (C) 2011 Pakfire development team #
-# #
-# 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 3 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, see <http://www.gnu.org/licenses/>. #
-# #
-###############################################################################
-
-import glob
-import os
-import uuid
-
-from . import _pakfire
-from . import logger
-from . import util
-
-import logging
-log = logging.getLogger("pakfire.builder")
-
-from .constants import *
-from .i18n import _
-from .errors import *
-
-BUILD_LOG_HEADER = """
- ____ _ __ _ _ _ _ _
-| _ \ __ _| | __/ _(_)_ __ ___ | |__ _ _(_) | __| | ___ _ __
-| |_) / _` | |/ / |_| | '__/ _ \ | '_ \| | | | | |/ _` |/ _ \ '__|
-| __/ (_| | <| _| | | | __/ | |_) | |_| | | | (_| | __/ |
-|_| \__,_|_|\_\_| |_|_| \___| |_.__/ \__,_|_|_|\__,_|\___|_|
-
- Version : %(version)s
- Host : %(hostname)s (%(host_arch)s)
- Time : %(time)s
-
-"""
-
-class Builder(object):
- def __init__(self, conf, arch=None, build_id=None, logfile=None, **kwargs):
- self.conf = conf
-
- # Settings array.
- self.settings = {
- "enable_ccache" : True,
- "enable_snapshot" : True,
- }
-
- # Add settings from keyword arguments
- self.settings.update(kwargs)
-
- # Generate a build ID
- self.build_id = build_id or "%s" % uuid.uuid4()
-
- # Setup logging
- self.log = self.setup_logging(logfile)
-
- # Architecture to build for
- self.arch = arch or _pakfire.native_arch()
-
- def __enter__(self):
- self.log.debug("Entering %s" % self)
-
- # Initialise Pakfire instance
- pakfire = _pakfire.Pakfire(
- arch=self.arch,
- conf=self.conf,
-
- # Enable build mode
- build=True,
- **self.settings,
- )
-
- # Setup domain name resolution in chroot
- for i in ("/etc/resolv.conf", "/etc/hosts"):
- pakfire.copy_in(i, i)
-
- return BuilderContext(pakfire, self)
-
- def __exit__(self, type, value, traceback):
- self.log.debug("Leaving %s" % self)
-
- def setup_logging(self, logfile):
- l = log.getChild(self.build_id)
- l.setLevel(logging.DEBUG)
-
- # Print everything to the console
- console = logger.ConsoleHandler()
- l.addHandler(console)
-
- # If we have a log file, we write INFO and higher level messages to it
- # using the build formatter which adds a timestamp
- if logfile:
- h = logging.FileHandler(logfile)
- h.setLevel(logging.INFO)
- l.addHandler(h)
-
- # Set formatter
- f = logger.BuildFormatter()
- h.setFormatter(f)
-
- return l
-
-
-class BuilderContext(object):
- def __init__(self, pakfire, builder):
- self.pakfire = pakfire
- self.builder = builder
-
- # Get a reference to the logger
- self.log = self.builder.log
-
- def _install(self, packages):
- self.log.debug(_("Installing packages in build environment:"))
- for package in packages:
- self.log.debug(" %s" % package)
-
- # Initialise Pakfire
- with self.pakfire as p:
- # Install all required packages
- transaction = p.install(packages)
-
- # Return how many packages were installed/changed
- ret = len(transaction)
-
- # Dump transaction to log
- t = transaction.dump()
- self.log.info(t)
-
- # Download transaction
- transaction.download()
-
- # Run the transaction
- transaction.run()
-
- return ret
-
- def build(self, path, shell=True):
- # Install the package
- self._install([path])
-
- for makefile in glob.glob("%s/usr/src/packages/*/*.nm" % self.pakfire.path):
- self.pakfire.build(makefile, logging_callback=self.log.log, interactive=shell)
-
- def shell(self, packages=[], install=None):
- if install:
- packages += install
-
- self._install(packages)
-
- # Enter the shell
- self.pakfire.shell()
-