ReplaceJar&Res.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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):
  23. with open(full_path, "r") as fileObj:
  24. all_text = fileObj.read()
  25. fileObj.close()
  26. if old_text:
  27. all_text = all_text.replace(old_text, new_text)
  28. print ("\t替换: %s -> %s" % (old_text, new_text))
  29. with open(full_path, "w") as fileObj:
  30. fileObj.write(all_text)
  31. fileObj.close()
  32. else:
  33. print ('cant not find old text')
  34. def getoldJarDate(full_path):
  35. with open(full_path, "r") as fileObj:
  36. all_text = fileObj.read()
  37. fileObj.close()
  38. # 提取jar包日期
  39. old_text = (re.findall(r'jm_sdk_([^.jar]+)', all_text)[0])
  40. return old_text
  41. ######修改config字段 及替换Jar包
  42. def replaceJar():
  43. global sdkpath
  44. global currentSdkpath
  45. print ('start replace jar ...')
  46. print (sdkpath)
  47. jarName = ''
  48. for parent, folders, files in os.walk(currentSdkpath):
  49. for file in files:
  50. if file.find(".jar") > -1:
  51. jarName = file
  52. print('jarName -->'+ jarName)
  53. fullJarPath = os.path.join(currentSdkpath,jarName)
  54. newDate = (re.findall(r'jm_sdk_([^.jar]+)', jarName)[0])
  55. #修改config字段 及替换Jar包
  56. list = os.listdir(sdkpath)
  57. for l in list:
  58. if l in ignore_sdk:
  59. continue
  60. full_l_path = os.path.join(sdkpath,l)
  61. full_lib_path = os.path.join(full_l_path,'libs')
  62. if os.path.isdir(full_l_path):
  63. configPath = os.path.join(full_l_path,'libs/config.json')
  64. configPath = os.path.normcase(configPath)
  65. if os.path.exists(configPath):
  66. print(configPath)
  67. oldDate = getoldJarDate(configPath)
  68. # 修改config字段
  69. replaceJarNameInFile(configPath,newDate,oldDate)
  70. oldJarPath = '%s/jm_sdk_%s.jar' % (full_lib_path,oldDate)
  71. #删除旧文件
  72. if os.path.exists(oldJarPath):
  73. print ('delete ---> %s' % oldJarPath)
  74. try:
  75. os.remove(os.path.normcase(oldJarPath))
  76. except Exception as e:
  77. print (e)
  78. else:
  79. print("File is deleted successfully")
  80. newJarPath = '%s/jm_sdk_%s.jar' % (full_lib_path,newDate)
  81. print('复制: %s ---> %s' % (fullJarPath,newJarPath))
  82. shutil.copyfile(fullJarPath, newJarPath) # 复制文件
  83. print('-------------------------')
  84. print ("\nfinished replace jar")
  85. ######替换Res
  86. def replaceRes():
  87. global sdkpath
  88. global currentSdkpath
  89. print ('start replace jar ...')
  90. resPath = os.path.join(currentSdkpath,"res")
  91. #print (resPath)
  92. list = os.listdir(sdkpath)
  93. for l in list:
  94. if l.find("jm") > -1:
  95. des_res_path = os.path.join(sdkpath,l,"res")
  96. print (des_res_path)
  97. file_utils.copyDir(resPath,des_res_path)
  98. #修改config字段 及替换Jar包
  99. # for l in list:
  100. # full_l_path = os.path.join(sdkpath,l)
  101. # full_lib_path = os.path.join(full_l_path,'libs')
  102. # if os.path.isdir(full_l_path):
  103. # configPath = os.path.join(full_l_path,'libs/config.json')
  104. # configPath = os.path.normcase(configPath)
  105. # if os.path.exists(configPath):
  106. # print(configPath)
  107. # oldDate = getoldJarDate(configPath)
  108. # # 修改config字段
  109. # replaceJarNameInFile(configPath,newDate,oldDate)
  110. # oldJarPath = '%s/jm_sdk_%s.jar' % (full_lib_path,oldDate)
  111. # #删除旧文件
  112. # if os.path.exists(oldJarPath):
  113. # print ('delete ---> %s' % oldJarPath)
  114. # try:
  115. # os.remove(os.path.normcase(oldJarPath))
  116. # except Exception as e:
  117. # print (e)
  118. # else:
  119. # print("File is deleted successfully")
  120. # newJarPath = '%s/jm_sdk_%s.jar' % (full_lib_path,newDate)
  121. # print('复制: %s ---> %s' % (fullJarPath,newJarPath))
  122. # shutil.copyfile(fullJarPath, newJarPath) # 复制文件
  123. #
  124. # print('-------------------------')
  125. print ("\nfinished replace res")
  126. def main():
  127. replaceJar()
  128. #####修改了资源文件,要替换#####
  129. #replaceRes()
  130. if __name__ == "__main__":
  131. main()