复制文件
#encoding:utf-8
import shutil
shutil.copyfile('oldpath','newpath')
替换文字
#替换 manifest 包名
def replaceFileStr(parent,filename):
replaceFile = open(os.path.join(parent, filename), "r+") # 不用w w会清空数据
s = replaceFile.read() # 读出
replaceFile.close()
replaceFile = open(os.path.join(parent, filename), "w")
replaceFile.write(s.replace(oldId, newId))
replaceFile.close()
在指定行添加
#添加 ImagApplication.java R文件包名
ImagApplication = file(PROJECT_PATH+'/imag/src/main/java/com/imagjs/imag/ImagApplication.java')
s = ImagApplication.read()
ImagApplication.close()
a = s.split('\n')
a.insert(2, 'import '+ packageName +'.BuildConfig;') # 在第二行插入
s = '\n'.join(a)
ImagApplication = file(PROJECT_PATH+'/imag/src/main/java/com/imagjs/imag/ImagApplication.java', 'w')
ImagApplication.write(s)
ImagApplication.close()
读取property文件
def readProperty(path):
file = open(path, 'r+')
map = {}
while True:
line = file.readline()
if len(line) == 0:
break
processLine(line, map)
file.close()
return map
def processLine(line, map):
if(line.startswith('#') == False):
line = line.strip().replace('\:', ':')
nPos = line.index('=')
if (nPos != -1):
key= line[0:nPos]
value = line[nPos+1:len(line)]
map[key] = value
def getProperty(map, name):
if (map.has_key(name)):
return map[name]
else:
return ''
#读取properties
print '=========================READ PROPERTIES======================='
map = readProperty(SCRIPT_PATH + '/build.properties')
for key, value in map.items():
print key + '=' + value
devVersion = getProperty(map, 'devVersion')
对 zip 中的文件进行操作.
#coding=utf-8
import os
os.system('unzip ImagiAppOfficeJSPlugin-release.aar -d ImagiAppOfficeJSPlugin')
os.system('unzip ImagiAppOfficeJSPlugin/libs/iAppOffice_*.jar -d ImagiAppOfficeJSPlugin/libs/')
os.system('rm ImagiAppOfficeJSPlugin/libs/*.jar')
os.system('rm -rf ImagiAppOfficeJSPlugin/libs/org')
os.chdir("./ImagiAppOfficeJSPlugin/libs") # 改变目录,注意双引号和反斜杠
os.system('zip -r jinge.jar *')
os.system('rm -rf com cn')
os.chdir("..") # 改变目录,注意双引号和反斜杠
os.system('zip -r ../ImagiAppOfficeJSPlugin.aar *')
os.chdir("..") # 改变目录,注意双引号和反斜杠
os.system('rm -rf ImagiAppOfficeJSPlugin')
print os.getcwd() #打印当前目录
对文件名称,文件夹名称,文件内容进行批量重命名和替换
# encoding: utf-8
import os
import os.path
rootDir = os.getcwd()
oldId = 'JSTemplate'
pluginName = "JSGO"
oldPackageName = "jstemplate"
pluginPackageName = "jsgo"
oldFieldName = "jsTemplate"
pluginFieldName = "jsGO"
def replaceFileStr(parent,filename):
replaceFile = open(os.path.join(parent, filename), "r+") # 不用w w会清空数据
s = replaceFile.read() # 读出
replaceFile.close()
replaceFile = open(os.path.join(parent, filename), "w")
replaceFile.write(s.replace(oldId, pluginName).replace(oldPackageName, pluginPackageName).replace(oldFieldName, pluginFieldName))
replaceFile.close()
# 替换文件内容和重名名
for parent, dirnames, filenames in os.walk(rootDir):
for filename in filenames:
replaceFileStr(parent,filename)
if filename.find(oldId) != -1:
newname = filename.replace(oldId, pluginName)
print(filename, "--->", newname)
os.rename(os.path.join(parent, filename), os.path.join(parent, newname))
# 重命名文件夹
def renameDir(dir):
for parent, dirnames, filenames in os.walk(rootDir):
for dirname in dirnames:
if dirname.find(oldId) != -1:
newname = dirname.replace(oldId, pluginName)
print(dirname, "--->", newname)
os.rename(os.path.join(parent, dirname), os.path.join(parent, newname))
renameDir(dir)
if dirname.find(oldPackageName) != -1:
newname = dirname.replace(oldPackageName, pluginPackageName)
print(dirname, "--->", newname)
os.rename(os.path.join(parent, dirname), os.path.join(parent, newname))
renameDir(dir)
renameDir(rootDir)
对 zip 中的 xml 进行操作
# 设置插件变量
pluginVariables = getProperty(map, 'pluginVariables')
pluginVariables = pluginVariables.replace('\:', ':').replace('\=', '=').replace('/', '\/')
print pluginVariables
pluginVariablesObj = json.loads(pluginVariables)
for pluginName in pluginVariablesObj.keys():
manifestName = 'AndroidManifest.xml'
aarPath = PROJECT_PATH + '/imag/plugins/' + pluginName + '.aar'
aarFile = zipfile.ZipFile(aarPath, 'r')
manifestContent = aarFile.read(manifestName)
manifestContent = setPluginVariables(manifestContent, pluginVariablesObj[pluginName])
if (pluginName == 'ImagShareSDKJSPlugin'):
print '========================SET ImagShareSDKJSPlugin VARIBALRS======================='
shareSDKMap = pluginVariablesObj['ImagShareSDKJSPlugin']
shareSDKName = 'assets/ShareSDK.xml'
shareSDKXml = aarFile.read(shareSDKName)
# 1. xml文件操作
tree = ET.fromstring(shareSDKXml)
zipOut = zipfile.ZipFile(aarPath, 'a')
for name in shareSDKMap.keys():
print name + ' : ' + shareSDKMap[name]
if (name == 'QQ_AppId'):
nodes = tree.findall("QQ")
nodes[0].set("AppId", shareSDKMap[name])
if (name == 'QQ_AppKey'):
nodes = tree.findall("QQ")
nodes[0].set("AppKey", shareSDKMap[name])
if (name == 'Wechat_AppId'):
nodes = tree.findall("Wechat")
nodes[0].set("AppId", shareSDKMap[name])
xmlstr = ET.tostring(tree, encoding='utf8')
zipOut.writestr(shareSDKName, xmlstr.encode('utf-8'))
zipOut.close()
aarFile.close()
removeFromZip(aarPath, manifestName)
zipFile = zipfile.ZipFile(aarPath, 'a')
zipFile.writestr(manifestName, manifestContent.encode('utf-8'))
zipFile.close()