ReVancedBuilder/ReVancedBuilder.py

154 lines
5.3 KiB
Python
Raw Normal View History

2023-08-08 23:33:09 -05:00
#!/usr/bin/env python3
import sys
import os
import configparser as cp
import requests as req
import json
from packaging.version import Version
from APKPure_dl import *
2023-08-09 02:17:29 -05:00
from JAVABuilder import *
from datetime import datetime
from Notifications import send_notif
from Cleanup import *
2023-08-09 02:17:29 -05:00
# TODO: Logging
# TODO: Notifications
# TODO: Run post_script (preferably in any language)
# TODO: README
# TODO: PATCHES_GUIDE.md (maybe delete it?)
2023-08-10 13:07:00 -05:00
# TODO: Make it PIP installable
2023-08-08 23:33:09 -05:00
# Update the ReVanced tools, if needed
def update_tools(appstate):
2023-08-08 23:33:09 -05:00
for item in ['revanced-cli', 'revanced-integrations', 'revanced-patches']:
*_, tool = filter(lambda x: x['repository'] == 'revanced/'+item, tools) # Get the last result
latest_ver = Version(tool['version'])
try:
present_ver = Version(appstate['present_vers'][item])
2023-08-08 23:33:09 -05:00
except KeyError:
present_ver = Version('0')
2023-08-09 02:17:29 -05:00
output_file = item+os.path.splitext(tool['name'])[1]
if flag == 'force' or not os.path.isfile(output_file) or present_ver < latest_ver:
appstate['up-to-date'] = False
2023-08-08 23:33:09 -05:00
print(f"{item} has an update ({str(present_ver)} -> {str(latest_ver)})")
if flag != 'checkonly':
print(f"Downloading {output_file}...")
res = req.get(tool['browser_download_url'], stream=True)
res.raise_for_status()
with open(output_file, 'wb') as f:
for chunk in res.iter_content(chunk_size=8192):
f.write(chunk)
appstate['present_vers'].update({item: str(latest_ver)})
2023-08-08 23:33:09 -05:00
print("Done!")
return appstate
2023-08-08 23:33:09 -05:00
# Update microG, if needed
def update_microg(appstate):
2023-08-08 23:33:09 -05:00
try:
data = req.get('https://api.github.com/repos/inotia00/VancedMicroG/releases/latest').json()['tag_name']
latest_ver = Version(data)
except req.exceptions.RequestException as e:
clean_exit(e, appstate)
2023-08-08 23:33:09 -05:00
try:
present_ver = Version(appstate['present_vers']['VancedMicroG'])
2023-08-08 23:33:09 -05:00
except KeyError:
present_ver = Version('0')
2023-08-09 02:17:29 -05:00
if flag == 'force' or not os.path.isfile('microg.apk') or present_ver < latest_ver:
appstate['up-to-date'] = False
2023-08-08 23:33:09 -05:00
print(f"Vanced microG has an update ({str(present_ver)} -> {str(latest_ver)})")
if flag != 'checkonly':
2023-08-09 02:17:29 -05:00
print(f"Downloading vanced-microg.apk...")
2023-08-08 23:33:09 -05:00
res = req.get('https://github.com/inotia00/VancedMicroG/releases/latest/download/microg.apk', stream=True)
res.raise_for_status()
with open('microg.apk', 'wb') as f:
for chunk in res.iter_content(chunk_size=8192):
f.write(chunk)
appstate['present_vers'].update({'VancedMicroG': str(latest_ver)})
2023-08-08 23:33:09 -05:00
print("Done!")
2023-08-10 13:07:00 -05:00
appstate['microg_updated'] = True
2023-08-08 23:33:09 -05:00
return appstate
# ------------------------------
# The main function starts here
# ------------------------------
# Create a dict for storing important data
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('----------------------------------------------------------------------')
2023-08-08 23:33:09 -05:00
# Read configs
try:
os.chdir(sys.argv[1])
except IndexError:
clean_exit('Please provide a working directory as argument!', appstate)
2023-08-08 23:33:09 -05:00
except FileNotFoundError:
clean_exit('Invalid working directory provided!', appstate)
2023-08-08 23:33:09 -05:00
try:
flag = sys.argv[2]
except:
flag = None
appstate['flag'] = flag
2023-08-10 13:07:00 -05:00
appstate['microg_updated'] = False
2023-08-08 23:33:09 -05:00
try:
appstate['build_config']=cp.ConfigParser()
appstate['build_config'].read_file(open('build_config.toml', 'r'))
2023-08-08 23:33:09 -05:00
except FileNotFoundError:
clean_exit('No build config provided, exiting. Please look at the GitHub page for more information:\n https://github.com/SinTan1729/ReVancedBuilder', appstate)
2023-08-08 23:33:09 -05:00
appstate['notification_config'] = cp.ConfigParser()
appstate['notification_config'].read('notification_config.toml')
2023-08-08 23:33:09 -05:00
# Pull the latest information using the ReVanced API
try:
tools = req.get('https://releases.revanced.app/tools').json()['tools']
except req.exceptions.RequestException as e:
clean_exit(e, appstate)
2023-08-08 23:33:09 -05:00
try:
with open('versions.json', 'r') as f:
appstate['present_vers'] = json.load(f)
2023-08-08 23:33:09 -05:00
except:
# We'll treat empty as 0 later
appstate['present_vers'] = json.loads('{}')
2023-08-08 23:33:09 -05:00
appstate['up-to-date'] = True
# send_notif(appstate, error=False) # <,,,,,,,,<,,,,,,,,,,,,,
2023-08-08 23:33:09 -05:00
if flag != 'buildonly':
appstate = update_tools(appstate)
appstate = update_microg(appstate)
if not appstate['up-to-date'] or flag == 'force':
appstate = get_apks(appstate)
2023-08-08 23:33:09 -05:00
if (flag != 'checkonly' and not appstate['up-to-date']) or flag in ['force', 'buildonly']:
build_apps(appstate)
move_apps(appstate)
2023-08-08 23:33:09 -05:00
# Update version numbers in the versions.json file
if appstate['up-to-date'] and flag != 'buildonly':
2023-08-08 23:33:09 -05:00
print('There\'s nothing to do.')
elif flag != ['checkonly']:
send_notif(appstate)
try:
os.rename('versions.json', 'versions-old.json')
except FileNotFoundError:
pass
if flag != 'buildonly':
with open('versions.json', 'w') as f:
json.dump(appstate['present_vers'], f, indent=4)