diff --git a/Cleanup.py b/Cleanup.py index 94f8aaf..2fc6484 100644 --- a/Cleanup.py +++ b/Cleanup.py @@ -1,10 +1,12 @@ import os import sys from Notifications import send_notif +import time # Move apps to proper location def move_apps(appstate): build_config = appstate['build_config'] + print = appstate['logger'].info try: os.mkdir('archive') @@ -24,20 +26,33 @@ def move_apps(appstate): # sys.exit('There was an error moving the final apk files!') # Do some cleanup, keep only the last 3 build's worth of files and a week worth of logs - files = [] - dir = os.scandir('archive') - 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) - dir.close() + 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): - send_notif(appstate, error=True) + print = appstate['logger'].info + + try: + appstate['notification_config'] + send_notif(appstate, error=True) + except: + pass + if msg: - print(msg, file=sys.stderr) + print(msg) exit(code) \ No newline at end of file diff --git a/JAVABuilder.py b/JAVABuilder.py index 6a6796a..84456e4 100644 --- a/JAVABuilder.py +++ b/JAVABuilder.py @@ -2,13 +2,14 @@ import os import sys import configparser as cp import json -import subprocess from Cleanup import clean_exit +import subprocess # Build the revanced apps def build_apps(appstate): build_config = appstate['build_config'] flag = appstate['flag'] + print = appstate['logger'].info chosen_patches = cp.ConfigParser() chosen_patches.read('chosen_patches.toml') @@ -69,9 +70,13 @@ def build_apps(appstate): print(f"Building {pretty_name} (nonroot)...") try: - output = subprocess.run(cmd, shell=True) + with subprocess.Popen(cmd, shell=True, bufsize=0, stdout=subprocess.PIPE, stderr=subprocess.STDOUT).stdout as output: + for line in output: + line_utf = line.decode('utf-8').strip('\n') + if line_utf: + print(line_utf) except Exception as e: - clean_exit(f"There was an error while building {pretty_name}!", appstate) + clean_exit(f"There was an error while building {pretty_name}!\n{e}", appstate) try: os.rename(output_name+'.apk', output_name+'.apk') # TODO: Add timestamp here diff --git a/Notifications.py b/Notifications.py index 5d1c7d7..dfee15b 100644 --- a/Notifications.py +++ b/Notifications.py @@ -4,7 +4,9 @@ import requests as req import subprocess def send_notif(appstate, error=False): + print = appstate['logger'].info timestamp = appstate['timestamp'] + if error: msg = f"There was an error during build! Please check the logs.\nTimestamp: {timestamp}" else: @@ -72,9 +74,13 @@ def send_notif(appstate, error=False): continue cmd = f"./telegram.sh -t {token} -c {chat} -T {encoded_title} -M \"{msg}\"" try: - subprocess.run(cmd, shell=True) + with subprocess.Popen(cmd, shell=True, bufsize=0, stdout=subprocess.PIPE, stderr=subprocess.STDOUT).stdout as output: + for line in output: + line_utf = line.decode('utf-8').strip('\n') + if line_utf: + print(line_utf) except Exception as e: - print('Failed!' + str(e)) + clean_exit(f"Failed!\n{e}", appstate) case _: print('Don\'t know how to send notifications to ' + entry) diff --git a/ReVancedBuilder.py b/ReVancedBuilder.py index c3ad1f9..e4d5e96 100755 --- a/ReVancedBuilder.py +++ b/ReVancedBuilder.py @@ -11,9 +11,8 @@ from JAVABuilder import * from datetime import datetime from Notifications import send_notif from Cleanup import * +import logging -# TODO: Logging -# TODO: Notifications # TODO: Run post_script (preferably in any language) # TODO: README # TODO: PATCHES_GUIDE.md (maybe delete it?) @@ -85,10 +84,8 @@ appstate = {} # Get a timestamp time = datetime.now() appstate['timestamp'] = time.strftime('%Y%m%d%H%M%S') -print(f"Started building ReVanced apps at {time.strftime('%d %B, %Y %H:%M:%S')}") -print('----------------------------------------------------------------------') -# Read configs +# Read arguments try: os.chdir(sys.argv[1]) except IndexError: @@ -96,14 +93,34 @@ except IndexError: except FileNotFoundError: clean_exit('Invalid working directory provided!', appstate) +# Set up logging +try: + os.mkdir('logs') +except FileExistsError: + pass + +logging.basicConfig(level=logging.INFO, format='%(message)s') +logger = logging.getLogger() +logger.addHandler(logging.FileHandler(f"logs/{appstate['timestamp']}.log", 'w')) +print = logger.info +appstate['logger'] = logger + +# Get the flag try: flag = sys.argv[2] except: flag = None +if flag not in ['buildonly', 'checkonly', 'force', 'experimental']: + clean_exit(f"Unknown flag: {flag}", appstate) + appstate['flag'] = flag appstate['microg_updated'] = False +print(f"Started building ReVanced apps at {time.strftime('%d %B, %Y %H:%M:%S')}") +print('----------------------------------------------------------------------') + +# Read configs try: appstate['build_config']=cp.ConfigParser() appstate['build_config'].read_file(open('build_config.toml', 'r'))