This repository has been archived by the owner on Dec 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·204 lines (153 loc) · 6.3 KB
/
main.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import os
import sys
import json
import shutil
import zipfile
import fnmatch
from termcolor import cprint
# Resize window to fit banner
sys.stdout.write("\x1b[8;35;110t")
BANNER = r"""
_____ _ _ _____
/ ____| | | | | / ____| /\
| (___ _ _ ___| |_ ___ _ __ ___ | | ___ ___ ___ | | __ / \ _ __ _ __ ___
\___ \| | | / __| __/ _ \ '_ ` _ \| |/ _ \/ __/ __| | | |_ | / /\ \ | '_ \| '_ \/ __|
____) | |_| \__ \ || __/ | | | | | | __/\__ \__ \ | |__| |/ ____ \| |_) | |_) \__ \
|_____/ \__, |___/\__\___|_| |_| |_|_|\___||___/___/ \_____/_/ \_\ .__/| .__/|___/
__/ | | | | |
|___/ |_| |_|
* Systemless GApps by @chickendrop89, @Wacko1805
* NOTE: Make sure to edit preferences.json
* USAGE: python3 main.py <input: nikgapps package> <output directory>
"""
def printbanner():
"""Prints Systemless GApps banner"""
cprint(BANNER, "black", "on_green", attrs=["bold"])
# I/O Parameters
if len(sys.argv) != 3:
printbanner()
sys.exit(1)
else:
inputFilename = os.path.abspath(sys.argv[1])
outputDirectory = os.path.abspath(sys.argv[2])
def cleanenvironment():
"""Cleans environment of build-related folders"""
cprint("Cleaning environment", "black", "on_green", attrs=["bold"])
try:
# Sets correct permissions for deletion
os.chmod("gapps", 0o777)
os.chmod("builds", 0o777)
os.chmod("appset", 0o777)
# Deletes build-related folders and files
shutil.rmtree("gapps", ignore_errors=True)
shutil.rmtree("builds", ignore_errors=True)
shutil.rmtree("appset", ignore_errors=True)
os.remove("template/module.prop")
except OSError:
pass
with open('preferences.json', encoding="utf-8") as preferences:
contents = json.load(preferences)
# Module properties
moduleId = contents['magisk']['id']
moduleName = contents['magisk']['name']
moduleVersion = contents['magisk']['version']
moduleVersionCode = contents['magisk']['versionCode']
moduleAuthor = contents['magisk']['author']
moduleDescription = contents['magisk']['description']
def createmoduleprop():
"""Creates module.prop from strings included in preferences.json"""
with open("template/module.prop", "w", encoding="utf-8") as module:
module.write(
"id="
+ moduleId
+ "\n"
+ "name="
+ moduleName
+ "\n"
+ "version="
+ moduleVersion
+ "\n"
+ "versionCode="
+ moduleVersionCode
+ "\n"
+ "author="
+ moduleAuthor
+ "\n"
+ "description="
+ moduleDescription
)
def extractgapps():
"""Extracts specified NikGApps package"""
if os.path.exists(inputFilename):
with zipfile.ZipFile(os.path.join(".", inputFilename), "r") as zip_ref:
zip_ref.extractall("gapps")
else:
cprint(f"{inputFilename} is not present in parent directory", "white", "on_red", attrs=["bold"])
sys.exit(1)
# Define the appsetPath variable to point to the 'appset' directory
appsetpath = os.path.join(".", "appset")
# Check if the 'appset' directory exists before proceeding
if not os.path.exists(appsetpath):
os.makedirs(appsetpath)
rootpath = os.path.join("gapps", "AppSet")
pattern = "*.zip"
for root, dest, files in os.walk(rootpath):
for filename in fnmatch.filter(files, pattern):
cprint(f"Extracting {os.path.join(root, filename)}", "black", "on_magenta", attrs=["bold"])
zipfile.ZipFile(os.path.join(root, filename)).extractall(appsetpath)
os.remove("appset/installer.sh")
os.remove("appset/uninstaller.sh")
# Renames Files from ___ to /
for filename in os.listdir(appsetpath):
src_file = os.path.join(appsetpath, filename)
dst_file = os.path.join(appsetpath, filename.replace("___", os.sep).lstrip(os.sep))
# Check if the destination directory exists, if not create it
dst_dir = os.path.dirname(dst_file)
if not os.path.exists(dst_dir):
os.makedirs(dst_dir)
if os.path.exists(dst_file):
if os.path.isdir(dst_file):
shutil.rmtree(dst_file)
else:
os.remove(dst_file)
shutil.move(src_file, dst_file)
def copytemplate():
"""Copies the MMT template to builds folder"""
source_folder = "template"
destination_folder = "builds"
cprint("Combining Template", "black", "on_green", attrs=["bold"])
shutil.copytree(source_folder, destination_folder)
def combinegapps():
"""Combines NikGApps with the MMT template"""
source_folder = "appset"
destination_folder = "builds/system"
cprint("Building Module", "black", "on_green", attrs=["bold"])
shutil.copytree(source_folder, destination_folder)
def createarchive():
"""Creates Systemless GApps archive"""
splitinputfilename = os.path.split(inputFilename)[1]
if not os.path.exists(outputDirectory):
os.makedirs(outputDirectory)
archivepath = f"{outputDirectory}/SyG - {splitinputfilename} - {moduleVersion}"
archivepath = archivepath.replace(".zip", "")
# Delete existing archive if exists
if os.path.exists(archivepath):
os.remove(archivepath)
cprint("Building SystemlessGApps ZIP archive", "black", "on_green", attrs=["bold"])
shutil.make_archive(archivepath, 'zip', "builds")
# Print absolute archive path
cprint(os.path.abspath(f"{archivepath}.zip"), "black", "on_green", attrs=["bold"])
if __name__ == "__main__":
printbanner()
cleanenvironment()
# Creates module.prop from strings included in preferences.json
createmoduleprop()
# Extracts specified NikGApps package
extractgapps()
# Copies the MMT template to builds folder
copytemplate()
# Combines NikGApps with the MMT template
combinegapps()
# Creates SystemlessGApps archive
createarchive()
cleanenvironment()