configure_file.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python2.7
  2. from __future__ import print_function
  3. import sys
  4. import os.path
  5. import re
  6. cmakeDef01 = "#cmakedefine01"
  7. cmakeDef = "#cmakedefine"
  8. def replaceLine(l, varDict, define):
  9. words = l.split()
  10. if words:
  11. if words[0] == cmakeDef:
  12. sPos = l.find(cmakeDef)
  13. ePos = sPos + len(cmakeDef)
  14. l = l[:sPos] + define + l[ePos:] + '\n'
  15. if words[0] == cmakeDef01:
  16. var = words[1]
  17. cmakeValue = varDict.get(var)
  18. if cmakeValue == 'yes':
  19. val = '1'
  20. else:
  21. val = '0'
  22. sPos = l.find(cmakeDef01)
  23. ePos = l.find(var) + len(var)
  24. l = l[:sPos] + define + ' ' + var + ' ' + val + l[ePos + 1 :] + '\n'
  25. finder = re.compile(".*?(@[a-zA-Z0-9_]+@).*")
  26. while True:
  27. re_result = finder.match(l)
  28. if not re_result:
  29. return l
  30. key = re_result.group(1)[1:-1]
  31. l = l[: re_result.start(1)] + varDict.get(key, '') + l[re_result.end(1) :]
  32. def main(inputPath, outputPath, varDict):
  33. define = '#define' if os.path.splitext(outputPath)[1] != '.asm' else '%define'
  34. with open(outputPath, 'w') as output:
  35. with open(inputPath, 'r') as input:
  36. for l in input:
  37. output.write(replaceLine(l, varDict, define))
  38. def usage():
  39. print("usage: configure_file.py inputPath outputPath key1=value1 ...")
  40. exit(1)
  41. if __name__ == "__main__":
  42. if len(sys.argv) < 3:
  43. usage()
  44. varDict = {}
  45. for x in sys.argv[3:]:
  46. try:
  47. key, value = str(x).split('=', 1)
  48. value = value.replace("#BACKSLASH#", "\\\\")
  49. value = value.replace("#DOUBLE_QUOTE#", '"')
  50. except Exception:
  51. continue
  52. varDict[key] = value
  53. main(sys.argv[1], sys.argv[2], varDict)