-
Notifications
You must be signed in to change notification settings - Fork 4
/
clean.py
executable file
·127 lines (97 loc) · 5.07 KB
/
clean.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
#!/usr/bin/env python
import argparse
import sys
import os
from variable_delay.src.argparse.help_formatter import BlankLinesHelpFormatter
SENDER = 'sender'
RECEIVER = 'receiver'
PCAP = '.pcap'
JSON = '.json'
PNG = '.png'
LOG = '.log'
WORKING_DIR = os.path.dirname(os.path.realpath(__file__))
DEFAULT_PCAPS_DIR_NAME = 'dumps'
DEFAULT_DATA_DIR_NAME = os.path.join('graphs', 'data')
DEFAULT_GRAPHS_DIR_NAME = 'graphs'
DEFAULT_PCAPS_PATH = os.path.join(WORKING_DIR, DEFAULT_PCAPS_DIR_NAME)
DEFAULT_DATA_PATH = os.path.join(WORKING_DIR, DEFAULT_DATA_DIR_NAME)
DEFAULT_GRAPHS_PATH = os.path.join(WORKING_DIR, DEFAULT_GRAPHS_DIR_NAME)
parser = argparse.ArgumentParser(formatter_class=BlankLinesHelpFormatter, description=
'The script cleans three output directories. The script deletes only pcap/json/png/log files and '
'does not touch any subdirectories. If any of the chosen directories gets completely empty the '
'script also deletes the directory.')
parser.add_argument('-a', '--all', action='store_true',
help='delete all files in the three directories, same as -pdg')
parser.add_argument('-p', '--pcap', '--pcaps', action='store_true',
help='delete all files in directory with pcap-files')
parser.add_argument('-d', '--data', action='store_true',
help='delete all files in directory with data-files')
parser.add_argument('-g', '--graph', '--graphs', action='store_true',
help='delete all files in directory with graphs')
parser.add_argument('-s', '--senders', '--sender', action='store_true',
help='among chosen files, delete files belonging exclusively to senders')
parser.add_argument('-r', '--receivers', '--receiver', action='store_true',
help='among chosen files, delete files belonging exclusively to receivers')
parser.add_argument('-m', '--mutual', action='store_true',
help='among chosen files, delete files common for senders and receivers')
parser.add_argument('-f1', '--folder1', default=DEFAULT_PCAPS_PATH,
help='directory with pcap-files to clean, default is "{}"'.
format(DEFAULT_PCAPS_DIR_NAME))
parser.add_argument('-f2', '--folder2', default=DEFAULT_DATA_PATH,
help='directory with data-files to clean, default is "{}"'.
format(DEFAULT_DATA_DIR_NAME))
parser.add_argument('-f3', '--folder3', default=DEFAULT_GRAPHS_PATH,
help='directory with graphs to clean, default is "{}"'.
format(DEFAULT_GRAPHS_DIR_NAME))
args = parser.parse_args()
if args.all and (args.pcap or args.data or args.graph):
sys.exit('Error: argument -a is mutually exclusive with arguments -p, -d, -g')
if not args.all and not args.pcap and not args.data and not args.graph:
sys.exit('Error: argument -a or at least one of arguments -p, -d, -g should be chosen')
directories = []
if args.all or args.pcap:
pcapsDirPath = os.path.realpath(os.path.expanduser(args.folder1))
if os.path.exists(pcapsDirPath):
directories.append(pcapsDirPath)
else:
sys.stderr.write('\nWARNING: Directory does not exist: %s\n\n' % pcapsDirPath)
if args.all or args.data:
dataDirPath = os.path.realpath(os.path.expanduser(args.folder2))
if os.path.exists(dataDirPath):
directories.append(dataDirPath)
else:
sys.stderr.write('\nWARNING: Directory does not exist: %s\n\n' % dataDirPath)
if args.all or args.graph:
graphsDirPath = os.path.realpath(os.path.expanduser(args.folder3))
if os.path.exists(graphsDirPath):
directories.append(graphsDirPath)
else:
sys.stderr.write('\nWARNING: Directory does not exist: %s\n\n' % graphsDirPath)
filesToDelete = []
for directory in directories:
files = [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))]
for file in files:
if not file.endswith(PCAP) and not file.endswith(JSON) and not file.endswith(PNG)\
and not file.endswith(LOG):
continue
if SENDER in file:
if args.senders:
filesToDelete.append(os.path.join(directory, file))
elif RECEIVER in file:
if args.receivers:
filesToDelete.append(os.path.join(directory, file))
else:
if args.mutual:
filesToDelete.append(os.path.join(directory, file))
if not args.senders and not args.receivers and not args.mutual:
filesToDelete.append(os.path.join(directory, file))
for file in filesToDelete:
os.remove(file)
print("The following files have been deleted:")
print("======================================")
print("\n".join(filesToDelete))
print("======================================")
for directory in directories:
if len(os.listdir(directory)) == 0:
os.rmdir(directory)
print("Directory has been deleted as completely empty: %s" % directory)