栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python 打包EXE代码封装

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/755211.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号