c784dab4aa
The get_system_release_packages function can never be called without a variant, so it makes no sense to check for that condition. Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
494 lines
20 KiB
Python
494 lines
20 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
|
|
# 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 Library 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 <https://gnu.org/licenses/>.
|
|
|
|
|
|
import os
|
|
import shutil
|
|
import json
|
|
|
|
from kobo.rpmlib import parse_nvra
|
|
from productmd.rpms import Rpms
|
|
|
|
from pungi.wrappers.scm import get_file_from_scm
|
|
from .link import link_files
|
|
|
|
from pungi.util import get_arch_variant_data, get_arch_data
|
|
from pungi.phases.base import PhaseBase
|
|
from pungi.arch import split_name_arch, get_compatible_arches
|
|
|
|
|
|
def get_gather_source(name):
|
|
import pungi.phases.gather.sources
|
|
from source import GatherSourceContainer
|
|
GatherSourceContainer.register_module(pungi.phases.gather.sources)
|
|
container = GatherSourceContainer()
|
|
return container["GatherSource%s" % name]
|
|
|
|
|
|
def get_gather_method(name):
|
|
import pungi.phases.gather.methods
|
|
from method import GatherMethodContainer
|
|
GatherMethodContainer.register_module(pungi.phases.gather.methods)
|
|
container = GatherMethodContainer()
|
|
return container["GatherMethod%s" % name]
|
|
|
|
|
|
class GatherPhase(PhaseBase):
|
|
"""GATHER"""
|
|
name = "gather"
|
|
|
|
def __init__(self, compose, pkgset_phase):
|
|
PhaseBase.__init__(self, compose)
|
|
# pkgset_phase provides package_sets and path_prefix
|
|
self.pkgset_phase = pkgset_phase
|
|
# Prepare empty manifest
|
|
self.manifest_file = self.compose.paths.compose.metadata("rpms.json")
|
|
self.manifest = Rpms()
|
|
self.manifest.compose.id = self.compose.compose_id
|
|
self.manifest.compose.type = self.compose.compose_type
|
|
self.manifest.compose.date = self.compose.compose_date
|
|
self.manifest.compose.respin = self.compose.compose_respin
|
|
|
|
def _write_manifest(self):
|
|
self.compose.log_info("Writing RPM manifest: %s" % self.manifest_file)
|
|
self.manifest.dump(self.manifest_file)
|
|
|
|
def run(self):
|
|
pkg_map = gather_wrapper(self.compose, self.pkgset_phase.package_sets,
|
|
self.pkgset_phase.path_prefix)
|
|
|
|
for arch in self.compose.get_arches():
|
|
for variant in self.compose.get_variants(arch=arch):
|
|
if variant.is_empty:
|
|
continue
|
|
link_files(self.compose, arch, variant,
|
|
pkg_map[arch][variant.uid],
|
|
self.pkgset_phase.package_sets,
|
|
manifest=self.manifest)
|
|
|
|
def stop(self):
|
|
self._write_manifest()
|
|
super(GatherPhase, self).stop()
|
|
|
|
|
|
def _mk_pkg_map(rpm=None, srpm=None, debuginfo=None, iterable_class=list):
|
|
return {
|
|
"rpm": rpm or iterable_class(),
|
|
"srpm": srpm or iterable_class(),
|
|
"debuginfo": debuginfo or iterable_class(),
|
|
}
|
|
|
|
|
|
def get_parent_pkgs(arch, variant, result_dict):
|
|
"""Find packages for parent variant (if any).
|
|
|
|
:param result_dict: already known packages; a mapping from arch to variant uid
|
|
to package type to a list of dicts with path to package
|
|
"""
|
|
result = _mk_pkg_map(iterable_class=set)
|
|
if variant.parent is None:
|
|
return result
|
|
for pkg_type, pkgs in result_dict.get(arch, {}).get(variant.parent.uid, {}).iteritems():
|
|
for pkg in pkgs:
|
|
nvra = parse_nvra(pkg["path"])
|
|
result[pkg_type].add((nvra["name"], nvra["arch"]))
|
|
return result
|
|
|
|
|
|
def gather_packages(compose, arch, variant, package_sets, fulltree_excludes=None):
|
|
# multilib white/black-list is per-arch, common for all variants
|
|
multilib_whitelist = get_multilib_whitelist(compose, arch)
|
|
multilib_blacklist = get_multilib_blacklist(compose, arch)
|
|
GatherMethod = get_gather_method(compose.conf["gather_method"])
|
|
|
|
msg = "Gathering packages (arch: %s, variant: %s)" % (arch, variant)
|
|
|
|
if variant.is_empty:
|
|
compose.log_info("[SKIP ] %s" % msg)
|
|
return _mk_pkg_map()
|
|
|
|
compose.log_info("[BEGIN] %s" % msg)
|
|
|
|
packages, groups, filter_packages = get_variant_packages(compose, arch, variant, package_sets)
|
|
prepopulate = get_prepopulate_packages(compose, arch, variant)
|
|
fulltree_excludes = fulltree_excludes or set()
|
|
|
|
method = GatherMethod(compose)
|
|
pkg_map = method(arch, variant, packages, groups, filter_packages,
|
|
multilib_whitelist, multilib_blacklist, package_sets,
|
|
fulltree_excludes=fulltree_excludes, prepopulate=prepopulate)
|
|
|
|
compose.log_info("[DONE ] %s" % msg)
|
|
return pkg_map
|
|
|
|
|
|
def write_packages(compose, arch, variant, pkg_map, path_prefix):
|
|
"""Write a list of packages to a file (one per package type).
|
|
|
|
If any path begins with ``path_prefix``, this prefix will be stripped.
|
|
"""
|
|
msg = "Writing package list (arch: %s, variant: %s)" % (arch, variant)
|
|
compose.log_info("[BEGIN] %s" % msg)
|
|
|
|
for pkg_type, pkgs in pkg_map.iteritems():
|
|
file_name = compose.paths.work.package_list(arch=arch, variant=variant, pkg_type=pkg_type)
|
|
with open(file_name, "w") as pkg_list:
|
|
for pkg in pkgs:
|
|
# TODO: flags?
|
|
pkg_path = pkg["path"]
|
|
if pkg_path.startswith(path_prefix):
|
|
pkg_path = pkg_path[len(path_prefix):]
|
|
pkg_list.write("%s\n" % pkg_path)
|
|
|
|
compose.log_info("[DONE ] %s" % msg)
|
|
|
|
|
|
def trim_packages(compose, arch, variant, pkg_map, parent_pkgs=None, remove_pkgs=None):
|
|
"""Remove parent variant's packages from pkg_map <-- it gets modified in this function
|
|
|
|
There are three cases where changes may happen:
|
|
|
|
* If a package is mentioned explicitly in ``remove_pkgs``, it will be
|
|
removed from the addon. Sources and debuginfo are not removed from
|
|
layered-products though.
|
|
* If a packages is present in parent, it will be removed from addon
|
|
unconditionally.
|
|
* A package in addon that is not present in parent and has
|
|
``fulltree-exclude`` flag will be moved to parent (unless it's
|
|
explicitly included into the addon).
|
|
|
|
:param parent_pkgs: mapping from pkg_type to a list of tuples (name, arch)
|
|
of packages present in parent variant
|
|
:param remove_pkgs: mapping from pkg_type to a list of package names to be
|
|
removed from the variant
|
|
"""
|
|
# TODO: remove debuginfo and srpm leftovers
|
|
|
|
if not variant.parent:
|
|
return
|
|
|
|
msg = "Trimming package list (arch: %s, variant: %s)" % (arch, variant)
|
|
compose.log_info("[BEGIN] %s" % msg)
|
|
|
|
remove_pkgs = remove_pkgs or {}
|
|
parent_pkgs = parent_pkgs or {}
|
|
|
|
addon_pkgs = _mk_pkg_map(iterable_class=set)
|
|
move_to_parent_pkgs = _mk_pkg_map()
|
|
removed_pkgs = _mk_pkg_map()
|
|
for pkg_type, pkgs in pkg_map.iteritems():
|
|
|
|
new_pkgs = []
|
|
for pkg in pkgs:
|
|
pkg_path = pkg["path"]
|
|
if not pkg_path:
|
|
continue
|
|
nvra = parse_nvra(pkg_path)
|
|
key = ((nvra["name"], nvra["arch"]))
|
|
|
|
if nvra["name"] in remove_pkgs.get(pkg_type, set()):
|
|
# TODO: make an option to turn this off
|
|
if variant.type == "layered-product" and pkg_type in ("srpm", "debuginfo"):
|
|
new_pkgs.append(pkg)
|
|
# User may not have addons available, therefore we need to
|
|
# keep addon SRPMs in layered products in order not to violate GPL.
|
|
# The same applies on debuginfo availability.
|
|
continue
|
|
compose.log_warning("Removed addon package (arch: %s, variant: %s): %s: %s" % (
|
|
arch, variant, pkg_type, pkg_path))
|
|
removed_pkgs[pkg_type].append(pkg)
|
|
elif key not in parent_pkgs.get(pkg_type, set()):
|
|
if "fulltree-exclude" in pkg["flags"] and "input" not in pkg["flags"]:
|
|
# If a package wasn't explicitly included ('input') in an
|
|
# addon, move it to parent variant (cannot move it to
|
|
# optional, because addons can't depend on optional). This
|
|
# is a workaround for not having $addon-optional.
|
|
move_to_parent_pkgs[pkg_type].append(pkg)
|
|
else:
|
|
new_pkgs.append(pkg)
|
|
addon_pkgs[pkg_type].add(nvra["name"])
|
|
else:
|
|
removed_pkgs[pkg_type].append(pkg)
|
|
|
|
pkg_map[pkg_type] = new_pkgs
|
|
compose.log_info("Removed packages (arch: %s, variant: %s): %s: %s" % (
|
|
arch, variant, pkg_type, len(removed_pkgs[pkg_type])))
|
|
compose.log_info("Moved to parent (arch: %s, variant: %s): %s: %s" % (
|
|
arch, variant, pkg_type, len(move_to_parent_pkgs[pkg_type])))
|
|
|
|
compose.log_info("[DONE ] %s" % msg)
|
|
return addon_pkgs, move_to_parent_pkgs, removed_pkgs
|
|
|
|
|
|
def _gather_variants(result, compose, variant_type, package_sets, exclude_fulltree=False):
|
|
"""Run gathering on all arches of all variants of given type.
|
|
|
|
If ``exclude_fulltree`` is set, all source packages from parent variants
|
|
will be added to fulltree excludes for the processed variants.
|
|
"""
|
|
for arch in compose.get_arches():
|
|
for variant in compose.get_variants(arch=arch, types=[variant_type]):
|
|
fulltree_excludes = set()
|
|
if exclude_fulltree:
|
|
for pkg_name, pkg_arch in get_parent_pkgs(arch, variant, result)["srpm"]:
|
|
fulltree_excludes.add(pkg_name)
|
|
pkg_map = gather_packages(compose, arch, variant, package_sets, fulltree_excludes=fulltree_excludes)
|
|
result.setdefault(arch, {})[variant.uid] = pkg_map
|
|
|
|
|
|
def _trim_variants(result, compose, variant_type, remove_pkgs=None, move_to_parent=True):
|
|
"""Trim all varians of given type.
|
|
|
|
Returns a map of all packages included in these variants.
|
|
"""
|
|
all_included_packages = {}
|
|
for arch in compose.get_arches():
|
|
for variant in compose.get_variants(arch=arch, types=[variant_type]):
|
|
pkg_map = result[arch][variant.uid]
|
|
parent_pkgs = get_parent_pkgs(arch, variant, result)
|
|
included_packages, move_to_parent_pkgs, removed_pkgs = trim_packages(
|
|
compose, arch, variant, pkg_map, parent_pkgs, remove_pkgs=remove_pkgs)
|
|
|
|
# update all_addon_pkgs
|
|
for pkg_type, pkgs in included_packages.iteritems():
|
|
all_included_packages.setdefault(pkg_type, set()).update(pkgs)
|
|
|
|
if move_to_parent:
|
|
# move packages to parent
|
|
parent_pkg_map = result[arch][variant.parent.uid]
|
|
for pkg_type, pkgs in move_to_parent_pkgs.iteritems():
|
|
for pkg in pkgs:
|
|
compose.log_debug("Moving package to parent (arch: %s, variant: %s, pkg_type: %s): %s"
|
|
% (arch, variant.uid, pkg_type, os.path.basename(pkg["path"])))
|
|
if pkg not in parent_pkg_map[pkg_type]:
|
|
parent_pkg_map[pkg_type].append(pkg)
|
|
return all_included_packages
|
|
|
|
|
|
def gather_wrapper(compose, package_sets, path_prefix):
|
|
result = {}
|
|
|
|
_gather_variants(result, compose, 'variant', package_sets)
|
|
_gather_variants(result, compose, 'addon', package_sets, exclude_fulltree=True)
|
|
_gather_variants(result, compose, 'layered-product', package_sets, exclude_fulltree=True)
|
|
_gather_variants(result, compose, 'optional', package_sets)
|
|
|
|
all_addon_pkgs = _trim_variants(result, compose, 'addon')
|
|
# TODO do we really want to move packages to parent here?
|
|
all_lp_pkgs = _trim_variants(result, compose, 'layered-product', remove_pkgs=all_addon_pkgs)
|
|
|
|
# merge all_addon_pkgs with all_lp_pkgs
|
|
for pkg_type in set(all_addon_pkgs.keys()) | set(all_lp_pkgs.keys()):
|
|
all_addon_pkgs.setdefault(pkg_type, set()).update(all_lp_pkgs.get(pkg_type, set()))
|
|
|
|
_trim_variants(result, compose, 'optional', remove_pkgs=all_addon_pkgs, move_to_parent=False)
|
|
|
|
# write packages (package lists) for all variants
|
|
for arch in compose.get_arches():
|
|
for variant in compose.get_variants(arch=arch):
|
|
pkg_map = result[arch][variant.uid]
|
|
write_packages(compose, arch, variant, pkg_map, path_prefix=path_prefix)
|
|
|
|
return result
|
|
|
|
|
|
def write_prepopulate_file(compose):
|
|
"""Download prepopulate file according to configuration.
|
|
|
|
It is stored in a location where ``get_prepopulate_packages`` function
|
|
expects.
|
|
"""
|
|
if 'gather_prepopulate' not in compose.conf:
|
|
return
|
|
|
|
prepopulate_file = os.path.join(compose.paths.work.topdir(arch="global"), "prepopulate.json")
|
|
msg = "Writing prepopulate file: %s" % prepopulate_file
|
|
|
|
if compose.DEBUG and os.path.isfile(prepopulate_file):
|
|
compose.log_warning("[SKIP ] %s" % msg)
|
|
else:
|
|
scm_dict = compose.conf["gather_prepopulate"]
|
|
if isinstance(scm_dict, dict):
|
|
file_name = os.path.basename(scm_dict["file"])
|
|
if scm_dict["scm"] == "file":
|
|
scm_dict["file"] = os.path.join(compose.config_dir, os.path.basename(scm_dict["file"]))
|
|
else:
|
|
file_name = os.path.basename(scm_dict)
|
|
scm_dict = os.path.join(compose.config_dir, os.path.basename(scm_dict))
|
|
|
|
compose.log_debug(msg)
|
|
tmp_dir = compose.mkdtemp(prefix="prepopulate_file_")
|
|
get_file_from_scm(scm_dict, tmp_dir, logger=compose._logger)
|
|
shutil.copy2(os.path.join(tmp_dir, file_name), prepopulate_file)
|
|
shutil.rmtree(tmp_dir)
|
|
|
|
|
|
def get_prepopulate_packages(compose, arch, variant):
|
|
"""Read prepopulate file and return list of packages for given tree.
|
|
|
|
If ``variant`` is ``None``, all variants in the file are considered. The
|
|
result of this function is a set of strings of format
|
|
``package_name.arch``.
|
|
"""
|
|
result = set()
|
|
|
|
prepopulate_file = os.path.join(compose.paths.work.topdir(arch="global"), "prepopulate.json")
|
|
if not os.path.isfile(prepopulate_file):
|
|
return result
|
|
|
|
with open(prepopulate_file, "r") as f:
|
|
prepopulate_data = json.load(f)
|
|
|
|
variants = [variant.uid] if variant else prepopulate_data.keys()
|
|
|
|
for var in variants:
|
|
for build, packages in prepopulate_data.get(var, {}).get(arch, {}).iteritems():
|
|
for i in packages:
|
|
pkg_name, pkg_arch = split_name_arch(i)
|
|
if pkg_arch not in get_compatible_arches(arch, multilib=True):
|
|
raise ValueError(
|
|
"Incompatible package arch '%s' for tree arch '%s' in prepopulate package '%s'"
|
|
% (pkg_arch, arch, pkg_name))
|
|
result.add(i)
|
|
return result
|
|
|
|
|
|
def get_additional_packages(compose, arch, variant):
|
|
result = set()
|
|
for i in get_arch_variant_data(compose.conf, "additional_packages", arch, variant):
|
|
pkg_name, pkg_arch = split_name_arch(i)
|
|
if pkg_arch is not None and pkg_arch not in get_compatible_arches(arch, multilib=True):
|
|
raise ValueError(
|
|
"Incompatible package arch '%s' for tree arch '%s' in additional package '%s'"
|
|
% (pkg_arch, arch, pkg_name))
|
|
result.add((pkg_name, pkg_arch))
|
|
return result
|
|
|
|
|
|
def get_filter_packages(compose, arch, variant):
|
|
result = set()
|
|
for i in get_arch_variant_data(compose.conf, "filter_packages", arch, variant):
|
|
result.add(split_name_arch(i))
|
|
return result
|
|
|
|
|
|
def get_multilib_whitelist(compose, arch):
|
|
return set(get_arch_data(compose.conf, "multilib_whitelist", arch))
|
|
|
|
|
|
def get_multilib_blacklist(compose, arch):
|
|
return set(get_arch_data(compose.conf, "multilib_blacklist", arch))
|
|
|
|
|
|
def get_lookaside_repos(compose, arch, variant):
|
|
return get_arch_variant_data(compose.conf, "gather_lookaside_repos", arch, variant)
|
|
|
|
|
|
def get_variant_packages(compose, arch, variant, package_sets=None):
|
|
"""Find inputs for depsolving of variant.arch combination.
|
|
|
|
Returns a triple: a list of input packages, a list of input comps groups
|
|
and a list of packages to be filtered out of the variant.
|
|
|
|
For addons and layered products the inputs of parent variant are added as
|
|
well. For optional it's parent and all its addons and layered products.
|
|
|
|
The filtered packages are never inherited from parent.
|
|
|
|
When system-release packages should be filtered, the ``package_sets``
|
|
argument is required.
|
|
"""
|
|
GatherSource = get_gather_source(compose.conf["gather_source"])
|
|
source = GatherSource(compose)
|
|
packages, groups = source(arch, variant)
|
|
filter_packages = set()
|
|
|
|
if variant is None:
|
|
# no variant -> no parent -> we have everything we need
|
|
# doesn't make sense to do any package filtering
|
|
return packages, groups, filter_packages
|
|
|
|
packages |= get_additional_packages(compose, arch, variant)
|
|
filter_packages |= get_filter_packages(compose, arch, variant)
|
|
|
|
if compose.conf['filter_system_release_packages']:
|
|
system_release_packages, system_release_filter_packages = get_system_release_packages(
|
|
compose, arch, variant, package_sets)
|
|
packages |= system_release_packages
|
|
filter_packages |= system_release_filter_packages
|
|
|
|
if variant.type == "optional":
|
|
for var in variant.parent.get_variants(
|
|
arch=arch, types=["self", "variant", "addon", "layered-product"]):
|
|
var_packages, var_groups, _ = get_variant_packages(
|
|
compose, arch, var, package_sets=package_sets)
|
|
packages |= var_packages
|
|
groups |= var_groups
|
|
|
|
if variant.type in ["addon", "layered-product"]:
|
|
var_packages, var_groups, _ = get_variant_packages(
|
|
compose, arch, variant.parent, package_sets=package_sets)
|
|
packages |= var_packages
|
|
groups |= var_groups
|
|
|
|
return packages, groups, filter_packages
|
|
|
|
|
|
def get_system_release_packages(compose, arch, variant, package_sets):
|
|
packages = set()
|
|
filter_packages = set()
|
|
|
|
if not package_sets or not package_sets.get(arch, None):
|
|
return packages, filter_packages
|
|
|
|
package_set = package_sets[arch]
|
|
|
|
system_release_packages = set()
|
|
for i in package_set:
|
|
pkg = package_set[i]
|
|
|
|
if pkg.is_system_release:
|
|
system_release_packages.add(pkg)
|
|
|
|
if not system_release_packages:
|
|
return packages, filter_packages
|
|
elif len(system_release_packages) == 1:
|
|
# always include system-release package if available
|
|
pkg = list(system_release_packages)[0]
|
|
packages.add((pkg.name, None))
|
|
else:
|
|
if variant.type == "variant":
|
|
# search for best match
|
|
best_match = None
|
|
for pkg in system_release_packages:
|
|
if pkg.name.endswith("release-%s" % variant.uid.lower()) or pkg.name.startswith("%s-release" % variant.uid.lower()):
|
|
best_match = pkg
|
|
break
|
|
else:
|
|
# addons: return release packages from parent variant
|
|
return get_system_release_packages(compose, arch, variant.parent, package_sets)
|
|
|
|
if not best_match:
|
|
# no package matches variant name -> pick the first one
|
|
best_match = sorted(system_release_packages)[0]
|
|
|
|
packages.add((best_match.name, None))
|
|
for pkg in system_release_packages:
|
|
if pkg.name == best_match.name:
|
|
continue
|
|
filter_packages.add((pkg.name, None))
|
|
|
|
return packages, filter_packages
|