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
|
#!/usr/bin/env python3
import shutil
import sys
import os
import re
import logging
sys.path.extend([
os.path.realpath(
os.path.join(os.path.dirname(os.path.join(__file__)), '..')
)
])
from functools import cmp_to_key
from datetime import datetime
from pprint import pprint
from src.home.config import config
logger = logging.getLogger(__name__)
datetime_format = '%Y-%m-%d-%H.%M.%S'
datetime_format_re = r'\d{4}-\d{2}-\d{2}-\d{2}\.\d{2}.\d{2}'
def cleanup_job():
def fn2dt(name: str) -> datetime:
name = os.path.basename(name)
if name.startswith('record_'):
return datetime.strptime(re.match(r'record_(.*?)\.mp4', name).group(1), datetime_format)
m = re.match(rf'({datetime_format_re})__{datetime_format_re}\.mp4', name)
if m:
return datetime.strptime(m.group(1), datetime_format)
raise ValueError(f'unrecognized filename format: {name}')
def compare(i1: str, i2: str) -> int:
dt1 = fn2dt(i1)
dt2 = fn2dt(i2)
if dt1 < dt2:
return -1
elif dt1 > dt2:
return 1
else:
return 0
gb = float(1 << 30)
for storage in config['storages']:
if os.path.exists(storage['mountpoint']):
total, used, free = shutil.disk_usage(storage['mountpoint'])
free_gb = free // gb
if free_gb < config['cleanup_min_gb']:
# print(f"{storage['mountpoint']}: free={free}, free_gb={free_gb}")
# continue
cleaned = 0
files = []
for cam in storage['cams']:
for _dir in (config['camera'][cam]['recordings_path'], config['camera'][cam]['motion_path']):
files += list(map(lambda file: os.path.join(_dir, file), os.listdir(_dir)))
files = list(filter(lambda path: os.path.isfile(path) and path.endswith('.mp4'), files))
files.sort(key=cmp_to_key(compare))
# files = list(sorted(files, key=compare))
for file in files:
size = os.stat(file).st_size
try:
# os.unlink(file)
print(f'unlink {file}')
cleaned += size
except OSError as e:
logger.exception(e)
if (free + cleaned) // gb >= config['cleanup_min_gb']:
break
else:
logger.error(f"cleanup_job: {storage['mountpoint']} not found")
if __name__ == '__main__':
config.load_app('ipcam_server')
cleanup_job()
|