Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from . import OsError
2from . import cwd
3from . import getenv
4from .path import Path
6c"""source-before-namespace
7#include <unistd.h>
8"""
10func _create_template(directory: Path?) -> bytes:
11 if directory is None:
12 tmpdir = getenv("TMPDIR", "/tmp").strip_right("/")
13 else:
14 tmpdir = str(directory)
16 name = f"{tmpdir}/tmp.XXXXXX".to_utf8()
17 name += 0
19 return name
21class File:
22 """A temporary file.
24 """
26 _path: Path?
27 _remove: bool
29 func __init__(self, remove: bool = True, directory: Path? = None):
30 self._path = None
31 self._remove = remove
32 name = _create_template(directory)
34 c"""
35 int fd = mkstemp((char *)name.m_bytes->data());
37 if (fd != -1) {
38 close(fd);
39 name.m_bytes->pop_back();
40 this->_path = mys::make_shared<path::Path>(String(name));
41 }
42 """
44 if self._path is None:
45 raise OsError("Failed to create temporary file.")
47 func __del__(self):
48 self._del()
50 func _del(self):
51 if self._remove:
52 self._path.rm(force=True)
54 func path(self) -> Path:
55 """The file system path to the file.
57 """
59 return self._path
61class Directory:
62 """A temporary directory.
64 """
66 _path: Path?
67 _remove: bool
69 func __init__(self, remove: bool = True, directory: Path? = None):
70 self._path = None
71 self._remove = remove
72 name = _create_template(directory)
74 c"""
75 if (mkdtemp((char *)name.m_bytes->data()) != NULL) {
76 name.m_bytes->pop_back();
77 this->_path = mys::make_shared<path::Path>(String(name));
78 }
79 """
81 if self._path is None:
82 raise OsError("Failed to create temporary directory.")
84 func __del__(self):
85 self._del()
87 func _del(self):
88 if self._remove:
89 self._path.rm(force=True, recursive=True)
91 func path(self) -> Path:
92 """The file system path to the directory.
94 """
96 return self._path
98test file():
99 tmpfile = File()
100 assert tmpfile.path().exists()
102func _create_files() -> (Path, Path):
103 return (File().path(), File(remove=False).path())
105test file_remove():
106 path_remove_true, path_remove_false = _create_files()
107 assert not path_remove_true.exists()
108 assert path_remove_false.exists()
109 path_remove_false.rm()
111test file_in_custom_directory():
112 tmpdir = Directory()
113 directory = tmpdir.path()
114 tmpfile = File(directory=directory)
115 assert tmpfile.path().exists()
116 assert tmpfile.path().parent() == directory
118test directory():
119 tmpdir = Directory()
120 assert tmpdir.path().exists()
122func _create_directories() -> (Path, Path):
123 return (Directory().path(), Directory(remove=False).path())
125test directory_remove():
126 path_remove_true, path_remove_false = _create_directories()
127 assert not path_remove_true.exists()
128 assert path_remove_false.exists()
129 path_remove_false.rm()
131test remove_directory():
132 tmpdir = Directory()
133 original_path = cwd()
134 tmpdir.path().cd()
135 Path("foo.txt").touch()
136 original_path.cd()
138test directory_in_custom_directory():
139 tmpdir = Directory()
140 directory = tmpdir.path()
141 tmpfile = Directory(directory=directory)
142 assert tmpfile.path().exists()
143 assert tmpfile.path().parent() == directory