登陆

运用python批量编译Qt工程脚本

admin 2020-02-14 185人围观 ,发现0个评论
首要用于windows渠道(msvc编译器)。

布置环境

设置脚本内的build_list变量的值即可。

  • qmake.exe途径;
  • jom.exe途径;
  • 编译环境变量;
  • 编译项目。

源码

私信小编01 获取更多学习教程!

import os
import datetime
import subprocess
build_list = [
{
'qmake': r'C:\Qt\Qt5.12.2\5.12.2\msvc2017_64\bin\qmake.exe',
'qmake_params': r'"CONFIG+=qtquickcompiler"',
'jom': r'C:\Qt\Qt5.12.2\Tools\QtCreator\bin\jom.exe',
'env_bat': r'C:\"Program Files (x86)"\"Microsoft Visual Studio"\2017\Community\VC\Auxiliary\Build\vcvarsall.bat amd64',
'project': r'C:\Users\Document\Example.pro'
}
]
class Builder:
def __init__(self, project, qmake, qmake_params, jom, en运用python批量编译Qt工程脚本v_bat):
self.project = project
self.qmake = qmake
self.qmake_params = qmake_params
self.jom = jom
self.env_bat = env_bat
(self.qmake_path, self.qmake_name) = os.path.split(qmake)
(self.jom_path, self.jom_name) = os.path.split(jom)
(self.project_path, self.proj运用python批量编译Qt工程脚本ect_name) = os.path.split(project)
self.qt_version = self.qmake_path.split('\\')[-3]
self.compiler_version = self.qmake_path.split('\\')[-2]
def execute_qmake_cmd_line(self):
create_qmake_cmd_line = self.qmake + ' ' + self.project + ' -spec win32-msvc ' + self.qmake_params
return True if execute(create_qmake_cmd_line) else False
def execute运用python批量编译Qt工程脚本_make_cmd_line(self):
cmd_line = '{0} -j8'.format(self.jom)
return True if execute(cmd_line) else False
def execute_make_clean_cmd_line(self):
if os.path.exists('Makefile') | os.path.exists('makefile'):
retxcafeurn True if execute('{0} clean'.format(self.jom)) else False
return True
def init_env(self):
tmp = os.popen("{0} && {1}".format(self.env_bat, 'set'))
env_str = tmp.read()
tmp.close()
env_list = str(env_str).split('\n')
for each in env_list:
if '=' in each:
key = each.split('=')[0].lower()
value = each.split('=')[1]
os.environ[key] = value
return True
def build(self):
os.chdir(self.project_path)
print('Starting build [{0}]'.format(os.path.splitext(self.project_name)[0]))
isOk = True
status = self.init_env()
isOk = isOk and status
print('Init env: ', 'Success' if status else 'Failed')
status = self.execute_make_clean_cmd_line()
isOk = isOk and status
print('Make clean: ', 'Success' if status else 'Failed')
status = self.execute_qmake_cmd_line()
isOk = isOk and stat运用python批量编译Qt工程脚本us
print('QMake: ', 'Success' if status else 'Failed')
status = self.execute_make_cmd_line()
isOk = isOk and status
print('Make: ', 'Success' if status else 'Failed')
print('End build ', 'Success' if isOk else 'Failed')
return isOk
def execute(cmd):
result = subprocess.run(cmd, encoding='gbk',shell=True, capture_output=True)
if result.returncode != 0:
print(result.stdout, result.stderr)
return True if result.returncode == 0 else False
def update_project_code(project_path, branch):
print('Update ', project_path)
os.chdir(project_path)
return execute('git pull origin '运用python批量编译Qt工程脚本 + branch)
def update_code(build_list):
print('Updating...')
for each in build_list:
project = '' if each.get('project') == None else each.get('project')
path = os.path.split(project)[0]
if not update_project_code(path, 'master'):
print('Updated failed')
exit(-1)
print('Updated success')
def build(build_list):
for each in build_list:
qmake = each.get('qmake')
qmake_params = '' if each.get('qmake_params') == None else each.get('qmake_params')
jom = each.get('jom')
env_bat = each.get('env_bat')
project = each.get('project')
builder = Builder(project, qmake, qmake_params, jom, env_bat)
if (builder.build() == False):
print('[{0}] Deploy failed!!!'.format(datetime.datetime.now()))
exit(-1)
if __name__ == '__main__':
print('[{0}] Start deploy...'.format(datetime.datetime.now()))
# update_code(build_list)
build(build_list)
print('[{0}] Deploy success!!!'.format(datetime.datetime.now()))
请关注微信公众号
微信二维码
不容错过
Powered By Z-BlogPHP