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
# 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
with umask(~0o644):
(state.root / "usr/lib/systemd/system/ssh.socket").write_text(
- dedent(
+ textwrap.dedent(
"""\
[Unit]
Description=Mkosi SSH Server VSock Socket
)
(state.root / "usr/lib/systemd/system/ssh@.service").write_text(
- dedent(
+ textwrap.dedent(
"""\
[Unit]
Description=Mkosi SSH Server
if add:
(definitions / "00-esp.conf").write_text(
- dedent(
+ textwrap.dedent(
"""\
[Partition]
Type=esp
)
(definitions / "10-root.conf").write_text(
- dedent(
+ textwrap.dedent(
f"""\
[Partition]
Type=root
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.
# 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
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)}
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
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())
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
"-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
"--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())
# 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
# 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
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
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, ...]]:
with cd_temp_dir():
parent = Path("mkosi.conf")
parent.write_text(
- dedent(
+ textwrap.dedent(
f"""\
[Distribution]
Distribution={dist1}
child1 = Path("mkosi.conf.d/child1.conf")
child1.write_text(
- dedent(
+ textwrap.dedent(
f"""\
[Match]
Distribution={dist1}
)
child2 = Path("mkosi.conf.d/child2.conf")
child2.write_text(
- dedent(
+ textwrap.dedent(
f"""\
[Match]
Distribution={dist2}
)
child3 = Path("mkosi.conf.d/child3.conf")
child3.write_text(
- dedent(
+ textwrap.dedent(
f"""\
[Match]
Distribution=|{dist1}
with cd_temp_dir():
parent = Path("mkosi.conf")
parent.write_text(
- dedent(
+ textwrap.dedent(
f"""\
[Distribution]
Distribution=fedora
child1 = Path("mkosi.conf.d/child1.conf")
child1.write_text(
- dedent(
+ textwrap.dedent(
f"""\
[Match]
Release={release1}
)
child2 = Path("mkosi.conf.d/child2.conf")
child2.write_text(
- dedent(
+ textwrap.dedent(
f"""\
[Match]
Release={release2}
)
child3 = Path("mkosi.conf.d/child3.conf")
child3.write_text(
- dedent(
+ textwrap.dedent(
f"""\
[Match]
Release=|{release1}
with cd_temp_dir():
parent = Path("mkosi.conf")
parent.write_text(
- dedent(
+ textwrap.dedent(
f"""\
[Distribution]
Distribution=fedora
child1 = Path("mkosi.conf.d/child1.conf")
child1.write_text(
- dedent(
+ textwrap.dedent(
f"""\
[Match]
ImageId={image1}
)
child2 = Path("mkosi.conf.d/child2.conf")
child2.write_text(
- dedent(
+ textwrap.dedent(
f"""\
[Match]
ImageId={image2}
)
child3 = Path("mkosi.conf.d/child3.conf")
child3.write_text(
- dedent(
+ textwrap.dedent(
f"""\
[Match]
ImageId=|{image1}
)
child4 = Path("mkosi.conf.d/child4.conf")
child4.write_text(
- dedent(
+ textwrap.dedent(
"""\
[Match]
ImageId=image*
with cd_temp_dir():
parent = Path("mkosi.conf")
parent.write_text(
- dedent(
+ textwrap.dedent(
"""\
[Distribution]
ImageId=testimage
Path("mkosi.conf.d").mkdir()
child1 = Path("mkosi.conf.d/child1.conf")
child1.write_text(
- dedent(
+ textwrap.dedent(
f"""\
[Match]
ImageVersion={op}{version}
)
child2 = Path("mkosi.conf.d/child2.conf")
child2.write_text(
- dedent(
+ textwrap.dedent(
f"""\
[Match]
ImageVersion=<200
)
child3 = Path("mkosi.conf.d/child3.conf")
child3.write_text(
- dedent(
+ textwrap.dedent(
f"""\
[Match]
ImageVersion=>9000