您的位置:首页 > 编程语言 > Python开发

python之生成Qt pro文件

2015-11-19 00:26 591 查看
##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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: