patchport: Fix #92

This commit is contained in:
ave 2021-01-10 03:39:47 +03:00
parent f3ee05255c
commit 1cc00f6a48

View file

@ -54,23 +54,25 @@ def fix_offset(patch_contents):
for line in patch_lines: for line in patch_lines:
if "diff -crB" in line: if "diff -crB" in line:
patch_target = line.split(" ")[2].replace("from/", "") patch_target = line.split(" ")[2].replace("from/", "")
if not os.path.exists(os.path.join(apk_folder, patch_target)):
return False
os.makedirs(os.path.dirname(os.path.join(tmp_folder, patch_target)), exist_ok=True) 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)) shutil.copy(os.path.join(apk_folder, patch_target), os.path.join(tmp_folder, patch_target))
#shutil.copytree(apk_folder, tmp_folder) # shutil.copytree(apk_folder, tmp_folder)
subprocess.run("patch -p1 --no-backup-if-mismatch --force", subprocess.run("patch -p1 --no-backup-if-mismatch --force",
shell=True, input=patch_contents, text=True, shell=True, input=patch_contents, text=True,
cwd=tmp_folder, capture_output=True) cwd=tmp_folder, capture_output=True)
out = subprocess.run(f"diff -crB {apk_folder} {tmp_folder}", out = subprocess.run(f"diff -crB {apk_folder} {tmp_folder}",
shell=True, input=patch_contents, text=True, shell=True, input=patch_contents, text=True,
cwd=tmp_folder, capture_output=True) cwd=tmp_folder, capture_output=True)
shutil.rmtree(tmp_folder, ignore_errors=True) shutil.rmtree(tmp_folder, ignore_errors=True)
actual_difflines = [] actual_difflines = []
for line in out.stdout.splitlines(): for line in out.stdout.splitlines():
if line[0:4] != "Only": if line[0:4] != "Only":
actual_difflines.append(line) actual_difflines.append(line)
patch_out = ("\n".join(actual_difflines)+"\n").replace(apk_folder, "from").replace(tmp_folder, "to") patch_out = ("\n".join(actual_difflines)+"\n").replace(apk_folder, "from").replace(tmp_folder, "to")
return patch_out return patch_out
@ -204,8 +206,14 @@ for patch in os.listdir(os.path.join(cutthecord_folder, "patches")):
failures.append(patch) failures.append(patch)
out_path += "-failed" out_path += "-failed"
elif "offset" in out.stdout: elif "offset" in out.stdout:
patch_contents = fix_offset(patch_contents) temp_patch_contents = fix_offset(patch_contents)
print(f"WARNING: {patch} has offsets which were auto corrected.") 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"
if debug: if debug:
print(out.stdout) print(out.stdout)