-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanup.py
executable file
·316 lines (244 loc) · 10.4 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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/env python
"""Cleans up one or many given directories until a given amount of free space is
available on the device of those directories. See the usage method below for
further details.
Copyright (c) 2014 Jan Bretschneider <[email protected]>
License: The MIT License (http://opensource.org/licenses/MIT)
"""
def usage():
"""Prints the usage information of this script."""
print('''
Cleans up one or many given directories until a given amount of free space is
available on the device of those directories. See the usage method below for
further details.
Usage: cleanup.py [OPTIONS] [DIRECTORY ...]
You can specify one or many directories to clean up. All those directories
must be on the same device. Otherwise the script aborts. If you do not specify
a directory then the default directory is used: /hdd/media/movie.
Options:
-h, --help Show this help message.
-s MIN_AVAIL_SPACE Specifies the minimal amount of space in megabytes you
wish to have available on the device with the given
directories. The default is 51200 MB (50 GB).
If MIN_AVAIL_SPACE is already available on the device with the given directories
then this script deletes nothing and exists with a corresponding message.
Otherwise the script cleans up the given directories as follows:
1. It recursively finds all files in the given directories.
2. It deletes the oldest files (according to their modification time (mtime))
until MIN_AVAIL_SPACE is available on the device with the given
directories or there are no more files to delete.
The script does NOT delete any (sub) directories.
Examples:
./cleanup.py
./cleanup.py /path/to/my/recordings
./cleanup.py -s 12000 /path/to/my/recordings
./cleanup.py -s 12000 /path/to/my/recordings /some/other/dir
'''.strip())
import sys
import os
import getopt
class File:
"""A simple file class. A File has a path and a modification time. The
modification time is stored as seconds since the epoch.
Usage:
>>> f = File('/tmp/foo', 1395506398)
>>> f.path
'/tmp/foo'
>>> f.mtime
1395506398
"""
def __init__(self, path, mtime):
"""Creates a File with the given path and modification time."""
self.path = path
self.mtime = mtime
def __repr__(self):
"""Print File('/tmp/foo', 1395506398) as ('/tmp/foo', 1395506398)."""
return repr((self.path, self.mtime))
def __key(self):
return (self.path)
def __eq__(x, y):
return x.__key() == y.__key()
def __hash__(self):
return hash(self.__key())
def process_dir(all_files, dirname, filenames):
"""Is called by find_files_sorted_by_mtime() for every found directory
while recursively scanning a parent directory.
Adds all files given in filenames to all_files. all_files contains only
File objects. All fields (path and mtime) of all File objects are set.
Parameters:
all_files (list of File objects): Accumulates all found files.
dirname (string): Name of the current directory.
filenames (list of strings): Names of all files in the current
directory.
"""
for filename in filenames:
fullpath = os.path.join(dirname, filename)
if not os.path.isfile(fullpath):
continue
mtime = os.path.getmtime(fullpath)
file = File(fullpath, mtime)
all_files.add(file)
def find_files_sorted_by_mtime(directories):
"""Recursively scans the given directories and returns a list of all found
files.
Parameters:
directories (list of strings) - The directories to scan.
Returns:
A list of File objects. The list is sorted by the modification time
(mtime) of the files with the oldest file at index 0. If no files were
found the returned list is empty.
"""
files=set()
for directory in directories:
for dirpath, dirnames, filenames in os.walk(directory):
process_dir(files, dirpath, filenames)
return sorted(files, key=lambda file: file.mtime)
def avail_space_in_mb(directory):
"""Returns the amount of megabytes that are free (available) in the given
directory.
Parameters:
directory (string) - The directory to check.
"""
st = os.statvfs(directory)
return st.f_frsize*st.f_bavail/1024/1024
def cleanup(directories, min_avail_space):
"""Cleans up the given directories until there is at least the given minimal
amount of space available. The cleanup method is described at the top of
this script.
Parameters:
directories (list of strings) - The directories that should be cleaned
up. Must have at least one element.
min_avail_space (int) - The amount of free space in megabytes that
should be available in the given directory.
"""
if avail_space_in_mb(directories[0]) >= min_avail_space:
print("There is enough space available: %d MB" % \
avail_space_in_mb(directories[0]))
print("No cleanup necessary. Exiting.")
return
# all_files contains a list of all Files in path.
all_files = find_files_sorted_by_mtime(directories)
# Delete the oldest file until there is enough space available.
while avail_space_in_mb(directories[0]) < min_avail_space and \
len(all_files) > 0:
file = all_files.pop(0)
print("Removing %s" % file.path)
os.remove(file.path)
print("Space now available: %d MB." % avail_space_in_mb(directories[0]))
# Report if there is not enough space available and no more file to delete.
if avail_space_in_mb(directories[0]) < min_avail_space and \
len(all_files) == 0:
print("There is NOT enough space available: %d MB" % \
avail_space_in_mb(directories[0]))
print("And there are no more files to delete.")
def check_directories(directories):
"""Checks if all given directories are really directories and on the same
device.
Parameters:
directories (list of strings) - The directories to check.
Returns:
The tuple (ok, ok_dirs) where ok is a boolean and ok_dirs a list of
directories (as strings). If the given directories contained no
existing directories or it contained at least two directories that are
not on the same device, then ok is False and ok_dirs is empty.
Otherwise ok is True and ok_dirs contains all directories in the given
directories that really exist.
"""
ok_dirs = []
for d in directories:
if not os.path.exists(d):
print("'%s' does not exist. Ignoring." % d)
continue
if not os.path.isdir(d):
print("'%s' is no directory. Ignoring." % d)
continue
ok_dirs.append(d)
if len(ok_dirs) == 0:
print("No existing directory given. Exiting.")
return False, []
prev_dir = None
prev_device = None
for d in ok_dirs:
current_device = os.stat(d).st_dev
if prev_device is not None and current_device != prev_device:
print("'%s' and '%s' are not on the same device. Exiting." % \
(d, prev_dir))
return False, []
prev_dir = d
prev_device = current_device
return True, ok_dirs
def parse_opts(directory, min_avail_space):
"""Parses the command line options passed to this script.
If this script is called with -h or --help then this method prints the
usage information to stdout and exits the scripts.
Otherwise it returns the directory and minimal available space given on
the command line or the default values for both options passed into this
method.
Parameters:
directory (string): default value for the directory to clean.
min_avail_space (int): default value for the desired minimal available
space.
Returns:
(directory, min_avail_space) where directory and min_avail_space are
the values given on the command line or the default values.
"""
# We use getopt to parse the command line arguments because argparse or
# optparse are not available on Dreamboxes.
try:
opts, args = getopt.getopt(sys.argv[1:], "hs:", ["help"])
except getopt.GetoptError as err:
print(str(err))
usage()
sys.exit(1)
for opt, arg in opts:
if opt in ('-h', '--help'):
usage()
sys.exit()
elif opt == '-s':
try:
min_avail_space = int(arg)
except ValueError as err:
print("You have given the option %s %s. " \
"But '%s' is not a number." % (opt, arg, arg))
print("Run %s --help for help." % sys.argv[0])
sys.exit(1)
else:
assert False, "unhandled option"
directories = args
if (len(directories) == 0):
directories = [directory]
print("You requested to have %d MB available in the following "\
"directories:" % min_avail_space)
for dir in directories:
print(dir)
return directories, min_avail_space
def main():
"""Main method of this script."""
# Use the directory that contains recordings on Dreamboxes as default.
default_directory = '/media/hdd/movie'
# Per default we want to have 50GB of free space.
min_avail_space = 50*1024
# Override defaults with values given on the command line.
directories, min_avail_space = parse_opts(default_directory, min_avail_space)
# Check if all given directories are really directories and on the same
# device. If not, abort.
ok, directories = check_directories(directories)
if not ok:
return 1
# Do the actual cleanup.
cleanup(directories, min_avail_space)
return 0
PROFILE = 0
if __name__ == '__main__':
if PROFILE:
import cProfile
import pstats
profile_filename = 'cleanup.cleanup_profile.txt'
cProfile.run('main()', profile_filename)
statsfile = open('profile_stats.txt', 'wb')
p = pstats.Stats(profile_filename, stream=statsfile)
stats = p.strip_dir().sort_stats('cumulative')
stats.print_stats()
statsfile.close()
sys.exit(0)
sys.exit(main())