From: Jörg Behrmann Date: Thu, 5 Sep 2024 14:22:45 +0000 (+0200) Subject: Move show_docs to its own module X-Git-Tag: v25~311^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e2238f5dc7515efbddf6bab8d310948684943ebc;p=thirdparty%2Fmkosi.git Move show_docs to its own module --- diff --git a/mkosi/__init__.py b/mkosi/__init__.py index 9064ccf4f..a347e9cf4 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -55,7 +55,6 @@ from mkosi.config import ( Compression, Config, ConfigFeature, - DocFormat, JsonEncoder, KeySourceType, ManifestFormat, @@ -76,6 +75,7 @@ from mkosi.config import ( ) from mkosi.context import Context from mkosi.distributions import Distribution +from mkosi.documentation import show_docs from mkosi.installer import clean_package_manager_metadata from mkosi.kmod import gen_required_kernel_modules, loaded_modules, process_kernel_modules from mkosi.log import ARG_DEBUG, complete_step, die, log_notice, log_step @@ -90,7 +90,6 @@ from mkosi.run import ( chroot_options, finalize_interpreter, finalize_passwd_mounts, - find_binary, fork_and_wait, run, ) @@ -3586,47 +3585,6 @@ def bump_image_version() -> None: version_file.write_text(f"{new_version}\n") -def show_docs(args: Args, *, resources: Path) -> None: - if args.doc_format == DocFormat.auto: - formats = [DocFormat.man, DocFormat.pandoc, DocFormat.markdown, DocFormat.system] - else: - formats = [args.doc_format] - - manual = args.cmdline[0] if args.cmdline else "mkosi" - - while formats: - form = formats.pop(0) - try: - if form == DocFormat.man: - man = resources / f"man/{manual}.1" - if not man.exists(): - raise FileNotFoundError() - run(["man", "--local-file", man]) - return - elif form == DocFormat.pandoc: - if not find_binary("pandoc"): - logging.error("pandoc is not available") - pandoc = run( - ["pandoc", "-t", "man", "-s", resources / f"man/{manual}.md"], - stdout=subprocess.PIPE, - stderr=subprocess.DEVNULL, - log=False, - ) - run(["man", "--local-file", "-"], input=pandoc.stdout) - return - elif form == DocFormat.markdown: - page((resources / f"man/{manual}.md").read_text(), args.pager) - return - elif form == DocFormat.system: - run(["man", manual], log=False) - return - except (FileNotFoundError, subprocess.CalledProcessError) as e: - if not formats: - if isinstance(e, FileNotFoundError): - die("The mkosi package does not contain the man page {manual:r}.") - raise e - - def expand_specifier(s: str) -> str: return s.replace("%u", INVOKING_USER.name()) diff --git a/mkosi/documentation.py b/mkosi/documentation.py new file mode 100644 index 000000000..e081d036b --- /dev/null +++ b/mkosi/documentation.py @@ -0,0 +1,51 @@ +# SPDX-License-Identifier: LGPL-2.1-or-later + +import logging +import subprocess +from pathlib import Path + +from mkosi.config import Args, DocFormat +from mkosi.log import die +from mkosi.pager import page +from mkosi.run import find_binary, run + + +def show_docs(args: Args, *, resources: Path) -> None: + if args.doc_format == DocFormat.auto: + formats = [DocFormat.man, DocFormat.pandoc, DocFormat.markdown, DocFormat.system] + else: + formats = [args.doc_format] + + manual = args.cmdline[0] if args.cmdline else "mkosi" + + while formats: + form = formats.pop(0) + try: + if form == DocFormat.man: + man = resources / f"man/{manual}.1" + if not man.exists(): + raise FileNotFoundError() + run(["man", "--local-file", man]) + return + elif form == DocFormat.pandoc: + if not find_binary("pandoc"): + logging.error("pandoc is not available") + pandoc = run( + ["pandoc", "-t", "man", "-s", resources / f"man/{manual}.md"], + stdout=subprocess.PIPE, + stderr=subprocess.DEVNULL, + log=False, + ) + run(["man", "--local-file", "-"], input=pandoc.stdout) + return + elif form == DocFormat.markdown: + page((resources / f"man/{manual}.md").read_text(), args.pager) + return + elif form == DocFormat.system: + run(["man", manual], log=False) + return + except (FileNotFoundError, subprocess.CalledProcessError) as e: + if not formats: + if isinstance(e, FileNotFoundError): + die("The mkosi package does not contain the man page {manual:r}.") + raise e