Skip to content

Commit

Permalink
Make mkosi a python module and generate script via entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
behrmann committed Aug 3, 2020
1 parent 9a264fc commit 3cf93d1
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 53 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,15 @@ 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 }}

- name: Build ${{ matrix.distro }}/${{ matrix.format }} UEFI
if: matrix.format != 'directory' && matrix.format != 'tar' && matrix.format != 'plain_squashfs' &&
(matrix.distro != 'clear' || matrix.format != 'gpt_squashfs')
run: sudo ./mkosi
run: sudo python3 -m mkosi
--debug run
--distribution ${{ matrix.distro }}
--format ${{ matrix.format }}
Expand All @@ -259,7 +259,7 @@ jobs:

- name: Build ${{ matrix.distro }}/${{ matrix.format }} BIOS
if: matrix.format != 'directory' && matrix.format != 'tar' && !contains(matrix.format, 'squashfs')
run: sudo ./mkosi
run: sudo python3 -m mkosi
--debug run
--distribution ${{ matrix.distro }}
--format ${{ matrix.format }}
Expand Down
1 change: 0 additions & 1 deletion mkosi.py

This file was deleted.

28 changes: 0 additions & 28 deletions mkosi → mkosi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK
# SPDX-License-Identifier: LGPL-2.1+

import argparse
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -5278,25 +5272,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()
23 changes: 23 additions & 0 deletions mkosi/__main__.py
Original file line number Diff line number Diff line change
@@ -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)
11 changes: 4 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/python3
# SPDX-License-Identifier: LGPL-2.1+

import sys

from setuptools import setup, Command

class BuildManpage(Command):
Expand All @@ -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",
Expand All @@ -29,6 +24,8 @@ def run(self):
maintainer="mkosi contributors",
maintainer_email="systemd-devel@lists.freedesktop.org",
license="LGPLv2+",
scripts=["mkosi"],
cmdclass = { "man": BuildManpage }
python_requires=">=3.6",
packages = ["mkosi"],
cmdclass = { "man": BuildManpage },
entry_points = {"console_scripts": ["mkosi=mkosi.__main__"]},
)
7 changes: 1 addition & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 4 additions & 8 deletions tests/test_config_parser.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down

0 comments on commit 3cf93d1

Please sign in to comment.