1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
import os
import tempfile
from pathlib import Path
class TemporarySendmail:
def __init__(self, exitcode):
self._tmpdir = tempfile.TemporaryDirectory()
_config = tempfile.NamedTemporaryFile(
dir=self._tmpdir.name, delete=False)
self.config = Path(_config.name)
with tempfile.NamedTemporaryFile(
dir=self._tmpdir.name, delete=False) as _bin:
_bin.write(
b'''#!/bin/sh
echo "Sendmail failing for reasons..."
exit %s''' % bytes(str(exitcode), 'utf-8'))
self.bin = Path(_bin.name)
os.chmod(str(self.bin), 0o700)
def cleanup(self):
self._tmpdir.cleanup()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.cleanup()
|