PHP 8.5.2
Preview: temp_utils.py Size: 3.15 KB
//usr/lib/python3/dist-packages/cloudinit/temp_utils.py

# This file is part of cloud-init. See LICENSE file for license information.

import contextlib
import errno
import logging
import os
import shutil
import tempfile

from cloudinit import util

LOG = logging.getLogger(__name__)
_TMPDIR = None
_ROOT_TMPDIR = "/run/cloud-init/tmp"
_EXE_ROOT_TMPDIR = "/var/tmp/cloud-init"


def get_tmp_ancestor(odir=None, needs_exe: bool = False):
    if odir is not None:
        return odir
    if needs_exe:
        return _EXE_ROOT_TMPDIR
    if _TMPDIR:
        return _TMPDIR
    if os.getuid() == 0:
        return _ROOT_TMPDIR
    return os.environ.get("TMPDIR", "/tmp")


def _tempfile_dir_arg(odir=None, needs_exe: bool = False):
    """Return the proper 'dir' argument for tempfile functions.

    When root, cloud-init will use /run/cloud-init/tmp to avoid
    any cleaning that a distro boot might do on /tmp (such as
    systemd-tmpfiles-clean).

    If the caller of this function (mkdtemp or mkstemp) was provided
    with a 'dir' argument, then that is respected.

    @param odir: original 'dir' arg to 'mkdtemp' or other.
    @param needs_exe: Boolean specifying whether or not exe permissions are
        needed for tempdir. This is needed because /run is mounted noexec.
    """
    tdir = get_tmp_ancestor(odir, needs_exe)
    if not os.path.isdir(tdir):
        os.makedirs(tdir)
        os.chmod(tdir, 0o1777)

    if needs_exe:
        if util.has_mount_opt(tdir, "noexec"):
            LOG.warning(
                "Requested temporal dir with exe permission `%s` is"
                " mounted as noexec",
                tdir,
            )

    if odir is None and not needs_exe:
        global _TMPDIR
        _TMPDIR = tdir

    return tdir


def ExtendedTemporaryFile(**kwargs):
    kwargs["dir"] = _tempfile_dir_arg(
        kwargs.pop("dir", None), kwargs.pop("needs_exe", False)
    )
    fh = tempfile.NamedTemporaryFile(**kwargs)
    # Replace its unlink with a quiet version
    # that does not raise errors when the
    # file to unlink has been unlinked elsewhere..

    def _unlink_if_exists(path):
        try:
            os.unlink(path)
        except OSError as e:
            if e.errno != errno.ENOENT:
                raise e

    fh.unlink = _unlink_if_exists

    # Add a new method that will unlink
    # right 'now' but still lets the exit
    # method attempt to remove it (which will
    # not throw due to our del file being quiet
    # about files that are not there)
    def unlink_now():
        fh.unlink(fh.name)

    setattr(fh, "unlink_now", unlink_now)
    return fh


@contextlib.contextmanager
def tempdir(rmtree_ignore_errors=False, **kwargs):
    # This seems like it was only added in python 3.2
    # Make it since its useful...
    # See: http://bugs.python.org/file12970/tempdir.patch
    tdir = mkdtemp(**kwargs)
    try:
        yield tdir
    finally:
        shutil.rmtree(tdir, ignore_errors=rmtree_ignore_errors)


def mkdtemp(dir=None, needs_exe: bool = False, **kwargs):
    dir = _tempfile_dir_arg(dir, needs_exe)
    return tempfile.mkdtemp(dir=dir, **kwargs)


def mkstemp(dir=None, needs_exe: bool = False, **kwargs):
    dir = _tempfile_dir_arg(dir, needs_exe)
    return tempfile.mkstemp(dir=dir, **kwargs)

Directory Contents

Dirs: 11 × Files: 29

Name Size Perms Modified Actions
analyze DIR
- drwxr-xr-x 2026-01-23 08:58:12
Edit Download
cmd DIR
- drwxr-xr-x 2026-01-23 08:58:12
Edit Download
config DIR
- drwxr-xr-x 2026-01-23 08:58:12
Edit Download
distros DIR
- drwxr-xr-x 2026-01-23 08:58:12
Edit Download
filters DIR
- drwxr-xr-x 2026-01-23 08:58:12
Edit Download
handlers DIR
- drwxr-xr-x 2026-01-23 08:58:12
Edit Download
mergers DIR
- drwxr-xr-x 2026-01-23 08:58:12
Edit Download
net DIR
- drwxr-xr-x 2026-01-23 08:58:12
Edit Download
reporting DIR
- drwxr-xr-x 2026-01-23 08:58:12
Edit Download
sources DIR
- drwxr-xr-x 2026-01-23 08:58:12
Edit Download
- drwxr-xr-x 2026-01-23 08:58:13
Edit Download
10.10 KB lrw-r--r-- 2024-03-27 13:14:04
Edit Download
2.45 KB lrw-r--r-- 2024-03-27 13:14:04
Edit Download
3.71 KB lrw-r--r-- 2024-03-27 13:14:04
Edit Download
7.78 KB lrw-r--r-- 2024-03-27 13:14:04
Edit Download
2.00 KB lrw-r--r-- 2024-03-27 13:14:04
Edit Download
3.38 KB lrw-r--r-- 2024-03-27 13:14:04
Edit Download
4.28 KB lrw-r--r-- 2024-03-27 13:14:04
Edit Download
15.99 KB lrw-r--r-- 2024-03-27 13:14:04
Edit Download
2.43 KB lrw-r--r-- 2024-03-27 13:14:04
Edit Download
6.00 KB lrw-r--r-- 2024-03-27 13:14:04
Edit Download
22.97 KB lrw-r--r-- 2024-03-27 13:14:04
Edit Download
2.52 KB lrw-r--r-- 2024-03-27 13:14:04
Edit Download
1022 B lrw-r--r-- 2024-03-27 13:14:04
Edit Download
10.28 KB lrw-r--r-- 2024-03-27 13:14:04
Edit Download
2.06 KB lrw-r--r-- 2024-03-27 13:14:04
Edit Download
1.74 KB lrw-r--r-- 2024-03-27 13:14:04
Edit Download
1.93 KB lrw-r--r-- 2024-03-27 13:14:04
Edit Download
22.20 KB lrw-r--r-- 2024-03-27 13:14:04
Edit Download
38.76 KB lrw-r--r-- 2024-03-27 13:14:04
Edit Download
12.50 KB lrw-r--r-- 2024-03-27 13:14:04
Edit Download
7.64 KB lrw-r--r-- 2024-03-27 13:14:04
Edit Download
3.15 KB lrw-r--r-- 2024-03-27 13:14:04
Edit Download
703 B lrw-r--r-- 2024-03-27 13:14:04
Edit Download
28.06 KB lrw-r--r-- 2024-04-05 23:18:48
Edit Download
14.44 KB lrw-r--r-- 2024-03-27 13:14:04
Edit Download
97.25 KB lrw-r--r-- 2024-03-27 13:14:04
Edit Download
560 B lrw-r--r-- 2024-04-05 23:18:48
Edit Download
3.76 KB lrw-r--r-- 2024-03-27 13:14:04
Edit Download
0 B lrw-r--r-- 2024-03-27 13:14:04
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).