NFVBENCH-153 Add support for python3
[nfvbench.git] / docker / cleanup_generators.py
1 # Copyright 2016 Cisco Systems, Inc.  All rights reserved.
2 #
3 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
4 #    not use this file except in compliance with the License. You may obtain
5 #    a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 #    Unless required by applicable law or agreed to in writing, software
10 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 #    License for the specific language governing permissions and limitations
13 #    under the License.
14
15 import os
16 import shutil
17
18 TREX_OPT = '/opt/trex'
19
20
21 TREX_UNUSED = [
22     '_t-rex-64-debug', '_t-rex-64-debug-o', 'bp-sim-64', 'bp-sim-64-debug',
23     't-rex-64-debug', 't-rex-64-debug-o', 'automation/__init__.py',
24     'automation/graph_template.html',
25     'automation/config', 'automation/h_avc.py', 'automation/phantom',
26     'automation/readme.txt', 'automation/regression', 'automation/report_template.html',
27     'automation/sshpass.exp', 'automation/trex_perf.py', 'wkhtmltopdf-amd64'
28 ]
29
30
31 def remove_unused_libs(path, files):
32     """
33     Remove files not used by traffic generator.
34     """
35     for f in files:
36         f = os.path.join(path, f)
37         try:
38             if os.path.isdir(f):
39                 shutil.rmtree(f)
40             else:
41                 os.remove(f)
42         except OSError:
43             print("Skipped file:")
44             print(f)
45             continue
46
47
48 def get_dir_size(start_path='.'):
49     """
50     Computes size of directory.
51
52     :return: size of directory with subdirectiories
53     """
54     total_size = 0
55     for dirpath, dirnames, filenames in os.walk(start_path):
56         for f in filenames:
57             try:
58                 fp = os.path.join(dirpath, f)
59                 total_size += os.path.getsize(fp)
60             except OSError:
61                 continue
62     return total_size
63
64 if __name__ == "__main__":
65     versions = os.listdir(TREX_OPT)
66     for version in versions:
67         trex_path = os.path.join(TREX_OPT, version)
68         print('Cleaning TRex', version)
69         try:
70             size_before = get_dir_size(trex_path)
71             remove_unused_libs(trex_path, TREX_UNUSED)
72             size_after = get_dir_size(trex_path)
73             print('==== Saved Space ====')
74             print(size_before - size_after)
75         except OSError:
76             import traceback
77             print(traceback.print_exc())
78             print('Cleanup was not finished.')