forked from ahawker/scratchdir
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscratchdir.py
More file actions
313 lines (264 loc) · 13.3 KB
/
scratchdir.py
File metadata and controls
313 lines (264 loc) · 13.3 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
"""
scratchdir
~~~~~~~~~~
Context manager used to maintain your temporary directories/files.
"""
import functools
import os
import shutil
import sys
import tempfile
import typing
import uuid
__all__ = ['ScratchDirError', 'ScratchDirInactiveError', 'ScratchDir']
# Prefix/suffix defaults in stdlib change starting in Python 3.5
if sys.version_info >= (3, 5):
DEFAULT_PREFIX = None
DEFAULT_SUFFIX = None
else:
DEFAULT_PREFIX = 'tmp'
DEFAULT_SUFFIX = ''
class ScratchDirError(Exception):
"""
Base exception used for all :mod:`scratchdir` related errors.
"""
class ScratchDirInactiveError(ScratchDirError):
"""
Exception raised when incorrectly attempting to perform an action against a :class:`~scratchdir.ScratchDir`
that is not active.
"""
def requires_activation(func):
"""
Decorate a bound method on the :class:`~scratchdir.ScratchDir` to assert that it is active before
performing certain actions.
"""
@functools.wraps(func)
def decorator(self, *args, **kwargs): # pylint: disable=missing-docstring
if not self.is_active:
raise ScratchDirInactiveError('ScratchDir must be active to perform this action')
return func(self, *args, **kwargs)
return decorator
class ScratchDir:
"""
Represents a directory on disk within the default temporary directory that can be used to store context
specific subdirectories and files.
"""
def __init__(self, prefix: str = '', suffix: str = '.scratchdir', base: typing.Optional[str] = None,
root: typing.Optional[str] = tempfile.tempdir, wd: typing.Optional[str] = None) -> None:
self.prefix = prefix
self.suffix = suffix
self.base = base
self.root = root
self.wd = wd
def __enter__(self) -> 'ScratchDir':
self.setup()
return self
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
self.teardown()
@property
def is_active(self) -> bool:
"""
Check to see if the current scratch dir is active.
A scratch dir is active if :meth:`~scratchdir.ScratchDir.setup` has been called and its
working directory exists on disk.
:return: Boolean indicating if the scratch dir is active or not
:rtype: :class:`~bool`
"""
return bool(self.wd) and os.path.exists(self.wd)
def setup(self) -> None:
"""
Setup the scratch dir by creating a temporary directory to become the root of all new temporary directories
and files created by this instance.
:return: Nothing
:rtype: :class:`~NoneType`
"""
tempfile.tempdir = self.root
self.wd = self.wd or tempfile.mkdtemp(self.suffix, self.prefix, self.base)
@requires_activation
def teardown(self) -> None:
"""
Teardown the scratch dir by deleting all directories and files within the scratch dir.
:return: Nothing
:rtype: :class:`~NoneType`
"""
shutil.rmtree(self.wd)
self.wd = None
@requires_activation
def child(self, prefix: str = '', suffix: str = '.scratchdir') -> 'ScratchDir':
"""
Create a new :class:`~scratchdir.ScratchDir` instance whose working directory is a temporary directory
within this scratch dir.
:param prefix: (Optional) prefix of the temporary directory for this child scratch dir
:type prefix: :class:`~str`
:param suffix: (Optional) suffix of the temporary directory for this child scratch dir
:type suffix: :class:`~str`
:return: ScratchDir instance that is scoped within the current scratch dir
:rtype: :class:`~scratchdir.ScratchDir`
"""
return self.__class__(prefix, suffix, self.wd)
@requires_activation
def file(self, mode: str = 'w+b', buffering: int = -1, encoding: typing.Optional[str] = None,
newline: typing.Optional[str] = None, suffix: typing.Optional[str] = DEFAULT_SUFFIX,
prefix: typing.Optional[str] = DEFAULT_PREFIX, dir: typing.Optional[str] = None) -> typing.IO:
"""
Create a new temporary file within the scratch dir.
This returns the result of :func:`~tempfile.TemporaryFile` which returns a nameless, file-like object that
will cease to exist once it is closed.
:param mode: (Optional) mode to open the file with
:type mode: :class:`~str`
:param buffering: (Optional) size of the file buffer
:type buffering: :class:`~int`
:param encoding: (Optional) encoding to open the file with
:type encoding: :class:`~str` or :class:`~NoneType`
:param newline: (Optional) newline argument to open the file with
:type newline: :class:`~str` or :class:`~NoneType`
:param suffix: (Optional) filename suffix
:type suffix: :class:`~str` or :class:`~NoneType`
:param prefix: (Optional) filename prefix
:type prefix: :class:`~str` or :class:`~NoneType`
:param dir: (Optional) relative path to directory within the scratch dir where the file should exist
:type dir: :class:`~str` or :class:`~NoneType`
:return: file-like object as returned by :func:`~tempfile.TemporaryFile`
:rtype: :class:`~_io.BufferedRandom`
"""
return tempfile.TemporaryFile(mode, buffering, encoding, newline,
suffix, prefix, self.join(dir))
@requires_activation
def named(self, mode: str = 'w+b', buffering: int = -1, encoding: typing.Optional[str] = None,
newline: typing.Optional[str] = None, suffix: typing.Optional[str] = DEFAULT_SUFFIX,
prefix: typing.Optional[str] = DEFAULT_PREFIX, dir: typing.Optional[str] = None,
delete: bool = True) -> typing.IO:
"""
Create a new named temporary file within the scratch dir.
This returns the result of :func:`~tempfile.NamedTemporaryFile` which returns a named, file-like object that
will cease to exist once it is closed unless `delete` is set to `False`.
:param mode: (Optional) mode to open the file with
:type mode: :class:`~str`
:param buffering: (Optional) size of the file buffer
:type buffering: :class:`~int`
:param encoding: (Optional) encoding to open the file with
:type encoding: :class:`~str` or :class:`~NoneType`
:param newline: (Optional) newline argument to open the file with
:type newline: :class:`~str` or :class:`~NoneType`
:param suffix: (Optional) filename suffix
:type suffix: :class:`~str` or :class:`~NoneType`
:param prefix: (Optional) filename prefix
:type prefix: :class:`~str` or :class:`~NoneType`
:param dir: (Optional) relative path to directory within the scratch dir where the file should exist
:type dir: :class:`~str` or :class:`~NoneType`
:param delete: (Optional) flag to indicate if the file should be deleted from disk when it is closed
:type delete: :class:`~bool`
:return: file-like object as returned by :func:`~tempfile.NamedTemporaryFile`
:rtype: :class:`~_io.TemporaryFileWrapper`
"""
return tempfile.NamedTemporaryFile(mode, buffering, encoding, newline,
suffix, prefix, self.join(dir), delete)
@requires_activation
def spooled(self, max_size: int = 0, mode: str = 'w+b', buffering: int = -1,
encoding: typing.Optional[str] = None, newline: typing.Optional[str] = None,
suffix: typing.Optional[str] = DEFAULT_SUFFIX, prefix: typing.Optional[str] = DEFAULT_PREFIX,
dir: typing.Optional[str] = None) -> typing.IO:
"""
Create a new spooled temporary file within the scratch dir.
This returns a :class:`~tempfile.SpooledTemporaryFile` which is a specialized object that wraps a
:class:`StringIO`/:class:`BytesIO` instance that transparently overflows into a file on the disk once it
reaches a certain size.
By default, a spooled file will never roll over to disk.
:param max_size: (Optional) max size before the in-memory buffer rolls over to disk
:type max_size: :class:`~int`
:param mode: (Optional) mode to open the file with
:type mode: :class:`~str`
:param buffering: (Optional) size of the file buffer
:type buffering: :class:`~int`
:param encoding: (Optional) encoding to open the file with
:type encoding: :class:`~str`
:param newline: (Optional) newline argument to open the file with
:type newline: :class:`~str` or :class:`~NoneType`
:param suffix: (Optional) filename suffix
:type suffix: :class:`~str` or :class:`~NoneType`
:param prefix: (Optional) filename prefix
:type prefix: :class:`~str` or :class:`~NoneType`
:param dir: (Optional) relative path to directory within the scratch dir where the file should exist
:type dir: :class:`~bool`
:return: SpooledTemporaryFile instance
:rtype: :class:`~tempfile.SpooledTemporaryFile`
"""
return tempfile.SpooledTemporaryFile(max_size, mode, buffering, encoding,
newline, suffix, prefix, self.join(dir))
@requires_activation
def secure(self, suffix: typing.Optional[str] = DEFAULT_SUFFIX,
prefix: typing.Optional[str] = DEFAULT_PREFIX, dir: typing.Optional[str] = None,
text: bool = False, return_fd: bool = True) -> typing.Union[str, typing.Tuple[int, str]]:
"""
Create a new temporary file within the scratch dir in the most secure manner possible
with the lowest possibility of race conditions during creation.
This returns the result of :func:`~tempfile.mkstemp` which returns the file descriptor and filename to
the newly created temporary file.
By default, the caller is only passed back the file descriptor and filename unless
`return_fd` is set to `False`.
:param suffix: (Optional) filename suffix
:type suffix: :class:`~str` or :class:`~NoneType`
:param prefix: (Optional) filename prefix
:type prefix: :class:`~str` or :class:`~NoneType`
:param dir: (Optional) relative path to directory within the scratch dir where the file should exist
:type dir: :class:`~str` or :class:`~NoneType`
:param text: (Optional) flag to indicate if the file should be opened in text mode instead of binary
:type text: :class:`~bool`
:param return_fd: (Optional) flag to indicate if the file descriptor should also be returned
:type text: :class:`~bool`
:return: String representing the temporary file name or Tuple of file descriptor and filename
:rtype: :class:`~str` or :class:`~tuple`
"""
fd, filename = tempfile.mkstemp(suffix, prefix, self.join(dir), text)
if return_fd:
return fd, filename
os.close(fd)
return filename
@requires_activation
def directory(self, suffix: typing.Optional[str] = DEFAULT_SUFFIX,
prefix: typing.Optional[str] = DEFAULT_PREFIX, dir: bool = None) -> str:
"""
Creates a new temporary directory within the scratch dir in the most secure manner possible and no
chance of race conditions.
This returns the result of :func:`~tempfile.mkdtemp` which returns the fully qualified path to the directory.
:param suffix: (Optional) directory name suffix
:type suffix: :class:`~str` or :class:`~NoneType`
:param prefix: (Optional) directory name prefix
:type prefix: :class:`~str` or :class:`~NoneType`
:param dir: (Optional) relative path to directory within the scratch dir where the directory should exist
:type dir: :class:`~str` or :class:`~NoneType`
:return: Path on disk where new directory exists
:rtype: :class:`~str`
"""
return tempfile.mkdtemp(suffix, prefix, self.join(dir))
@requires_activation
def filename(self, suffix: typing.Optional[str] = DEFAULT_SUFFIX,
prefix: typing.Optional[str] = DEFAULT_PREFIX) -> str:
"""
Create a filename that is unique within the scratch dir.
This returns the fully qualified path to the unique filename within the scratch dir but does not create
a temporary file for that location.
:param suffix: (Optional) filename suffix
:type suffix: :class:`~str` or :class:`~NoneType`
:param prefix: (Optional) filename prefix
:type prefix: :class:`~str` or :class:`~NoneType`
:return: Path in scratch dir for unique filename
:rtype: :class:`~str`
"""
return self.join(''.join((prefix, str(uuid.uuid4()), suffix)))
@requires_activation
def join(self, *paths: str) -> str:
"""
Builds a fully qualified path to the given location relative to the scratch dir.
:param paths: One or more paths to join
:type paths: :class:`~tuple of `:class:`~str`
:return: Fully qualified path within scratch dir
:rtype: :class:`~str`
"""
return os.path.join(self.wd, *(str(p) for p in paths if p is not None))
# Aliases
TemporaryFile = file
NamedTemporaryFile = named
SpooledTemporaryFile = spooled
mkstemp = secure
mkdtemp = directory