169 lines
7.4 KiB
Python
169 lines
7.4 KiB
Python
|
|
# This Python file uses the following encoding: utf-8
|
|||
|
|
import sys
|
|||
|
|
import uuid
|
|||
|
|
import re
|
|||
|
|
import shutil
|
|||
|
|
import zipfile
|
|||
|
|
import hashlib
|
|||
|
|
import os
|
|||
|
|
import json
|
|||
|
|
from PyQt6 import *
|
|||
|
|
from PyQt6.QtCore import *
|
|||
|
|
from PyQt6.QtGui import *
|
|||
|
|
from PyQt6.QtWidgets import *
|
|||
|
|
|
|||
|
|
from ui_form import Ui_Widget
|
|||
|
|
|
|||
|
|
class Widget(QWidget):
|
|||
|
|
def __init__(self, parent=None):
|
|||
|
|
super().__init__(parent)
|
|||
|
|
self.ui = Ui_Widget()
|
|||
|
|
self.ui.setupUi(self)
|
|||
|
|
self.block = False
|
|||
|
|
self.appjs = ""
|
|||
|
|
self.ui.pbPackage.clicked.connect(self.compressPackage)
|
|||
|
|
self.ui.leappId.textChanged.connect(self.updateInfo)
|
|||
|
|
self.ui.leDes.textChanged.connect(self.updateInfo)
|
|||
|
|
self.ui.leName.textChanged.connect(self.updateInfo)
|
|||
|
|
self.ui.leVersion.textChanged.connect(self.updateInfo)
|
|||
|
|
self.ui.pbOpenFile.clicked.connect(self.openFileDialog)
|
|||
|
|
self.ui.pbOpen.clicked.connect(self.openDir)
|
|||
|
|
|
|||
|
|
def openFileDialog(self):
|
|||
|
|
file_dialog = QFileDialog(self)
|
|||
|
|
file_dialog.setWindowTitle("选择工程")
|
|||
|
|
file_dialog.setFileMode(QFileDialog.FileMode.Directory)
|
|||
|
|
if file_dialog.exec():
|
|||
|
|
file_path = file_dialog.selectedFiles()[0]
|
|||
|
|
# 获取info文件的路径
|
|||
|
|
info_path = os.path.join(file_path, "info")
|
|||
|
|
self.block = True
|
|||
|
|
if os.path.exists(info_path):
|
|||
|
|
# 读取文件内容
|
|||
|
|
with open(info_path, 'r', encoding="utf-8") as f:
|
|||
|
|
content = f.read()
|
|||
|
|
info = json.loads(content)
|
|||
|
|
# 将文件内容保存到列表中
|
|||
|
|
self.ui.leappId.setText(info["General"]["AppId"])
|
|||
|
|
self.ui.leName.setText(info["General"]["Name"])
|
|||
|
|
self.ui.leDes.setText(info["General"]["Description"])
|
|||
|
|
self.ui.leVersion.setText(info["General"]["Version"])
|
|||
|
|
else:
|
|||
|
|
self.ui.leappId.setText(str(uuid.uuid4()))
|
|||
|
|
self.ui.leName.setText("")
|
|||
|
|
self.ui.leDes.setText("")
|
|||
|
|
self.ui.leVersion.setText("")
|
|||
|
|
self.block = False
|
|||
|
|
self.ui.lePro.setText(file_path)
|
|||
|
|
|
|||
|
|
def updateInfo(self):
|
|||
|
|
if self.block:
|
|||
|
|
return
|
|||
|
|
# 获取info文件的路径
|
|||
|
|
info_path = os.path.join(self.ui.lePro.text(), "info")
|
|||
|
|
info = {
|
|||
|
|
"General": {
|
|||
|
|
"AppId": self.ui.leappId.text(),
|
|||
|
|
"Name": self.ui.leName.text(),
|
|||
|
|
"Description": self.ui.leDes.text(),
|
|||
|
|
"Version": self.ui.leVersion.text(),
|
|||
|
|
"Show": False
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
# 将文件内容保存到列表中
|
|||
|
|
with open(info_path, 'w', encoding="utf-8") as f:
|
|||
|
|
json.dump(info, f, ensure_ascii=False, indent=4)
|
|||
|
|
|
|||
|
|
def calculate_file_md5(self, file_path):
|
|||
|
|
# 计算给定文件的MD5值。
|
|||
|
|
md5 = hashlib.md5()
|
|||
|
|
with open(file_path, 'rb') as f:
|
|||
|
|
for chunk in iter(lambda: f.read(4096), b""):
|
|||
|
|
md5.update(chunk)
|
|||
|
|
return md5.hexdigest()
|
|||
|
|
|
|||
|
|
def calculate_directory_md5(self, directory_path):
|
|||
|
|
#计算给定目录下除md5.txt文件以外的全部文件的MD5值。
|
|||
|
|
md5_values = {}
|
|||
|
|
with os.scandir(directory_path) as it:
|
|||
|
|
for entry in it:
|
|||
|
|
if entry.is_file() and entry.name != 'md5.txt':
|
|||
|
|
file_md5 = self.calculate_file_md5(entry.path)
|
|||
|
|
md5_values[entry.name] = file_md5
|
|||
|
|
elif entry.is_dir():
|
|||
|
|
sub_directory_md5 = self.calculate_directory_md5(entry.path)
|
|||
|
|
md5_values[entry.name] = sub_directory_md5
|
|||
|
|
return {k: v for k, v in md5_values.items() if v != {} }
|
|||
|
|
|
|||
|
|
def openDir(self):
|
|||
|
|
directory_path = self.ui.leDir.text()
|
|||
|
|
os.makedirs( directory_path, exist_ok=True)
|
|||
|
|
os.startfile(directory_path)
|
|||
|
|
def compressPackage(self):
|
|||
|
|
try:
|
|||
|
|
filename = self.ui.leName.text()+".tpa"
|
|||
|
|
packeagePath = os.path.join(self.ui.leDir.text(), filename)
|
|||
|
|
fname = QFileDialog.getSaveFileName(self, '保存文件', packeagePath, 'TPA files (*.tpa)')
|
|||
|
|
if fname[0]:
|
|||
|
|
packeagePath = fname[0]
|
|||
|
|
if os.path.exists(packeagePath):
|
|||
|
|
os.remove(packeagePath)
|
|||
|
|
print('文件已删除')
|
|||
|
|
filename_with_extension = os.path.basename(fname[0]) # 获取带有后缀的文件名
|
|||
|
|
filename = filename_with_extension.replace('.tpa', '') # 删除后缀,获取没有后缀的文件名
|
|||
|
|
#更新app.js
|
|||
|
|
js_path = os.path.join(self.ui.lePro.text(), "app.js")
|
|||
|
|
with open(js_path, 'r') as file:
|
|||
|
|
self.appjs = file.read()
|
|||
|
|
|
|||
|
|
with open(js_path, 'w', encoding="utf-8") as file:
|
|||
|
|
file.write('var appId = \"' + self.ui.leappId.text() + '\"\nvar plusFile = \"plus\"')
|
|||
|
|
|
|||
|
|
self.ui.lbStatus.setText("开始打包")
|
|||
|
|
directory_path = self.ui.lePro.text()
|
|||
|
|
target_path = os.path.dirname(fname[0])
|
|||
|
|
directory_md5 = self.calculate_directory_md5(directory_path)
|
|||
|
|
json_str = json.dumps(directory_md5, indent=4, sort_keys=True)
|
|||
|
|
md5_str = ""
|
|||
|
|
md5_str = hashlib.md5(json_str.encode('utf-8')).hexdigest()
|
|||
|
|
if md5_str != "":
|
|||
|
|
with open(os.path.join(self.ui.lePro.text(),"md5.txt"), 'w') as f:
|
|||
|
|
f.write(md5_str)
|
|||
|
|
os.makedirs( self.ui.leDir.text(), exist_ok=True)
|
|||
|
|
zip_path = os.path.join(self.ui.leDir.text(), filename +".zip")
|
|||
|
|
# 创建ZIP文件并添加目录内容
|
|||
|
|
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
|||
|
|
# 递归添加目录和文件到ZIP文件中
|
|||
|
|
self.add_dir_to_zip(directory_path, zipf, directory_path)
|
|||
|
|
filename, ext = os.path.splitext(zip_path)
|
|||
|
|
os.rename( zip_path, os.path.join( target_path, filename_with_extension) )
|
|||
|
|
#还原debug app.js
|
|||
|
|
with open(js_path, 'w', encoding="utf-8") as file:
|
|||
|
|
file.write(self.appjs)
|
|||
|
|
self.ui.lbStatus.setText("打包成功")
|
|||
|
|
else:
|
|||
|
|
self.ui.lbStatus.setText("md5为空")
|
|||
|
|
except Exception as e:
|
|||
|
|
try:
|
|||
|
|
os.remove(zip_path)
|
|||
|
|
except:
|
|||
|
|
pass
|
|||
|
|
self.ui.lbStatus.setText(f"打包失败{e}")
|
|||
|
|
|
|||
|
|
def add_dir_to_zip(self, dir_path, zipf, parent_dir):
|
|||
|
|
for root, dirs, files in os.walk(dir_path):
|
|||
|
|
for file in files:
|
|||
|
|
file_path = os.path.join(root, file)
|
|||
|
|
# 使用os.path.relpath获取相对于parent_dir的路径作为归档名称
|
|||
|
|
arcname = os.path.relpath(file_path, parent_dir)
|
|||
|
|
zipf.write(file_path, arcname=arcname)
|
|||
|
|
for subdir in dirs:
|
|||
|
|
subdir_path = os.path.join(root, subdir)
|
|||
|
|
# 递归调用add_dir_to_zip()函数,将空目录也添加到ZIP文件中
|
|||
|
|
self.add_dir_to_zip(subdir_path, zipf, parent_dir)
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
app = QApplication(sys.argv)
|
|||
|
|
widget = Widget()
|
|||
|
|
widget.show()
|
|||
|
|
sys.exit(app.exec())
|