ReplaceJar&Res.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #! /usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3. # coding: utf-8
  4. import os,sys
  5. import random
  6. import string
  7. import re
  8. import time
  9. import json
  10. import shutil
  11. import hashlib
  12. import time
  13. import argparse
  14. import file_utils
  15. import sys
  16. script_path = os.path.split(os.path.realpath(sys.argv[0]))[0]
  17. currentSdkpath = script_path + '/currentSdk'
  18. sdkpath = script_path + '/sdk'
  19. ignore_path_text = [".a", ".storyboard", ".py",".framework",".DS_Store",".xcuserstate",".jpg",".png"]
  20. keyFile = 'zooKeys.json'
  21. def replaceJarNameInFile(full_path, new_text, old_text):
  22. with open(full_path, "r") as fileObj:
  23. all_text = fileObj.read()
  24. fileObj.close()
  25. if old_text:
  26. all_text = all_text.replace(old_text, new_text)
  27. print ("\t替换: %s -> %s" % (old_text, new_text))
  28. with open(full_path, "w") as fileObj:
  29. fileObj.write(all_text)
  30. fileObj.close()
  31. else:
  32. print ('cant not find old text')
  33. def getoldJarDate(full_path):
  34. with open(full_path, "r") as fileObj:
  35. all_text = fileObj.read()
  36. fileObj.close()
  37. # 提取jar包日期
  38. old_text = (re.findall(r'jm_sdk_([^.jar]+)', all_text)[0])
  39. return old_text
  40. ######修改config字段 及替换Jar包
  41. def replaceJar():
  42. global sdkpath
  43. global currentSdkpath
  44. print ('start replace jar ...')
  45. print (sdkpath)
  46. jarName = ''
  47. for parent, folders, files in os.walk(currentSdkpath):
  48. for file in files:
  49. if file.find(".jar") > -1:
  50. jarName = file
  51. print('jarName -->'+ jarName)
  52. fullJarPath = os.path.join(currentSdkpath,jarName)
  53. newDate = (re.findall(r'jm_sdk_([^.jar]+)', jarName)[0])
  54. #修改config字段 及替换Jar包
  55. list = os.listdir(sdkpath)
  56. for l in list:
  57. full_l_path = os.path.join(sdkpath,l)
  58. full_lib_path = os.path.join(full_l_path,'libs')
  59. if os.path.isdir(full_l_path):
  60. configPath = os.path.join(full_l_path,'libs/config.json')
  61. configPath = os.path.normcase(configPath)
  62. if os.path.exists(configPath):
  63. print(configPath)
  64. oldDate = getoldJarDate(configPath)
  65. # 修改config字段
  66. replaceJarNameInFile(configPath,newDate,oldDate)
  67. oldJarPath = '%s/jm_sdk_%s.jar' % (full_lib_path,oldDate)
  68. #删除旧文件
  69. if os.path.exists(oldJarPath):
  70. print ('delete ---> %s' % oldJarPath)
  71. try:
  72. os.remove(os.path.normcase(oldJarPath))
  73. except Exception as e:
  74. print (e)
  75. else:
  76. print("File is deleted successfully")
  77. newJarPath = '%s/jm_sdk_%s.jar' % (full_lib_path,newDate)
  78. print('复制: %s ---> %s' % (fullJarPath,newJarPath))
  79. shutil.copyfile(fullJarPath, newJarPath) # 复制文件
  80. print('-------------------------')
  81. print ("\nfinished replace jar")
  82. ######替换Res
  83. def replaceRes():
  84. global sdkpath
  85. global currentSdkpath
  86. print ('start replace jar ...')
  87. resPath = os.path.join(currentSdkpath,"res")
  88. #print (resPath)
  89. list = os.listdir(sdkpath)
  90. for l in list:
  91. if l.find("jm") > -1:
  92. des_res_path = os.path.join(sdkpath,l,"res")
  93. print (des_res_path)
  94. file_utils.copyDir(resPath,des_res_path)
  95. #修改config字段 及替换Jar包
  96. # for l in list:
  97. # full_l_path = os.path.join(sdkpath,l)
  98. # full_lib_path = os.path.join(full_l_path,'libs')
  99. # if os.path.isdir(full_l_path):
  100. # configPath = os.path.join(full_l_path,'libs/config.json')
  101. # configPath = os.path.normcase(configPath)
  102. # if os.path.exists(configPath):
  103. # print(configPath)
  104. # oldDate = getoldJarDate(configPath)
  105. # # 修改config字段
  106. # replaceJarNameInFile(configPath,newDate,oldDate)
  107. # oldJarPath = '%s/jm_sdk_%s.jar' % (full_lib_path,oldDate)
  108. # #删除旧文件
  109. # if os.path.exists(oldJarPath):
  110. # print ('delete ---> %s' % oldJarPath)
  111. # try:
  112. # os.remove(os.path.normcase(oldJarPath))
  113. # except Exception as e:
  114. # print (e)
  115. # else:
  116. # print("File is deleted successfully")
  117. # newJarPath = '%s/jm_sdk_%s.jar' % (full_lib_path,newDate)
  118. # print('复制: %s ---> %s' % (fullJarPath,newJarPath))
  119. # shutil.copyfile(fullJarPath, newJarPath) # 复制文件
  120. #
  121. # print('-------------------------')
  122. print ("\nfinished replace res")
  123. def main():
  124. replaceJar()
  125. #####修改了资源文件,要替换#####
  126. #replaceRes()
  127. if __name__ == "__main__":
  128. main()