enotkrutoy commited on
Commit
0e7e5ca
·
verified ·
1 Parent(s): 5e450d9

Upload eviltree.py

Browse files
Files changed (1) hide show
  1. test/eviltree.py +573 -0
test/eviltree.py ADDED
@@ -0,0 +1,573 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/python3
2
+ #
3
+ # Author: Panagiotis Chartas (t3l3machus)
4
+ # https://github.com/t3l3machus
5
+
6
+ import os, re, argparse
7
+ from pathlib import Path
8
+ from stat import S_ISCHR, ST_MODE, S_ISBLK, S_ISFIFO, S_ISSOCK
9
+ from platform import system as get_system_type
10
+ from sys import exit as _exit
11
+
12
+
13
+ def move_on():
14
+ pass
15
+
16
+
17
+ # Enable colors if Windows
18
+ WINDOWS = True if get_system_type() == 'Windows' else False
19
+ os.system('') if WINDOWS else move_on
20
+
21
+ ''' Colors '''
22
+ LINK = '\033[1;38;5;37m'
23
+ BROKEN = '\033[48;5;234m\033[1;31m'
24
+ CHARSPEC = '\033[0;38;5;228m'
25
+ #CHARSPEC = '\033[48;5;234m\033[1;33m'
26
+ #PIPE = '\033[38;5;214m'
27
+ PIPE = '\033[48;5;234m\033[1;30m'
28
+ SOCKET = '\033[1;38;5;98m'
29
+ EXECUTABLE = '\033[1;38;5;43m'
30
+ DENIED = '\033[38;5;222m'
31
+ DEBUG = '\033[0;38;5;214m'
32
+ GREEN = '\033[38;5;47m'
33
+ DIR = '\033[1;38;5;12m'
34
+ MATCH = '\033[1;38;5;201m'
35
+ FAILED = '\033[1;31m'
36
+ END = '\033[0m'
37
+ BOLD = '\033[1m'
38
+
39
+ # -------------- Arguments & Usage -------------- #
40
+ parser = argparse.ArgumentParser(
41
+ formatter_class = argparse.RawTextHelpFormatter,
42
+ epilog = '''
43
+
44
+ Usage tips:
45
+
46
+ - Regex -x search actually returns a unique list of all matched patterns in a file. Be careful when combining it with -v (--verbose), try to be specific and limit the length of chars to match.
47
+ - You can use this tool as the classic "tree" command if you do not provide keywords -k and regex -x values. This is useful in case you have gained a limited shell on a machine and want to have "tree" with colored output to look around.
48
+
49
+ Useful keywords/regex patterns:
50
+
51
+ Regex to look for passwords: -x ".{0,3}passw.{0,3}[=]{1}.{0,18}"
52
+ Keywords to look for sensitive info: -k passw,db_,admin,account,user,token
53
+
54
+ '''
55
+ )
56
+
57
+ parser.add_argument("-r", "--root-path", action="store", help = "The root path to walk.", required = True)
58
+ parser.add_argument("-k", "--keywords", action="store", help = "Comma separated keywords to search for in files.")
59
+ parser.add_argument("-x", "--regex", action="store", help = "Regex pattern to search for in files. In combination with --verbose, this option returns all values that matched the given pattern for each file, you should use it with caution while setting character limits before and after your actual pattern.")
60
+ parser.add_argument("-a", "--match-all", action="store_true", help = "By default, files are highlighted when at least one keyword is matched. Use this option to highlight files that match all given keywords only. This option has no effect when used with regex.")
61
+ parser.add_argument("-L", "--level", action="store", help = "Descend only level directories deep.", type = int)
62
+ parser.add_argument("-c", "--case-sensitive", action="store_true", help = "Enables case sensitive keywords/regex search.")
63
+ parser.add_argument("-b", "--binary", action="store_true", help = "Search for keywords/regex in binary files too.")
64
+ parser.add_argument("-v", "--verbose", action="store_true", help = "Print information about which keyword(s) matched.")
65
+ parser.add_argument("-i", "--interesting-only", action="store_true", help = "List only files with matching keywords/regex content (significantly reduces output length).")
66
+ parser.add_argument("-f", "--full-pathnames", action="store_true", help = "Print absolute file and directory paths.")
67
+ parser.add_argument("-A", "--ascii", action="store_true", help = "Use ASCII instead of extended characters (use this in case of \"UnicodeEncodeError: 'ascii' codec...\" along with -q).")
68
+ parser.add_argument("-d", "--directories-only", action="store_true", help = "List directories only.")
69
+ parser.add_argument("-l", "--follow-links", action="store_true", help = "Follows symbolic links if they point to directories, as if they were directories. Symbolic links that will result in recursion are avoided when detected.")
70
+ parser.add_argument("-q", "--quiet", action="store_true", help = "Do not print the banner on startup.")
71
+
72
+ args = parser.parse_args()
73
+
74
+
75
+ def exit_with_msg(msg):
76
+ print('[' + DEBUG + 'Debugger' + END + '] ' + msg)
77
+ _exit(1)
78
+
79
+
80
+ # Init keyword(s)/regex list if any provided
81
+ if args.keywords and args.regex:
82
+ exit_with_msg("You can't use keywords (-k) and regex (-x) at the same time.")
83
+
84
+ keywords = []
85
+
86
+ if args.keywords:
87
+ for word in args.keywords.split(","):
88
+ if len(word.strip()) > 0:
89
+ keywords.append(word.strip())
90
+
91
+ verify = [k for k in keywords if k.strip() != '']
92
+
93
+ if not len(verify):
94
+ exit_with_msg("Illegal keyword(s) value(s).")
95
+
96
+
97
+ elif args.regex:
98
+ keywords = [args.regex]
99
+
100
+ total_keywords = len(keywords)
101
+
102
+
103
+ # Define depth level
104
+ if isinstance(args.level, int):
105
+ depth_level = args.level if (args.level > 0) else exit_with_msg('Level (-L) must be greater than 0.')
106
+
107
+ else:
108
+ depth_level = 4096
109
+
110
+
111
+ process_files = True if (args.keywords or args.regex) else False
112
+ print_fnames = True if not args.directories_only else False
113
+
114
+ # Filetypes to avoid searching for keywords/regex:
115
+ filetype_blacklist = ['gz', 'zip', 'tar', 'rar', '7z', 'bz2', 'xz', 'deb', 'img', 'iso', 'vmdk', 'dll', 'ovf', 'ova']
116
+
117
+ # -------------- Functions -------------- #
118
+
119
+ def print_banner():
120
+
121
+ print('\r')
122
+ padding = ' '
123
+
124
+ E = [[' ', '┌', '─', '┐'], [' ', '├┤',' '], [' ', '└','─','┘']]
125
+ V = [[' ', '┬', ' ', ' ', '┬'], [' ', '└','┐','┌', '┘'], [' ', ' ','└','┘', ' ']]
126
+ I = [[' ', '┬'], [' ', '│',], [' ', '┴']]
127
+ L = [[' ', '┬',' ',' '], [' ', '│',' ', ' '], [' ', '┴','─','┘']]
128
+ T = [[' ', '┌','┬','┐'], [' ', ' ','│',' '], [' ', ' ','┴',' ']]
129
+ R = [[' ', '┬','─','┐'], [' ', '├','┬','┘'], [' ', '┴','└','─']]
130
+
131
+ banner = [E,V,I,L,T,R,E,E]
132
+ final = []
133
+ init_color = 43
134
+ txt_color = init_color
135
+ cl = 0
136
+
137
+ for charset in range(0, 3):
138
+ for pos in range(0, len(banner)):
139
+ for i in range(0, len(banner[pos][charset])):
140
+ clr = '\033[38;5;' + str(txt_color) + 'm'
141
+ char = clr + banner[pos][charset][i]
142
+ final.append(char)
143
+ cl += 1
144
+ txt_color = txt_color + 36 if cl <= 3 else txt_color
145
+
146
+ cl = 0
147
+
148
+ txt_color = init_color
149
+ init_color += 31
150
+
151
+ if charset < 2: final.append('\n ')
152
+
153
+ print(' ' + ''.join(final))
154
+ print(END + padding +' by t3l3machus\n')
155
+
156
+
157
+
158
+ def load_file(file_path, mode):
159
+ f = open(file_path, mode)
160
+ content = f.read()
161
+ f.close()
162
+ return content
163
+
164
+
165
+
166
+ def decoder(l):
167
+
168
+ decoded = []
169
+
170
+ for item in l:
171
+ if isinstance(item, bytes):
172
+ decoded.append(item.decode('utf-8', 'ignore'))
173
+ else:
174
+ decoded.append(item)
175
+
176
+ return decoded
177
+
178
+
179
+
180
+ def file_inspector(file_path, mode = 0):
181
+
182
+ try:
183
+
184
+ ''' Load file content accordingly '''
185
+ try:
186
+ content = load_file(file_path, 'r')
187
+ binary = False
188
+
189
+ except UnicodeDecodeError:
190
+ content = load_file(file_path, 'rb')
191
+ binary = True
192
+
193
+ except PermissionError:
194
+ return 1
195
+
196
+ except MemoryError:
197
+ return 2
198
+
199
+ except KeyboardInterrupt:
200
+ return 999
201
+ #exit_with_msg('Keyboard interrupt.')
202
+
203
+ except:
204
+ return FAILED
205
+
206
+ ''' Search content '''
207
+
208
+ matched = []
209
+
210
+ for w in keywords:
211
+
212
+ ''' Set regex '''
213
+ if args.keywords:
214
+ w = re.escape(w)
215
+
216
+ if binary:
217
+ regex = re.compile(bytes(w.encode('utf-8'))) if args.case_sensitive else re.compile(bytes(w.encode('utf-8')), re.IGNORECASE)
218
+ else:
219
+ regex = w
220
+
221
+ ''' Search file contents '''
222
+ if binary and args.binary:
223
+ found = re.search(regex, content) if args.keywords else re.findall(regex, content)
224
+
225
+ elif not binary:
226
+ if args.keywords:
227
+ found = re.search(regex, content) if args.case_sensitive else re.search(regex, content, re.IGNORECASE)
228
+ elif args.regex:
229
+ found = re.findall(regex, content)
230
+
231
+ else:
232
+ found = False
233
+
234
+ ''' Handle matches '''
235
+ if found:
236
+ if args.keywords:
237
+ matched.append(w)
238
+ elif args.regex:
239
+ for match in found:
240
+ matched.append(match)
241
+
242
+ if args.keywords and not args.match_all and not args.verbose:
243
+ return MATCH
244
+
245
+ if args.binary:
246
+ matched = decoder(matched)
247
+
248
+ if not args.match_all and args.keywords and len(matched):
249
+ return [MATCH, " " + GREEN + "[" + ', '.join(matched) + "]" + END]
250
+
251
+ if args.match_all and args.keywords and len(matched) == total_keywords:
252
+ return MATCH if not args.verbose else [MATCH, " " + GREEN + "[" + ', '.join(matched) + "]" + END]
253
+
254
+ if args.regex:
255
+
256
+ if not args.verbose and matched:
257
+ return MATCH
258
+
259
+ elif args.verbose and matched:
260
+ unique = list(set(matched))
261
+ return [MATCH, " " + GREEN + "[" + ', '.join(unique) + "]" + END]
262
+
263
+ return ''
264
+
265
+ except:
266
+ return FAILED
267
+
268
+
269
+
270
+ def fake2realpath(path, target):
271
+
272
+ sep_count = target.count(".." + os.sep)
273
+ regex_chk_1 = "^" + re.escape(".." + os.sep)
274
+ regex1_chk_2 = "^" + re.escape("." + os.sep)
275
+ regex1_chk_3 = "^" + re.escape(os.sep)
276
+
277
+ if (re.search(regex_chk_1, target)) and (sep_count <= (path.count(os.sep) - 1)):
278
+ dirlist = [d for d in path.split(os.sep) if d.strip()]
279
+ dirlist.insert(0, os.sep)
280
+
281
+ try:
282
+ realpath = ''
283
+
284
+ for i in range(0, len(dirlist) - sep_count ):
285
+ realpath = realpath + (dirlist[i] + os.sep) if dirlist[i] != "/" else dirlist[i]
286
+
287
+ realpath += target.split(".." + os.sep)[-1]
288
+ return str(Path(realpath).resolve())
289
+
290
+ except:
291
+ return None
292
+
293
+ elif re.search(regex1_chk_2, target):
294
+ return str(Path((path + (target.replace("." + os.sep, "")))).resolve())
295
+
296
+ elif not re.search(regex1_chk_3, target):
297
+ return str(Path(path + target).resolve())
298
+
299
+ else:
300
+ return str(Path(target).resolve())
301
+
302
+
303
+
304
+ def adjustUnicodeError():
305
+ exit_with_msg('The system seems to have an uncommon default encoding. Restart eviltree with options -q and -A to resolve this issue.')
306
+
307
+
308
+ # ~ child = '├── ' if not args.ascii else '|-- '
309
+ # ~ child_last = '└── ' if not args.ascii else '|-- '
310
+ # ~ parent = '│ ' if not args.ascii else '| '
311
+ child = (chr(9500) + (chr(9472) * 2) + ' ') if not args.ascii else '|-- '
312
+ child_last = (chr(9492) + (chr(9472) * 2) + ' ') if not args.ascii else '\-- '
313
+ parent = (chr(9474) + ' ') if not args.ascii else '| '
314
+ total_dirs_processed = 0
315
+ total_files_processed = 0
316
+
317
+
318
+ def eviltree(root_dir, intent = 0, depth = '', depth_level = depth_level):
319
+
320
+ try:
321
+ global total_dirs_processed, total_files_processed
322
+ root_dirs = next(os.walk(root_dir))[1]
323
+ root_files = next(os.walk(root_dir))[2]
324
+ total_dirs = len(root_dirs)
325
+ total_files = len(root_files)
326
+ symlinks = []
327
+ recursive = []
328
+ print('\r' + DIR + root_dir + END) if not intent else move_on()
329
+
330
+
331
+ ''' Handle symlinks '''
332
+ for d in root_dirs:
333
+ if os.path.islink(root_dir + d):
334
+ symlinks.append(d)
335
+
336
+
337
+ ''' Process files '''
338
+ root_files.sort()
339
+
340
+ for i in range(0, total_files):
341
+
342
+ total_files_processed += 1
343
+ file_path = root_dir + root_files[i]
344
+ is_link = True if os.path.islink(file_path) else False
345
+
346
+ if not WINDOWS:
347
+
348
+ try:
349
+ is_char_special = True if S_ISCHR((os.lstat(file_path)[ST_MODE])) else False
350
+ except:
351
+ is_char_special = False
352
+
353
+ try:
354
+ is_block_special = True if S_ISBLK(os.stat(file_path)[ST_MODE]) else False
355
+ except:
356
+ is_block_special = False
357
+
358
+ try:
359
+ is_pipe = True if S_ISFIFO(os.stat(file_path).st_mode) else False
360
+ except:
361
+ is_pipe = False
362
+
363
+ try:
364
+ is_socket = True if S_ISSOCK(os.stat(file_path).st_mode) else False
365
+ except:
366
+ is_socket = False
367
+
368
+ try:
369
+ is_executable = True if (os.access(file_path, os.X_OK) and not is_char_special and not is_block_special and not is_pipe and not is_socket) else False
370
+ except:
371
+ is_executable = False
372
+
373
+ else:
374
+ is_executable, is_socket, is_pipe, is_block_special, is_char_special = False, False, False, False, False
375
+
376
+
377
+ ''' Verify target path if file is a symlink '''
378
+ if is_link:
379
+
380
+ symlink_target = target = os.readlink(file_path) if is_link else None
381
+ target = fake2realpath(root_dir, target)
382
+ is_dir = True if os.path.isdir(str(target)) else False
383
+ is_broken = True if (not os.path.exists(str(target)) or is_dir) else False
384
+
385
+ if not WINDOWS:
386
+
387
+ try:
388
+ target_is_char_special = True if S_ISCHR((os.lstat(target)[ST_MODE])) else False
389
+ except:
390
+ target_is_char_special = False
391
+
392
+ try:
393
+ target_is_block_special = True if S_ISBLK(os.stat(target).st_mode) else False
394
+ except:
395
+ target_is_block_special = False
396
+
397
+ try:
398
+ target_is_pipe = True if S_ISFIFO(os.stat(target).st_mode) else False
399
+ except:
400
+ target_is_pipe = False
401
+
402
+ try:
403
+ target_is_socket = True if S_ISSOCK(os.stat(target).st_mode) else False
404
+ except:
405
+ target_is_socket = False
406
+
407
+ try:
408
+ target_is_executable = True if (os.access(target, os.X_OK) and not target_is_char_special and not target_is_block_special and not target_is_pipe and not target_is_socket) else False
409
+ except:
410
+ target_is_executable = False
411
+
412
+ else:
413
+ target_is_executable, target_is_socket, target_is_pipe, target_is_block_special, target_is_char_special = False, False, False, False, False
414
+
415
+ else:
416
+ is_broken = None
417
+
418
+ ''' Submit file for content inspection if it's not a broken symlink / character special / block special / pipe / socket'''
419
+ if process_files:
420
+
421
+ # Check if file extension in blacklist
422
+ fname = root_files[i] if not is_link else target
423
+ file_ext = fname.rsplit(".", 1)[-1].lower()
424
+ blacklisted = True if file_ext in filetype_blacklist else False
425
+
426
+ if (not is_link and not is_char_special and not is_block_special and not is_pipe and not is_socket and not blacklisted) or (is_link and not is_broken and not target_is_char_special and not target_is_block_special and not target_is_pipe and not target_is_socket and not blacklisted):
427
+ details = file_inspector(file_path) if not is_link else file_inspector(target)
428
+
429
+ if isinstance(details, list):
430
+ color, verbose, errormsg = details[0], details[1], ''
431
+
432
+ elif details == 1:
433
+ color, verbose, errormsg = details, '', ' ' + DENIED + '[permission denied]' + END
434
+
435
+ elif details == 2:
436
+ color, verbose, errormsg = details, '', ' ' + DENIED + '[memory error]' + END
437
+
438
+ elif details == 999:
439
+ exit_with_msg('Keyboard interrupt.')
440
+
441
+ else:
442
+ color, verbose, errormsg = details, '', ''
443
+
444
+ else:
445
+ color, verbose, errormsg = '', '', ''
446
+
447
+ else:
448
+ color, verbose, errormsg = '', '', ''
449
+
450
+
451
+ ''' Color mark and print file accordingly in relation to its type and content inspection results '''
452
+ if print_fnames:
453
+
454
+ color = str(color)
455
+
456
+ if is_link:
457
+ linkcolor = BROKEN if is_broken else LINK
458
+ color = CHARSPEC if target_is_block_special or target_is_char_special else color
459
+ color = PIPE if target_is_pipe else color
460
+ color = SOCKET if target_is_socket else color
461
+ color = EXECUTABLE if (target_is_executable and color not in [MATCH, FAILED]) else color
462
+ filename = (linkcolor + root_files[i] + END + ' -> ' + color + symlink_target + errormsg) if not args.full_pathnames else (linkcolor + root_dir + root_files[i] + END + ' -> ' + color + symlink_target + errormsg)
463
+
464
+ elif is_char_special or is_block_special:
465
+ filename = (CHARSPEC + root_files[i]) if not args.full_pathnames else (CHARSPEC + root_dir + root_files[i])
466
+
467
+ elif is_pipe:
468
+ filename = (PIPE + root_files[i]) if not args.full_pathnames else (PIPE + root_dir + root_files[i])
469
+
470
+ elif is_socket:
471
+ filename = (SOCKET + root_files[i]) if not args.full_pathnames else (SOCKET + root_dir + root_files[i])
472
+
473
+ elif is_executable:
474
+ if color not in [MATCH, FAILED]:
475
+ filename = (EXECUTABLE + root_files[i]) if not args.full_pathnames else (EXECUTABLE + root_dir + root_files[i])
476
+ else:
477
+ filename = (color + root_files[i]) if not args.full_pathnames else (color + root_dir + root_files[i])
478
+ else:
479
+
480
+ filename = (color + root_files[i]) if not args.full_pathnames else (color + root_dir + root_files[i])
481
+
482
+ ''' Print file branch '''
483
+ if not args.interesting_only:
484
+ print(depth + child + color + filename + END + verbose + errormsg) if (i < (total_files + total_dirs) - 1) else print(depth + child_last + color + filename + END + verbose + errormsg)
485
+
486
+ elif args.interesting_only and ((color and color == MATCH) and not errormsg):
487
+ print(depth + child + color + filename + END + verbose + errormsg) if (i < (total_files + total_dirs) - 1) else print(depth + child_last + color + filename + END + verbose + errormsg)
488
+
489
+
490
+
491
+ ''' Process dirs '''
492
+ root_dirs.sort()
493
+
494
+ for i in range(0, total_dirs):
495
+
496
+ total_dirs_processed += 1
497
+ joined_path = root_dir + root_dirs[i]
498
+ is_recursive = False
499
+ directory = root_dirs[i] if not args.full_pathnames else (joined_path + os.sep)
500
+
501
+ ''' Access permissions check '''
502
+ try:
503
+ sub_dirs = len(next(os.walk(joined_path))[1])
504
+ sub_files = len(next(os.walk(joined_path))[2])
505
+ errormsg = ''
506
+
507
+ except StopIteration:
508
+ sub_dirs, sub_files = 0, 0
509
+ errormsg = ' [error accessing dir]'
510
+
511
+
512
+ ''' Check if symlink and if target leads to recursion '''
513
+ if root_dirs[i] in symlinks:
514
+ symlink_target = target = os.readlink(joined_path)
515
+ target = fake2realpath(root_dir, target)
516
+ is_recursive = ' [recursive, not followed]' if target == root_dir[0:-1] else ''
517
+
518
+ if len(is_recursive):
519
+ recursive.append(joined_path)
520
+
521
+ print(depth + child + LINK + directory + END + ' -> ' + DIR + symlink_target + END + is_recursive + errormsg) if i < total_dirs - 1 else print(depth + child_last + LINK + directory + END + ' -> ' + DIR + symlink_target + END + is_recursive + errormsg)
522
+
523
+ else:
524
+ print(depth + child + DIR + directory + END + errormsg) if i < total_dirs - 1 else print(depth + child_last + DIR + directory + END + errormsg)
525
+
526
+ ''' Iterate next dir '''
527
+ if (not args.follow_links and root_dirs[i] not in symlinks) or (args.follow_links and not is_recursive):
528
+ if (sub_dirs or sub_files) and (intent + 1) < depth_level:
529
+ tmp = depth
530
+ depth = depth + parent if i < (total_dirs - 1) else depth + ' '
531
+ eviltree(joined_path + os.sep, intent + 1, depth)
532
+ depth = tmp
533
+
534
+
535
+ except StopIteration:
536
+ print('\r' + DIR + root_dir + END + ' [Permission Denied]')
537
+ #print('\r' + DIR + root_dir + END + ' [error accessing dir]')
538
+
539
+ except UnicodeEncodeError:
540
+ adjustUnicodeError()
541
+
542
+ except KeyboardInterrupt:
543
+ exit_with_msg('Keyboard interrupt.')
544
+
545
+ except PermissionError:
546
+ print('\r' + DIR + root_dir + END + ' [Permission Denied]')
547
+
548
+ except Exception as e:
549
+ exit_with_msg('Something went wrong. Consider creating an issue about this in the original repo (https://github.com/t3l3machus/eviltree)\n' + BOLD + 'Error Details' + END +': ' + str(e))
550
+
551
+
552
+
553
+ def main():
554
+
555
+ try:
556
+ print_banner() if not args.quiet else move_on()
557
+
558
+ except UnicodeEncodeError:
559
+ adjustUnicodeError()
560
+
561
+ root_dir = args.root_path if args.root_path[-1] == os.sep else args.root_path + os.sep
562
+
563
+ if os.path.exists(root_dir):
564
+ eviltree(root_dir)
565
+ print('\n' + str(total_dirs_processed) + ' directories, ' + str(total_files_processed) + ' files')
566
+
567
+ else:
568
+ exit_with_msg('Directory does not exist.')
569
+
570
+
571
+
572
+ if __name__ == '__main__':
573
+ main()