Skip to content

Commit f2157f5

Browse files
committed
Replaced global variable with a custom cache wrapper.
1 parent 284279d commit f2157f5

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

distutils/dir_util.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,39 @@
1010
from ._log import log
1111
from .errors import DistutilsFileError, DistutilsInternalError
1212

13-
# cache for by mkpath() -- in addition to cheapening redundant calls,
14-
# eliminates redundant "creating /foo/bar/baz" messages in dry-run mode
15-
_path_created = set()
13+
14+
class SkipRepeatAbsolutePaths(set):
15+
"""
16+
Cache for mkpath.
17+
18+
In addition to cheapening redundant calls, eliminates redundant
19+
"creating /foo/bar/baz" messages in dry-run mode.
20+
"""
21+
22+
def __init__(self):
23+
SkipRepeatAbsolutePaths.instance = self
24+
25+
@classmethod
26+
def clear(cls):
27+
super(cls, cls.instance).clear()
28+
29+
def wrap(self, func):
30+
@functools.wraps(func)
31+
def wrapper(path, *args, **kwargs):
32+
if path.absolute() in self:
33+
return
34+
self.add(path.absolute())
35+
return func(path, *args, **kwargs)
36+
37+
return wrapper
38+
39+
40+
# Python 3.8 compatibility
41+
wrapper = SkipRepeatAbsolutePaths().wrap
1642

1743

1844
@functools.singledispatch
45+
@wrapper
1946
def mkpath(name: pathlib.Path, mode=0o777, verbose=True, dry_run=False):
2047
"""Create a directory and any missing ancestor directories.
2148
@@ -26,12 +53,6 @@ def mkpath(name: pathlib.Path, mode=0o777, verbose=True, dry_run=False):
2653
If 'verbose' is true, log the directory created.
2754
Return the list of directories actually created.
2855
"""
29-
30-
global _path_created
31-
32-
if str(name.absolute()) in _path_created:
33-
return
34-
3556
if verbose and not name.is_dir():
3657
log.info("creating %s", name)
3758

@@ -40,7 +61,6 @@ def mkpath(name: pathlib.Path, mode=0o777, verbose=True, dry_run=False):
4061

4162
try:
4263
dry_run or name.mkdir(mode=mode, parents=True, exist_ok=True)
43-
_path_created.add(name.absolute())
4464
except OSError as exc:
4565
raise DistutilsFileError(f"could not create '{name}': {exc.args[-1]}")
4666

@@ -185,8 +205,6 @@ def remove_tree(directory, verbose=True, dry_run=False):
185205
Any errors are ignored (apart from being reported to stdout if 'verbose'
186206
is true).
187207
"""
188-
global _path_created
189-
190208
if verbose >= 1:
191209
log.info("removing '%s' (and everything under it)", directory)
192210
if dry_run:
@@ -196,10 +214,8 @@ def remove_tree(directory, verbose=True, dry_run=False):
196214
for cmd in cmdtuples:
197215
try:
198216
cmd[0](cmd[1])
199-
# remove dir from cache if it's already there
200-
abspath = os.path.abspath(cmd[1])
201-
if abspath in _path_created:
202-
_path_created.remove(abspath)
217+
# Clear the cache
218+
SkipRepeatAbsolutePaths.clear()
203219
except OSError as exc:
204220
log.warning("error removing %s: %s", directory, exc)
205221

0 commit comments

Comments
 (0)