diff options
Diffstat (limited to 'debian/lib/python/debian_linux/gencontrol.py')
-rw-r--r-- | debian/lib/python/debian_linux/gencontrol.py | 124 |
1 files changed, 57 insertions, 67 deletions
diff --git a/debian/lib/python/debian_linux/gencontrol.py b/debian/lib/python/debian_linux/gencontrol.py index 49f52e878b..c3305112c0 100644 --- a/debian/lib/python/debian_linux/gencontrol.py +++ b/debian/lib/python/debian_linux/gencontrol.py @@ -4,7 +4,6 @@ import contextlib import itertools import pathlib import re -from collections import OrderedDict from collections.abc import ( Generator, ) @@ -21,23 +20,12 @@ from .config_v2 import ( ConfigMergedFeatureset, ConfigMergedFlavour, ) +from .dataclasses_deb822 import write_deb822 from .debian import Changelog, PackageArchitecture, \ - Version, _ControlFileDict + Version, SourcePackage, BinaryPackage from .utils import Templates -class PackagesList(OrderedDict): - def append(self, package) -> None: - self[package['Package']] = package - - def extend(self, packages) -> None: - for package in packages: - self[package['Package']] = package - - def setdefault(self, package) -> Any: - return super().setdefault(package['Package'], package) - - class Makefile: rules: dict[str, MakefileRule] @@ -149,23 +137,35 @@ class MakeFlags(dict): class PackagesBundle: + class BinaryPackages(dict[str, BinaryPackage]): + def add(self, package: BinaryPackage) -> BinaryPackage: + return super().setdefault(package.name, package) + name: str | None templates: Templates base: pathlib.Path makefile: Makefile - packages: PackagesList + source: SourcePackage + packages: BinaryPackages def __init__( self, name: str | None, + source_template: str, + replace: dict[str, str], templates: Templates, base: pathlib.Path = pathlib.Path('debian'), + override_name: str | None = None, ) -> None: self.name = name self.templates = templates self.base = base self.makefile = Makefile() - self.packages = PackagesList() + self.source = list(self.templates.get_source_control(source_template, replace))[0] + self.packages = self.BinaryPackages() + + if not self.source.name: + self.source.name = override_name def add( self, @@ -179,14 +179,14 @@ class PackagesBundle: ) -> list[Any]: ret = [] for raw_package in self.templates.get_control(f'{pkgid}.control', replace): - package = self.packages.setdefault(raw_package) - package_name = package['Package'] + package = self.packages.add(raw_package) + package_name = package.name ret.append(package) - package.meta.setdefault('rules-ruleids', {})[ruleid] = makeflags + package.meta_rules_ruleids[ruleid] = makeflags if arch: - package.meta.setdefault('architectures', PackageArchitecture()).add(arch) - package.meta['rules-check-packages'] = check_packages + package.meta_architectures.add(arch) + package.meta_rules_check_packages = check_packages for name in ( 'NEWS', @@ -211,7 +211,7 @@ class PackagesBundle: def add_packages( self, - packages: Iterable[_ControlFileDict], + packages: Iterable[BinaryPackage], ruleid: Iterable[str], makeflags: MakeFlags, *, @@ -219,11 +219,11 @@ class PackagesBundle: check_packages: bool = True, ) -> None: for package in packages: - package = self.packages.setdefault(package) - package.meta.setdefault('rules-ruleids', {})[ruleid] = makeflags + package = self.packages.add(package) + package.meta_rules_ruleids[ruleid] = makeflags if arch: - package.meta.setdefault('architectures', PackageArchitecture()).add(arch) - package.meta['rules-check-packages'] = check_packages + package.meta_architectures.add(arch) + package.meta_rules_check_packages = check_packages def path(self, name) -> pathlib.Path: if self.name: @@ -262,20 +262,19 @@ class PackagesBundle: targets: dict[frozenset[str], dict] = {} for package_name, package in self.packages.items(): - target_name = package.meta.get('rules-target') - ruleids = package.meta.get('rules-ruleids') - makeflags = MakeFlags({ - # Requires Python 3.9+ - k.removeprefix('rules-makeflags-').upper(): v - for (k, v) in package.meta.items() if k.startswith('rules-makeflags-') - }) + if not isinstance(package, BinaryPackage): + continue + + target_name = package.meta_rules_target + ruleids = package.meta_rules_ruleids + makeflags = MakeFlags(package.meta_rules_makeflags) if ruleids: - arches = package.meta.get('architectures') + arches = package.meta_architectures if arches: - package['Architecture'] = arches + package.architecture = arches else: - arches = package.get('Architecture') + arches = package.architecture if target_name: for ruleid, makeflags_package in ruleids.items(): @@ -291,7 +290,7 @@ class PackagesBundle: }, ) - if package.meta['rules-check-packages']: + if package.meta_rules_check_packages: target.setdefault('packages', set()).add(package_name) else: target.setdefault('packages_extra', set()).add(package_name) @@ -331,46 +330,40 @@ class PackagesBundle: def merge_build_depends(self) -> None: # Merge Build-Depends pseudo-fields from binary packages into the # source package - source = self.packages["source"] arch_all = PackageArchitecture("all") for name, package in self.packages.items(): - if name == "source": - continue - dep = package.get("Build-Depends") + dep = package.build_depends if not dep: continue - del package["Build-Depends"] - if package["Architecture"] == arch_all: - dep_type = "Build-Depends-Indep" + if package.architecture == arch_all: + build_dep = self.source.build_depends_indep else: - dep_type = "Build-Depends-Arch" + build_dep = self.source.build_depends_arch for group in dep: for item in group: - if package["Architecture"] != arch_all and not item.arches: - item.arches = package["Architecture"] - if package.get("Build-Profiles") and not item.restrictions: - item.restrictions = package["Build-Profiles"] - source.setdefault(dep_type).merge(group) + if package.architecture != arch_all and not item.arches: + item.arches = package.architecture + if package.build_profiles and not item.restrictions: + item.restrictions = package.build_profiles + build_dep.merge(group) def write(self) -> None: self.write_control() self.write_makefile() def write_control(self) -> None: + p = [self.source] + sorted( + self.packages.values(), + # Sort deb before udeb and then according to name + key=lambda i: (i.package_type or '', i.name), + ) with self.open('control') as f: - self.write_rfc822(f, self.packages.values()) + write_deb822(p, f) def write_makefile(self) -> None: with self.open('rules.gen') as f: self.makefile.write(f) - def write_rfc822(self, f: IO, entries: Iterable) -> None: - for entry in entries: - for key, value in entry.items(): - if value: - f.write(u"%s: %s\n" % (key, value)) - f.write('\n') - class Gencontrol(object): config: ConfigMerged @@ -381,7 +374,7 @@ class Gencontrol(object): self.config, self.templates = config, templates self.changelog = Changelog(version=version) self.vars = {} - self.bundles = {'': PackagesBundle(None, templates)} + self.bundles = {} @property def bundle(self) -> PackagesBundle: @@ -395,10 +388,10 @@ class Gencontrol(object): self.write() def do_source(self) -> None: - source = self.templates.get_source_control("source.control", self.vars)[0] - if not source.get('Source'): - source['Source'] = self.changelog[0].source - self.bundle.packages['source'] = source + self.bundles[''] = PackagesBundle( + None, 'source.control', self.vars, self.templates, + override_name=self.changelog[0].source, + ) def do_main(self) -> None: vars = self.vars.copy() @@ -645,10 +638,7 @@ class Gencontrol(object): ) -> None: pass - def substitute(self, s: str | list | tuple, vars) -> str | list: - if isinstance(s, (list, tuple)): - return [self.substitute(i, vars) for i in s] - + def substitute(self, s: str, vars) -> str: def subst(match) -> str: return vars[match.group(1)] |