]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Import full modules instead of individual members
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Sun, 13 Aug 2023 18:30:03 +0000 (20:30 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 14 Aug 2023 09:21:11 +0000 (11:21 +0200)
Let's lean more towards importing full modules except for a few
exceptions like typing and Path from pathlib.

mkosi/__init__.py
mkosi/manifest.py
tests/test_parse_load_args.py

index 2a655de88b5fac709693952af96fff5957ed0b92..9bffb91923b9835bcebb9c21b60b0c1332368dc5 100644 (file)
@@ -15,10 +15,10 @@ import shutil
 import subprocess
 import sys
 import tempfile
+import textwrap
 import uuid
 from collections.abc import Iterator, Sequence
 from pathlib import Path
-from textwrap import dedent
 from typing import Any, ContextManager, Mapping, Optional, TextIO, Union
 
 from mkosi.archive import extract_tar, make_cpio, make_tar
@@ -839,7 +839,7 @@ def install_unified_kernel(state: MkosiState, partitions: Sequence[Partition]) -
                 # to make sure we load pefile from the tools tree if one is used.
 
                 # TODO: Use ignore_padding=True instead of length once we can depend on a newer pefile.
-                pefile = dedent(
+                pefile = textwrap.dedent(
                     f"""\
                     import pefile
                     from pathlib import Path
@@ -1091,7 +1091,7 @@ def configure_ssh(state: MkosiState) -> None:
 
     with umask(~0o644):
         (state.root / "usr/lib/systemd/system/ssh.socket").write_text(
-            dedent(
+            textwrap.dedent(
                 """\
                 [Unit]
                 Description=Mkosi SSH Server VSock Socket
@@ -1109,7 +1109,7 @@ def configure_ssh(state: MkosiState) -> None:
         )
 
         (state.root / "usr/lib/systemd/system/ssh@.service").write_text(
-            dedent(
+            textwrap.dedent(
                 """\
                 [Unit]
                 Description=Mkosi SSH Server
@@ -1360,7 +1360,7 @@ def make_image(state: MkosiState, skip: Sequence[str] = [], split: bool = False)
 
             if add:
                 (definitions / "00-esp.conf").write_text(
-                    dedent(
+                    textwrap.dedent(
                         """\
                         [Partition]
                         Type=esp
@@ -1373,7 +1373,7 @@ def make_image(state: MkosiState, skip: Sequence[str] = [], split: bool = False)
                 )
 
             (definitions / "10-root.conf").write_text(
-                dedent(
+                textwrap.dedent(
                     f"""\
                     [Partition]
                     Type=root
@@ -1670,7 +1670,7 @@ def generate_key_cert_pair(args: MkosiArgs) -> None:
 
     log_step(f"Generating keys rsa:{keylength} for CN {cn!r}.")
     logging.info(
-        dedent(
+        textwrap.dedent(
             f"""
             The keys will expire in {args.genkey_valid_days} days ({expiration_date:%A %d. %B %Y}).
             Remember to roll them over to new ones before then.
index d81c5f098cdf3100a3860fcaf71abe472be7aa41..d288e4bb7d40dcad8382a949c0108b3ac54d413d 100644 (file)
@@ -1,11 +1,11 @@
 # SPDX-License-Identifier: LGPL-2.1+
 
 import dataclasses
+import datetime
 import json
-from datetime import datetime
+import subprocess
+import textwrap
 from pathlib import Path
-from subprocess import DEVNULL, PIPE
-from textwrap import dedent
 from typing import IO, Any, Optional
 
 from mkosi.config import ManifestFormat, MkosiConfig
@@ -48,7 +48,7 @@ class SourcePackageManifest:
     def report(self) -> str:
         size = sum(p.size for p in self.packages)
 
-        t = dedent(
+        t = textwrap.dedent(
             f"""\
             SourcePackage: {self.name}
             Packages:      {" ".join(p.name for p in self.packages)}
@@ -83,7 +83,7 @@ class Manifest:
     packages: list[PackageManifest] = dataclasses.field(default_factory=list)
     source_packages: dict[str, SourcePackageManifest] = dataclasses.field(default_factory=dict)
 
-    _init_timestamp: datetime = dataclasses.field(init=False, default_factory=datetime.now)
+    _init_timestamp: datetime.datetime = dataclasses.field(init=False, default_factory=datetime.datetime.now)
 
     def need_source_info(self) -> bool:
         return ManifestFormat.changelog in self.config.manifest_format
@@ -110,7 +110,7 @@ class Manifest:
                  f"--dbpath={dbpath}",
                  "-qa",
                  "--qf", r"%{NEVRA}\t%{SOURCERPM}\t%{NAME}\t%{ARCH}\t%{LONGSIZE}\t%{INSTALLTIME}\n"],
-                stdout=PIPE)
+                stdout=subprocess.PIPE)
 
         packages = sorted(c.stdout.splitlines())
 
@@ -129,7 +129,7 @@ class Manifest:
                 arch = ""
 
             size = int(size)
-            installtime = datetime.fromtimestamp(int(installtime))
+            installtime = datetime.datetime.fromtimestamp(int(installtime))
 
             # If we are creating a layer based on a BaseImage=, e.g. a sysext, filter by
             # packages that were installed in this execution of mkosi. We assume that the
@@ -151,8 +151,8 @@ class Manifest:
                          "-q",
                          "--changelog",
                          nevra],
-                        stdout=PIPE,
-                        stderr=DEVNULL)
+                        stdout=subprocess.PIPE,
+                        stderr=subprocess.DEVNULL)
                 changelog = c.stdout.strip()
                 source = SourcePackageManifest(srpm, changelog)
                 self.source_packages[srpm] = source
@@ -165,7 +165,7 @@ class Manifest:
                  "--show",
                  "--showformat",
                      r'${Package}\t${source:Package}\t${Version}\t${Architecture}\t${Installed-Size}\t${db-fsys:Last-Modified}\n'],
-                 stdout=PIPE)
+                 stdout=subprocess.PIPE)
 
         packages = sorted(c.stdout.splitlines())
 
@@ -177,7 +177,7 @@ class Manifest:
             # the manifest for sysext when building on very old distributions by setting the
             # timestamp to epoch. This only affects Ubuntu Bionic which is nearing EOL.
             size = int(size) * 1024 if size else 0
-            installtime = datetime.fromtimestamp(int(installtime) if installtime else 0)
+            installtime = datetime.datetime.fromtimestamp(int(installtime) if installtime else 0)
 
             # If we are creating a layer based on a BaseImage=, e.g. a sysext, filter by
             # packages that were installed in this execution of mkosi. We assume that the
@@ -224,7 +224,7 @@ class Manifest:
                 # We have to run from the root, because if we use the RootDir option to make
                 # apt from the host look at the repositories in the image, it will also pick
                 # the 'methods' executables from there, but the ABI might not be compatible.
-                result = run(cmd, stdout=PIPE)
+                result = run(cmd, stdout=subprocess.PIPE)
                 source_package = SourcePackageManifest(source, result.stdout.strip())
                 self.source_packages[source] = source_package
 
index 118a18456605caa380e97867c180175edd47d0d8..2ac275215a1af28f50152e6ec6b31d480812d1a5 100644 (file)
@@ -4,10 +4,10 @@ import argparse
 import itertools
 import operator
 import tempfile
-from contextlib import contextmanager
-from os import chdir, getcwd
+import contextlib
+import os
 from pathlib import Path
-from textwrap import dedent
+import textwrap
 from typing import Iterator, List, Optional
 
 import pytest
@@ -16,16 +16,16 @@ from mkosi.config import Compression, MkosiArgs, MkosiConfig, MkosiConfigParser,
 from mkosi.distributions import Distribution
 
 
-@contextmanager
+@contextlib.contextmanager
 def cd_temp_dir() -> Iterator[None]:
-    old_dir = getcwd()
+    old_dir = os.getcwd()
 
     with tempfile.TemporaryDirectory() as tmp_dir:
-        chdir(tmp_dir)
+        os.chdir(tmp_dir)
         try:
             yield
         finally:
-            chdir(old_dir)
+            os.chdir(old_dir)
 
 
 def parse(argv: Optional[List[str]] = None) -> tuple[MkosiArgs, tuple[MkosiConfig, ...]]:
@@ -86,7 +86,7 @@ def test_match_distribution(dist1: Distribution, dist2: Distribution) -> None:
     with cd_temp_dir():
         parent = Path("mkosi.conf")
         parent.write_text(
-            dedent(
+            textwrap.dedent(
                 f"""\
                 [Distribution]
                 Distribution={dist1}
@@ -98,7 +98,7 @@ def test_match_distribution(dist1: Distribution, dist2: Distribution) -> None:
 
         child1 = Path("mkosi.conf.d/child1.conf")
         child1.write_text(
-            dedent(
+            textwrap.dedent(
                 f"""\
                 [Match]
                 Distribution={dist1}
@@ -110,7 +110,7 @@ def test_match_distribution(dist1: Distribution, dist2: Distribution) -> None:
         )
         child2 = Path("mkosi.conf.d/child2.conf")
         child2.write_text(
-            dedent(
+            textwrap.dedent(
                 f"""\
                 [Match]
                 Distribution={dist2}
@@ -122,7 +122,7 @@ def test_match_distribution(dist1: Distribution, dist2: Distribution) -> None:
         )
         child3 = Path("mkosi.conf.d/child3.conf")
         child3.write_text(
-            dedent(
+            textwrap.dedent(
                 f"""\
                 [Match]
                 Distribution=|{dist1}
@@ -150,7 +150,7 @@ def test_match_release(release1: int, release2: int) -> None:
     with cd_temp_dir():
         parent = Path("mkosi.conf")
         parent.write_text(
-            dedent(
+            textwrap.dedent(
                 f"""\
                 [Distribution]
                 Distribution=fedora
@@ -163,7 +163,7 @@ def test_match_release(release1: int, release2: int) -> None:
 
         child1 = Path("mkosi.conf.d/child1.conf")
         child1.write_text(
-            dedent(
+            textwrap.dedent(
                 f"""\
                 [Match]
                 Release={release1}
@@ -175,7 +175,7 @@ def test_match_release(release1: int, release2: int) -> None:
         )
         child2 = Path("mkosi.conf.d/child2.conf")
         child2.write_text(
-            dedent(
+            textwrap.dedent(
                 f"""\
                 [Match]
                 Release={release2}
@@ -187,7 +187,7 @@ def test_match_release(release1: int, release2: int) -> None:
         )
         child3 = Path("mkosi.conf.d/child3.conf")
         child3.write_text(
-            dedent(
+            textwrap.dedent(
                 f"""\
                 [Match]
                 Release=|{release1}
@@ -217,7 +217,7 @@ def test_match_imageid(image1: str, image2: str) -> None:
     with cd_temp_dir():
         parent = Path("mkosi.conf")
         parent.write_text(
-            dedent(
+            textwrap.dedent(
                 f"""\
                 [Distribution]
                 Distribution=fedora
@@ -230,7 +230,7 @@ def test_match_imageid(image1: str, image2: str) -> None:
 
         child1 = Path("mkosi.conf.d/child1.conf")
         child1.write_text(
-            dedent(
+            textwrap.dedent(
                 f"""\
                 [Match]
                 ImageId={image1}
@@ -242,7 +242,7 @@ def test_match_imageid(image1: str, image2: str) -> None:
         )
         child2 = Path("mkosi.conf.d/child2.conf")
         child2.write_text(
-            dedent(
+            textwrap.dedent(
                 f"""\
                 [Match]
                 ImageId={image2}
@@ -254,7 +254,7 @@ def test_match_imageid(image1: str, image2: str) -> None:
         )
         child3 = Path("mkosi.conf.d/child3.conf")
         child3.write_text(
-            dedent(
+            textwrap.dedent(
                 f"""\
                 [Match]
                 ImageId=|{image1}
@@ -267,7 +267,7 @@ def test_match_imageid(image1: str, image2: str) -> None:
         )
         child4 = Path("mkosi.conf.d/child4.conf")
         child4.write_text(
-            dedent(
+            textwrap.dedent(
                 """\
                 [Match]
                 ImageId=image*
@@ -307,7 +307,7 @@ def test_match_imageversion(op: str, version: str) -> None:
     with cd_temp_dir():
         parent = Path("mkosi.conf")
         parent.write_text(
-            dedent(
+            textwrap.dedent(
                 """\
                 [Distribution]
                 ImageId=testimage
@@ -319,7 +319,7 @@ def test_match_imageversion(op: str, version: str) -> None:
         Path("mkosi.conf.d").mkdir()
         child1 = Path("mkosi.conf.d/child1.conf")
         child1.write_text(
-            dedent(
+            textwrap.dedent(
                 f"""\
                 [Match]
                 ImageVersion={op}{version}
@@ -331,7 +331,7 @@ def test_match_imageversion(op: str, version: str) -> None:
         )
         child2 = Path("mkosi.conf.d/child2.conf")
         child2.write_text(
-            dedent(
+            textwrap.dedent(
                 f"""\
                 [Match]
                 ImageVersion=<200
@@ -344,7 +344,7 @@ def test_match_imageversion(op: str, version: str) -> None:
         )
         child3 = Path("mkosi.conf.d/child3.conf")
         child3.write_text(
-            dedent(
+            textwrap.dedent(
                 f"""\
                 [Match]
                 ImageVersion=>9000