2019-02-04 20:49:46 +00:00
|
|
|
#!/bin/env python3
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
import os
|
2019-05-18 06:54:20 +00:00
|
|
|
import json
|
2019-02-04 20:49:46 +00:00
|
|
|
import subprocess
|
|
|
|
import datetime
|
2019-03-08 11:53:24 +00:00
|
|
|
import shutil
|
2019-02-04 20:49:46 +00:00
|
|
|
|
|
|
|
# Example invocation:
|
2019-05-18 06:54:20 +00:00
|
|
|
# python3 patchport.py /home/ave/apks/com.discord-900/
|
2019-02-04 20:49:46 +00:00
|
|
|
|
2021-08-06 17:07:52 +00:00
|
|
|
with open("/opt/ctc/gitrepo/resources/patchport-state.json", "r") as f:
|
2019-05-18 06:54:20 +00:00
|
|
|
jin = json.load(f)
|
|
|
|
from_versioncode = jin["versioncode"]
|
|
|
|
from_versionname = jin["versionname"]
|
|
|
|
|
|
|
|
apk_folder = sys.argv[1]
|
2021-09-20 15:23:55 +00:00
|
|
|
cutthecord_folder = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
|
2019-02-04 20:49:46 +00:00
|
|
|
debug = False
|
2019-03-08 11:53:24 +00:00
|
|
|
tmp_folder = "/tmp/patchport"
|
2019-02-04 20:49:46 +00:00
|
|
|
|
2019-07-10 21:32:10 +00:00
|
|
|
if debug:
|
|
|
|
print(f"ctc folder: {cutthecord_folder}")
|
|
|
|
|
2019-02-04 20:49:46 +00:00
|
|
|
|
|
|
|
def modify_patch(patch_name, patch_path):
|
|
|
|
with open(patch_path) as f:
|
|
|
|
patch_content = f.read()
|
|
|
|
|
|
|
|
if patch_name in ["branding", "customversion"]:
|
|
|
|
patch_content = patch_content.replace(from_versioncode, to_versioncode)
|
|
|
|
patch_content = patch_content.replace(from_versionname, to_versionname)
|
|
|
|
|
|
|
|
return patch_content
|
|
|
|
|
|
|
|
|
2019-04-25 10:12:30 +00:00
|
|
|
def apply_patch(patch_contents):
|
|
|
|
subprocess.run("patch -p1 --no-backup-if-mismatch --force",
|
|
|
|
shell=True, input=patch_contents, text=True,
|
|
|
|
cwd=apk_folder, capture_output=True)
|
|
|
|
|
|
|
|
|
2019-03-08 11:53:24 +00:00
|
|
|
def fix_offset(patch_contents):
|
|
|
|
# OH GOD OH FUCK
|
|
|
|
shutil.rmtree(tmp_folder, ignore_errors=True)
|
2021-01-05 20:33:23 +00:00
|
|
|
os.makedirs(tmp_folder)
|
|
|
|
patch_lines = patch_contents.splitlines()
|
|
|
|
for line in patch_lines:
|
|
|
|
if "diff -crB" in line:
|
|
|
|
patch_target = line.split(" ")[2].replace("from/", "")
|
2021-01-10 00:39:47 +00:00
|
|
|
if not os.path.exists(os.path.join(apk_folder, patch_target)):
|
|
|
|
return False
|
2021-01-05 20:33:23 +00:00
|
|
|
os.makedirs(os.path.dirname(os.path.join(tmp_folder, patch_target)), exist_ok=True)
|
|
|
|
shutil.copy(os.path.join(apk_folder, patch_target), os.path.join(tmp_folder, patch_target))
|
2021-01-10 00:39:47 +00:00
|
|
|
# shutil.copytree(apk_folder, tmp_folder)
|
2019-03-08 11:53:24 +00:00
|
|
|
subprocess.run("patch -p1 --no-backup-if-mismatch --force",
|
|
|
|
shell=True, input=patch_contents, text=True,
|
2019-03-08 13:17:28 +00:00
|
|
|
cwd=tmp_folder, capture_output=True)
|
2019-03-08 11:53:24 +00:00
|
|
|
out = subprocess.run(f"diff -crB {apk_folder} {tmp_folder}",
|
|
|
|
shell=True, input=patch_contents, text=True,
|
|
|
|
cwd=tmp_folder, capture_output=True)
|
2021-01-10 00:39:47 +00:00
|
|
|
|
2019-03-08 11:53:24 +00:00
|
|
|
shutil.rmtree(tmp_folder, ignore_errors=True)
|
2021-01-05 20:33:23 +00:00
|
|
|
actual_difflines = []
|
|
|
|
for line in out.stdout.splitlines():
|
|
|
|
if line[0:4] != "Only":
|
|
|
|
actual_difflines.append(line)
|
|
|
|
patch_out = ("\n".join(actual_difflines)+"\n").replace(apk_folder, "from").replace(tmp_folder, "to")
|
2021-01-10 00:39:47 +00:00
|
|
|
|
2019-03-08 13:17:28 +00:00
|
|
|
return patch_out
|
2019-03-08 11:53:24 +00:00
|
|
|
|
2021-08-10 18:56:58 +00:00
|
|
|
# TODO replace with xml-patch version
|
2019-04-25 10:12:30 +00:00
|
|
|
def make_necessary(version_name, version_code):
|
|
|
|
# OH GOD OH FUCK
|
|
|
|
shutil.rmtree(tmp_folder, ignore_errors=True)
|
2021-01-05 20:33:23 +00:00
|
|
|
os.makedirs(tmp_folder)
|
|
|
|
shutil.copy(os.path.join(apk_folder, "AndroidManifest.xml"), os.path.join(tmp_folder, "AndroidManifest.xml"))
|
2019-04-25 10:12:30 +00:00
|
|
|
|
|
|
|
# Set version code and name
|
|
|
|
# Due to https://github.com/iBotPeaches/Apktool/issues/2046
|
|
|
|
# Code based on https://stackoverflow.com/a/4128192/3286892
|
|
|
|
with open(os.path.join(tmp_folder, "AndroidManifest.xml")) as fin:
|
|
|
|
filec = fin.read()
|
|
|
|
incorrect_versioncode = re_versioncode_xml.findall(filec)[0]
|
2021-08-10 18:56:58 +00:00
|
|
|
filec = filec.replace(incorrect_versioncode, f'platformBuildVersionCode="{to_versioncode}"')
|
2019-04-25 10:12:30 +00:00
|
|
|
incorrect_versionname = re_versionname_xml.findall(filec)[0]
|
2021-08-10 18:56:58 +00:00
|
|
|
filec = filec.replace(incorrect_versionname, f'platformBuildVersionName="{to_versionname}"')
|
2019-04-25 10:12:30 +00:00
|
|
|
with open(os.path.join(tmp_folder, "AndroidManifest.xml"), "w") as fout:
|
|
|
|
fout.write(filec)
|
|
|
|
|
|
|
|
out = subprocess.run(f"diff -crB {apk_folder} {tmp_folder}",
|
|
|
|
shell=True, text=True,
|
|
|
|
cwd=tmp_folder, capture_output=True)
|
|
|
|
shutil.rmtree(tmp_folder, ignore_errors=True)
|
2021-01-05 20:33:23 +00:00
|
|
|
actual_difflines = []
|
|
|
|
for line in out.stdout.splitlines():
|
|
|
|
if line[0:4] != "Only":
|
|
|
|
actual_difflines.append(line)
|
|
|
|
patch_out = ("\n".join(actual_difflines)+"\n").replace(apk_folder, "from").replace(tmp_folder, "to")
|
2019-04-25 10:12:30 +00:00
|
|
|
return patch_out
|
|
|
|
|
|
|
|
|
|
|
|
re_versioncode_xml = re.compile(r'(platformBuildVersionCode="[0-9]+")')
|
|
|
|
re_versionname_xml = re.compile(r'(platformBuildVersionName="[0-9a-z.]+")')
|
|
|
|
|
|
|
|
re_versioncode_yml = re.compile(r'versionCode: \'([0-9]+)\'')
|
2020-03-01 16:11:06 +00:00
|
|
|
re_versionname_yml = re.compile(r'versionName: \'?(.+?)\'?$')
|
2019-04-25 10:12:30 +00:00
|
|
|
|
2019-02-04 20:49:46 +00:00
|
|
|
re_releasedate = re.compile(r'released on ([0-9]{4}-[0-9]{2}-[0-9]{2})')
|
|
|
|
|
|
|
|
|
|
|
|
# Get version code and name
|
2019-04-25 10:12:30 +00:00
|
|
|
with open(os.path.join(apk_folder, "apktool.yml")) as f:
|
2019-02-04 20:49:46 +00:00
|
|
|
file_contents = f.read()
|
2019-04-25 10:12:30 +00:00
|
|
|
to_versioncode = re_versioncode_yml.findall(file_contents)[0]
|
|
|
|
to_versionname = re_versionname_yml.findall(file_contents)[0]
|
2019-02-04 20:49:46 +00:00
|
|
|
|
|
|
|
failures = []
|
|
|
|
|
2021-08-10 18:56:58 +00:00
|
|
|
for patch in os.listdir(os.path.join(cutthecord_folder, "resources/xmlpatches")):
|
|
|
|
if debug:
|
|
|
|
print(f"going over xml patch: {patch}")
|
|
|
|
|
|
|
|
# Ignore non-dirs
|
|
|
|
if not os.path.isdir(os.path.join(cutthecord_folder, "resources/xmlpatches", patch)):
|
|
|
|
if debug:
|
|
|
|
print(f"patch is not a folder, skipping: {patch}")
|
|
|
|
continue
|
|
|
|
|
|
|
|
pre_in_path = os.path.join(cutthecord_folder, "resources/xmlpatches", patch, f"{from_versioncode}-pre.sh")
|
|
|
|
post_in_path = os.path.join(cutthecord_folder, "resources/xmlpatches", patch, f"{from_versioncode}-post.sh")
|
|
|
|
pre_out_path = os.path.join(cutthecord_folder, "resources/xmlpatches", patch, f"{to_versioncode}-pre.sh")
|
|
|
|
post_out_path = os.path.join(cutthecord_folder, "resources/xmlpatches", patch, f"{to_versioncode}-post.sh")
|
|
|
|
|
|
|
|
patch_path = os.path.join(cutthecord_folder, "resources/xmlpatches", patch, f"{from_versioncode}.xml")
|
|
|
|
out_path = os.path.join(cutthecord_folder, "resources/xmlpatches", patch, f"{to_versioncode}.xml")
|
|
|
|
tmp_out_path = os.path.join("/tmp/", patch, f"{to_versioncode}.xml")
|
|
|
|
readme_path = os.path.join(cutthecord_folder, "resources/xmlpatches", patch, "README.md")
|
|
|
|
|
2021-09-20 15:23:55 +00:00
|
|
|
os.mkdir(os.path.dirname(tmp_out_path))
|
|
|
|
|
2021-08-10 18:56:58 +00:00
|
|
|
# Handle copying of versioned scripts, untested and dirty!
|
|
|
|
script_path = os.path.join(cutthecord_folder, "resources/xmlpatches", patch, f"{from_versioncode}.sh")
|
|
|
|
if os.path.exists(script_path):
|
|
|
|
script_out_path = os.path.join(cutthecord_folder, "resources/xmlpatches", patch, f"{to_versioncode}.sh")
|
|
|
|
with open(script_path) as f:
|
|
|
|
with open(script_out_path, "w") as f2:
|
|
|
|
f2.write(f.read())
|
|
|
|
|
|
|
|
# Check if patch exists for from_version, if it doesn't, warn user
|
|
|
|
if not os.path.isfile(patch_path) and patch not in ["necessary"]:
|
|
|
|
# Don't warn on instructional patches
|
|
|
|
if patch not in ["customfont", "customring", "bettertm", "bettertmlight", "blobs"]:
|
|
|
|
print(f"SKIPPED: No {from_versionname} version found for {patch}.")
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Check if pre-script exists, if it does copy it
|
|
|
|
if os.path.isfile(pre_in_path):
|
|
|
|
shutil.copyfile(pre_in_path, pre_out_path)
|
|
|
|
print(f"PRE COPIED: {patch}'s pre script was copied.")
|
|
|
|
|
|
|
|
# Check if post-script exists, if it does copy it
|
|
|
|
if os.path.isfile(post_in_path):
|
|
|
|
shutil.copyfile(post_in_path, post_out_path)
|
|
|
|
print(f"POST COPIED: {patch}'s post script was copied.")
|
|
|
|
|
|
|
|
|
|
|
|
# Create necessary instead of porting it.
|
|
|
|
if patch == "necessary":
|
|
|
|
patch_contents = make_necessary(to_versioncode, to_versionname)
|
|
|
|
else:
|
|
|
|
# Get a modified version of the patch
|
|
|
|
patch_contents = modify_patch(patch, patch_path)
|
|
|
|
|
|
|
|
# Pass the new patch to patch command and get it to attempt to patch
|
|
|
|
with open(tmp_out_path, "w") as f:
|
|
|
|
f.write(patch_contents)
|
|
|
|
out = subprocess.run("java -cp /opt/ctc/tools/xml-patch.jar com.github.dnault.xmlpatch.BatchPatcher --patch {tmp_out_path} --srcdir {apk_folder} --destdir /tmp/", shell=True,
|
|
|
|
cwd=apk_folder, text=True,
|
|
|
|
capture_output=True)
|
|
|
|
|
|
|
|
# Check for issues
|
|
|
|
if "ERROR" in out.stdout or "EXCEPTION" in out.stdout:
|
|
|
|
print(f"FAILED: {patch} failed, please fix by hand.")
|
|
|
|
failures.append(patch)
|
|
|
|
out_path += "-failed"
|
|
|
|
|
|
|
|
if debug:
|
|
|
|
print(out.stdout)
|
|
|
|
|
|
|
|
# Apply patch to main APK folder too
|
|
|
|
if patch in ["necessary"]:
|
|
|
|
apply_patch(patch_contents)
|
|
|
|
|
|
|
|
if from_versionname != to_versionname:
|
|
|
|
# Add supported version to readme of that patch, hacky
|
|
|
|
# https://stackoverflow.com/a/35130508/3286892
|
|
|
|
with open(readme_path, 'r') as f:
|
|
|
|
readme_text = f.read().replace(f'- {from_versionname}\n', f'- {from_versionname}\n' f'- {to_versionname}\n')
|
|
|
|
with open(readme_path, "w") as f:
|
|
|
|
f.write(readme_text)
|
|
|
|
|
|
|
|
# Save ported patch
|
|
|
|
with open(out_path, "w") as f:
|
|
|
|
f.write(patch_contents)
|
|
|
|
|
|
|
|
if not out_path.endswith("-failed"):
|
|
|
|
print(f"PORTED: {patch} was successfully ported.")
|
|
|
|
|
2021-08-06 17:07:52 +00:00
|
|
|
for patch in os.listdir(os.path.join(cutthecord_folder, "resources/patches")):
|
2019-07-10 21:32:10 +00:00
|
|
|
if debug:
|
|
|
|
print(f"going over patch: {patch}")
|
|
|
|
|
2019-06-25 12:11:01 +00:00
|
|
|
# Ignore non-dirs
|
2021-08-06 17:07:52 +00:00
|
|
|
if not os.path.isdir(os.path.join(cutthecord_folder, "resources/patches", patch)):
|
2019-07-10 21:32:10 +00:00
|
|
|
if debug:
|
|
|
|
print(f"patch is not a folder, skipping: {patch}")
|
2019-06-25 12:11:01 +00:00
|
|
|
continue
|
|
|
|
|
2021-08-10 18:56:58 +00:00
|
|
|
pre_in_path = os.path.join(cutthecord_folder, "resources/patches", patch, f"{from_versioncode}-pre.sh")
|
|
|
|
post_in_path = os.path.join(cutthecord_folder, "resources/patches", patch, f"{from_versioncode}-post.sh")
|
|
|
|
pre_out_path = os.path.join(cutthecord_folder, "resources/patches", patch, f"{to_versioncode}-pre.sh")
|
|
|
|
post_out_path = os.path.join(cutthecord_folder, "resources/patches", patch, f"{to_versioncode}-post.sh")
|
|
|
|
|
|
|
|
patch_path = os.path.join(cutthecord_folder, "resources/patches", patch, f"{from_versioncode}.patch")
|
|
|
|
out_path = os.path.join(cutthecord_folder, "resources/patches", patch, f"{to_versioncode}.patch")
|
2021-08-06 17:07:52 +00:00
|
|
|
readme_path = os.path.join(cutthecord_folder, "resources/patches", patch, "README.md")
|
2019-02-04 20:49:46 +00:00
|
|
|
|
2019-10-08 02:08:26 +00:00
|
|
|
# Handle copying of versioned scripts, untested and dirty!
|
2021-08-10 18:56:58 +00:00
|
|
|
script_path = os.path.join(cutthecord_folder, "resources/patches", patch, f"{from_versioncode}.sh")
|
2019-10-08 02:08:26 +00:00
|
|
|
if os.path.exists(script_path):
|
2021-08-10 18:56:58 +00:00
|
|
|
script_out_path = os.path.join(cutthecord_folder, "resources/patches", patch, f"{to_versioncode}.sh")
|
2019-10-08 02:08:26 +00:00
|
|
|
with open(script_path) as f:
|
|
|
|
with open(script_out_path, "w") as f2:
|
|
|
|
f2.write(f.read())
|
|
|
|
|
2019-02-04 20:49:46 +00:00
|
|
|
# Check if patch exists for from_version, if it doesn't, warn user
|
2019-04-25 10:12:30 +00:00
|
|
|
if not os.path.isfile(patch_path) and patch not in ["necessary"]:
|
2019-02-05 22:55:28 +00:00
|
|
|
# Don't warn on instructional patches
|
2021-08-10 18:56:58 +00:00
|
|
|
if patch not in ["customfont", "customring", "bettertm", "bettertmlight", "blobs"]:
|
2019-02-05 22:55:28 +00:00
|
|
|
print(f"SKIPPED: No {from_versionname} version found for {patch}.")
|
2019-02-04 20:49:46 +00:00
|
|
|
continue
|
|
|
|
|
2019-12-13 09:38:55 +00:00
|
|
|
# Check if pre-script exists, if it does copy it
|
|
|
|
if os.path.isfile(pre_in_path):
|
|
|
|
shutil.copyfile(pre_in_path, pre_out_path)
|
|
|
|
print(f"PRE COPIED: {patch}'s pre script was copied.")
|
|
|
|
|
|
|
|
# Check if post-script exists, if it does copy it
|
|
|
|
if os.path.isfile(post_in_path):
|
|
|
|
shutil.copyfile(post_in_path, post_out_path)
|
|
|
|
print(f"POST COPIED: {patch}'s post script was copied.")
|
|
|
|
|
2021-08-10 18:56:58 +00:00
|
|
|
|
2019-04-25 10:12:30 +00:00
|
|
|
# Create necessary instead of porting it.
|
|
|
|
if patch == "necessary":
|
|
|
|
patch_contents = make_necessary(to_versioncode, to_versionname)
|
|
|
|
else:
|
|
|
|
# Get a modified version of the patch
|
|
|
|
patch_contents = modify_patch(patch, patch_path)
|
2019-02-04 20:49:46 +00:00
|
|
|
|
|
|
|
# Pass the new patch to patch command and get it to attempt to patch
|
|
|
|
out = subprocess.run("patch -p1 --dry-run --force", shell=True,
|
|
|
|
cwd=apk_folder, input=patch_contents, text=True,
|
|
|
|
capture_output=True)
|
|
|
|
|
|
|
|
# Check for issues
|
2021-02-21 19:43:49 +00:00
|
|
|
if "FAILED" in out.stdout or "can't find file to patch" in out.stdout:
|
2019-02-04 20:49:46 +00:00
|
|
|
print(f"FAILED: {patch} failed, please fix by hand.")
|
|
|
|
failures.append(patch)
|
2019-03-08 11:53:24 +00:00
|
|
|
out_path += "-failed"
|
2019-02-04 20:49:46 +00:00
|
|
|
elif "offset" in out.stdout:
|
2021-01-10 00:39:47 +00:00
|
|
|
temp_patch_contents = fix_offset(patch_contents)
|
|
|
|
if temp_patch_contents:
|
|
|
|
patch_contents = temp_patch_contents
|
|
|
|
print(f"WARNING: {patch} has offsets which were auto corrected.")
|
|
|
|
else:
|
|
|
|
print(f"FAILED: {patch} is missing files, please fix by hand.")
|
|
|
|
failures.append(patch)
|
|
|
|
out_path += "-failed"
|
2019-02-04 20:49:46 +00:00
|
|
|
|
|
|
|
if debug:
|
|
|
|
print(out.stdout)
|
|
|
|
|
2019-04-25 10:12:30 +00:00
|
|
|
# Apply patch to main APK folder too
|
|
|
|
if patch in ["necessary"]:
|
|
|
|
apply_patch(patch_contents)
|
|
|
|
|
2020-09-08 10:01:23 +00:00
|
|
|
if from_versionname != to_versionname:
|
|
|
|
# Add supported version to readme of that patch, hacky
|
|
|
|
# https://stackoverflow.com/a/35130508/3286892
|
|
|
|
with open(readme_path, 'r') as f:
|
2021-08-10 18:56:58 +00:00
|
|
|
readme_text = f.read().replace(f'- {from_versionname}\n', f'- {from_versionname}\n' f'- {to_versionname}\n')
|
2020-09-08 10:01:23 +00:00
|
|
|
with open(readme_path, "w") as f:
|
|
|
|
f.write(readme_text)
|
2019-02-04 20:49:46 +00:00
|
|
|
|
|
|
|
# Save ported patch
|
|
|
|
with open(out_path, "w") as f:
|
|
|
|
f.write(patch_contents)
|
|
|
|
|
2019-03-21 11:02:40 +00:00
|
|
|
if not out_path.endswith("-failed"):
|
|
|
|
print(f"PORTED: {patch} was successfully ported.")
|
2019-02-04 20:49:46 +00:00
|
|
|
|
2019-05-18 06:54:20 +00:00
|
|
|
ctcreadme_path = os.path.join(cutthecord_folder, "README.md")
|
|
|
|
# TODO: can we pull the correct date from distok?
|
|
|
|
out_datestamp = datetime.datetime.utcnow().strftime("%Y-%m-%d")
|
|
|
|
# Update readme with latest version, hacky
|
|
|
|
# https://stackoverflow.com/a/35130508/3286892
|
|
|
|
with open(ctcreadme_path, 'r') as f:
|
|
|
|
ctcr_text = f.read().replace(f'{from_versionname} ({from_versioncode})',
|
|
|
|
f'{to_versionname} ({to_versioncode})')
|
|
|
|
in_datestamp = re_releasedate.findall(ctcr_text)[0]
|
|
|
|
ctcr_text = ctcr_text.replace(in_datestamp, out_datestamp)
|
|
|
|
with open(ctcreadme_path, "w") as f:
|
|
|
|
f.write(ctcr_text)
|
|
|
|
|
|
|
|
with open("patchport-state.json", "w") as f:
|
|
|
|
jout = {"versionname": to_versionname, "versioncode": to_versioncode}
|
|
|
|
json.dump(jout, f)
|
|
|
|
|
2019-02-04 20:49:46 +00:00
|
|
|
if failures:
|
|
|
|
print(f"Port complete. Following patches failed: {', '.join(failures)}")
|
|
|
|
else:
|
2019-03-08 12:27:43 +00:00
|
|
|
print("Port complete. All patches completed successfully.")
|