-
Notifications
You must be signed in to change notification settings - Fork 0
/
flatten_folder_tree.py
77 lines (63 loc) · 2 KB
/
flatten_folder_tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import argparse
from collections import Counter
from pathlib import Path
from shutil import move, rmtree
from typing import Iterable
from more_itertools import consume
from utils import git_mv
# get all files in a list
# move them to root folder
# delete all the empty folders
def parse_args():
parser = argparse.ArgumentParser(
description='Flatten folder structure; moving files to root folder.'
)
parser.add_argument(
'--folder',
type=Path,
help='Folder where the files are located',
)
parser.add_argument(
'--git',
action="store_true",
help='Signal whether the files are inside a git repo.',
)
parser.add_argument(
'--fast',
action="store_true",
help='Signal whether to run safety checks.',
)
args = parser.parse_args()
return args
def just_move(folder: Path, paths: Iterable[Path]):
for file in paths:
if file.is_file():
move_function(str(file), str(folder / file.name))
def check_and_move(folder: Path, paths: Iterable[Path]):
files = list(paths)
filenames = set(file.name for file in files)
if len(files) != len(filenames):
counts = Counter(file.name for file in files)
repeated = {
item: count for item, count in counts.items() if count > 1
}
print(repeated)
raise("Non-unique filenames found. Aborting.")
just_move(folder, files)
def delete_folders(paths: Iterable[Path]):
for dir_path in root_level_files:
if dir_path.is_dir():
try:
rmtree(dir_path)
except OSError as e:
print(f"Error: {dir_path} : {e.strerror}")
if __name__ == "__main__":
args = parse_args()
move_function = git_mv if args.git else move
paths = args.folder.rglob("*")
root_level_files = args.folder.glob("*")
if args.fast:
just_move(args.folder, paths)
else:
check_and_move(args.folder, paths)
delete_folders(root_level_files)