]> git.ipfire.org Git - pakfire.git/blame - src/pakfire/builder.py
pakfire: Drop old base module
[pakfire.git] / src / pakfire / builder.py
CommitLineData
964aa579 1#!/usr/bin/python3
b792d887
MT
2###############################################################################
3# #
4# Pakfire - The IPFire package management system #
5# Copyright (C) 2011 Pakfire development team #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
47a4cb89 21
57411b15 22import glob
47a4cb89 23import os
f02283bb 24import uuid
47a4cb89 25
964aa579 26from . import _pakfire
964aa579 27from . import logger
964aa579 28from . import util
47a4cb89 29
8b6bc023 30import logging
2274aab3 31log = logging.getLogger("pakfire.builder")
8b6bc023 32
964aa579
MT
33from .constants import *
34from .i18n import _
eb0a3b92 35from .errors import *
47a4cb89 36
9eac53ba
MT
37BUILD_LOG_HEADER = """
38 ____ _ __ _ _ _ _ _
39| _ \ __ _| | __/ _(_)_ __ ___ | |__ _ _(_) | __| | ___ _ __
40| |_) / _` | |/ / |_| | '__/ _ \ | '_ \| | | | | |/ _` |/ _ \ '__|
41| __/ (_| | <| _| | | | __/ | |_) | |_| | | | (_| | __/ |
42|_| \__,_|_|\_\_| |_|_| \___| |_.__/ \__,_|_|_|\__,_|\___|_|
43
9eac53ba 44 Version : %(version)s
c62d93f1
MT
45 Host : %(hostname)s (%(host_arch)s)
46 Time : %(time)s
9eac53ba
MT
47
48"""
49
2274aab3 50class Builder(object):
e947f34f
MT
51 def __init__(self, conf, arch=None, build_id=None, logfile=None, **kwargs):
52 self.conf = conf
9eac53ba 53
992b09d6 54 # Settings array.
36b328f2 55 self.settings = {
1c2ff7a3
MT
56 "enable_ccache" : True,
57 "enable_snapshot" : True,
36b328f2 58 }
7c8f2953 59
2274aab3 60 # Add settings from keyword arguments
392371f7
MT
61 self.settings.update(kwargs)
62
2274aab3
MT
63 # Generate a build ID
64 self.build_id = build_id or "%s" % uuid.uuid4()
3817ae8e 65
f810ceaa
MT
66 # Setup logging
67 self.log = self.setup_logging(logfile)
68
2274aab3 69 # Architecture to build for
a5600261 70 self.arch = arch or _pakfire.native_arch()
f02283bb 71
2274aab3 72 def __enter__(self):
69e754ab 73 self.log.debug("Entering %s" % self)
8930b228 74
5c311471 75 # Initialise Pakfire instance
28d8677b 76 pakfire = _pakfire.Pakfire(
5c311471 77 arch=self.arch,
e947f34f 78 conf=self.conf,
5c311471 79
26452aef
MT
80 # Enable build mode
81 build=True,
82 **self.settings,
83 )
5c311471 84
2274aab3 85 # Setup domain name resolution in chroot
78f7a47c
MT
86 for i in ("/etc/resolv.conf", "/etc/hosts"):
87 pakfire.copy_in(i, i)
8930b228 88
5c311471 89 return BuilderContext(pakfire, self)
8930b228 90
2274aab3 91 def __exit__(self, type, value, traceback):
69e754ab 92 self.log.debug("Leaving %s" % self)
2274aab3 93
2274aab3 94 def setup_logging(self, logfile):
f810ceaa
MT
95 l = log.getChild(self.build_id)
96 l.setLevel(logging.DEBUG)
8930b228 97
f810ceaa 98 # Print everything to the console
9d17e0d9 99 console = logger.ConsoleHandler()
23f32387 100 l.addHandler(console)
f810ceaa
MT
101
102 # If we have a log file, we write INFO and higher level messages to it
103 # using the build formatter which adds a timestamp
104 if logfile:
2274aab3 105 h = logging.FileHandler(logfile)
f810ceaa
MT
106 h.setLevel(logging.INFO)
107 l.addHandler(h)
992b09d6 108
f810ceaa 109 # Set formatter
2274aab3
MT
110 f = logger.BuildFormatter()
111 h.setFormatter(f)
f810ceaa
MT
112
113 return l
36b328f2 114
2274aab3
MT
115
116class BuilderContext(object):
5c311471
MT
117 def __init__(self, pakfire, builder):
118 self.pakfire = pakfire
2274aab3
MT
119 self.builder = builder
120
2274aab3
MT
121 # Get a reference to the logger
122 self.log = self.builder.log
123
3b3a0abd
MT
124 def _install(self, packages):
125 self.log.debug(_("Installing packages in build environment:"))
126 for package in packages:
127 self.log.debug(" %s" % package)
2274aab3 128
b21de24a 129 # Initialise Pakfire
2274aab3 130 with self.pakfire as p:
b21de24a
MT
131 # Install all required packages
132 transaction = p.install(packages)
133
6a160d30
MT
134 # Return how many packages were installed/changed
135 ret = len(transaction)
136
b21de24a
MT
137 # Dump transaction to log
138 t = transaction.dump()
139 self.log.info(t)
140
141 # Download transaction
4a81ff85 142 transaction.download()
b21de24a
MT
143
144 # Run the transaction
145 transaction.run()
2274aab3 146
6a160d30 147 return ret
3b3a0abd 148
6a160d30 149 def build(self, path, shell=True):
57411b15
MT
150 # Install the package
151 self._install([path])
e4cfcbaa 152
57411b15 153 for makefile in glob.glob("%s/usr/src/packages/*/*.nm" % self.pakfire.path):
7d999600 154 self.pakfire.build(makefile, logging_callback=self.log.log, interactive=shell)
c8c215e8 155
b3cdc86c 156 def shell(self, packages=[], install=None):
6a160d30 157 if install:
57411b15 158 packages += install
2274aab3 159
57411b15 160 self._install(packages)
b3cdc86c 161
8a33c116 162 # Enter the shell
1a276007 163 self.pakfire.shell()
23f32387 164