Skip to content

Commit b8e357d

Browse files
authored
Merge pull request #3 from TheJJ/build-improvements
build improvements
2 parents d5d66b7 + 7259138 commit b8e357d

File tree

16 files changed

+116
-64
lines changed

16 files changed

+116
-64
lines changed

pyproject.toml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@ exclude_gitignore = true
3131
[tool.ruff]
3232
line-length = 120
3333
target-version = "py312"
34-
exclude = [".git", ".idea", ".mypy_cache", ".venv*", "docs", "debian"]
34+
extend-exclude = [".idea", ".mypy_cache", ".venv*", "docs", "debian", "__pycache__", "*.egg_info"]
3535

3636
[tool.ruff.lint]
37-
select = ["E4", "E7", "E9", "F", "I", "PLE", "PLW"]
38-
ignore = ["E722"]
37+
select = ["E", "W", "F", "I", "C", "N", "PL", "RUF", "I001"]
38+
ignore = ["E722", "PLR2004", "PLR0912", "PLR5501", "PLC0415"]
39+
mccabe.max-complexity = 25
40+
pylint.max-args = 10
3941

4042
[tool.pytest]
4143
testpaths = ["tests"]
44+
addopts = ["--ignore=tests/integration/packages"]

src/debmagic/_build.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from __future__ import annotations
22

3-
import shlex
43
import typing
54
from dataclasses import dataclass, field
65
from pathlib import Path
6+
from typing import Sequence
77

88
from ._build_stage import BuildStage
99
from ._utils import run_cmd
@@ -29,21 +29,13 @@ class Build:
2929
dry_run: bool = False
3030

3131
_completed_stages: set[BuildStage] = field(default_factory=set)
32-
_selected_packages: list[BinaryPackage] | None = field(default_factory=list)
3332

34-
def cmd(self, cmd: list[str] | str, **kwargs):
33+
def cmd(self, cmd: Sequence[str] | str, **kwargs):
3534
"""
3635
execute a command, auto-converts command strings/lists.
3736
use this to supports build dry-runs.
3837
"""
39-
cmd_args: list[str] | str = cmd
40-
is_shell = kwargs.get("shell")
41-
if not is_shell and isinstance(cmd, str):
42-
cmd_args = shlex.split(cmd)
43-
elif is_shell and not isinstance(cmd, str):
44-
cmd_args = shlex.join(cmd)
45-
46-
run_cmd(cmd_args, dry_run=self.dry_run, **kwargs)
38+
run_cmd(cmd, dry_run=self.dry_run, **kwargs)
4739

4840
@property
4941
def install_dirs(self) -> dict[str, Path]:
@@ -52,17 +44,20 @@ def install_dirs(self) -> dict[str, Path]:
5244

5345
def select_packages(self, names: set[str]):
5446
"""only build those packages"""
55-
if not names:
56-
self._selected_packages = None
47+
self.binary_packages = []
5748

58-
self._selected_packages = list()
59-
for pkg in self.binary_packages:
49+
for pkg in self.source_package.binary_packages:
6050
if pkg.name in names:
61-
self._selected_packages.append(pkg)
51+
self.binary_packages.append(pkg)
6252

6353
def filter_packages(self, package_filter: PackageFilter) -> None:
6454
"""apply filter to only build those packages"""
65-
self.select_packages({pkg.name for pkg in package_filter.get_packages(self.binary_packages)})
55+
self.select_packages({pkg.name for pkg in package_filter.get_packages(self.source_package.binary_packages)})
56+
57+
def filtered_binary_packages(self, names: set[str]) -> typing.Iterator[BinaryPackage]:
58+
for pkg in self.binary_packages:
59+
if pkg.name in names:
60+
yield pkg
6661

