python之生成Qt pro文件

本文介绍了如何利用genQtPro.py脚本生成Qt项目的pro文件,以CmdWifi为例,详细说明了生成CmdWif.pro的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

##genQtPro.py

import os,sys,re
import shutil,string

#	[OK]
#	global var for app
#	calc the baseLen, we should add the divator char '/' or '\'
gBaseLen = len(os.getcwd()) + 1
gSrcList = "SOURCES += \\" + "\n"
gHeadList = "HEADERS += \\" + "\n"
gUiList = "FORMS += \\" + "\n"
gTarget = "Test"
argLen = len(sys.argv)
if argLen >= 2:
	gTarget = sys.argv[1]
if gTarget.find('.') != -1:
    gTarget = gTarget.split('.',1)[0]

###################################################################################################
#   define the blakc list,which we ignore (dirname)                                             [OK]
###################################################################################################
bUseWhite = False
gRangeList = ["debug"]

###################################################################################################
#   Judge one file can be accessed through the black or white list.                             [OK]
###################################################################################################
def canAcessFile(file):
	global bUseWhite,gRangeList
	bInRange = (file in gRangeList)
	return (bUseWhite and bInRange ) or (not bUseWhite and not bInRange)

###################################################################################################
#	walk the dirname,
#	if type is file,then call pFunc to process it.
#	if type is dir,then recurse to walk the dir.                            [OK]
###################################################################################################
def getList(dirname,pFunc):
    try:
        ls=os.listdir(dirname)
    except:
        print dirname,'is access deny'
    else:
        for file in ls:
            temp = os.path.join(dirname,file)
            if(os.path.isdir(temp)):
                if canAcessFile(file):
                    getList(temp,pFunc)
            else:
                pFunc(dirname,file)

###################################################################################################
#   define the blakc list,which we ignore (dirname)
###################################################################################################
def convertDivChar(path):
	return path.replace('\\','/')
	
###################################################################################################
#   this function is used to process the certain file && dirname.                           [OK]
###################################################################################################
def printFile(dirname,file):
	global gSrcList,gHeadList,gUiList
	newPath = ""
	if len(dirname) == gBaseLen:
		newPath = file
	else:
		newPath = dirname[gBaseLen:] + "/" + file
	#filter
	if newPath[0] == '/':
		newPath = newPath[1:]
	newPath = convertDivChar(newPath)
	if re.search("\.h$",newPath):
		gHeadList = gHeadList + "\t" + newPath + " \\" + "\n"
	if re.search("\.hpp$",newPath):
		gHeadList = gHeadList + "\t" + newPath + " \\" + "\n"
	if re.search("\.cpp$",newPath):
		gSrcList = gSrcList + "\t" + newPath + " \\" + "\n"
	if re.search("\.c$",newPath):
		gSrcList = gSrcList + "\t" + newPath + " \\" + "\n"
	if re.search("\.ui$",newPath):
		gUiList = gUiList + "\t" + newPath + " \\" + "\n"

###################################################################################################
#   write string to file.                          [OK]
###################################################################################################
def writeToFile(str,fname):
	fp = open(fname,"w")
	fp.write(str)
	fp.close()

###################################################################################################
#   read content from fname.
###################################################################################################
def readFile(fname):
	with open(fname, 'r') as f:
		return f.read()

###################################################################################################
#   generate the pro body.
###################################################################################################
def getBasicHead():
	ret = "#hand-generate-code"
	ret = "%s\n%s" % (ret,"QT	+= core gui")
	ret = "%s\nTARGET = %s" % (ret,gTarget)
	ret = "%s\n%s" % (ret,"TEMPLATE = app")
	ret = "%s\n%s" % (ret,"")
	ret = "%s\n%s" % (ret,"#LIBS	+= -L/usr/lib/mysql -lmysqlclient")
	ret = "%s\n%s" % (ret,"#INCLUDEPATH += /usr/include/mysql\n\n")
	return ret

###################################################################################################
#   generate the pro body.
###################################################################################################
gKeyword = "#auto-generate-code#"
getList(os.getcwd(),printFile)
gContent = ""
gFname = "%s.pro" % (gTarget)
if os.path.exists(gFname):
	print "Update"
	cnt = readFile(gFname)
	pos = cnt.find(gKeyword)
	gContent = cnt[0:pos]
else:
	print "Create"
	gContent = getBasicHead()

fOutCnt = "%s%s\n\n%s\n%s\n%s" % (gContent ,gKeyword ,gHeadList ,gSrcList ,gUiList)
writeToFile(fOutCnt ,gFname)


@@生成Qt的pro文件

topro CmdWifi

生成CmdWif.pro

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值