From 38d08270c67e310fd5625f3255ce65b9873ac8f8 Mon Sep 17 00:00:00 2001 From: Joerg Behrmann Date: Thu, 30 Jul 2020 20:45:06 +0200 Subject: [PATCH] Make mkosi a python module and generate script via entrypoint --- .github/workflows/ci.yml | 2 +- mkosi.py | 1 - mkosi => mkosi/__init__.py | 28 ---------------------------- mkosi/__main__.py | 23 +++++++++++++++++++++++ setup.py | 12 ++++++------ tests/conftest.py | 7 +------ tests/test_config_parser.py | 12 ++++-------- 7 files changed, 35 insertions(+), 50 deletions(-) delete mode 120000 mkosi.py rename mkosi => mkosi/__init__.py (99%) create mode 100644 mkosi/__main__.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 102ba425a..819d3115b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -232,7 +232,7 @@ jobs: run: sudo apt-get --assume-yes --no-install-recommends install zypper - name: Build ${{ matrix.distro }}/${{ matrix.format }} - run: sudo ./mkosi + run: sudo python3 -m mkosi --debug run --distribution ${{ matrix.distro }} --format ${{ matrix.format }} diff --git a/mkosi.py b/mkosi.py deleted file mode 120000 index b5f44fa8e..000000000 --- a/mkosi.py +++ /dev/null @@ -1 +0,0 @@ -mkosi \ No newline at end of file diff --git a/mkosi b/mkosi/__init__.py similarity index 99% rename from mkosi rename to mkosi/__init__.py index 4550c7215..82f7aa180 100755 --- a/mkosi +++ b/mkosi/__init__.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python3 -# PYTHON_ARGCOMPLETE_OK # SPDX-License-Identifier: LGPL-2.1+ import argparse @@ -58,10 +56,6 @@ __version__ = '5' -if sys.version_info < (3, 6): - sys.exit("Sorry, we need at least Python 3.6.") - - # These types are only generic during type checking and not at runtime, leading # to a TypeError during compilation. # Let's be as strict as we can with the description for the usage we have. @@ -5261,25 +5255,3 @@ def run_verb(args: CommandLineArguments) -> None: if args.verb == "qemu": run_qemu(args) - - -def main() -> None: - try: - args = parse_args() - - for job_name, a in args.items(): - # Change working directory if --directory is passed - if a.directory: - work_dir = a.directory - if os.path.isdir(work_dir): - os.chdir(work_dir) - else: - die("Error: %s is not a directory!" % work_dir) - with complete_step('Processing ' + job_name): - run_verb(a) - except MkosiException: - sys.exit(1) - - -if __name__ == "__main__": - main() diff --git a/mkosi/__main__.py b/mkosi/__main__.py new file mode 100644 index 000000000..0ba6a4785 --- /dev/null +++ b/mkosi/__main__.py @@ -0,0 +1,23 @@ +# SPDX-License-Identifier: LGPL-2.1+ +# PYTHON_ARGCOMPLETE_OK +import os +import sys + +from . import parse_args, complete_step, run_verb, die, MkosiException + + +try: + args = parse_args() + + for job_name, a in args.items(): + # Change working directory if --directory is passed + if a.directory: + work_dir = a.directory + if os.path.isdir(work_dir): + os.chdir(work_dir) + else: + die(f"Error: {work_dir} is not a directory!") + with complete_step(f"Processing {job_name}"): + run_verb(a) +except MkosiException: + sys.exit(1) diff --git a/setup.py b/setup.py index 813921e22..93e23928a 100755 --- a/setup.py +++ b/setup.py @@ -1,8 +1,6 @@ #!/usr/bin/python3 # SPDX-License-Identifier: LGPL-2.1+ -import sys - from setuptools import setup, Command class BuildManpage(Command): @@ -17,9 +15,6 @@ def finalize_options(self): def run(self): self.spawn(['pandoc', '-t', 'man', '-o', 'mkosi.1', 'mkosi.md']) -if sys.version_info < (3, 6): - sys.exit("Sorry, we need at least Python 3.6.") - setup( name="mkosi", @@ -29,6 +24,11 @@ def run(self): maintainer="mkosi contributors", maintainer_email="systemd-devel@lists.freedesktop.org", license="LGPLv2+", + python_requires=">=3.6", scripts=["mkosi"], - cmdclass = { "man": BuildManpage } + cmdclass = { "man": BuildManpage }, + entry_point = """ + [console_scripts] + mkosi:__main__ + """ ) diff --git a/tests/conftest.py b/tests/conftest.py index 5a00d434b..938fcbcd1 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,13 +2,8 @@ import sys import os -dir_path = os.path.dirname(os.path.realpath(__file__)) -import importlib.util -spec = importlib.util.spec_from_file_location('mkosi', os.path.join(dir_path, '../mkosi.py')) -mkosi_module = importlib.util.module_from_spec(spec) -spec.loader.exec_module(mkosi_module) -sys.modules['mkosi'] = mkosi_module +import mkosi from tests.test_config_parser import MkosiConfig diff --git a/tests/test_config_parser.py b/tests/test_config_parser.py index a85651577..8c2fc3b7a 100644 --- a/tests/test_config_parser.py +++ b/tests/test_config_parser.py @@ -1,16 +1,12 @@ # SPDX-License-Identifier: LGPL-2.1+ +import configparser +import copy import os -dir_path = os.path.dirname(os.path.realpath(__file__)) - -import importlib.util -spec = importlib.util.spec_from_file_location('mkosi', os.path.join(dir_path, '../mkosi.py')) -mkosi = importlib.util.module_from_spec(spec) -spec.loader.exec_module(mkosi) import pytest -import configparser -import copy + +import mkosi class ChangeCwd(object):