当前位置: 首页 > article >正文

macos自动制作dmg安装包脚本

macos下,使用脚本制作dmg安装包脚本:

目录结构:

% tree helloworld/
test
|-- Applications -> /Applications
`-- Helloworld.app
    `-- Contents
        |-- Frameworks
        |   |-- QtCore.framework
        |   |   |-- QtCore -> Versions/Current/QtCore
        |   |   |-- Resources -> Versions/Current/Resources
        |   |   `-- Versions
        |   |       |-- 5
        |   |       |   |-- QtCore
        |   |       |   `-- Resources
        |   |       |       |-- Info.plist
        |   |       |       `-- QtCore.prl
        |   |       `-- Current -> 5
        |   |-- QtDBus.framework
        |   |   |-- QtDBus -> Versions/Current/QtDBus
        |   |   |-- Resources -> Versions/Current/Resources
        |   |   `-- Versions
        |   |       |-- 5
        |   |       |   |-- QtDBus
        |   |       |   `-- Resources
        |   |       |       |-- Info.plist
        |   |       |       `-- QtDBus.prl
        |   |       `-- Current -> 5
        |   |-- QtGui.framework
        |   |   |-- QtGui -> Versions/Current/QtGui
        |   |   |-- Resources -> Versions/Current/Resources
        |   |   `-- Versions
        |   |       |-- 5
        |   |       |   |-- QtGui
        |   |       |   `-- Resources
        |   |       |       |-- Info.plist
        |   |       |       `-- QtGui.prl
        |   |       `-- Current -> 5
        |   |-- QtNetwork.framework
        |   |   |-- QtNetwork -> Versions/Current/QtNetwork
        |   |   |-- Resources -> Versions/Current/Resources
        |   |   `-- Versions
        |   |       |-- 5
        |   |       |   |-- QtNetwork
        |   |       |   `-- Resources
        |   |       |       |-- Info.plist
        |   |       |       `-- QtNetwork.prl
        |   |       `-- Current -> 5
        |   |-- QtPrintSupport.framework
        |   |   |-- QtPrintSupport -> Versions/Current/QtPrintSupport
        |   |   |-- Resources -> Versions/Current/Resources
        |   |   `-- Versions
        |   |       |-- 5
        |   |       |   |-- QtPrintSupport
        |   |       |   `-- Resources
        |   |       |       |-- Info.plist
        |   |       |       `-- QtPrintSupport.prl
        |   |       `-- Current -> 5
        |   |-- QtSvg.framework
        |   |   |-- QtSvg -> Versions/Current/QtSvg
        |   |   |-- Resources -> Versions/Current/Resources
        |   |   `-- Versions
        |   |       |-- 5
        |   |       |   |-- QtSvg
        |   |       |   `-- Resources
        |   |       |       |-- Info.plist
        |   |       |       `-- QtSvg.prl
        |   |       `-- Current -> 5
        |   `-- QtWidgets.framework
        |       |-- QtWidgets -> Versions/Current/QtWidgets
        |       |-- Resources -> Versions/Current/Resources
        |       `-- Versions
        |           |-- 5
        |           |   |-- QtWidgets
        |           |   `-- Resources
        |           |       |-- Info.plist
        |           |       `-- QtWidgets.prl
        |           `-- Current -> 5
        |-- Info.plist
        |-- MacOS
        |   |-- HelloworldUI
        |   `-- HelloworldService
        |-- PlugIns
        |   |-- bearer
        |   |   `-- libqgenericbearer.dylib
        |   |-- iconengines
        |   |   `-- libqsvgicon.dylib
        |   |-- imageformats
        |   |   |-- libqgif.dylib
        |   |   |-- libqicns.dylib
        |   |   |-- libqico.dylib
        |   |   |-- libqjpeg.dylib
        |   |   |-- libqmacheif.dylib
        |   |   |-- libqmacjp2.dylib
        |   |   |-- libqtga.dylib
        |   |   |-- libqtiff.dylib
        |   |   |-- libqwbmp.dylib
        |   |   `-- libqwebp.dylib
        |   |-- platforms
        |   |   `-- libqcocoa.dylib
        |   |-- printsupport
        |   |   `-- libcocoaprintersupport.dylib
        |   `-- styles
        |       `-- libqmacstyle.dylib
        `-- Resources
            |-- AppCtrl.json
            |-- AppIcon.icns
            |-- font
            |   |-- Alibaba-PuHuiTi-Medium.otf
            |   `-- Alibaba-PuHuiTi-Regular.otf
            |-- auth
            |   |-- auth.html
            |   `-- images
            |       |-- error.png
            |       `-- logo.png
            |-- ui
            |   |-- images
            |   |   |-- search.png
            |   |   `-- warning.png
            |   |-- ui.css
            |   |-- ui.html
            |   |-- ui.js
            |   `-- ui_header.html
            `-- resourceList
                |-- images
                |   |-- logo.png
                |   `-- user.png
                `-- resource_list.html

