2
2
3
3
Utility functions for manipulating directories and directory trees."""
4
4
5
+ import functools
5
6
import itertools
6
7
import os
7
8
import pathlib
14
15
_path_created = set ()
15
16
16
17
17
- def mkpath (name , mode = 0o777 , verbose = True , dry_run = False ):
18
+ @functools .singledispatch
19
+ def mkpath (name : pathlib .Path , mode = 0o777 , verbose = True , dry_run = False ):
18
20
"""Create a directory and any missing ancestor directories.
19
21
20
22
If the directory already exists (or if 'name' is the empty string, which
@@ -27,12 +29,6 @@ def mkpath(name, mode=0o777, verbose=True, dry_run=False):
27
29
28
30
global _path_created
29
31
30
- # Detect a common bug -- name is None
31
- if not isinstance (name , str ):
32
- raise DistutilsInternalError (f"mkpath: 'name' must be a string (got { name !r} )" )
33
-
34
- name = pathlib .Path (name )
35
-
36
32
if str (name .absolute ()) in _path_created :
37
33
return
38
34
@@ -51,6 +47,19 @@ def mkpath(name, mode=0o777, verbose=True, dry_run=False):
51
47
return list (map (str , missing ))
52
48
53
49
50
+ @mkpath .register
51
+ def _ (name : str , * args , ** kwargs ):
52
+ return mkpath (pathlib .Path (name ), * args , ** kwargs )
53
+
54
+
55
+ @mkpath .register
56
+ def _ (name : None , * args , ** kwargs ):
57
+ """
58
+ Detect a common bug -- name is None.
59
+ """
60
+ raise DistutilsInternalError (f"mkpath: 'name' must be a string (got { name !r} )" )
61
+
62
+
54
63
def create_tree (base_dir , files , mode = 0o777 , verbose = True , dry_run = False ):
55
64
"""Create all the empty directories under 'base_dir' needed to put 'files'
56
65
there.
0 commit comments