uengine-runner/uengine-apk-builder

451 lines
18 KiB
Plaintext
Raw Normal View History

2021-08-19 08:49:50 +08:00
#!/usr/bin/env python3
# 使用系统默认的 python3 运行
###########################################################################################
# 作者gfdgd xi
2022-05-15 20:13:18 +08:00
# 版本1.6.1
# 更新时间2022年04月30日五一了
2021-08-19 08:49:50 +08:00
# 感谢anbox、deepin 和 统信
# 基于 Python3 的 tkinter 构建
###########################################################################################
#################
# 引入所需的库
#################
import os
import sys
import json
import shutil
import random
import zipfile
import traceback
import threading
import subprocess
import ttkthemes
import tkinter as tk
import tkinter.ttk as ttk
import tkinter.messagebox as messagebox
import tkinter.filedialog as filedialog
from getxmlimg import getsavexml
def FindApk():
2021-08-19 14:08:05 +08:00
path = filedialog.askopenfilename(title="选择 Apk", filetypes=[("APK 文件", "*.apk"), ("所有文件", "*.*")], initialdir=json.loads(readtxt(get_home() + "/.config/uengine-runner/FindApkBuild.json"))["path"])
2021-08-19 08:49:50 +08:00
if path != "" and path != "()":
try:
combobox1.set(path)
2021-08-19 14:08:05 +08:00
write_txt(get_home() + "/.config/uengine-runner/FindApkBuild.json", json.dumps({"path": os.path.dirname(path)})) # 写入配置文件
2021-08-19 08:49:50 +08:00
except:
pass
def BuildDeb():
if combobox1.get() == "":
messagebox.showerror(title="提示", message="信息没有填写完整,无法继续打包 APK")
return
if not os.path.exists(combobox1.get()):
messagebox.showerror(title="提示", message="信息填写错误,无法继续打包 APK")
return
DisabledAndEnbled(True)
2021-12-12 11:28:22 +08:00
threading.Thread(target=GetBuildApkDebError, args=(combobox1.get(),)).start()
2021-08-19 08:49:50 +08:00
def RunCommandShow(command):
TextboxAddText1("$> {}".format(command))
TextboxAddText1(GetCommandReturn(command))
2021-12-12 11:28:22 +08:00
def GetBuildApkDebError(apkPath):
try:
BuildApkDeb(apkPath)
except:
traceback.print_exc()
messagebox.showerror(title="错误", message=traceback.format_exc())
DisabledAndEnbled(False)
2021-08-19 08:49:50 +08:00
def BuildApkDeb(apkPath):
tempPath = "/tmp/uengine-apk-builder-{}".format(int(random.randint(0, 1024)))
RunCommandShow("echo '======================================New===================================='")
RunCommandShow("echo '创建目录'")
RunCommandShow("mkdir -pv '{}/DEBIAN'".format(tempPath))
RunCommandShow("mkdir -pv '{}/usr/share/applications'".format(tempPath))
RunCommandShow("mkdir -pv '{}/usr/share/uengine/apk'".format(tempPath))
RunCommandShow("mkdir -pv '{}/usr/share/uengine/icons'".format(tempPath))
RunCommandShow("echo '写入文件,因为写入过程过于复杂,不显示写入命令……'")
2021-12-12 11:28:22 +08:00
apkPackageName = GetApkPackageName(apkPath, False)
2022-02-11 19:25:47 +08:00
if qianZhui.get():
2022-05-15 20:13:18 +08:00
apkPackageNameNew = GetApkPackageName(apkPath, True).lower()
2022-02-11 19:25:47 +08:00
else:
2022-05-15 20:13:18 +08:00
apkPackageNameNew = GetApkPackageName(apkPath, False).lower()
2021-08-19 08:49:50 +08:00
apkPackageVersion = GetApkVersion(apkPath)
2022-02-15 21:56:32 +08:00
if apkPackageVersion[0].upper() == "V":
package = list(apkPackageVersion)
package.pop(0)
apkPackageVersion = "".join(package)
2021-08-19 08:49:50 +08:00
apkChineseLabel = GetApkChineseLabel(apkPath)
apkActivityName = GetApkActivityName(apkPath)
2021-12-12 11:28:22 +08:00
iconSavePath = "{}/usr/share/uengine/icons/{}.png".format(tempPath, apkPackageNameNew)
2021-08-19 08:49:50 +08:00
debControl = '''Package: {}
Version: {}
Architecture: all
Maintainer: {}
Depends: deepin-elf-verify (>= 0.0.16.7-1), uengine (>= 1.0.1)
Section: utils
Priority: optional
2021-12-12 11:28:22 +08:00
Description: {}\n'''.format(apkPackageNameNew, apkPackageVersion, apkChineseLabel, apkChineseLabel)
2021-08-19 08:49:50 +08:00
debPostinst = '''#!/bin/sh
APK_DIR="/usr/share/uengine/apk"
2021-12-12 11:28:22 +08:00
APK_NAME="{}"
2021-08-19 08:49:50 +08:00
APK_PATH="$APK_DIR/$APK_NAME"
2021-12-12 11:28:22 +08:00
DESKTOP_FILE="{}"
2021-08-19 08:49:50 +08:00
if [ -f $APK_PATH ]; then
echo "Installing $APK_NAME"
else
2021-12-12 11:28:22 +08:00
echo "ERROR: $APK_NAME does not exist."
exit 0
2021-08-19 08:49:50 +08:00
fi
session_manager=`ps -ef | grep "uengine session-manager" | grep -v grep`
if test -z "$session_manager"; then
2021-12-12 11:28:22 +08:00
echo "ERROR: app install failed(session-manager is not running)."
sess_dir="/usr/share/uengine/session_install"
if [ ! -d $sess_dir ]; then
mkdir $sess_dir
chmod 777 $sess_dir
fi
apk_name=${{APK_PATH##*/}}
fileName="$sess_dir/$apk_name"
echo $DESKTOP_FILE > $fileName
abistr=""
if test -n "$abistr"; then
abi=`echo $abistr |awk -F \= '{{print $2}}'`
echo $abi >> $fileName
fi
chmod 766 $fileName
2021-08-19 08:49:50 +08:00
fi
2021-12-12 11:28:22 +08:00
/usr/bin/uengine-session-launch-helper -- uengine install --apk="$APK_PATH"
2021-08-19 08:49:50 +08:00
2021-12-12 11:28:22 +08:00
exit 0'''.format(apkPackageNameNew + ".apk", "/usr/share/applications/{}.desktop".format(apkPackageNameNew))
2021-08-19 08:49:50 +08:00
debPrerm = '''#!/bin/sh
APP_NAME="{}"
2021-12-12 11:28:22 +08:00
DESKTOP_FILE="{}"
2021-08-19 08:49:50 +08:00
session_manager=`ps -ef | grep "uengine session-manager" | grep -v grep`
if test -z "$session_manager"; then
2021-12-12 11:28:22 +08:00
echo "ERROR: app uninstall failed(session-manager is not running)."
sess_dir="/usr/share/uengine/session_uninstall"
if [ ! -d $sess_dir ]; then
mkdir $sess_dir
chmod 777 $sess_dir
fi
fileName="$sess_dir/$APP_NAME"
echo $DESKTOP_FILE > $fileName
chmod 766 $fileName
2021-08-19 08:49:50 +08:00
fi
echo "Uninstalling $APP_NAME"
2021-12-12 11:28:22 +08:00
/usr/bin/uengine-session-launch-helper -- uengine uninstall --pkg="$APP_NAME"
2021-08-19 08:49:50 +08:00
2021-12-12 11:28:22 +08:00
exit 0'''.format(apkPackageName, "/usr/share/applications/{}.desktop".format(apkPackageNameNew))
2021-08-19 08:49:50 +08:00
desktopFile = '''[Desktop Entry]
Categories=Other;
Exec=uengine launch --action=android.intent.action.MAIN --package={} --component={}
2021-08-19 08:49:50 +08:00
Icon=/usr/share/uengine/icons/{}.png
Terminal=false
Type=Application
GenericName={}
Name={}
'''
#RunCommandShow("echo '{}' > '{}/DEBIAN/control'".format(debControl, tempPath))
RunCommandShow("echo 正在写入文件:'{}/DEBIAN/control'".format(tempPath))
write_txt("{}/DEBIAN/control".format(tempPath), debControl)
RunCommandShow("echo 正在写入文件:'{}/DEBIAN/postinst'".format(tempPath))
write_txt("{}/DEBIAN/postinst".format(tempPath), debPostinst)
RunCommandShow("echo 正在写入文件:'{}/DEBIAN/prerm'".format(tempPath))
write_txt("{}/DEBIAN/prerm".format(tempPath), debPrerm)
2021-12-12 11:28:22 +08:00
RunCommandShow("echo 正在写入文件:'/usr/share/applications/{}.desktop'".format(apkPackageNameNew))
#write_txt("{}/usr/share/applications/{}.desktop".format(tempPath, apkPackageNameNew), desktopFile)
BuildUengineDesktop(apkPackageName, apkActivityName, apkChineseLabel, "/usr/share/uengine/icons/{}.png".format(apkPackageNameNew),
"{}/usr/share/applications/{}.desktop".format(tempPath, apkPackageNameNew))
2021-08-19 08:49:50 +08:00
RunCommandShow("echo '复制文件'")
RunCommandShow("echo '写入 APK 软件图标'")
SaveApkIcon(apkPath, iconSavePath)
RunCommandShow("echo '复制 APK 文件'")
2021-12-12 11:28:22 +08:00
RunCommandShow("cp -rv '{}' '{}/usr/share/uengine/apk/{}.apk'".format(apkPath, tempPath, apkPackageNameNew))
2021-08-19 08:49:50 +08:00
RunCommandShow("echo '正在设置文件权限……'")
RunCommandShow("chmod 0775 -vR '{}/DEBIAN/postinst'".format(tempPath))
RunCommandShow("chmod 0775 -vR '{}/DEBIAN/prerm'".format(tempPath))
RunCommandShow("echo '打包 deb 到桌面……'")
2021-12-12 11:28:22 +08:00
RunCommandShow("dpkg -b '{}' '{}/{}_{}.deb'".format(tempPath, get_desktop_path(),apkPackageNameNew, apkPackageVersion))
RunCommandShow("echo '正在删除临时目录……'")
RunCommandShow("rm -rfv '{}'".format(tempPath))
2021-08-19 08:49:50 +08:00
RunCommandShow("echo '完成!'")
findApkHistory.append(apkPath)
combobox1['value'] = findApkHistory
2021-08-19 14:08:05 +08:00
write_txt(get_home() + "/.config/uengine-runner/FindApkBuildHistory.json", str(json.dumps(ListToDictionary(findApkHistory)))) # 将历史记录的数组转换为字典并写入
2021-08-19 08:49:50 +08:00
messagebox.showinfo(title="提示", message="打包完成")
DisabledAndEnbled(False)
def DisabledAndEnbled(choose):
userChoose = {True: tk.DISABLED, False: tk.NORMAL}
a = userChoose[choose]
combobox1.configure(state=a)
2021-09-11 15:51:23 +08:00
check.configure(state=a)
2021-08-19 08:49:50 +08:00
button2.configure(state=a)
button3.configure(state=a)
# 需引入 subprocess
def GetCommandReturn(cmd):
# cmd 是要获取输出的命令
return subprocess.getoutput(cmd)
# 重启本应用程序
def ReStartProgram():
python = sys.executable
os.execl(python, python, * sys.argv)
# 获取用户主目录
def get_home():
return os.path.expanduser('~')
2021-09-20 17:10:05 +08:00
# 获取当前语言
def get_now_lang()->"获取当前语言":
return os.getenv('LANG')
2021-08-19 08:49:50 +08:00
# 获取用户桌面目录
def get_desktop_path():
for line in open(get_home() + "/.config/user-dirs.dirs"): # 以行来读取配置文件
desktop_index = line.find("XDG_DESKTOP_DIR=\"") # 寻找是否有对应项,有返回 0没有返回 -1
if desktop_index != -1: # 如果有对应项
break # 结束循环
if desktop_index == -1: # 如果是提前结束,值一定≠-1如果是没有提前结束值一定-1
return -1
else:
get = line[17:-2] # 截取桌面目录路径
get_index = get.find("$HOME") # 寻找是否有对应的项,需要替换内容
if get != -1: # 如果有
get = get.replace("$HOME", get_home()) # 则把其替换为用户目录(~)
return get # 返回目录
# 数组转字典
def ListToDictionary(list):
dictionary = {}
for i in range(len(list)):
dictionary[i] = list[i]
return dictionary
# 读取文本文档
def readtxt(path):
f = open(path, "r") # 设置文件对象
str = f.read() # 获取内容
f.close() # 关闭文本对象
return str # 返回结果
# 写入文本文档
def write_txt(path, things):
file = open(path, 'w', encoding='UTF-8') # 设置文件对象
file.write(things) # 写入文本
file.close() # 关闭文本对象
def GetApkInformation(apkFilePath):
return GetCommandReturn("aapt dump badging '{}'".format(apkFilePath))
def GetApkActivityName(apkFilePath):
info = GetApkInformation(apkFilePath)
for line in info.split('\n'):
if "launchable-activity" in line:
line = line[0: line.index("label='")]
line = line.replace("launchable-activity: ", "")
line = line.replace("'", "")
line = line.replace(" ", "")
line = line.replace("name=", "")
line = line.replace("label=", "")
line = line.replace("icon=", "")
return line
2021-12-12 11:28:22 +08:00
def GetApkPackageName(apkFilePath, setting):
2021-09-11 15:51:23 +08:00
# 提示:此函数有被为此程序适配而调整,如果需要最原始(无调整的)请使用主程序(此为附属组件)里的函数
2021-08-19 08:49:50 +08:00
info = GetApkInformation(apkFilePath)
for line in info.split('\n'):
if "package:" in line:
line = line[0: line.index("versionCode='")]
line = line.replace("package:", "")
line = line.replace("name=", "")
line = line.replace("'", "")
line = line.replace(" ", "")
2021-09-11 15:51:23 +08:00
# 此较为特殊,因为需要判断用户是否要添加前缀
2021-12-12 11:28:22 +08:00
if setting:
2022-05-15 20:13:18 +08:00
return "uengine-dc-{}".format(line)
return line
2021-08-19 08:49:50 +08:00
def GetApkVersion(apkFilePath):
info = GetApkInformation(apkFilePath)
for line in info.split('\n'):
if "package:" in line:
if "compileSdkVersion='" in line:
line = line.replace(line[line.index("compileSdkVersion='"): -1], "")
if "platform" in line:
line = line.replace(line[line.index("platform"): -1], "")
line = line.replace(line[0: line.index("versionName='")], "")
line = line.replace("versionName='", "")
line = line.replace("'", "")
line = line.replace(" ", "")
return line
def BuildUengineDesktop(packageName, activityName, showName, iconPath, savePath):
2022-02-07 22:35:15 +08:00
if showName == "" or showName == None:
showName = "未知应用"
2021-08-19 08:49:50 +08:00
things = '''
[Desktop Entry]
Categories=app;
Encoding=UTF-8
2021-12-12 11:28:22 +08:00
Exec=/usr/bin/uengine launch --action=android.intent.action.MAIN --package={} --component={}
2021-08-19 08:49:50 +08:00
GenericName={}
Icon={}
MimeType=
Name={}
StartupWMClass={}
Terminal=false
Type=Application
'''.format(packageName, activityName, showName, iconPath, showName, showName)
write_txt(savePath, things)
def GetApkChineseLabel(apkFilePath):
info = GetApkInformation(apkFilePath)
for line in info.split('\n'):
if "application-label:" in line:
line = line.replace("application-label:", "")
line = line.replace("'", "")
return line
#合并两个函数到一起
def SaveApkIcon(apkFilePath, iconSavePath)->"获取 apk 文件的图标":
try:
info = GetApkInformation(apkFilePath)
for line in info.split('\n'):
if "application:" in line:
xmlpath = line.split(":")[-1].split()[-1].split("=")[-1].replace("'","")
if xmlpath.endswith('.xml'):
xmlsave = getsavexml()
print(xmlpath)
xmlsave.savexml(apkFilePath,xmlpath,iconSavePath)
else:
zip = zipfile.ZipFile(apkFilePath)
iconData = zip.read(xmlpath)
with open(iconSavePath, 'w+b') as saveIconFile:
saveIconFile.write(iconData)
2022-02-07 22:35:15 +08:00
return
print("Show defult icon")
shutil.copy(programPath + "/defult.png", iconSavePath)
2021-08-19 08:49:50 +08:00
except:
traceback.print_exc()
print("Error, show defult icon")
shutil.copy(programPath + "/defult.png", iconSavePath)
def TextboxAddText1(message):
global textbox1
textbox1.configure(state=tk.NORMAL)
textbox1.insert(tk.END,message + "\n")
textbox1.configure(state=tk.DISABLED)
# 获取用户桌面目录
def get_desktop_path():
for line in open(get_home() + "/.config/user-dirs.dirs"): # 以行来读取配置文件
desktop_index = line.find("XDG_DESKTOP_DIR=\"") # 寻找是否有对应项,有返回 0没有返回 -1
if desktop_index != -1: # 如果有对应项
break # 结束循环
if desktop_index == -1: # 如果是提前结束,值一定≠-1如果是没有提前结束值一定-1
return -1
else:
get = line[17:-2] # 截取桌面目录路径
get_index = get.find("$HOME") # 寻找是否有对应的项,需要替换内容
if get != -1: # 如果有
get = get.replace("$HOME", get_home()) # 则把其替换为用户目录(~)
return get # 返回目录
# 获取用户主目录
def get_home():
return os.path.expanduser('~')
###########################
# 程序信息
###########################
2021-08-19 14:08:05 +08:00
programPath = os.path.split(os.path.realpath(__file__))[0] # 返回 string
2021-09-20 17:10:05 +08:00
lang = get_now_lang()
langFile = json.loads(readtxt(programPath + "/Language.json"))
if not lang in langFile.keys():
lang = "en_US.UTF-8"
2021-08-19 14:08:05 +08:00
information = json.loads(readtxt(programPath + "/information.json"))
version = information["Version"]
2021-09-20 17:10:05 +08:00
title = "{} {}".format(langFile[lang]["Uengine Apk Builder"]["Title"], version)
2022-02-07 22:35:15 +08:00
iconPath = "{}/builer.png".format(os.path.split(os.path.realpath(__file__))[0])
2021-08-19 08:49:50 +08:00
###########################
# 加载配置
###########################
2021-08-19 14:08:05 +08:00
if not os.path.exists(get_home() + "/.config/uengine-runner"): # 如果没有配置文件夹
2021-09-11 15:51:23 +08:00
os.makedirs(get_home() + "/.config/uengine-runner") # 创建配置文件夹
2021-08-19 14:08:05 +08:00
if not os.path.exists(get_home() + "/.config/uengine-runner/FindApkBuildHistory.json"): # 如果没有配置文件
write_txt(get_home() + "/.config/uengine-runner/FindApkBuildHistory.json", json.dumps({})) # 创建配置文件
if not os.path.exists(get_home() + "/.config/uengine-runner/FindApkBuild.json"): # 如果没有配置文件
write_txt(get_home() + "/.config/uengine-runner/FindApkBuild.json", json.dumps({"path": "~"})) # 创建配置文件
2021-08-19 08:49:50 +08:00
###########################
# 设置变量
###########################
2021-08-19 14:08:05 +08:00
findApkHistory = list(json.loads(readtxt(get_home() + "/.config/uengine-runner/FindApkBuildHistory.json")).values())
2021-08-19 08:49:50 +08:00
###########################
# 窗口创建
###########################
2022-07-07 14:35:44 +08:00
# 读取主题
try:
theme = not ("dark" in readtxt(get_home() + "/.gtkrc-2.0") and "gtk-theme-name=" in readtxt(get_home() + "/.gtkrc-2.0"))
except:
print("主题读取错误,默认使用浅色主题")
theme = True
if theme:
win = tk.Tk()
themes = ttkthemes.ThemedStyle(win)
themes.set_theme("breeze")
else:
import ttkbootstrap
style = ttkbootstrap.Style(theme="darkly")
win = style.master # 创建窗口
2021-09-11 15:51:23 +08:00
qianZhui = tk.BooleanVar()
2021-08-19 08:49:50 +08:00
window = ttk.Frame(win)
frame2 = ttk.Frame(window)
2021-09-20 17:10:05 +08:00
label1 = ttk.Label(window, text=langFile[lang]["Uengine Apk Builder"]["label1"])
2021-08-19 08:49:50 +08:00
combobox1 = ttk.Combobox(window, width=100)
2021-09-20 17:10:05 +08:00
button2 = ttk.Button(window, text=langFile[lang]["Uengine Apk Builder"]["button2"], command=FindApk)
button3 = ttk.Button(frame2, text=langFile[lang]["Uengine Apk Builder"]["button3"], command=BuildDeb)
check = ttk.Checkbutton(frame2, variable=qianZhui,text=langFile[lang]["Uengine Apk Builder"]["check"])
2021-08-19 08:49:50 +08:00
textbox1 = tk.Text(window, width=100)
menu = tk.Menu(window, background="white") # 设置菜单栏
programmenu = tk.Menu(menu, tearoff=0, background="white") # 设置“程序”菜单栏
2021-09-11 15:51:23 +08:00
2021-09-20 17:10:05 +08:00
menu.add_cascade(label=langFile[lang]["Uengine Apk Builder"]["Menu"][0]["Name"], menu=programmenu)
programmenu.add_command(label=langFile[lang]["Uengine Apk Builder"]["Menu"][0]["Menu"][0], command=window.quit) # 设置“退出程序”项
2021-08-19 08:49:50 +08:00
# 设置控件
combobox1['value'] = findApkHistory
textbox1.configure(state=tk.DISABLED)
textbox1.config(foreground='white', background='black')
2021-09-11 16:38:31 +08:00
# 如果有参数
if len(sys.argv) > 1:
combobox1.set(sys.argv[1])
2021-09-11 15:51:23 +08:00
# 设置窗口
win.title(title)
win.resizable(0, 0)
win.iconphoto(False, tk.PhotoImage(file=iconPath))
2021-08-19 08:49:50 +08:00
#
win.config(menu=menu) # 显示菜单栏
label1.grid(row=2, column=0)
combobox1.grid(row=2, column=1)
button2.grid(row=2, column=2)
2021-09-11 15:51:23 +08:00
button3.grid(row=0, column=1)
check.grid(row=0, column=0)
2021-08-19 08:49:50 +08:00
frame2.grid(row=3, columnspa=3)
textbox1.grid(row=4, columnspa=3)
window.pack()
win.mainloop()