Skip to content

Commit f31bb9a

Browse files
Added mktemp method to FileProxy (fixes #121) (#124)
* added method api * generated command * implemented mktemp * bug fix: missing import * added basic test_mktemp * test suffixes * bug fix: use --suffix and --tmpdir * added prefix test * added test for specified dir * added test for non-existent dir * bug fix: check directory existence * trailing space * bug fix: use prefix to build template
1 parent ea8aa2b commit f31bb9a

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

src/rozzy/proxy/file.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
This file implements a proxy for accessing container file systems.
4+
"""
15
__all__ = ('FileProxy',)
26

3-
from typing import List, Union, overload
7+
from typing import List, Union, Optional, overload
48
from typing_extensions import Literal
59
import os
610
import shlex
@@ -337,3 +341,45 @@ def rmtree(self, d: str) -> None:
337341
code, output, duration = self.__shell.execute(cmd)
338342
if code != 0:
339343
raise OSError(f"failed to remove directory tree: {d}")
344+
345+
def mktemp(self,
346+
suffix: Optional[str] = None,
347+
prefix: Optional[str] = None,
348+
dirname: Optional[str] = None
349+
) -> str:
350+
"""Creates a temporary file.
351+
352+
Parameters
353+
----------
354+
suffix: str, optional
355+
an optional suffix for the filename.
356+
prefix: str, optional
357+
an optional prefix for the filename.
358+
dirname: str, optional
359+
if specified, the temporary file will be created in the given
360+
directory.
361+
362+
Raises
363+
------
364+
FileNotFoundError:
365+
if specified directory does not exist.
366+
OSError:
367+
if the temporary file could not be constructed.
368+
"""
369+
template = shlex.quote(f"{prefix if prefix else 'tmp'}.XXXXXXXXXX")
370+
cmd_parts = ['mktemp', template]
371+
if suffix:
372+
cmd_parts += ['--suffix', shlex.quote(suffix)]
373+
if dirname:
374+
cmd_parts += ['-p', shlex.quote(dirname)]
375+
if not self.isdir(dirname):
376+
m = f'directory does not exist: {dirname}'
377+
raise FileNotFoundError(m)
378+
cmd = ' '.join(cmd_parts)
379+
380+
code, output, duration = self.__shell.execute(cmd)
381+
# TODO capture context
382+
if code != 0:
383+
raise OSError(f"failed to create temporary directory")
384+
385+
return output

test/test_file.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,34 @@ def test_write():
321321
# write to a file that belongs to a non-existent directory
322322
with pytest.raises(FileNotFoundError):
323323
files.write('/tmp/bar/bork', 'code things')
324+
325+
326+
def test_mktemp():
327+
with build_file_proxy() as files:
328+
# create a temporary file
329+
fn = files.mktemp()
330+
assert files.isfile(fn)
331+
332+
# use specified dir
333+
d = '/boop'
334+
files.makedirs(d, exist_ok=True)
335+
assert files.isdir(d)
336+
fn = files.mktemp(dirname=d)
337+
assert os.path.dirname(fn) == d
338+
assert files.isfile(fn)
339+
340+
# use non-existent dir
341+
with pytest.raises(FileNotFoundError):
342+
d = '/idontexist'
343+
assert not files.isdir(d)
344+
files.mktemp(dirname=d)
345+
346+
# add suffix
347+
fn = files.mktemp(suffix='.foo')
348+
assert fn.endswith('.foo')
349+
assert files.isfile(fn)
350+
351+
# add prefix
352+
fn = files.mktemp(prefix='foo')
353+
assert os.path.basename(fn).startswith('foo')
354+
assert files.isfile(fn)

0 commit comments

Comments
 (0)