make.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import os
  2. import shutil
  3. import subprocess
  4. # 设置路径
  5. dest_path = os.path.join(os.getcwd(), "AppFile", "laser")
  6. dest_files = os.path.join(os.getcwd(), "AppCode","laser", "app.nsh")
  7. target_nsi_file = r"I:\prox\laser_pc\AppCode\laser\app\laser_setup.nsi"
  8. # 删除文件
  9. if os.path.exists(dest_files):
  10. os.remove(dest_files)
  11. total = 0
  12. curr = 0
  13. def process_directory(directory):
  14. global curr
  15. global total
  16. # 设置输出路径
  17. output_path = directory.replace(dest_path, "$INSTDIR")
  18. if output_path !='$INSTDIR':
  19. with open(dest_files, "a", encoding="UTF-8") as f:
  20. f.write('SetOutPath "{}"\n'.format(output_path))
  21. # 处理当前目录下的文件
  22. for file in os.listdir(directory):
  23. file_path = os.path.join(directory, file)
  24. if os.path.isfile(file_path):
  25. total += 1
  26. curr += 1
  27. with open(dest_files, "a", encoding="UTF-8") as f:
  28. f.write('Push ${APP_FILE_COUNT}\n')
  29. f.write('Push {}\n'.format(curr))
  30. f.write('Call ExtractCallback\n')
  31. f.write('SetOverwrite ifnewer\n')
  32. f.write('File "{}"\n'.format(file_path.replace(dest_path, "${APP_FILE_DIR}")))
  33. # 递归处理子目录
  34. for file in os.listdir(directory):
  35. file_path = os.path.join(directory, file)
  36. if os.path.isdir(file_path):
  37. process_directory(file_path)
  38. # 处理首级目录
  39. process_directory(dest_path)
  40. with open(dest_files, "a", encoding="UTF-8") as f:
  41. f.write('Push ${APP_FILE_COUNT}\n')
  42. f.write('Push ${APP_FILE_COUNT}\n')
  43. f.write('Call ExtractCallback\n')
  44. # 替换NSI文件中的值
  45. with open(target_nsi_file, "r", encoding="UTF-16 LE") as nsi_file:
  46. lines = nsi_file.readlines()
  47. # 寻找并替换特定行
  48. for i, line in enumerate(lines):
  49. if line.startswith("!define APP_FILE_COUNT"):
  50. lines[i] = '!define APP_FILE_COUNT {}\n'.format(total)
  51. break
  52. # 写入修改后的内容
  53. with open(target_nsi_file, "w", encoding="UTF-16 LE") as nsi_file:
  54. nsi_file.writelines(lines)
  55. print("总计:{}".format(total))
  56. print("执行打包命令>laser-nozip-N.bat")
  57. # 执行命令并获取输出结果
  58. process = subprocess.Popen("laser-nozip-N1.bat", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
  59. # 持续输出命令的结果
  60. for line in process.stdout:
  61. print(line, end='')
  62. # 等待命令执行完成
  63. process.wait()
  64. print("结束")