-
-
Notifications
You must be signed in to change notification settings - Fork 1
build drivers and self building #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mikonse
wants to merge
8
commits into
main
Choose a base branch
from
milo/build-drivers-and-self-building
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1f0dd25
feat: enable debmagic to build itself via debuild
mikonse c34a9e6
feat: add debmagic cli entrypoint with build drivers
mikonse d13bab0
ci: add stage where debmagic packages itself
mikonse 2b7c4f3
fix: properly handle root requirement for apt build-dep in build drivers
mikonse 16dd7cd
ci: change self-build to use docker driver
mikonse c474de2
feat(module/dh): also allow string as dh_args
mikonse 390ce97
refactor(driver): move build directory to host
mikonse 08ead93
chore: linter / formatter fixes after rebase
mikonse File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| .\" DO NOT MODIFY THIS FILE! It was generated by help2man 1.49.3. | ||
| .TH DEBMAGIC "1" "November 2025" "debmagic 0.1.0" "User Commands" | ||
| .SH NAME | ||
| debmagic \- manual page for debmagic 0.1.0 | ||
| .SH DESCRIPTION | ||
| usage: debmagic [\-h] [\-\-version] {help,version,debuild,build} ... | ||
| .PP | ||
| Debmagic | ||
| .SS "positional arguments:" | ||
| .IP | ||
| {help,version,debuild,build} | ||
| .TP | ||
| help | ||
| Show this help page and exit | ||
| .TP | ||
| version | ||
| Print the version information and exit | ||
| .TP | ||
| debuild | ||
| Simply run debuild in the current working directory | ||
| .TP | ||
| build | ||
| Buidl a debian package with the selected | ||
| containerization driver | ||
| .SS "options:" | ||
| .TP | ||
| \fB\-h\fR, \fB\-\-help\fR | ||
| show this help message and exit | ||
| .TP | ||
| \fB\-\-version\fR | ||
| show program's version number and exit | ||
| .SH "SEE ALSO" | ||
| The full documentation for | ||
| .B debmagic | ||
| is maintained as a Texinfo manual. If the | ||
| .B info | ||
| and | ||
| .B debmagic | ||
| programs are properly installed at your site, the command | ||
| .IP | ||
| .B info debmagic | ||
| .PP | ||
| should give you access to the complete manual. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| from .cli import main | ||
|
|
||
| if __name__ == "__main__": | ||
| main() |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import re | ||
| import shutil | ||
| from pathlib import Path | ||
|
|
||
| from debmagic._build_driver.driver_docker import BuildDriverDocker | ||
| from debmagic._build_driver.driver_lxd import BuildDriverLxd | ||
| from debmagic._build_driver.driver_none import BuildDriverNone | ||
|
|
||
| from .common import BuildConfig, BuildDriver, BuildDriverType | ||
|
|
||
| DEBMAGIC_TEMP_BUILD_PARENT_DIR = Path("/tmp/debmagic") | ||
|
|
||
|
|
||
| def _create_driver(build_driver: BuildDriverType, config: BuildConfig) -> BuildDriver: | ||
| match build_driver: | ||
| case "docker": | ||
| return BuildDriverDocker.create(config=config) | ||
| case "lxd": | ||
| return BuildDriverLxd.create(config=config) | ||
| case "none": | ||
| return BuildDriverNone.create(config=config) | ||
|
|
||
|
|
||
| def _ignore_patterns_from_gitignore(gitignore_path: Path): | ||
| if not gitignore_path.is_file(): | ||
| return None | ||
|
|
||
| contents = gitignore_path.read_text().strip().splitlines() | ||
| relevant_lines = filter(lambda line: not re.match(r"\s*#.*", line) and line.strip(), contents) | ||
| return shutil.ignore_patterns(*relevant_lines) | ||
|
|
||
|
|
||
| def _prepare_build_env(source_dir: Path, output_dir: Path, dry_run: bool) -> BuildConfig: | ||
| package_name = "debmagic" # TODO | ||
| package_version = "0.1.0" # TODO | ||
|
|
||
| package_identifier = f"{package_name}-{package_version}" | ||
| build_root = DEBMAGIC_TEMP_BUILD_PARENT_DIR / package_identifier | ||
| if build_root.exists(): | ||
| shutil.rmtree(build_root) | ||
|
|
||
| config = BuildConfig( | ||
| package_identifier=package_identifier, | ||
| source_dir=source_dir, | ||
| output_dir=output_dir, | ||
| build_root_dir=build_root, | ||
| distro="debian", | ||
| distro_version="trixie", | ||
| dry_run=dry_run, | ||
| sign_package=False, | ||
| ) | ||
|
|
||
| # prepare build environment, create the build directory structure, copy the sources | ||
| config.create_dirs() | ||
| source_ignore_pattern = _ignore_patterns_from_gitignore(source_dir / ".gitignore") | ||
| shutil.copytree(config.source_dir, config.build_source_dir, dirs_exist_ok=True, ignore=source_ignore_pattern) | ||
|
|
||
| return config | ||
|
|
||
|
|
||
| def _copy_file_if_exists(source: Path, glob: str, dest: Path): | ||
| for file in source.glob(glob): | ||
| if file.is_dir(): | ||
| shutil.copytree(file, dest) | ||
| elif file.is_file(): | ||
| shutil.copy(file, dest) | ||
| else: | ||
| raise NotImplementedError("Don't support anything besides files and directories") | ||
|
|
||
|
|
||
| def build(build_driver: BuildDriverType, source_dir: Path, output_dir: Path, dry_run: bool = False): | ||
| config = _prepare_build_env(source_dir=source_dir, output_dir=output_dir, dry_run=dry_run) | ||
|
|
||
| driver = _create_driver(build_driver, config) | ||
| try: | ||
| driver.run_command(["apt-get", "-y", "build-dep", "."], cwd=config.build_source_dir, requires_root=True) | ||
| driver.run_command(["dpkg-buildpackage", "-us", "-uc", "-ui", "-nc", "-b"], cwd=config.build_source_dir) | ||
| if config.sign_package: | ||
| pass | ||
| # SIGN .changes and .dsc files | ||
| # changes = *.changes / *.dsc | ||
| # driver.run_command(["debsign", opts, changes], cwd=config.source_dir) | ||
| # driver.run_command(["debrsign", opts, username, changes], cwd=config.source_dir) | ||
|
|
||
| # TODO: copy packages to output directory | ||
| _copy_file_if_exists(source=config.build_source_dir / "..", glob="*.deb", dest=config.output_dir) | ||
| _copy_file_if_exists(source=config.build_source_dir / "..", glob="*.buildinfo", dest=config.output_dir) | ||
| _copy_file_if_exists(source=config.build_source_dir / "..", glob="*.changes", dest=config.output_dir) | ||
| _copy_file_if_exists(source=config.build_source_dir / "..", glob="*.dsc", dest=config.output_dir) | ||
| except Exception as e: | ||
| print(e) | ||
| print( | ||
| "Something failed during building -" | ||
| " dropping into interactive shell in build environment for easier debugging" | ||
| ) | ||
| driver.drop_into_shell() | ||
| raise e | ||
| finally: | ||
| driver.cleanup() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import abc | ||
| from dataclasses import dataclass | ||
| from pathlib import Path | ||
| from typing import Literal, Self, Sequence | ||
|
|
||
| BuildDriverType = Literal["docker"] | Literal["lxd"] | Literal["none"] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not enum? |
||
| SUPPORTED_BUILD_DRIVERS: list[BuildDriverType] = ["docker", "none"] | ||
|
|
||
|
|
||
| class BuildError(RuntimeError): | ||
| pass | ||
|
|
||
|
|
||
| @dataclass | ||
| class BuildConfig: | ||
| package_identifier: str | ||
| source_dir: Path | ||
| output_dir: Path | ||
| dry_run: bool | ||
| distro_version: str # e.g. trixie | ||
| distro: str # e.g. debian | ||
| sign_package: bool # TODO: figure out if this is the right place | ||
|
|
||
| # build paths | ||
| build_root_dir: Path | ||
|
|
||
| @property | ||
| def build_work_dir(self) -> Path: | ||
| return self.build_root_dir / "work" | ||
|
|
||
| @property | ||
| def build_temp_dir(self) -> Path: | ||
| return self.build_root_dir / "temp" | ||
|
|
||
| @property | ||
| def build_source_dir(self) -> Path: | ||
| return self.build_work_dir / self.package_identifier | ||
|
|
||
| def create_dirs(self): | ||
| self.output_dir.mkdir(exist_ok=True, parents=True) | ||
| self.build_work_dir.mkdir(exist_ok=True, parents=True) | ||
| self.build_temp_dir.mkdir(exist_ok=True, parents=True) | ||
| self.build_source_dir.mkdir(exist_ok=True, parents=True) | ||
|
|
||
|
|
||
| class BuildDriver: | ||
| @classmethod | ||
| @abc.abstractmethod | ||
| def create(cls, config: BuildConfig) -> Self: | ||
| pass | ||
|
|
||
| @abc.abstractmethod | ||
| def run_command(self, cmd: Sequence[str | Path], cwd: Path | None = None, requires_root: bool = False): | ||
| pass | ||
|
|
||
| @abc.abstractmethod | ||
| def cleanup(self): | ||
| pass | ||
|
|
||
| @abc.abstractmethod | ||
| def drop_into_shell(self): | ||
| pass | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a way to get into the build (and stop it at/after/... arbitrary stages) directly would be very handy