fix_archext.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env python3
  2. import os
  3. import argparse
  4. import json
  5. def get_all_jsonfiles(rootdir, debug=False):
  6. jsonfiles = []
  7. try:
  8. for root, dirs, files in os.walk(rootdir):
  9. for file in files:
  10. if file.endswith(".json") or file.endswith(".json.libncrt"):
  11. filepath = "%s/%s" %(root, file)
  12. if debug:
  13. print("Found %s" % (filepath) )
  14. jsonfiles.append(filepath)
  15. except:
  16. pass
  17. return jsonfiles
  18. def fix_archext_in_json(jsonfile):
  19. if os.path.isfile(jsonfile) == False:
  20. return False
  21. lines = []
  22. with open(jsonfile, "r") as f:
  23. lines = f.readlines()
  24. fixcnt = 0
  25. with open(jsonfile, "w") as f:
  26. for line in lines:
  27. vext_name = "_zve32f"
  28. pext_name = "_xxldspn1x"
  29. if "\"nx" in line or "\"ux" in line:
  30. pext_name = "_xxldsp"
  31. if "fd" in line:
  32. vext_name = "v"
  33. else:
  34. vext_name = "_zve64f"
  35. oldext=''
  36. if '"bpv"' in line:
  37. oldext = '"bpv"'
  38. newext = "\"%s_zba_zbb_zbc_zbs%s\"" % (vext_name, pext_name)
  39. elif '"bv"' in line:
  40. oldext = '"bv"'
  41. newext = "\"%s_zba_zbb_zbc_zbs\"" % (vext_name)
  42. elif '"bp"' in line:
  43. oldext = '"bp"'
  44. newext = "\"_zba_zbb_zbc_zbs%s\"" % (pext_name)
  45. elif '"pv"' in line:
  46. oldext = '"pv"'
  47. newext = "\"%s%s\"" % (vext_name, pext_name)
  48. elif '"v"' in line:
  49. oldext = '"v"'
  50. newext = "\"%s\"" % (vext_name)
  51. elif '"p"' in line:
  52. oldext = '"p"'
  53. newext = "\"%s\"" % (pext_name)
  54. elif '"b"' in line:
  55. oldext = '"b"'
  56. newext = "\"_zba_zbb_zbc_zbs\""
  57. elif 'xxldspn1x' in line and pext_name == "_xxldsp":
  58. oldext = 'xxldspn1x'
  59. newext = 'xxldsp'
  60. elif '"v' in line and vext_name == "_zve64f":
  61. oldext = '"v'
  62. newext = "\"%s" % (vext_name)
  63. else:
  64. oldext = ""
  65. newext = ""
  66. if oldext != "":
  67. line = line.replace(oldext, newext)
  68. fixcnt = fixcnt + 1
  69. f.write(line)
  70. if fixcnt > 0:
  71. print("Fix json file %s, replace count %s" % (jsonfile, fixcnt))
  72. return True
  73. def fix_jsonfiles(rootdir):
  74. if os.path.isdir(rootdir) == False:
  75. return False
  76. jsonfiles = get_all_jsonfiles(rootdir)
  77. for jsfile in jsonfiles:
  78. #print("Fix file %s" % (jsfile))
  79. fix_archext_in_json(jsfile)
  80. for jsfile in jsonfiles:
  81. try:
  82. json.load(open(jsfile, 'r'))
  83. except:
  84. print("ERROR:Json file %s is invalid" % (jsfile))
  85. return True
  86. if __name__ == '__main__':
  87. parser = argparse.ArgumentParser(description="Nuclei SDK CLI Configuration Fixup Tools For GCC 13 upgrade")
  88. parser.add_argument('--jsondir', '-d', required=True, default='tools/scripts/nsdk_cli/', help="Where json configuration files located")
  89. args = parser.parse_args()
  90. fix_jsonfiles(args.jsondir)
  91. pass