python之生成Qt pro文件

来源:互联网 发布:中国鉴藏 数据 编辑:程序博客网 时间:2024/05/19 19:57

##genQtPro.py

import os,sys,reimport shutil,string#[OK]#global var for app#calc the baseLen, we should add the divator char '/' or '\'gBaseLen = len(os.getcwd()) + 1gSrcList = "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 = FalsegRangeList = ["debug"]####################################################################################################   Judge one file can be accessed through the black or white list.                             [OK]###################################################################################################def canAcessFile(file):global bUseWhite,gRangeListbInRange = (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,gUiListnewPath = ""if len(dirname) == gBaseLen:newPath = fileelse:newPath = dirname[gBaseLen:] + "/" + file#filterif 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

0 0
原创粉丝点击