-
Notifications
You must be signed in to change notification settings - Fork 1
/
cleanup.py
128 lines (99 loc) · 4.86 KB
/
cleanup.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
#!/usr/bin/env python3
"""Cleanup script for UAF-Plankline
Usage:
./cleanup.py -c <config.ini> -d <project directory>
License:
MIT License
Copyright (c) 2023 Thomas Kelly
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import os
import sys
import shutil
import argparse
import configparser # TBK: To read config file
import glob
if __name__ == "__main__":
"""Main entry point for cleanup.py"""
v_string = "V2023.09s.05"
# create a parser for command line arguments
parser = argparse.ArgumentParser(description="")
parser.add_argument("-d", "--directory", required = False, help = "Input directory containing ./raw/")
parser.add_argument("-f", action = 'store_true')
# read in the arguments
args = parser.parse_args()
## Read in config options:
if args.directory is None:
skip_working = True
else:
skip_working = False
if not skip_working:
working_dir = os.path.abspath(args.directory)
seg_tmp = glob.glob('/tmp/segment*')
class_tmp = glob.glob('/tmp/class*')
train_tmp = glob.glob('/tmp/train*')
if args.f is True:
if not skip_working:
if os.path.isdir(working_dir + '/segmentation/'):
shutil.rmtree(working_dir + '/segmentation/', ignore_errors = True)
if os.path.isdir(working_dir + '/classification/'):
shutil.rmtree(working_dir + '/classification/', ignore_errors = True)
if os.path.isdir(working_dir + '/R/'):
shutil.rmtree(working_dir + '/R/', ignore_errors = True)
if len(seg_tmp) > 0:
for d in seg_tmp:
shutil.rmtree(d, ignore_errors = True)
if len(class_tmp) > 0:
for d in class_tmp:
shutil.rmtree(d, ignore_errors = True)
if len(train_tmp) > 0:
for d in train_tmp:
shutil.rmtree(d, ignore_errors = True)
else:
def get_size(start_path = '.'):
total_size = 0
for dirpath, dirnames, filenames in os.walk(start_path):
for f in filenames:
fp = os.path.join(dirpath, f)
# skip if it is symbolic link
if not os.path.islink(fp):
total_size += os.path.getsize(fp)
return total_size
print("No -f flag provided. Rerun with -f flag if you want to delete the following directories:")
if not skip_working:
if os.path.isdir(working_dir + '/segmentation/'):
print(f"Directory: {working_dir + '/segmentation/':30} containing {(get_size(working_dir + '/segmentation/')/10**9):.1f} GB")
else:
print(f"Directory: {working_dir + '/segmentation/':30} not found.")
if os.path.isdir(working_dir + '/classification/'):
print(f"Directory: {working_dir + '/classification/':30} containing {(get_size(working_dir + '/classification/')/10**9):.1f} GB")
else:
print(f"Directory: {working_dir + '/classification/':30} not found.")
if os.path.isdir(working_dir + '/R/'):
print(f"Directory: {working_dir + '/R/':30} containing {(get_size(working_dir + '/R/')/10**9):.1f} GB")
else:
print(f"Directory: {working_dir + '/R/':30} not found.")
if len(seg_tmp) > 0:
for d in seg_tmp:
print(f"Directory: {d:30} containing {(get_size(d)/10**9):.1f} GB")
if len(class_tmp) > 0:
for d in class_tmp:
print(f"Directory: {d:30} containing {(get_size(d)/10**9):.1f} GB")
if len(train_tmp) > 0:
for d in train_tmp:
print(f"Directory: {d:30} containing {(get_size(d)/10**9):.1f} GB")
print('Done.')