ReVancedBuilder/Cleanup.py

58 lines
1.6 KiB
Python
Raw Normal View History

import os
import sys
from Notifications import send_notif
2023-08-10 14:50:42 -05:00
import time
# Move apps to proper location
def move_apps(appstate):
build_config = appstate['build_config']
2023-08-10 14:50:42 -05:00
print = appstate['logger'].info
try:
os.mkdir('archive')
except FileExistsError:
pass
for app in build_config:
if not build_config[app].getboolean('build'):
continue
name = build_config[app]['output_name']
final_name = f"{name}_{appstate['timestamp']}.apk"
try:
os.rename(name+'.apk', 'archive/'+final_name)
except FileNotFoundError:
pass
# sys.exit('There was an error moving the final apk files!')
2023-08-10 13:07:00 -05:00
# Do some cleanup, keep only the last 3 build's worth of files and a week worth of logs
2023-08-10 14:50:42 -05:00
with os.scandir('archive') as dir:
files = []
for f in dir:
if name in f.name:
files.append(f)
files.sort(key=lambda f: f.stat().st_ctime)
files.reverse()
for f in files[3:]:
os.remove(f)
print('Deleted old build '+f.name)
# Delete logs older than 7 days
with os.scandir('logs') as dir:
now = time.now()
for f in dir:
if f.stat().st_ctime < now - 7 * 86400:
os.remove(f)
def clean_exit(msg, appstate, code=1):
2023-08-10 14:50:42 -05:00
print = appstate['logger'].info
try:
appstate['notification_config']
send_notif(appstate, error=True)
except:
pass
if msg:
2023-08-10 14:50:42 -05:00
print(msg)
exit(code)