@@ -86,17 +86,19 @@ def get_ipython_dir() -> str:
86
86
def migrate_dir (src : str , dst : str ) -> bool :
87
87
"""Migrate a directory from src to dst"""
88
88
log = get_logger ()
89
- if not os .listdir (src ):
89
+ src_path = Path (src )
90
+ dst_path = Path (dst )
91
+ if not any (src_path .iterdir ()):
90
92
log .debug ("No files in %s" , src )
91
93
return False
92
- if Path ( dst ) .exists ():
93
- if os . listdir ( dst ):
94
+ if dst_path .exists ():
95
+ if any ( dst_path . iterdir () ):
94
96
# already exists, non-empty
95
97
log .debug ("%s already exists" , dst )
96
98
return False
97
- Path ( dst ) .rmdir ()
99
+ dst_path .rmdir ()
98
100
log .info ("Copying %s -> %s" , src , dst )
99
- ensure_dir_exists (Path ( dst ) .parent )
101
+ ensure_dir_exists (dst_path .parent )
100
102
shutil .copytree (src , dst , symlinks = True )
101
103
return True
102
104
@@ -107,19 +109,20 @@ def migrate_file(src: str | Path, dst: str | Path, substitutions: Any = None) ->
107
109
substitutions is an optional dict of {regex: replacement} for performing replacements on the file.
108
110
"""
109
111
log = get_logger ()
110
- if Path (dst ).exists ():
112
+ dst_path = Path (dst )
113
+ if dst_path .exists ():
111
114
# already exists
112
115
log .debug ("%s already exists" , dst )
113
116
return False
114
117
log .info ("Copying %s -> %s" , src , dst )
115
- ensure_dir_exists (Path ( dst ) .parent )
118
+ ensure_dir_exists (dst_path .parent )
116
119
shutil .copy (src , dst )
117
120
if substitutions :
118
- with Path .open (Path ( dst ), encoding = "utf-8" ) as f :
121
+ with dst_path .open () as f :
119
122
text = f .read ()
120
123
for pat , replacement in substitutions .items ():
121
124
text = pat .sub (replacement , text )
122
- with Path .open (Path ( dst ), "w" , encoding = "utf-8 " ) as f :
125
+ with dst_path .open ("w " ) as f :
123
126
f .write (text )
124
127
return True
125
128
0 commit comments