From 7d333e95e9b7cee140e3266ae4443d05ae0453b0 Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Mon, 17 Feb 2025 11:10:14 -0800 Subject: [PATCH 1/2] entry: add verbose argument And place logs into files when verbose is not set --- flatpaker/entry.py | 67 +++++++++++++++++++++++++++------------------- flatpaker/util.py | 10 ++++--- 2 files changed, 46 insertions(+), 31 deletions(-) diff --git a/flatpaker/entry.py b/flatpaker/entry.py index 8cc09ec..0585ef9 100644 --- a/flatpaker/entry.py +++ b/flatpaker/entry.py @@ -3,6 +3,7 @@ from __future__ import annotations import argparse +import contextlib import importlib import importlib.resources import pathlib @@ -30,6 +31,7 @@ class BaseArguments(typing.Protocol): export: bool cleanup: bool deltas: bool + verbose: bool class BuildArguments(BaseArguments, typing.Protocol): descriptions: typing.List[str] @@ -55,14 +57,14 @@ def build(args: BaseArguments, description: Description) -> None: flatpaker.util.build_flatpak(args, wd, appid) -def static_deltas(args: BaseArguments) -> None: +def static_deltas(args: BaseArguments, out: None | typing.BinaryIO, err: None | typing.BinaryIO) -> None: if not (args.deltas or args.export): return command = ['flatpak', 'build-update-repo', args.repo, '--generate-static-deltas'] if args.gpg: command.extend(['--gpg-sign', args.gpg]) - subprocess.run(command, check=True) + subprocess.run(command, check=True, stdout=out, stderr=err) def main() -> None: @@ -82,6 +84,7 @@ def main() -> None: parser.add_argument('--install', action='store_true', help="Install for the user (useful for testing)") parser.add_argument('--no-cleanup', action='store_false', dest='cleanup', help="don't delete the temporary directory") parser.add_argument('--static-deltas', action='store_true', dest='deltas', help="generate static deltas when exporting") + parser.add_argument('--verbose', action='store_true', help="Print more information to the terminal") subparsers = parser.add_subparsers() build_parser = subparsers.add_parser('build', help='Build flatpaks from descriptions') @@ -93,36 +96,44 @@ def main() -> None: args = typing.cast('BaseArguments', parser.parse_args()) + flatpaker.util.LOGDIR.mkdir(parents=True, exist_ok=True) + if args.action == 'build': descriptions = typing.cast('BuildArguments', args).descriptions for d in descriptions: description = load_description(d) build(args, description) if args.deltas: - static_deltas(args) + with contextlib.ExitStack() as manager: + o = None if args.verbose else manager.enter_context((flatpaker.util.LOGDIR / 'static-deltas.stdout').open('wb')) + e = None if args.verbose else manager.enter_context((flatpaker.util.LOGDIR / 'static-deltas.stderr').open('wb')) + static_deltas(args, o, e) if args.action == 'install-deps': - command = [ - 'flatpak', 'install', '--no-auto-pin', '--user', - f'org.freedesktop.Platform//{flatpaker.util.RUNTIME_VERSION}', - f'org.freedesktop.Sdk//{flatpaker.util.RUNTIME_VERSION}', - ] - subprocess.run(command, check=True) - - sdk_file = importlib.resources.files('flatpaker') / 'data' / 'com.github.dcbaker.flatpaker.Sdk.yml' - platform_file = importlib.resources.files('flatpaker') / 'data' / 'com.github.dcbaker.flatpaker.Platform.yml' - for bfile in [sdk_file, platform_file]: - with importlib.resources.as_file(bfile) as sdk: - build_command: typing.List[str] = [ - 'flatpak-builder', '--force-clean', '--user', 'build', sdk.as_posix()] - - if args.export: - build_command.extend(['--repo', args.repo]) - if args.gpg: - build_command.extend(['--gpg-sign', args.gpg]) - if args.install: - build_command.extend(['--install']) - - subprocess.run(build_command, check=True) - - if args.deltas: - static_deltas(args) + with contextlib.ExitStack() as manager: + o = None if args.verbose else manager.enter_context((flatpaker.util.LOGDIR / 'runtime.stdout').open('wb')) + e = None if args.verbose else manager.enter_context((flatpaker.util.LOGDIR / 'runtime.stderr').open('wb')) + command = [ + 'flatpak', 'install', '--no-auto-pin', '--user', + f'org.freedesktop.Platform//{flatpaker.util.RUNTIME_VERSION}', + f'org.freedesktop.Sdk//{flatpaker.util.RUNTIME_VERSION}', + ] + subprocess.run(command, check=True, stdout=o, stderr=e) + + sdk_file = importlib.resources.files('flatpaker') / 'data' / 'com.github.dcbaker.flatpaker.Sdk.yml' + platform_file = importlib.resources.files('flatpaker') / 'data' / 'com.github.dcbaker.flatpaker.Platform.yml' + for bfile in [sdk_file, platform_file]: + with importlib.resources.as_file(bfile) as sdk: + build_command: typing.List[str] = [ + 'flatpak-builder', '--force-clean', '--user', 'build', sdk.as_posix()] + + if args.export: + build_command.extend(['--repo', args.repo]) + if args.gpg: + build_command.extend(['--gpg-sign', args.gpg]) + if args.install: + build_command.extend(['--install']) + + subprocess.run(build_command, check=True, stdout=o, stderr=e) + + if args.deltas: + static_deltas(args, o, e) diff --git a/flatpaker/util.py b/flatpaker/util.py index 03d74ab..b92029f 100644 --- a/flatpaker/util.py +++ b/flatpaker/util.py @@ -1,5 +1,5 @@ # SPDX-License-Identifier: MIT -# Copyright © 2022-2024 Dylan Baker +# Copyright © 2022-2025 Dylan Baker from __future__ import annotations from xml.etree import ElementTree as ET @@ -18,7 +18,7 @@ from .entry import BaseArguments RUNTIME_VERSION = "24.08" - +LOGDIR = pathlib.Path('.flatpaker') / 'logs' def _subelem(elem: ET.Element, tag: str, text: typing.Optional[str] = None, **extra: str) -> ET.Element: new = ET.SubElement(elem, tag, extra) @@ -150,7 +150,11 @@ def build_flatpak(args: BaseArguments, workdir: pathlib.Path, appid: str) -> Non if args.install: build_command.extend(['--install']) - subprocess.run(build_command, check=True) + with contextlib.ExitStack() as manager: + o = None if args.verbose else manager.enter_context((LOGDIR / f'{appid}.stdout').open('wb')) + e = None if args.verbose else manager.enter_context((LOGDIR / f'{appid}.stderr').open('wb')) + subprocess.run(build_command, check=True, stdout=o, stderr=e) + if args.cleanup: shutil.rmtree('build', ignore_errors=True) From 573572d5bc0157838d2b54b0402577d30daa717f Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Mon, 17 Feb 2025 11:22:17 -0800 Subject: [PATCH 2/2] Print basic progress message when not verbose --- flatpaker/entry.py | 16 +++++++++++++++- flatpaker/util.py | 6 +++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/flatpaker/entry.py b/flatpaker/entry.py index 0585ef9..ed61676 100644 --- a/flatpaker/entry.py +++ b/flatpaker/entry.py @@ -32,6 +32,7 @@ class BaseArguments(typing.Protocol): cleanup: bool deltas: bool verbose: bool + keep_going: bool class BuildArguments(BaseArguments, typing.Protocol): descriptions: typing.List[str] @@ -47,6 +48,9 @@ def build(args: BaseArguments, description: Description) -> None: # TODO: This could be common appid = f"{description['common']['reverse_url']}.{flatpaker.util.sanitize_name(description['common']['name'])}" + if not args.verbose: + print('Building', appid, end=' ', flush=True) + write_build_rules = select_impl(description['common']['engine']) with flatpaker.util.tmpdir(description['common']['name'], args.cleanup) as d: @@ -85,6 +89,7 @@ def main() -> None: parser.add_argument('--no-cleanup', action='store_false', dest='cleanup', help="don't delete the temporary directory") parser.add_argument('--static-deltas', action='store_true', dest='deltas', help="generate static deltas when exporting") parser.add_argument('--verbose', action='store_true', help="Print more information to the terminal") + parser.add_argument('--keep-going', action='store_true', help="If one flatpak fails to build, continue to the next.") subparsers = parser.add_subparsers() build_parser = subparsers.add_parser('build', help='Build flatpaks from descriptions') @@ -104,6 +109,8 @@ def main() -> None: description = load_description(d) build(args, description) if args.deltas: + if not args.verbose: + print('Generating static deltas', flush=True) with contextlib.ExitStack() as manager: o = None if args.verbose else manager.enter_context((flatpaker.util.LOGDIR / 'static-deltas.stdout').open('wb')) e = None if args.verbose else manager.enter_context((flatpaker.util.LOGDIR / 'static-deltas.stderr').open('wb')) @@ -123,6 +130,9 @@ def main() -> None: platform_file = importlib.resources.files('flatpaker') / 'data' / 'com.github.dcbaker.flatpaker.Platform.yml' for bfile in [sdk_file, platform_file]: with importlib.resources.as_file(bfile) as sdk: + if not args.verbose: + print('Building:', sdk.name, end=' ', flush=True) + build_command: typing.List[str] = [ 'flatpak-builder', '--force-clean', '--user', 'build', sdk.as_posix()] @@ -133,7 +143,11 @@ def main() -> None: if args.install: build_command.extend(['--install']) - subprocess.run(build_command, check=True, stdout=o, stderr=e) + p = subprocess.run(build_command, stdout=o, stderr=e) + if not args.verbose: + print('Success' if p.returncode == 0 else 'Fail', flush=True) + if p.returncode != 0 and not args.keep_going: + p.check_returncode() if args.deltas: static_deltas(args, o, e) diff --git a/flatpaker/util.py b/flatpaker/util.py index b92029f..0c55a23 100644 --- a/flatpaker/util.py +++ b/flatpaker/util.py @@ -153,7 +153,11 @@ def build_flatpak(args: BaseArguments, workdir: pathlib.Path, appid: str) -> Non with contextlib.ExitStack() as manager: o = None if args.verbose else manager.enter_context((LOGDIR / f'{appid}.stdout').open('wb')) e = None if args.verbose else manager.enter_context((LOGDIR / f'{appid}.stderr').open('wb')) - subprocess.run(build_command, check=True, stdout=o, stderr=e) + p = subprocess.run(build_command, stdout=o, stderr=e) + if not args.verbose: + print('Success' if p.returncode == 0 else 'Fail', flush=True) + if p.returncode != 0 and not args.keep_going: + p.check_returncode() if args.cleanup: shutil.rmtree('build', ignore_errors=True)