-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeplicate_cli.py
222 lines (174 loc) · 6.56 KB
/
deplicate_cli.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# -*- coding: utf-8 -*-
#
# _/ _/ _/ _/
# _/_/_/ _/_/ _/_/_/ _/ _/_/_/ _/_/_/ _/_/_/_/ _/_/
# _/ _/ _/_/_/_/ _/ _/ _/ _/ _/ _/ _/ _/ _/_/_/_/
# _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/
# _/_/_/ _/_/_/ _/_/_/ _/ _/ _/_/_/ _/_/_/ _/_/ _/_/_/
# _/
# _/
from __future__ import absolute_import
import pkg_resources
import time
import click
import duplicate
from contextlib import closing
from multiprocessing.pool import ThreadPool
def _get_separator():
return '{{0:-^{0}}}'.format(click.get_terminal_size()[0])
def _echo_results(results, verbose, showerr):
dups, errors, scanerrors = results
sep = _get_separator()
if verbose:
click.secho(sep.format(' DUPLICATES '), bold=True)
click.echo()
with closing(ThreadPool()) as pool:
for files in dups:
pool.map(click.echo, files)
click.echo()
if not showerr:
return
click.echo()
if verbose:
click.secho(sep.format(' PROCESS ERRORED '), bold=True)
click.echo()
for files in errors:
map(click.echo, files)
click.echo()
if verbose:
click.secho(sep.format(' SCAN ERRORED '), bold=True)
click.echo()
map(click.echo, scanerrors)
def _notifier(message):
click.secho(' Done.', fg='green')
text = '> {0}...'.format(message.capitalize())
click.echo(text, nl=False)
def _get_deplicate_version():
return pkg_resources.get_distribution('deplicate').version
def _find_results(verbose, kwgs):
core = duplicate.core
paths = kwgs.pop('paths')
if verbose > 2:
text = '> Starting deplicate (v{0})...'
click.echo(text.format(_get_deplicate_version()), nl=False)
kwgs['notify'] = _notifier
start_time = time.time()
filedups, scanerrors = core._find(paths, **kwgs)
dups = core._listdups(filedups)
errors = core._listerrors(filedups)
elapsed_seconds = time.time() - start_time
formatted_seconds = _format_seconds(elapsed_seconds)
if verbose > 2:
click.secho(' Done.', fg='green')
click.echo()
if verbose > 3:
sep = _get_separator()
click.secho(sep.format(' STATISTICS '), bold=True)
click.echo()
text = 'Completed in {0:f} seconds ({1}).'
click.echo(text.format(elapsed_seconds, formatted_seconds))
click.echo()
return dups, errors, scanerrors
def _format_seconds(seconds):
m, s = divmod(int(seconds), 60)
h, m = divmod(m, 60)
d, h = divmod(h, 24)
text = '{0} days, {1} hours, {2} minutes, {3} seconds'
return text.format(d, h, m, s)
def _cli(verbose, showerr, kwgs):
if verbose:
click.echo()
dups, errors, scanerrors = results = _find_results(verbose, kwgs)
if verbose:
with closing(ThreadPool()) as pool:
duplen = sum(pool.imap(len, dups))
errlen = sum(pool.imap(len, errors))
scnerrlen = len(scanerrors)
if not showerr and (errlen or scnerrlen):
text = '>> Errors detected during processing! ' \
'(use option `--show-errors` to report errors)'
click.secho(text, fg='red')
click.echo()
if verbose > 1:
if verbose > 2:
sep = _get_separator()
click.secho(sep.format(' RESULT '), bold=True)
click.echo()
if errlen or scnerrlen:
color = 'yellow'
elif duplen:
color = 'reset'
else:
color = 'green'
click.secho('Found {0} duplicate files.'.format(duplen), fg=color)
click.echo()
if showerr:
color = 'red' if errlen else 'green'
click.secho('Failed to process {0} files.'.format(errlen),
fg=color)
color = 'red' if scnerrlen else 'green'
click.secho('Failed to scan {0} files.'.format(scnerrlen),
fg=color)
click.echo()
_echo_results(results, verbose, showerr)
@click.command()
@click.argument('paths',
type=click.Path(exists=True),
nargs=-1)
@click.option('--verbose', '-v',
count=True,
help='Verbose mode (repeat to increase verbosity).')
@click.option('--show-errors', '-a',
is_flag=True,
help='Show ignored files due errors.')
@click.option('--minsize', '-s',
type=click.IntRange(min=0, clamp=True),
default=None,
help='Minimum size of files to include in scanning.')
@click.option('--include', '-i',
help='Wildcard pattern of files to include in scanning.')
@click.option('--exclude', '-e',
help='Wildcard pattern of files to exclude from scanning.')
@click.option('--comparename', '-n',
is_flag=True,
help='Check file name.')
@click.option('--comparemtime', '-m',
is_flag=True,
help='Check file modification time.')
@click.option('--compareperms', '-p',
is_flag=True,
help='Check file mode (permissions).')
@click.option('--recursive/--no-recursive',
default=True,
help='Scan directory recursively.')
@click.option('--followlinks',
is_flag=True,
help='Follow symbolic links pointing to directory.')
@click.option('--scanlinks',
is_flag=True,
help='Scan symbolic links pointing to file.')
@click.option('--scanempties',
is_flag=True,
help='Scan empty files.')
@click.option('--scansystems/--ignoresystems',
default=True,
help='Scan OS files.')
@click.option('--scanarchived/--ignorearchived',
default=True,
help='Scan archived files.')
@click.option('--scanhidden/--ignorehidden',
default=True,
help='Scan hidden files.')
@click.option('--signsize',
type=click.IntRange(min=0, clamp=True),
default=None,
help='Size of Bytes to read from file as signature.')
@click.version_option()
def cli(**kwgs):
"""
Advanced Duplicate File Finder for Python. Nothing is impossible to solve.
Copyright 2017 Walter Purcaro <[email protected]>
"""
verbose = kwgs.pop('verbose')
showerr = kwgs.pop('show_errors')
_cli(verbose, showerr, kwgs)