-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
120 lines (101 loc) · 4.27 KB
/
main.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import typer
import os
import pathlib
import csv
from rich.console import Console
from rich.table import Table
from rich.tree import Tree
from rich.text import Text
from rich.filesize import decimal
from rich.markup import escape
app = typer.Typer(rich_markup_mode="rich")
console = Console()
@app.command(
epilog="Made with :heart: by [blue][link=https://github.com/Syntoxine]Syntoxine[/link][/blue]"
)
def main(
directory: str = typer.Argument(
".\\",
help="The directory which will be checked for duplicates.",
show_default="Current directory",
),
show_tree: bool = typer.Option(False, help="Show a tree of the working directory."),
export: bool = typer.Option(False, help="Exports duplicates to a .csv file.")
) -> None:
"""Utility script to check for file duplicates."""
if show_tree:
def walk_directory(directory: pathlib.Path, tree: Tree) -> None:
"""
Recursively builds a Tree with directory contents.
Provided by Will McGugan
"""
paths = sorted(
pathlib.Path(directory).iterdir(),
key=lambda path: (path.is_file(), path.name.lower()),
)
for path in paths:
# Remove hidden files
if path.name.startswith("."):
continue
if path.is_dir():
style = "dim" if path.name.startswith("__") else ""
branch = tree.add(
f"[bold magenta]:open_file_folder: [link file://{path}]{escape(path.name)}",
style=style,
guide_style=style,
)
walk_directory(path, branch)
else:
text_filename = Text(path.name, "green")
text_filename.highlight_regex(r"\..*$", "red")
text_filename.stylize(f"link file://{path}")
file_size = path.stat().st_size
text_filename.append(f" ({decimal(file_size)})", "blue")
match path.suffix.lower():
case ".py": icon = "🐍 "
case ".png" | ".jpeg" | ".jpg": icon = "🎨 "
case ".mov" | ".mp4": icon = "🎥 "
case _: icon = "📄 "
tree.add(Text(icon) + text_filename)
fdirectory = os.path.abspath(directory)
tree = Tree(
f":open_file_folder: [link file://{fdirectory}]{fdirectory}",
guide_style="bright_blue",
)
walk_directory(pathlib.Path(fdirectory), tree)
console.print(tree)
elif export:
duplicates, file_paths = dir_walk(directory)
if duplicates:
with open('doubloon-duplicates.csv', 'w', newline='') as file:
writer = csv.writer(file, dialect = 'excel')
writer.writerows([["Duplicates"],["Name", "Filepath"]])
for i in duplicates:
for j in file_paths[i]:
writer.writerow([i, j])
console.print(f"Duplicates successfully exported to [blue]{directory}doubloon-duplicates.csv[/blue].")
else: console.print("[blue]No duplicates[/blue] found!")
else:
table = Table("Name", "Path", show_lines=True, title="Duplicates")
duplicates, file_paths = dir_walk(directory)
for i in duplicates:
for j in file_paths[i]:
table.add_row(i, j)
if duplicates : console.print(table)
else: console.print("[blue]No duplicates[/blue] found!")
def dir_walk(directory : str) -> tuple[list,dict]:
file_paths = {}
duplicates = []
for root, dirs, files in os.walk(directory):
files = [f for f in files if not f[0] == '.']
dirs[:] = [d for d in dirs if not d[0] == '.']
for i in files:
file_path = os.path.join(root, i)
try:
file_paths[i] = [*file_paths[i],file_path]
duplicates.append(i)
except KeyError:
file_paths[i] = [file_path]
return (duplicates, file_paths)
if __name__ == "__main__":
app()