Show More
@@ -1,145 +1,5 b'' | |||||
1 | """TemporaryDirectory class, copied from Python 3.2. |
|
1 | from warnings import warn | |
2 |
|
2 | |||
3 | This is copied from the stdlib and will be standard in Python 3.2 and onwards. |
|
3 | warn("IPython.utils.tempdir is deprecated. Use testpath.tempdir") | |
4 | """ |
|
|||
5 | from __future__ import print_function |
|
|||
6 |
|
||||
7 | import os as _os |
|
|||
8 | import warnings as _warnings |
|
|||
9 | import sys as _sys |
|
|||
10 |
|
||||
11 | # This code should only be used in Python versions < 3.2, since after that we |
|
|||
12 | # can rely on the stdlib itself. |
|
|||
13 | try: |
|
|||
14 | from tempfile import TemporaryDirectory |
|
|||
15 |
|
||||
16 | except ImportError: |
|
|||
17 | from tempfile import mkdtemp, template |
|
|||
18 |
|
||||
19 | class TemporaryDirectory(object): |
|
|||
20 | """Create and return a temporary directory. This has the same |
|
|||
21 | behavior as mkdtemp but can be used as a context manager. For |
|
|||
22 | example: |
|
|||
23 |
|
||||
24 | with TemporaryDirectory() as tmpdir: |
|
|||
25 | ... |
|
|||
26 |
|
||||
27 | Upon exiting the context, the directory and everthing contained |
|
|||
28 | in it are removed. |
|
|||
29 | """ |
|
|||
30 |
|
||||
31 | def __init__(self, suffix="", prefix=template, dir=None): |
|
|||
32 | self.name = mkdtemp(suffix, prefix, dir) |
|
|||
33 | self._closed = False |
|
|||
34 |
|
||||
35 | def __enter__(self): |
|
|||
36 | return self.name |
|
|||
37 |
|
||||
38 | def cleanup(self, _warn=False): |
|
|||
39 | if self.name and not self._closed: |
|
|||
40 | try: |
|
|||
41 | self._rmtree(self.name) |
|
|||
42 | except (TypeError, AttributeError) as ex: |
|
|||
43 | # Issue #10188: Emit a warning on stderr |
|
|||
44 | # if the directory could not be cleaned |
|
|||
45 | # up due to missing globals |
|
|||
46 | if "None" not in str(ex): |
|
|||
47 | raise |
|
|||
48 | print("ERROR: {!r} while cleaning up {!r}".format(ex, self,), |
|
|||
49 | file=_sys.stderr) |
|
|||
50 | return |
|
|||
51 | self._closed = True |
|
|||
52 | if _warn: |
|
|||
53 | self._warn("Implicitly cleaning up {!r}".format(self), |
|
|||
54 | Warning) |
|
|||
55 |
|
||||
56 | def __exit__(self, exc, value, tb): |
|
|||
57 | self.cleanup() |
|
|||
58 |
|
||||
59 | def __del__(self): |
|
|||
60 | # Issue a ResourceWarning if implicit cleanup needed |
|
|||
61 | self.cleanup(_warn=True) |
|
|||
62 |
|
||||
63 |
|
||||
64 | # XXX (ncoghlan): The following code attempts to make |
|
|||
65 | # this class tolerant of the module nulling out process |
|
|||
66 | # that happens during CPython interpreter shutdown |
|
|||
67 | # Alas, it doesn't actually manage it. See issue #10188 |
|
|||
68 | _listdir = staticmethod(_os.listdir) |
|
|||
69 | _path_join = staticmethod(_os.path.join) |
|
|||
70 | _isdir = staticmethod(_os.path.isdir) |
|
|||
71 | _remove = staticmethod(_os.remove) |
|
|||
72 | _rmdir = staticmethod(_os.rmdir) |
|
|||
73 | _os_error = _os.error |
|
|||
74 | _warn = _warnings.warn |
|
|||
75 |
|
||||
76 | def _rmtree(self, path): |
|
|||
77 | # Essentially a stripped down version of shutil.rmtree. We can't |
|
|||
78 | # use globals because they may be None'ed out at shutdown. |
|
|||
79 | for name in self._listdir(path): |
|
|||
80 | fullname = self._path_join(path, name) |
|
|||
81 | try: |
|
|||
82 | isdir = self._isdir(fullname) |
|
|||
83 | except self._os_error: |
|
|||
84 | isdir = False |
|
|||
85 | if isdir: |
|
|||
86 | self._rmtree(fullname) |
|
|||
87 | else: |
|
|||
88 | try: |
|
|||
89 | self._remove(fullname) |
|
|||
90 | except self._os_error: |
|
|||
91 | pass |
|
|||
92 | try: |
|
|||
93 | self._rmdir(path) |
|
|||
94 | except self._os_error: |
|
|||
95 | pass |
|
|||
96 |
|
||||
97 |
|
||||
98 | class NamedFileInTemporaryDirectory(object): |
|
|||
99 |
|
||||
100 | def __init__(self, filename, mode='w+b', bufsize=-1, **kwds): |
|
|||
101 | """ |
|
|||
102 | Open a file named `filename` in a temporary directory. |
|
|||
103 |
|
||||
104 | This context manager is preferred over `NamedTemporaryFile` in |
|
|||
105 | stdlib `tempfile` when one needs to reopen the file. |
|
|||
106 |
|
||||
107 | Arguments `mode` and `bufsize` are passed to `open`. |
|
|||
108 | Rest of the arguments are passed to `TemporaryDirectory`. |
|
|||
109 |
|
||||
110 | """ |
|
|||
111 | self._tmpdir = TemporaryDirectory(**kwds) |
|
|||
112 | path = _os.path.join(self._tmpdir.name, filename) |
|
|||
113 | self.file = open(path, mode, bufsize) |
|
|||
114 |
|
||||
115 | def cleanup(self): |
|
|||
116 | self.file.close() |
|
|||
117 | self._tmpdir.cleanup() |
|
|||
118 |
|
||||
119 | __del__ = cleanup |
|
|||
120 |
|
||||
121 | def __enter__(self): |
|
|||
122 | return self.file |
|
|||
123 |
|
||||
124 | def __exit__(self, type, value, traceback): |
|
|||
125 | self.cleanup() |
|
|||
126 |
|
||||
127 |
|
||||
128 | class TemporaryWorkingDirectory(TemporaryDirectory): |
|
|||
129 | """ |
|
|||
130 | Creates a temporary directory and sets the cwd to that directory. |
|
|||
131 | Automatically reverts to previous cwd upon cleanup. |
|
|||
132 | Usage example: |
|
|||
133 |
|
||||
134 | with TemporaryWorkingDirectory() as tmpdir: |
|
|||
135 | ... |
|
|||
136 | """ |
|
|||
137 | def __enter__(self): |
|
|||
138 | self.old_wd = _os.getcwd() |
|
|||
139 | _os.chdir(self.name) |
|
|||
140 | return super(TemporaryWorkingDirectory, self).__enter__() |
|
|||
141 |
|
||||
142 | def __exit__(self, exc, value, tb): |
|
|||
143 | _os.chdir(self.old_wd) |
|
|||
144 | return super(TemporaryWorkingDirectory, self).__exit__(exc, value, tb) |
|
|||
145 |
|
4 | |||
|
5 | from testpath.tempdir import * |
@@ -269,7 +269,7 b' extras_require = dict(' | |||||
269 | parallel = [pyzmq], |
|
269 | parallel = [pyzmq], | |
270 | qtconsole = [pyzmq, 'pygments'], |
|
270 | qtconsole = [pyzmq, 'pygments'], | |
271 | doc = ['Sphinx>=1.1', 'numpydoc'], |
|
271 | doc = ['Sphinx>=1.1', 'numpydoc'], | |
272 | test = ['nose>=0.10.1', 'requests'], |
|
272 | test = ['nose>=0.10.1', 'requests', 'testpath'], | |
273 | terminal = [], |
|
273 | terminal = [], | |
274 | nbformat = ['jsonschema>=2.0'], |
|
274 | nbformat = ['jsonschema>=2.0'], | |
275 | notebook = ['tornado>=4.0', pyzmq, 'jinja2', 'pygments', 'mistune>=0.5'], |
|
275 | notebook = ['tornado>=4.0', pyzmq, 'jinja2', 'pygments', 'mistune>=0.5'], |
General Comments 0
You need to be logged in to leave comments.
Login now