123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- import os
- import shutil
- import subprocess
- # 设置路径
- dest_path = os.path.join(os.getcwd(), "AppFile", "laser")
- dest_files = os.path.join(os.getcwd(), "AppCode","laser", "app.nsh")
- target_nsi_file = r"I:\prox\laser_pc\AppCode\laser\app\laser_setup.nsi"
- # 删除文件
- if os.path.exists(dest_files):
- os.remove(dest_files)
- total = 0
- curr = 0
- def process_directory(directory):
- global curr
- global total
- # 设置输出路径
- output_path = directory.replace(dest_path, "$INSTDIR")
- if output_path !='$INSTDIR':
- with open(dest_files, "a", encoding="UTF-8") as f:
- f.write('SetOutPath "{}"\n'.format(output_path))
- # 处理当前目录下的文件
- for file in os.listdir(directory):
- file_path = os.path.join(directory, file)
- if os.path.isfile(file_path):
- total += 1
- curr += 1
- with open(dest_files, "a", encoding="UTF-8") as f:
- f.write('Push ${APP_FILE_COUNT}\n')
- f.write('Push {}\n'.format(curr))
- f.write('Call ExtractCallback\n')
- f.write('SetOverwrite ifnewer\n')
- f.write('File "{}"\n'.format(file_path.replace(dest_path, "${APP_FILE_DIR}")))
-
- # 递归处理子目录
- for file in os.listdir(directory):
- file_path = os.path.join(directory, file)
- if os.path.isdir(file_path):
- process_directory(file_path)
- # 处理首级目录
- process_directory(dest_path)
- with open(dest_files, "a", encoding="UTF-8") as f:
- f.write('Push ${APP_FILE_COUNT}\n')
- f.write('Push ${APP_FILE_COUNT}\n')
- f.write('Call ExtractCallback\n')
- # 替换NSI文件中的值
- with open(target_nsi_file, "r", encoding="UTF-16 LE") as nsi_file:
- lines = nsi_file.readlines()
- # 寻找并替换特定行
- for i, line in enumerate(lines):
- if line.startswith("!define APP_FILE_COUNT"):
- lines[i] = '!define APP_FILE_COUNT {}\n'.format(total)
- break
- # 写入修改后的内容
- with open(target_nsi_file, "w", encoding="UTF-16 LE") as nsi_file:
- nsi_file.writelines(lines)
- print("总计:{}".format(total))
- print("执行打包命令>laser-nozip-N.bat")
- # 执行命令并获取输出结果
- process = subprocess.Popen("laser-nozip-N1.bat", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
- # 持续输出命令的结果
- for line in process.stdout:
- print(line, end='')
- # 等待命令执行完成
- process.wait()
- print("结束")
|