forked from crimera/twitter-apk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
102 lines (79 loc) · 2.47 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import os
import shutil
import requests
import subprocess
import sys
from github import get_last_build_version
def panic(message: str):
print(message, file=sys.stderr)
exit(1)
def download(link, out, headers=None):
if os.path.exists(out):
print(f"{out} already exists skipping download")
return
# https://www.slingacademy.com/article/python-requests-module-how-to-download-files-from-urls/#Streaming_Large_Files
with requests.get(link, stream=True, headers=headers) as r:
r.raise_for_status()
with open(out, "wb") as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
def run_command(command: list[str]):
cmd = subprocess.run(command, capture_output=True, shell=True)
try:
cmd.check_returncode()
except subprocess.CalledProcessError:
print(cmd.stdout)
print(cmd.stderr)
exit(1)
def merge_apk(path: str):
subprocess.run(
["java", "-jar", "./bins/apkeditor.jar", "m", "-i", path]
).check_returncode()
def patch_apk(
cli: str,
integrations: str,
patches: str,
apk: str,
includes: list[str] | None = None,
excludes: list[str] | None = None,
out: str | None = None,
):
command = [
"java",
"-jar",
cli,
"patch",
"-b",
patches,
"-m",
integrations,
# use j-hc's keystore so we wouldn't need to reinstall
"--unsigned",
"--exclusive",
]
if includes is not None:
for i in includes:
command.append("-i")
command.append(i)
if excludes is not None:
for e in excludes:
command.append("-e")
command.append(e)
command.append(apk)
subprocess.run(command).check_returncode()
# remove -patched from the apk to match out
if out is not None:
cli_output = f"{str(apk).removesuffix(".apk")}-patched.apk"
if os.path.exists(out):
os.unlink(out)
shutil.move(cli_output, out)
def publish_release(tag: str, files: list[str], message: str):
key = os.environ.get("GH_TOKEN")
if key is None:
raise Exception("GH_TOKEN is not set")
command = ["gh", "release", "create", "--latest", tag, "--notes", message]
if len(files) == 0:
raise Exception("Files should have atleast one item")
for file in files:
command.append(file)
subprocess.run(command, env=os.environ.copy()).check_returncode()