import redis,requests,json,os,sys import platform if platform.system() == 'Windows': sys.path.append('e:\\make2\\libs\\') else: sys.path.append('/data/deploy/make3d/make2/libs/') import config import requests from PIL import Image from io import BytesIO import zipfile import os,time,shutil url = "https://mp.api.suwa3d.com" def transToIco(pngPath,icoPath): image = Image.open(pngPath) image.save(icoPath,format="ICO",sizes=[(48,48)]) def delete_exe_files_in_dir(directory): # 遍历目录中的所有文件和目录条目 for filename in os.listdir(directory): file_path = os.path.join(directory, filename) # 检查当前条目是否为文件且扩展名为.exe if os.path.isfile(file_path) and filename.endswith('.exe'): try: os.remove(file_path) # 删除文件 print(f"Deleted: {file_path}") except Exception as e: print(f"Error deleting {file_path}: {e}") def zip_dir(path, output=None): """压缩指定目录""" output = output or os.path.basename(path) + '.zip' # 压缩文件的名字 zip = zipfile.ZipFile(output, 'w', zipfile.ZIP_DEFLATED) for root, dirs, files in os.walk(path): relative_root = '' if root == path else root.replace(path, '') + os.sep # 计算文件相对路径 for filename in files: zip.write(os.path.join(root, filename), relative_root + filename) # 文件路径 压缩文件路径(相对路径) zip.close() def create_redis_connection(): """创建 Redis 连接,若连接失败则重试""" while True: try: r = redis.Redis(host="106.14.158.208",password="kcV2000",port=6379,db=1) # 尝试进行一次操作,检查连接是否有效 r.ping() # ping 操作是一个简单的连接测试 print("Redis连接成功!") return r except ConnectionError: print("Redis连接失败,正在重试...") time.sleep(5) def main(): #连接mp 的redis r = create_redis_connection() #本地测试环境 #r = redis.Redis(host="172.31.1.254",port=6379,db=1) #取出数据 oemId = r.lpop('setup') if oemId is None: print("没有从redis中获取到oemId的数据") return print("oemId",oemId) #请求mp的接口,获取对应的oem的数据 res = requests.get(url+"/api/oem/infoById",params={"id":oemId}) #获取数据解析数据 # print(res.text) #解析数据 data = json.loads(res.text) # print(data) if data['code'] != 1000: print("根据id获取oem数据失败") return print(data['data']) #pythob 进入到 E:\wails\oemProject ,将 data 数据写入到 wails.json 文件中的 ext_oem 字段中 with open("E:\\make2\\oemProject\\wails.json","r",encoding="utf-8") as f: wailsData = json.load(f) wailsData['ext_oem'] = data['data'] wailsData['name'] = data['data']['brand_name'] wailsData["outputfilename"] = str(data['data']['id']) with open("E:\\make2\\oemProject\\wails.json","w",encoding="utf-8") as f: json.dump(wailsData,f,ensure_ascii=False,indent=4) # #将图片下载到本地 getExeImg(data['data']['exe_logo'], "E:\\make2\\oemProject\\build\\appicon.png") #删除 D:\\oemProject\\build\\bin 目录下的所有exe 文件,不包含子目录 delete_exe_files_in_dir("E:\\make2\\oemProject\\build\\bin") # #重命名文件 # os.system(f"cd D:\\oemProject && ren build\\bin\\{data['data']['brand_name']}amd64-installer.exe "+str(data['data']['id'])+".exe") #写入成功后在 E:\\wails\\oemProject\\ 目录下执行 wails build 命令 -debug os.system("cd E:\\make2\\oemProject && wails build -nsis -devtools") time.sleep(35) #删除指定目录下的多余产生的文件 os.system("cd E:\\make2\\oemProject && del /s/q/f build\\bin\\"+str(data['data']['id'])+".exe") #打包压缩指定文件夹 # 指定要打包的文件夹路径 folder_to_zip = 'E:\\make2\\oemProject\\build\\bin' # 指定生成的压缩包名称和路径 zip_filename = f"{str(data['data']['id'])}.zip" zip_dir(folder_to_zip,zip_filename) #执行 wails build 命令后,将 E:\wails\oemProject\build\bin ,在上传到 oss 中 config.oss_bucket.put_object_from_file(f'exe/'+str(data['data']['id'])+".zip", str(data['data']['id'])+".zip") print("打包程序已上传完成-"+str(data['data']['id'])+".zip") #上传成功后,调用接口,告诉mp,已经生成exe及上传成功 requests.get(url+"/api/oem/infoSetupUpdate",params={"id":oemId}) # os.system("cd E:\\make2 && del /s/q/f timer\\"+str(data['data']['id'])+".zip") def getExeImg(image_url, output_path): # 从URL获取图片 response = requests.get(image_url) if response.status_code == 200: # 将内容读取为图片 image = Image.open(BytesIO(response.content)) # 修改图片尺寸为1024x1024 resized_image = image.resize((1024, 1024)) # 保存图片为PNG格式 resized_image.save(output_path, 'PNG') #转换格式 transToIco(output_path,"E:\\make2\\oemProject\\build\\windows\\icon.ico") #复制图片到指定目录 time.sleep(3) shutil.copy("E:\\make2\\oemProject\\build\\windows\\icon.ico","E:\\make2\\oemProject\\build\\bin\\icon.ico",) else: print(f"Failed to download image. Status code: {response.status_code}") if __name__ == '__main__': #开启定时循环模式 print("开启定时任务,检测是否打包") while True: try: main() time.sleep(10) continue except Exception as e: print(f"出现异常: {str(e)}") time.sleep(10) continue