-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch2.py
268 lines (248 loc) · 6.86 KB
/
search2.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/python
'''
v.14 As docs says about '(?aiLmsux)' switch
"Changed in version 3.11: This construction can only be used at the start of the expression"
so it's changed to work with 3.11
v.13, status: alpha - USE IT AT YOUR OWN RISK!!!
IT COULD POTENTIALLY CAUSE DAMAGE OR DATA LOSS
changes:
- '-a' | '--append' option added
- fixing "<driveletter>:" bug
v.12
changes:
- now it uses modified version of fnmatch.translate function instead of fnmatch module
this allows file multi-patterns i.e. patterns separated by semicolon e.g.
*.zip;*.py
v.11
changes:
- now when in scan mode it creates 'dirinfo.txt' file
which contains directory paths and summarized size of all files (of given pattern)
in each of them
'''
import os, sys, getopt, shutil, re
def usage():
print(
"usage:",
" search2 --move --search-dir=c:\\tym --dest-dir=f:\\tymczas --pattern=*.odt",
"or",
" search2.py -p*.odt -sc:\\tym -de:\\tu -c",
"multi-pattern case:",
" search2.py -p*.odt;*.ods -sc:\\tym -de:\\tu -c",
"options:",
" -s [or --search-dir=]",
" -d [or --dest-folder=]",
" -p [or --pattern=]",
" -c [or --copy] OR -m [or --move] OR -n [or --scan or none of]",
" -o [or --output=] OR if not specified output file is 'output.txt'",
" -a [or --append]",
"notes:",
" when specify whole disk drive use -s\"c:\" not -s\"c:\\\"",
" - output file (by default 'output.txt') contains list of full pathnames",
" that match the pattern",
" - 'error.log' file contains list of non-critical errors that occured",
" during scan",
" - when '-d' script creates output file (and 'error.log' and/or 'dirinfo.txt')",
" in specified directory",
" - when in scan mode ie '-n' [or '--scan' or none] it produces 'dirinfo.txt'",
" which contains directory paths and summarized size of all files",
" (of given pattern) in each of them",
" '--append' option makes data to be appended at the end of existing file(s)",
" not creating new ones",
sep="\n")
def failsafe_makedirs(dir):
try:
os.makedirs(dir)
except:
return False
else:
return True
'''
origin: fnmatch module
'''
def translate(pat):
"""Translate a shell PATTERN to a regular expression.
There is no way to quote meta-characters.
"""
i, n = 0, len(pat)
res = ''
while i < n:
c = pat[i]
i = i+1
if c == '*':
res = res + '.*'
elif c == '?':
res = res + '.'
elif c == '[':
j = i
if j < n and pat[j] == '!':
j = j+1
if j < n and pat[j] == ']':
j = j+1
while j < n and pat[j] != ']':
j = j+1
if j >= n:
res = res + '\\['
else:
stuff = pat[i:j].replace('\\','\\\\')
i = j+1
if stuff[0] == '!':
stuff = '^' + stuff[1:]
elif stuff[0] == '^':
stuff = '\\' + stuff
res = '%s[%s]' % (res, stuff)
# added
elif c == ';':
res = res + '\Z|'
else:
res = res + re.escape(c)
return '(?si)' + res + '\Z'
def main():
search_dir = ''
dest_dir = ''
pattern = ''
copy = False
move = False
scan = False
default_output = 'output.txt'
output = ''
work = False
append = False
try:
opts, args = getopt.getopt(sys.argv[1:], "s:d:p:cmno:ha", ["search-dir=","dest-dir=","pattern=","copy","move","scan","output=","help","append"])
except getopt.GetoptError as err:
print(err)
usage()
sys.exit(2)
for o, a in opts:
if o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("-s", "--search-dir"):
if a[-1] == ':':
a = a + '\\'
search_dir = a
print(o, a)
elif o in ("-d", "--dest-dir"):
dest_dir = a
print(o, a)
elif o in ("-p", "--pattern"):
pattern = a
print(o, a)
elif o in ("-c", "--copy"):
copy = True
print(o, a)
elif o in ("-m", "--move"):
move = True
print(o, a)
elif o in ("-n", "--scan"):
scan = True
print(o, a)
elif o in ("-o", "--output"):
output = a
print(o, a)
elif o in ("-a", "--append"):
append = True
print(o, a)
if not move and not copy:
scan = True
work = 'scan'
elif move and copy:
sys.exit("you provided both --copy and --move paramters!, quit")
elif move:
work = 'move'
elif copy:
work = 'copy'
if pattern == '':
sys.exit("you didn't provide -p or --pattern parameter, use --help for help")
if work in ('scan', 'move', 'copy'):
if search_dir == '':
sys.exit("you should have provided -s or --search-dir parameter!")
elif not os.path.isdir(search_dir):
sys.exit("--search-dir parameter '" + search_dir +"' you provided is not valid path!")
if work in ('move', 'copy'):
if dest_dir == '':
sys.exit("you should have provided -d or --dest-dir parameter!")
elif not os.path.isdir(dest_dir):
if not failsafe_makedirs(dest_dir):
sys.exit("1: error creating dir: " + dest_dir)
if work == 'scan' and dest_dir != '':
if not os.path.isdir(dest_dir):
if not failsafe_makedirs(dest_dir):
sys.exit("1: error creating dir: " + dest_dir)
'''
when -o is not specified then default_output ('output.txt')
'''
output = output or default_output
errlog = 'error.log'
dirinfo = ''
if work == 'scan':
dirinfo = 'dirinfo.txt'
'''
when -d then output file
is located inside --dest_dir
'''
if dest_dir != '':
output = dest_dir + '/' + output
errlog = dest_dir + '/' + errlog
if dirinfo:
dirinfo = dest_dir + '/' + dirinfo
try:
out = open(output, 'a' if append else 'w')
except IOError as err:
print(err)
sys.exit(2)
try:
log = open(errlog, 'a' if append else 'w')
except IOError as err:
print(err)
sys.exit(2)
if dirinfo:
try:
idir = open(dirinfo, 'a' if append else 'w')
except IOError as err:
print(err)
sys.exit(2)
fullpath = ''
stores = {}
pattern = translate(pattern)
print(pattern)
regex = re.compile(pattern)
for root, dirs, files in os.walk(search_dir):
for file in files:
if regex.match(file):
fullpath = os.path.join(root, file)
try:
size = os.path.getsize(fullpath)
except OSError as e:
print(f"Unable to open {fullpath}: {e}", file=log)
if work in ('scan', 'copy', 'move'):
try:
print(fullpath, size, sep=';', file=out)
except:
print(ascii(fullpath), size, sep=';', file=log)
if work in ('copy', 'move'):
extpath = os.path.splitdrive(fullpath)[1]
head = os.path.split(extpath)[0]
if not os.path.isdir(dest_dir + '/' + head):
if not failsafe_makedirs(dest_dir + '/' + head):
sys.exit("2: error creating dir: " + dest_dir + '/' + head)
if work == 'copy':
try:
shutil.copy(fullpath, dest_dir + '/' + extpath)
except WindowsError:
print(fullpath, "- can't copy file!!!", sep=';', file=log)
elif work == 'move':
try:
shutil.move(fullpath, dest_dir + '/' + extpath)
except WindowsError:
print(fullpath, "- can't move file!!!", sep=';', file=log)
if work == 'scan':
try:
stores[root] += size
except KeyError:
stores[root] = size
if work == 'scan':
for k in stores.keys():
print(k, stores[k], sep=';', file=idir)
if __name__ == "__main__":
main()