gitee.com/quant1x/engine@v1.8.4/publish-windows.py (about)

     1  # -*- coding: UTF-8 -*-
     2  # windows下的编译脚本
     3  # author: wangfeng
     4  # since: 2023-09-12
     5  
     6  import os
     7  import subprocess
     8  from git import Repo
     9  import platform
    10  
    11  def get_latest_tag(diff: int=0) -> str:
    12      """
    13      获取仓库最新的一个tag, 支持传入调整的数值, 默认只修改修订版本号
    14      """
    15      repo = Repo(r'./')
    16      tags = []
    17      for __tag in repo.tags:
    18          tag = str(__tag)
    19          tag = tag[1:]
    20          tags.append(tag)
    21      tags.sort(key=lambda x:tuple(int(v) for v in x.split('.')))
    22      latest = tags[-1]
    23      if diff != 0:
    24          last_vs = tags[-1].split('.')
    25          last_vs[-1] = str(int(last_vs[-1])+diff)
    26          latest = '.'.join(last_vs)
    27      return latest
    28  
    29  
    30  # 只能获取执行结果
    31  def subprocess_check_output(stmt, shell:bool=False):
    32      result = subprocess.check_output(stmt, shell).decode('utf-8')
    33      # 执行失败不需要特殊处理,命令执行失败会直接报错
    34      return result  # 返回执行结果,但是结果返回的是一个str字符串(不论有多少行),并且返回的结果需要转换编码
    35  
    36  def fix_version(v: str) -> str:
    37      if v.startswith(('v','V')):
    38          v = v[1:]
    39      return v
    40  
    41  def get_mod_version(module: str) -> str:
    42      # 获取依赖库stock的版本号
    43      cmd_result = subprocess_check_output(f'go list -m {module}').strip('\n')
    44      vs = cmd_result.split(' ')
    45      tag_latest = fix_version(vs[1])
    46      version = tag_latest
    47      return version
    48  
    49  if __name__ == '__main__':
    50      print(os.path.abspath('.'))
    51      # 获取应用的版本号
    52      version = get_latest_tag()
    53      # 获取依赖库的版本号
    54      gotdx_version = get_mod_version('gitee.com/quant1x/gotdx')
    55      print('gotdx version: ', gotdx_version)
    56      repo = 'gitee.com/quant1x/engine'
    57      BIN='./bin'
    58      current_path = os.path.dirname(os.path.abspath(__file__))
    59      #print(current_path)
    60      BIN=current_path + '/' + BIN
    61      APP = 'engine'
    62      EXT = '.exe'
    63      GOOS = platform.system().lower()
    64      machine = platform.machine().lower()
    65      GOARCH = 'amd64' if machine=='amd64' else 'arm64'
    66      print(f"正在编译应用:{APP} => {BIN}/{APP}{EXT}...")
    67      cmd= fr'''go env -w GOOS={GOOS} GOARCH={GOARCH} && go build -ldflags "-s -w -X 'main.MinVersion={version}' -X 'main.tdxVersion={gotdx_version}'" -o {BIN}/{APP}{EXT} {repo}'''
    68      print(cmd)
    69      subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    70      #subprocess.call(cmd,  stdout=subprocess.PIPE, shell=True)
    71      print(f"正在编译应用:{APP} => {BIN}/{APP}{EXT}...OK")
    72