ReplaceJar.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 sys
  15. script_path = os.path.split(os.path.realpath(sys.argv[0]))[0]
  16. currentSdkpath = script_path + '/currentSdk'
  17. sdkpath = script_path + '/sdk'
  18. ignore_path_text = [".a", ".storyboard", ".py",".framework",".DS_Store",".xcuserstate",".jpg",".png"]
  19. keyFile = 'zooKeys.json'
  20. def replaceJarNameInFile(full_path, new_text, old_text):
  21. with open(full_path, "r") as fileObj:
  22. all_text = fileObj.read()
  23. fileObj.close()
  24. if old_text:
  25. all_text = all_text.replace(old_text, new_text)
  26. print ("\t替换: %s -> %s" % (old_text, new_text))
  27. with open(full_path, "w") as fileObj:
  28. fileObj.write(all_text)
  29. fileObj.close()
  30. else:
  31. print ('cant not find old text')
  32. def getoldJarDate(full_path):
  33. with open(full_path, "r") as fileObj:
  34. all_text = fileObj.read()
  35. fileObj.close()
  36. # 提取jar包日期
  37. old_text = (re.findall(r'jm_sdk_([^.jar]+)', all_text)[0])
  38. return old_text
  39. def main():
  40. global sdkpath
  41. global currentSdkpath
  42. print ('start replace jar ...')
  43. print (sdkpath)
  44. jarName = ''
  45. for parent, folders, files in os.walk(currentSdkpath):
  46. for file in files:
  47. if file.find(".jar") > -1:
  48. jarName = file
  49. print('jarName -->'+ jarName)
  50. fullJarPath = os.path.join(currentSdkpath,jarName)
  51. newDate = (re.findall(r'jm_sdk_([^.jar]+)', jarName)[0])
  52. #修改config字段 及替换Jar包
  53. list = os.listdir(sdkpath)
  54. for l in list:
  55. full_l_path = os.path.join(sdkpath,l)
  56. full_lib_path = os.path.join(full_l_path,'libs')
  57. if os.path.isdir(full_l_path):
  58. configPath = os.path.join(full_l_path,'libs/config.json')
  59. configPath = os.path.normcase(configPath)
  60. if os.path.exists(configPath):
  61. print(configPath)
  62. oldDate = getoldJarDate(configPath)
  63. # 修改config字段
  64. replaceJarNameInFile(configPath,newDate,oldDate)
  65. oldJarPath = '%s/jm_sdk_%s.jar' % (full_lib_path,oldDate)
  66. #删除旧文件
  67. if os.path.exists(oldJarPath):
  68. print ('delete ---> %s' % oldJarPath)
  69. try:
  70. os.remove(os.path.normcase(oldJarPath))
  71. except Exception as e:
  72. print (e)
  73. else:
  74. print("File is deleted successfully")
  75. newJarPath = '%s/jm_sdk_%s.jar' % (full_lib_path,newDate)
  76. print('复制: %s ---> %s' % (fullJarPath,newJarPath))
  77. shutil.copyfile(fullJarPath, newJarPath) # 复制文件
  78. print('-------------------------')
  79. print ("\nfinished")
  80. if __name__ == "__main__":
  81. main()