6762
def is_stage_completed(self, stage: BuildStage) -> bool:
6863
return stage in self._completed_stages
@@ -95,13 +90,12 @@ def run(
9590
for preset in self.presets:
9691
print(f"debmagic: trying preset {preset}...")
9792
if preset_stage_function := preset.get_stage(stage):
98-
print("debmagic: preset has function")
93+
print("debmagic: running stage from preset")
9994
preset_stage_function(self)
10095
self._mark_stage_done(stage)
10196
break # stop preset processing
10297

10398
if not self.is_stage_completed(stage):
104-
breakpoint()
10599
raise RuntimeError(f"{stage!s} stage was never executed")
106100

107101
if stage == target_stage:
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@ def configure(self, build: Build, args: list[str] | None = None):
4747
# TODO: show some config.log if configure failed
4848

4949
def build(self, build: Build, args: list[str] = []):
50-
args = [f"-j{build.parallel}"] + args
50+
args = [f"-j{build.parallel}", *args]
5151

52-
build.cmd(["make"] + args, cwd=build.source_dir)
52+
build.cmd(["make", *args], cwd=build.source_dir)
5353

5454
# TODO: def test(): run make test or make check
5555

5656
def install(self, build: Build, args: list[str] = []):
5757
# TODO: figure out installdir handling for multi package builds
5858
destdir = build.install_dirs[build.source_package.name]
59-
build.cmd(["make", f"DESTDIR={destdir}", "install"] + args, cwd=build.source_dir)
59+
build.cmd(["make", f"DESTDIR={destdir}", "install", *args], cwd=build.source_dir)
6060

6161

6262
def autoreconf(build: Build):
Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@
1212
from .._build import Build
1313
from .._package import SourcePackage
1414
from .._preset import Preset as PresetBase
15-
from .._utils import prefix_idx, run_cmd
15+
from .._utils import list_strip_head, prefix_idx, run_cmd
1616

1717

1818
class DHSequenceID(StrEnum):
1919
clean = "clean"
2020
build = "build"
2121
build_arch = "build-arch"
2222
build_indep = "build-indep"
23+
install = "install"
24+
install_arch = "install-arch"
25+
install_indep = "install-indep"
2326
binary = "binary"
2427
binary_arch = "binary-arch"
2528
binary_indep = "binary-indep"
@@ -33,16 +36,16 @@ def __init__(self, dh_invocation: str | None = None):
3336
if not dh_invocation:
3437
dh_invocation = "dh"
3538
self._dh_invocation: str = dh_invocation
36-
self._overrides: dict[str, DHOverride] = dict()
39+
self._overrides: dict[str, DHOverride] = {}
3740
self._initialized = False
3841

3942
# debmagic's stages, with matching commands from the dh sequence
40-
self._clean_seq: list[str] = list()
41-
self._configure_seq: list[str] = list()
42-
self._build_seq: list[str] = list()
43-
self._test_seq: list[str] = list()
44-
self._install_seq: list[str] = list()
45-
self._package_seq: list[str] = list()
43+
self._clean_seq: list[str] = []
44+
self._configure_seq: list[str] = []
45+
self._build_seq: list[str] = []
46+
self._test_seq: list[str] = []
47+
self._install_seq: list[str] = []
48+
self._package_seq: list[str] = []
4649

4750
# all seen sequence cmd ids (the dh command script itself)
4851
self._seq_ids: set[str] = set()
@@ -106,10 +109,10 @@ def _populate_stages(self, dh_invocation: str, base_dir: Path) -> None:
106109
self._clean_seq = self._get_dh_seq(base_dir, dh_invocation, DHSequenceID.clean)
107110

108111
## untangle "build" to configure & build & test
109-
build_seq = self._get_dh_seq(base_dir, dh_invocation, DHSequenceID.build)
110-
if build_seq[-1] != "create-stamp debian/debhelper-build-stamp":
112+
build_seq_raw = self._get_dh_seq(base_dir, dh_invocation, DHSequenceID.build)
113+
if build_seq_raw[-1] != "create-stamp debian/debhelper-build-stamp":
111114
raise RuntimeError("build stamp creation line missing from dh build sequence")
112-
build_seq = build_seq[:-1] # remove that stamp line
115+
build_seq = build_seq_raw[:-1] # remove that stamp line
113116

114117
auto_cfg_idx = prefix_idx("dh_auto_configure", build_seq)
115118
# up to including dh_auto_configure
@@ -125,14 +128,12 @@ def _populate_stages(self, dh_invocation: str, base_dir: Path) -> None:
125128
self._test_seq = build_seq[auto_test_idx:]
126129

127130
## untangle "binary" to install & package
131+
install_seq = self._get_dh_seq(base_dir, dh_invocation, DHSequenceID.install)
132+
self._install_seq = list_strip_head(install_seq, build_seq_raw)
128133
binary_seq = self._get_dh_seq(base_dir, dh_invocation, DHSequenceID.binary)
129-
build_stamp_idx = prefix_idx("create-stamp debian/debhelper-build-stamp", binary_seq)
130-
auto_install_idx = prefix_idx("dh_auto_install", binary_seq)
131-
# one after the build-stamp, up to including dh_auto_install
132-
self._install_seq = binary_seq[build_stamp_idx + 1 : auto_install_idx + 1]
133-
# assume everything else is packing (which is a bit wrong, but packing & installing is mixed in dh)
134-
self._package_seq = binary_seq[auto_install_idx + 1 :]
134+
self._package_seq = list_strip_head(binary_seq, install_seq)
135135

136+
# register all sequence items for validity checks
136137
for seq in (
137138
self._clean_seq,
138139
self._configure_seq,
@@ -148,7 +149,7 @@ def _populate_stages(self, dh_invocation: str, base_dir: Path) -> None:
148149

149150
def _get_dh_seq(self, base_dir: Path, dh_invocation: str, seq: DHSequenceID) -> list[str]:
150151
dh_base_cmd = shlex.split(dh_invocation)
151-
cmd = dh_base_cmd + [str(seq), "--no-act"]
152+
cmd = [*dh_base_cmd, str(seq), "--no-act"]
152153
proc = run_cmd(cmd, cwd=base_dir, capture_output=True, text=True)
153154
lines = proc.stdout.splitlines()
154155
return [line.strip() for line in lines]

src/debmagic/_package.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class PackageFilter(Flag):
9595
architecture_independent = auto()
9696

9797
def get_packages(self, binary_packages: list[BinaryPackage]) -> list[BinaryPackage]:
98-
ret: list[BinaryPackage] = list()
98+
ret: list[BinaryPackage] = []
9999

100100
for pkg in binary_packages:
101101
if self.architecture_independent and pkg.arch_dependent:
@@ -157,7 +157,7 @@ def something(arg: str = 'stuff'):
157157

158158
# find arguments and its types to guess argparsing
159159
args_raw = inspect.getfullargspec(func)
160-
args: CustomFuncArgsT = dict()
160+
args: CustomFuncArgsT = {}
161161
default_count = 0 if not args_raw.defaults else len(args_raw.defaults)
162162
default_start = len(args_raw.args) - default_count
163163
for idx, arg in enumerate(args_raw.args):
@@ -253,7 +253,7 @@ def package(
253253
presets: list[Preset] = as_presets(preset)
254254

255255
# apply default preset last
256-
from ._modules.default import Preset as DefaultPreset
256+
from ._module.default import Preset as DefaultPreset
257257

258258
presets.append(DefaultPreset())
259259

@@ -263,7 +263,7 @@ def package(
263263

264264
src_pkg: SourcePackage | None = None
265265
# which binary packages should be produced?
266-
bin_pkgs: list[BinaryPackage] = list()
266+
bin_pkgs: list[BinaryPackage] = []
267267

268268
for block in deb822.DebControl.iter_paragraphs(
269269
(rules_file.package_dir / "debian/control").open(),

src/debmagic/_preset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def _get_member(obj: T | type[T], stage: BuildStage) -> BuildStep | _PresetBuild
9696

9797

9898
def as_presets(preset_elements: PresetsT) -> list[Preset]:
99-
presets: list[Preset] = list()
99+
presets: list[Preset] = []
100100

101101
if isinstance(preset_elements, list):
102102
for preset_module in preset_elements:

src/debmagic/_rules_file.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def find_rules_file() -> RulesFile:
1313
for frame in inspect.stack():
1414
file_path = Path(frame.filename)
1515
# TODO further validation, be in debian/
16-
if file_path.name == "rules" or file_path.name == "rules.py":
16+
if file_path.name in {"rules", "rules.py"}:
1717
return RulesFile(
1818
package_dir=file_path.parent.parent.resolve(),
1919
local_vars=frame.frame.f_locals,

0 commit comments

Comments
 (0)