制作dmg脚本:

#!/usr/bin/env python3
import os
import subprocess
import sys
import argparse

def extract_dmg(dmg_path, output_dir):
    """Extracts the contents of a DMG file to a specified output directory."""
    # Create the output directory if it doesn't exist
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    # Mount the DMG file
    mount_point = "/Volumes/dmg_tool_mount"
    subprocess.run(["hdiutil", "attach", dmg_path, "-mountpoint", mount_point], check=True)

    # Copy contents to the output directory
    try:
        subprocess.run(["cp", "-R", f"{mount_point}/.", output_dir], check=True)
    finally:
        # Unmount the DMG file
        subprocess.run(["hdiutil", "detach", mount_point], check=True)

    print(f"Extracted {dmg_path} to {output_dir}")

def create_dmg(source_dir, dmg_output):
    """Creates a DMG file from the specified source directory."""
    # Create the DMG file
    subprocess.run(["hdiutil", "create", dmg_output, "-srcfolder", source_dir, "-ov"], check=True)
    print(f"Created DMG {dmg_output} from {source_dir}")

def main():
    parser = argparse.ArgumentParser(description="Extract or create DMG files.")
    subparsers = parser.add_subparsers(dest='command')

    # Subparser for extracting
    extract_parser = subparsers.add_parser('extract', help='Extract a DMG file')
    extract_parser.add_argument('dmg', help='Path to the DMG file')
    extract_parser.add_argument('output', help='Directory to extract to')

    # Subparser for creating
    create_parser = subparsers.add_parser('create', help='Create a DMG file from a directory')
    create_parser.add_argument('source', help='Directory to create DMG from')
    create_parser.add_argument('dmg', help='Output path for the DMG file')

    args = parser.parse_args()

    if args.command == 'extract':
        extract_dmg(args.dmg, args.output)
    elif args.command == 'create':
        create_dmg(args.source, args.dmg)
    else:
        parser.print_help()

if __name__ == '__main__':
    main()

测试:

抽取Helloworld.dmg中的文件到test目录:
 % ./dmg_tool.py extrace Helloworld.dmg test
 % ls test
Applications    Helloworld.app
使用test目录下的文件,重新制作dmg文件为Helloworld2.dmg:
% ./dmg_tool.py create test Helloworld2.dmg
% ls Helloworld2.dmg

http://www.kler.cn/a/451256.html

相关文章:

  • 全国青少年信息学奥林匹克竞赛(信奥赛)备考实战之分支结构(实战训练三)
  • 各种网站(学习资源及其他)
  • 发际线不断后移,生发液排行榜第一名,让绒毛碎发爆出来
  • Y3地图制作1:水果缤纷乐、密室逃脱
  • 【C++基础】09、结构体
  • EsChatPro 接入国内 DeepSeek 大模型
  • 深入理解数据结构:数组、链表与列表
  • 【魅力golang】之-通道
  • Unity中如何实现绘制Sin函数图像
  • whisper.cpp: Android端测试 -- Android端手机部署音频大模型
  • 独一无二,万字详谈——Linux之文件管理
  • 虚幻引擎结构之UWorld
  • 16.1、网络安全风险评估过程
  • 基于Spring Boot的九州美食城商户一体化系统
  • HTML+CSS+JS制作外贸网站(内附源码,含5个页面)
  • 3.学习webpack配置 尝试打包ts文件
  • 【Git】-- 版本说明
  • 每天40分玩转Django:Django国际化
  • 【Kibana01】企业级日志分析系统ELK之Kibana的安装与介绍
  • 学习一下USB DFU
  • AI驱动的数据分析:利用自然语言实现数据查询到可视化呈现
  • 2.在 Vue 3 中使用 ECharts 实现动态时间轴效果
  • Android Studio打开一个外部的Android app程序
  • embeding 层到底是什么
  • YOLOv8 引入高效的可变形卷积网络 DCNv4 | 重新思考用于视觉应用的动态和稀疏算子
  • 【hackmyvm】BlackWidow靶机wp