Skip to content

Commit ee76397

Browse files
committed
add recursive flag
1 parent 375a6ce commit ee76397

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

fix_mangled_paths.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
# written by Joshua Boudreau <[email protected]> 2022
44

55
import os
6-
import re
76
import argparse
8-
import re
7+
import sys
98

109

1110
def get_unique_name(root, name, taken_paths):
@@ -33,11 +32,13 @@ def windows_allowed(c): return c not in windows_ill
3332
return "".join(map(lambda c: c if printable(c) and windows_allowed(c) else '_', string))
3433

3534

36-
def rename_mangled(paths, dry_run):
35+
def rename_mangled(paths, dry_run, recursive):
3736
taken_paths = []
37+
change_count = 0
3838

3939
def rename_entries(root, entries, rootfd):
4040
changes = []
41+
nonlocal change_count
4142
for src in entries:
4243
dst = legalize_name(src)
4344
if dst != src:
@@ -46,6 +47,7 @@ def rename_entries(root, entries, rootfd):
4647
print(f"'{src.encode('unicode_escape').decode('utf-8')}'", '->')
4748
print(f"'{dst.encode('unicode_escape').decode('utf-8')}'")
4849
print()
50+
change_count += 1
4951
if dry_run:
5052
continue
5153
try:
@@ -57,22 +59,35 @@ def rename_entries(root, entries, rootfd):
5759
entries.remove(src)
5860
entries.append(dst)
5961
for path in paths:
60-
for root, dirs, files, rootfd in os.fwalk(path, topdown=True):
61-
if '.zfs' in dirs:
62-
dirs.remove('.zfs')
63-
rename_entries(root, dirs, rootfd)
64-
rename_entries(root, files, rootfd)
62+
if os.path.isdir(path) and recursive:
63+
for root, dirs, files, rootfd in os.fwalk(path, topdown=True):
64+
if '.zfs' in dirs:
65+
dirs.remove('.zfs')
66+
rename_entries(root, dirs, rootfd)
67+
rename_entries(root, files, rootfd)
68+
if os.path.exists(path):
69+
fullpath = os.path.realpath(path)
70+
root = os.path.dirname(fullpath)
71+
entries = [os.path.basename(fullpath)]
72+
rootfd = os.open(root, os.O_RDONLY)
73+
rename_entries(root, entries, rootfd)
74+
return change_count
6575

6676

6777
def main():
68-
parser = argparse.ArgumentParser()
69-
parser.add_argument('roots', type=str, nargs='+', metavar='ROOT_PATH')
70-
# parser.add_argument('-d', '--dry-run', action='store_true', default=False)
78+
parser = argparse.ArgumentParser(
79+
description='Rename files and directories to play nice with Windows')
80+
parser.add_argument('roots', type=str, nargs='+', metavar='PATH',
81+
help='Path(s) to rename')
82+
parser.add_argument('-r', '--recursive', action='store_true', default=False, help='Recurse into directories')
7183
args = parser.parse_args()
72-
rename_mangled(args.roots, True) # dry run
73-
response = input("is this okay? [y/N]: ")
84+
mangled_count = rename_mangled(args.roots, True, args.recursive) # dry run
85+
print(f"Found {mangled_count} invalid paths")
86+
if mangled_count == 0:
87+
sys.exit()
88+
response = input("Are the above changes okay? [y/N]: ")
7489
if response.upper() in ['Y', 'YES']:
75-
rename_mangled(args.roots, False) # really rename
90+
rename_mangled(args.roots, False, args.recursive) # really rename
7691

7792

7893
if __name__ == '__main__':

0 commit comments

Comments
 (0)