This repository has been archived by the owner on Nov 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstartup
executable file
·73 lines (56 loc) · 1.71 KB
/
startup
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
#!/bin/python3
"""
Raspberry startup script
Check for previous runtime data
and move them to dedicated folder
"""
from pathlib import Path
from datetime import datetime
from main import runtime_scheduler, main
import json
def size_format(size):
for hp in ['bytes', 'KB', 'MB', 'GB', 'TB']:
if size < 1024.0:
return f'{round(size, 2)} {hp}'
size /= 1024.0
return size
here = Path(__file__).parent.resolve()
runtime_suffixes = [
'.jpg',
'.csv',
'.log',
'.json'
]
date_format = '%d.%m.%Y-%H:%M:%S'
files = list(filter(
lambda file: file.is_file() and file.suffix in runtime_suffixes,
here.iterdir()
))
if len(files) > 0:
oldest_modification = datetime.fromtimestamp(min(map(
lambda file: int(file.stat().st_mtime),
files
)))
runtime_folder = oldest_modification.strftime(date_format)
runtime_path = here / runtime_folder
funtime_files_path = runtime_path / 'runtime'
funtime_files_path.mkdir(parents=True, exist_ok=True)
total_size = 0
for file in files:
total_size += file.stat().st_size
destination = funtime_files_path / file.name
file.replace(destination)
meta_dir = runtime_path / 'meta.json'
with meta_dir.open("w", encoding="utf-8") as meta_f:
meta_f.write(json.dumps({
'size': {
'raw': total_size,
'formatted': size_format(total_size)
},
'date': {
'backup': datetime.now().strftime(date_format),
'oldest_modification': oldest_modification.strftime(date_format)
},
'files': list(map(lambda f: str(f.name), files))
}, indent=4))
runtime_scheduler(main)