diff options
author | Helmut Grohne <helmut@subdivi.de> | 2021-07-08 09:11:10 +0200 |
---|---|---|
committer | Helmut Grohne <helmut@subdivi.de> | 2021-07-08 09:11:10 +0200 |
commit | d18841b1c6729cfca1da0f0c91900a08c396a4f1 (patch) | |
tree | 316c578b53e8f72d088db251f6d1b7087f581d0b | |
parent | 1ae474fa8b329ea6fc422bbab593e04224290cea (diff) | |
download | mdbp-d18841b1c6729cfca1da0f0c91900a08c396a4f1.tar.gz |
uninline helper function temporary_static_file
-rw-r--r-- | mdbp/common.py | 8 | ||||
-rw-r--r-- | mdbp/pbuilder.py | 12 | ||||
-rw-r--r-- | mdbp/sbuild.py | 9 |
3 files changed, 18 insertions, 11 deletions
diff --git a/mdbp/common.py b/mdbp/common.py index 5c67f58..e4f4792 100644 --- a/mdbp/common.py +++ b/mdbp/common.py @@ -198,6 +198,14 @@ def clean_dir(directory: pathlib.Path, patterns: typing.List[str]) -> None: for pattern in patterns): entry.unlink() +@contextlib.contextmanager +def temporary_static_file(content: str) -> typing.Iterator[pathlib.Path]: + """Create a named temporary file with given content and return its path.""" + with tempfile.NamedTemporaryFile("w") as tmpf: + tmpf.write(content) + tmpf.flush() + yield pathlib.Path(tmpf.name) + class AddSpaceSeparatedValues(argparse.Action): """The action extends the destination array with the space-sparated parts of the passed value.""" diff --git a/mdbp/pbuilder.py b/mdbp/pbuilder.py index b42e2c7..f1c48e3 100644 --- a/mdbp/pbuilder.py +++ b/mdbp/pbuilder.py @@ -12,7 +12,8 @@ import sys import tempfile from .common import AddSpaceSeparatedValues, buildjson, clean_dir, \ - compute_env, get_dsc, make_option, profile_option + compute_env, get_dsc, make_option, profile_option, \ + temporary_static_file def main() -> None: """Entry point for mdbp-pbuilder backend""" @@ -80,10 +81,11 @@ def main() -> None: apt_get = ["apt-get", "-oAPT::Keep-Downloaded-Path=false", "--yes"] with contextlib.ExitStack() as stack: if build.get("build_path"): - rcfile = stack.enter_context(tempfile.NamedTemporaryFile("w")) - rcfile.write("BUILDDIR=%s\n" % shlex.quote(build["build_path"])) - rcfile.flush() - cmd.extend(["--configfile", rcfile.name]) + pbuilderrc = "BUILDDIR=%s\n" % shlex.quote(build["build_path"]) + cmd.extend([ + "--configfile", + str(stack.enter_context(temporary_static_file(pbuilderrc))), + ]) hookdir = pathlib.Path( stack.enter_context(tempfile.TemporaryDirectory())) hook = hookdir / "D50aptupdate" diff --git a/mdbp/sbuild.py b/mdbp/sbuild.py index 2841713..da0ac4b 100644 --- a/mdbp/sbuild.py +++ b/mdbp/sbuild.py @@ -7,11 +7,10 @@ import contextlib import pathlib import subprocess import sys -import tempfile import typing from .common import AddSpaceSeparatedValues, buildjson, clean_dir, \ - compute_env, get_dsc + compute_env, get_dsc, temporary_static_file PerlValue = typing.Union[None, str, typing.List[typing.Any], typing.Dict[str, typing.Any]] @@ -97,9 +96,7 @@ def main() -> None: ["mv /etc/resolv.conf.disabled /etc/resolv.conf"] with contextlib.ExitStack() as stack: - sbuildconf = stack.enter_context(tempfile.NamedTemporaryFile(mode="w")) - sbuildconf.write(perl_conf(sbc)) - sbuildconf.flush() + sbuildconf = stack.enter_context(temporary_static_file(perl_conf(sbc))) try: thing = build["input"]["sourcename"] @@ -109,7 +106,7 @@ def main() -> None: thing = str(stack.enter_context(get_dsc(build)).absolute()) ret = subprocess.call(["sbuild", *args.sbuildopts, thing], - env=dict(SBUILD_CONFIG=sbuildconf.name, + env=dict(SBUILD_CONFIG=str(sbuildconf), PATH="/usr/bin:/bin"), cwd=build["output"]["directory"], stdout=None if build["output"].get("log", True) |