2018-02-08 14:32:28 -06:00
|
|
|
from contextlib import contextmanager
|
|
|
|
|
import os
|
2022-01-08 16:26:02 -06:00
|
|
|
from shutil import copy
|
2018-02-08 14:32:28 -06:00
|
|
|
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
2022-01-08 16:26:02 -06:00
|
|
|
def cdtemp(files=None):
|
|
|
|
|
"""Context manager to change to/return from a tmpdir.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
files : Iterable of str or Path-like
|
|
|
|
|
Set of files to copy into the temporary directory
|
|
|
|
|
"""
|
2018-02-08 14:32:28 -06:00
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
|
|
cwd = os.getcwd()
|
2022-01-08 16:26:02 -06:00
|
|
|
if files:
|
|
|
|
|
for file in files:
|
|
|
|
|
copy(file, tmpdir, follow_symlinks=True)
|
2018-02-08 14:32:28 -06:00
|
|
|
try:
|
|
|
|
|
os.chdir(tmpdir)
|
|
|
|
|
yield
|
|
|
|
|
finally:
|
|
|
|
|
os.chdir(cwd)
|