Python 打包EXE代码封装

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time    : 2022/2/19 11:16
# @Author  : forward_huan
# @CSDN    : https://blog.csdn.net/forward_huan/category_11049604.html

import os
import subprocess
from datetime import datetime

_ver_info = """# UTF-8
#
VSVersionInfo(
  ffi=FixedFileInfo(
    # filevers and prodvers should be always a tuple with four items:
    # (1, 2, 3, 4)
    # Set not needed items to zero 0.
    filevers=(-FileVers-),
    prodvers=(-ProductVers-),
    # Contains a bitmask that specifies the valid bits 'flags'r
    mask=0x3f,
    # Contains a bitmask that specifies the Boolean attributes of the file.
    flags=0x0,
    # The operating system for which this file was designed.
    # 0x4 - NT and there is no need to change it.
    OS=0x4,
    # The general type of file.
    # 0x1 - the file is an application.
    fileType=0x1,
    # The function of the file.
    # 0x0 - the function is not defined for this fileType
    subtype=0x0,
    # Creation date and time stamp.
    date=(0, 0)
    ),
  kids=[
    StringFileInfo(
      [
      StringTable(
        u'000004b0',
        [StringStruct(u'Comments', u'-Comments-'),
        StringStruct(u'CompanyName', u'-CompanyName-'),
        StringStruct(u'FileDescription', u'-FileDescription-'),
        StringStruct(u'FileVersion', u'-FileVersion-'),
        StringStruct(u'InternalName', u'-InternalName-'),
        StringStruct(u'LegalCopyright', u'-LegalCopyright-'),
        StringStruct(u'LegalTrademarks', u'-LegalTrademarks-'),
        StringStruct(u'OriginalFilename', u'-OriginalFilename-'),
        StringStruct(u'ProductName', u'-ProductName-'),
        StringStruct(u'ProductVersion', u'-ProductVersion-'),
        StringStruct(u'Assembly Version', u'-AssemblyVersion-')])
      ]), 
    VarFileInfo([VarStruct(u'Translation', [0, 1200])])
  ]
)
"""


def create_version_info(
        product_version: str, file_version: str, product_name="",
        assembly_version="", comments="", company_name="",
        file_description="", internal_name="", legal_copyright="",
        legal_trademarks="", original_filename=""):
    """创建版本文件内容"""
    if not assembly_version:
        assembly_version = file_version
    return _ver_info \
        .replace("-ProductVersion-", product_version) \
        .replace("-ProductVers-", product_version.replace(".", ",")) \
        .replace("-FileVersion-", file_version) \
        .replace("-FileVers-", file_version.replace(".", ",")) \
        .replace("-ProductName-", product_name) \
        .replace("-AssemblyVersion-", assembly_version) \
        .replace("-Comments-", comments) \
        .replace("-CompanyName-", company_name) \
        .replace("-FileDescription-", file_description) \
        .replace("-InternalName-", internal_name) \
        .replace("-LegalCopyright-", legal_copyright) \
        .replace("-LegalTrademarks-", legal_trademarks) \
        .replace("-OriginalFilename-", original_filename)


def get_git_commit_id():
    """获取git管理当前最新的commit id"""
    pro = subprocess.Popen(
        "git pull", shell=False, stdout=subprocess.PIPE, encoding="utf-8")
    pro.wait(2000)
    var = os.popen("git rev-parse -short HEAD").read()
    return var.strip()


def delete_file_version(version_path):
    """删除生成的版本文件"""
    if os.path.exists(version_path):
        os.remove(version_path)


def create_version_file(version_path):
    """生成版本文件"""
    version_info = create_version_info(
        product_version="1.0.0.0",
        file_version=datetime.now().strftime(f"%Y.%#m.%#d.%#H"),
        product_name="项目名称",
        file_description=f"Git Commit Id: {get_git_commit_id()}"
    )
    dir_path = os.path.dirname(version_path)
    if not os.path.exists(dir_path):
        os.makedirs(dir_path)
    with open(version_path, "w", encoding="utf8") as f:
        f.write(version_info)


if __name__ == '__main__':
    root_path = "此处为项目的根目录"
    icon_path = "icon路径"
    name = "项目名称" + datetime.now().strftime("%Y_%#m_%#d")
    out_path = os.path.join(root_path, "out")
    dist_path = out_path
    build_path = os.path.join(out_path, "build")
    py_path = os.path.join(root_path, "main.py")
    file_version_path = os.path.join(out_path, "file_info.txt")
    res_path = os.path.join(root_path, "src", "res")
    dest_res_path = os.path.join("src", "res")
    command = f"pyinstaller.exe -w -F {py_path} " \
              f"-n {name} " \
              f"--distpath {dist_path} " \
              f"--workpath {build_path} " \
              f"--specpath {build_path} " \
              f"--version-file {file_version_path} " \
              f"--add-data {res_path};{dest_res_path} " \
              f"-i {icon_path}"
    print(command)
    create_version_file(file_version_path)
    log_path = os.path.join(out_path, "log")
    if not os.path.exists(log_path):
        os.makedirs(log_path)
    log_file = open(os.path.join(
        log_path, f"log_{datetime.now().strftime('%Y%#m%#d%H%#M%#S')}"), "w+")
    proc = subprocess.Popen(
        command, shell=False, stdout=log_file, stderr=log_file,
        encoding="utf-8")
    proc.wait(2000)
    log_file.close()
    delete_file_version(file_version_path)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值