ReplaceJar&Res.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. ignore_sdk = ['yfsdk']
  21. keyFile = 'zooKeys.json'
  22. def replaceJarNameInFile(full_path, new_text, old_text, tag):
  23. with open(full_path, "r") as fileObj:
  24. all_text = fileObj.read()
  25. fileObj.close()
  26. if old_text:
  27. oldjar = "%s_%s.jar" % (tag, old_text)
  28. newjar = "%s_%s.jar" % (tag, new_text)
  29. all_text = all_text.replace(oldjar, newjar)
  30. print ("\t替换: %s -> %s" % (oldjar, newjar))
  31. with open(full_path, "w") as fileObj:
  32. fileObj.write(all_text)
  33. fileObj.close()
  34. else:
  35. print ('cant not find old text')
  36. def getoldJarDate(full_path):
  37. with open(full_path, "r") as fileObj:
  38. all_text = fileObj.read()
  39. fileObj.close()
  40. # 提取jar包日期
  41. old_text = (re.findall(r'jm_sdk_([^.jar]+)', all_text)[0])
  42. return old_text
  43. def getoldNopayJarDate(full_path):
  44. with open(full_path, "r") as fileObj:
  45. all_text = fileObj.read()
  46. fileObj.close()
  47. # 提取jar包日期
  48. if all_text.find("jm_sdk_nopay") > -1:
  49. old_text = (re.findall(r'jm_sdk_nopay_([^.jar]+)', all_text)[0])
  50. return old_text
  51. return ""
  52. def replaceNopayJar():
  53. global sdkpath
  54. global currentSdkpath
  55. print ('start replace jar ...')
  56. print (sdkpath)
  57. jarName = ''
  58. for parent, folders, files in os.walk(currentSdkpath):
  59. for file in files:
  60. if file.find(".jar") > -1 and file.find("nopay") > -1:
  61. jarName = file
  62. print('jarName -->'+ jarName)
  63. fullJarPath = os.path.join(currentSdkpath,jarName)
  64. newDate = (re.findall(r'jm_sdk_nopay_([^.jar]+)', jarName)[0])
  65. #修改config字段 及替换Jar包
  66. list = os.listdir(sdkpath)
  67. for l in list:
  68. if l in ignore_sdk:
  69. continue
  70. full_l_path = os.path.join(sdkpath,l)
  71. full_lib_path = os.path.join(full_l_path,'libs')
  72. if os.path.isdir(full_l_path):
  73. configPath = os.path.join(full_l_path,'libs/config.json')
  74. configPath = os.path.normcase(configPath)
  75. if os.path.exists(configPath):
  76. print(configPath)
  77. oldDate = getoldNopayJarDate(configPath)
  78. if oldDate == "":
  79. continue
  80. # 修改config字段
  81. replaceJarNameInFile(configPath,newDate,oldDate,"jm_sdk_nopay")
  82. oldJarPath = '%s/jm_sdk_nopay_%s.jar' % (full_lib_path,oldDate)
  83. #删除旧文件
  84. if os.path.exists(oldJarPath):
  85. print ('delete ---> %s' % oldJarPath)
  86. try:
  87. os.remove(os.path.normcase(oldJarPath))
  88. except Exception as e:
  89. print (e)
  90. else:
  91. print("File is deleted successfully")
  92. newJarPath = '%s/jm_sdk_nopay_%s.jar' % (full_lib_path,newDate)
  93. print('复制: %s ---> %s' % (fullJarPath,newJarPath))
  94. shutil.copyfile(fullJarPath, newJarPath) # 复制文件
  95. print('-------------------------')
  96. print ("\nfinished replace jar")
  97. ######修改config字段 及替换Jar包
  98. def replaceJar():
  99. global sdkpath
  100. global currentSdkpath
  101. print ('start replace jar ...')
  102. print (sdkpath)
  103. jarName = ''
  104. for parent, folders, files in os.walk(currentSdkpath):
  105. for file in files:
  106. if file.find(".jar") > -1 and file.find("nopay") < 1:
  107. jarName = file
  108. print('jarName -->'+ jarName)
  109. fullJarPath = os.path.join(currentSdkpath,jarName)
  110. newDate = (re.findall(r'jm_sdk_([^.jar]+)', jarName)[0])
  111. #修改config字段 及替换Jar包
  112. list = os.listdir(sdkpath)
  113. for l in list:
  114. if l in ignore_sdk:
  115. continue
  116. full_l_path = os.path.join(sdkpath,l)
  117. full_lib_path = os.path.join(full_l_path,'libs')
  118. if os.path.isdir(full_l_path):
  119. configPath = os.path.join(full_l_path,'libs/config.json')
  120. configPath = os.path.normcase(configPath)
  121. if os.path.exists(configPath):
  122. print(configPath)
  123. oldDate = getoldJarDate(configPath)
  124. # 修改config字段
  125. replaceJarNameInFile(configPath,newDate,oldDate,"jm_sdk")
  126. oldJarPath = '%s/jm_sdk_%s.jar' % (full_lib_path,oldDate)
  127. #删除旧文件
  128. if os.path.exists(oldJarPath):
  129. print ('delete ---> %s' % oldJarPath)
  130. try:
  131. os.remove(os.path.normcase(oldJarPath))
  132. except Exception as e:
  133. print (e)
  134. else:
  135. print("File is deleted successfully")
  136. newJarPath = '%s/jm_sdk_%s.jar' % (full_lib_path,newDate)
  137. print('复制: %s ---> %s' % (fullJarPath,newJarPath))
  138. shutil.copyfile(fullJarPath, newJarPath) # 复制文件
  139. print('-------------------------')
  140. print ("\nfinished replace jar")
  141. ######替换Res
  142. def replaceRes():
  143. global sdkpath
  144. global currentSdkpath
  145. print ('start replace jar ...')
  146. resPath = os.path.join(currentSdkpath,"res")
  147. #print (resPath)
  148. list = os.listdir(sdkpath)
  149. for l in list:
  150. if l.find("jm") > -1:
  151. des_res_path = os.path.join(sdkpath,l,"res")
  152. print (des_res_path)
  153. file_utils.copy_dir(resPath, des_res_path)
  154. #修改config字段 及替换Jar包
  155. # for l in list:
  156. # full_l_path = os.path.join(sdkpath,l)
  157. # full_lib_path = os.path.join(full_l_path,'libs')
  158. # if os.path.isdir(full_l_path):
  159. # configPath = os.path.join(full_l_path,'libs/config.json')
  160. # configPath = os.path.normcase(configPath)
  161. # if os.path.exists(configPath):
  162. # print(configPath)
  163. # oldDate = getoldJarDate(configPath)
  164. # # 修改config字段
  165. # replaceJarNameInFile(configPath,newDate,oldDate)
  166. # oldJarPath = '%s/jm_sdk_%s.jar' % (full_lib_path,oldDate)
  167. # #删除旧文件
  168. # if os.path.exists(oldJarPath):
  169. # print ('delete ---> %s' % oldJarPath)
  170. # try:
  171. # os.remove(os.path.normcase(oldJarPath))
  172. # except Exception as e:
  173. # print (e)
  174. # else:
  175. # print("File is deleted successfully")
  176. # newJarPath = '%s/jm_sdk_%s.jar' % (full_lib_path,newDate)
  177. # print('复制: %s ---> %s' % (fullJarPath,newJarPath))
  178. # shutil.copyfile(fullJarPath, newJarPath) # 复制文件
  179. #
  180. # print('-------------------------')
  181. print ("\nfinished replace res")
  182. def main():
  183. #replaceJar()
  184. replaceNopayJar()
  185. #####修改了资源文件,要替换#####
  186. #replaceRes()
  187. if __name__ == "__main__":
  188. main()