13
13
from pip ._internal .utils .typing import MYPY_CHECK_RUNNING
14
14
15
15
if MYPY_CHECK_RUNNING :
16
- from typing import Any , Iterator , Optional , TypeVar
16
+ from typing import Any , Dict , Iterator , Optional , TypeVar
17
17
18
18
_T = TypeVar ('_T' , bound = 'TempDirectory' )
19
19
@@ -36,6 +36,47 @@ def global_tempdir_manager():
36
36
_tempdir_manager = old_tempdir_manager
37
37
38
38
39
+ class TempDirectoryTypeRegistry (object ):
40
+ """Manages temp directory behavior
41
+ """
42
+
43
+ def __init__ (self ):
44
+ # type: () -> None
45
+ self ._should_delete = {} # type: Dict[str, bool]
46
+
47
+ def set_delete (self , kind , value ):
48
+ # type: (str, bool) -> None
49
+ """Indicate whether a TempDirectory of the given kind should be
50
+ auto-deleted.
51
+ """
52
+ self ._should_delete [kind ] = value
53
+
54
+ def get_delete (self , kind ):
55
+ # type: (str) -> bool
56
+ """Get configured auto-delete flag for a given TempDirectory type,
57
+ default True.
58
+ """
59
+ return self ._should_delete .get (kind , True )
60
+
61
+
62
+ _tempdir_registry = None # type: Optional[TempDirectoryTypeRegistry]
63
+
64
+
65
+ @contextmanager
66
+ def tempdir_registry ():
67
+ # type: () -> Iterator[TempDirectoryTypeRegistry]
68
+ """Provides a scoped global tempdir registry that can be used to dictate
69
+ whether directories should be deleted.
70
+ """
71
+ global _tempdir_registry
72
+ old_tempdir_registry = _tempdir_registry
73
+ _tempdir_registry = TempDirectoryTypeRegistry ()
74
+ try :
75
+ yield _tempdir_registry
76
+ finally :
77
+ _tempdir_registry = old_tempdir_registry
78
+
79
+
39
80
class TempDirectory (object ):
40
81
"""Helper class that owns and cleans up a temporary directory.
41
82
@@ -68,8 +109,11 @@ def __init__(
68
109
69
110
if path is None and delete is None :
70
111
# If we were not given an explicit directory, and we were not given
71
- # an explicit delete option, then we'll default to deleting.
112
+ # an explicit delete option, then we'll default to deleting unless
113
+ # the tempdir_registry says otherwise.
72
114
delete = True
115
+ if _tempdir_registry :
116
+ delete = _tempdir_registry .get_delete (kind )
73
117
74
118
if path is None :
75
119
path = self ._create (kind )
0 commit comments