# -*- coding: UTF-8 -*- import os import sys import re from . import file_utils class MkUtils: #成员变量 srcContent = '' includeContent = '' exportIncludeContent = '' rootPath = '' mkPath = '' relativePath = '' def __init__(self): pass def init(self, rootPath, mkPath): self.rootPath = rootPath self.mkPath = mkPath def replaceContent(self, mkPath): with open(mkPath) as f: content = f.read() content = re.sub(r'#___AUTO_SRC_START___[\s\S]*#___AUTO_SRC_END___', '#___AUTO_SRC_START___\n' + 'LOCAL_SRC_FILES := ' + self.srcContent + '#___AUTO_SRC_END___\n', content) content = re.sub(r'#___AUTO_EXPORT_C_INCLUDES_START___[\s\S]*#___AUTO_EXPORT_C_INCLUDES_END___', '#___AUTO_EXPORT_C_INCLUDES_START___\n' + 'LOCAL_EXPORT_C_INCLUDES :=' + self.includeContent + '#___AUTO_EXPORT_C_INCLUDES_END___\n', content) content = re.sub(r'#___AUTO_C_INCLUDES_START___[\s\S]*#___AUTO_C_INCLUDES_END___', '#___AUTO_C_INCLUDES_START___\n' + 'LOCAL_C_INCLUDES :=' + self.includeContent + '#___AUTO_C_INCLUDES_END___\n', content) file_utils.writeToFile(content, mkPath) pass ''' 遍历目录中的c++文件加入Android.mk @param dirctory:文件路径 @param suffixList:拓展名数组 @param recursive:是否递归 ''' def addToMk(self, directory, suffixList, recursive): #遍历文件夹下的所有cpp文件 self.travelsalDir(directory, suffixList, recursive) pass def addToInclude(self, path): relativePath = self.getRelativePath(path) self.includeContent += ' $(LOCAL_PATH)/' + relativePath + ' \\' + '\n' pass def addToSrc(self, path): relativePath = self.getRelativePath(path) self.srcContent += ' ' + relativePath + ' \\' + '\n' pass def getRelativePath(self, path): mkLen = len(self.mkPath) rootLen = len(self.rootPath) splitPath = self.mkPath[rootLen + 1 : mkLen] mkPathArr = [] pathArr = [] if len(splitPath) > 0 : mkPathArr = splitPath.split('/') if len(path) > 0 : pathArr = path.split('/') relativePath = '' for dir in mkPathArr: if(dir in pathArr): pathArr.remove(dir) else: relativePath += '../' num = len(pathArr) for i in range(num): if(i == num - 1): relativePath += pathArr[i] else: relativePath += pathArr[i] + '/' return relativePath #遍历目录中的所有文件和目录 def travelsalDir(self, directory, suffixList, recursive): dirPath = self.rootPath + '/' + directory if(file_utils.isFile(dirPath)): print("不是文件夹:" + dirPath); return; self.addToInclude(directory) dirs = os.listdir(dirPath) dirs.sort()#排序 for file in dirs: if(file_utils.isFile(dirPath+ '/'+ file)): ext = os.path.splitext(dirPath+ '/'+ file)[1] if(ext in suffixList): if(directory == ''): self.addToSrc(file) print(file); else: self.addToSrc(directory+ '/'+ file) print(directory+ '/'+ file); else: if recursive == True: self.travelsalDir(directory +'/'+ file, suffixList, True) pass