Plain is headed towards 1.0! Subscribe for development updates →

 1from __future__ import annotations
 2
 3#
 4#
 5# This file is part of gunicorn released under the MIT license.
 6# See the LICENSE for more information.
 7#
 8# Vendored and modified for Plain.
 9import os
10import platform
11import tempfile
12import time
13from typing import TYPE_CHECKING
14
15from .. import util
16
17if TYPE_CHECKING:
18    from ..config import Config
19
20PLATFORM = platform.system()
21IS_CYGWIN = PLATFORM.startswith("CYGWIN")
22
23
24class WorkerTmp:
25    def __init__(self, cfg: Config) -> None:
26        fd, name = tempfile.mkstemp(prefix="wplain-")
27
28        # unlink the file so we don't leak temporary files
29        try:
30            if not IS_CYGWIN:
31                util.unlink(name)
32            # In Python 3.8, open() emits RuntimeWarning if buffering=1 for binary mode.
33            # Because we never write to this file, pass 0 to switch buffering off.
34            self._tmp = os.fdopen(fd, "w+b", 0)
35        except Exception:
36            os.close(fd)
37            raise
38
39    def notify(self) -> None:
40        new_time = time.monotonic()
41        os.utime(self._tmp.fileno(), (new_time, new_time))
42
43    def last_update(self) -> float:
44        return os.fstat(self._tmp.fileno()).st_mtime
45
46    def fileno(self) -> int:
47        return self._tmp.fileno()
48
49    def close(self) -> None:
50        return self._tmp.close()