cmakelib.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import sys, subprocess, json
  2. termwidth = 150
  3. print_communication = True
  4. def ordered(obj):
  5. if isinstance(obj, dict):
  6. return sorted((k, ordered(v)) for k, v in obj.items())
  7. if isinstance(obj, list):
  8. return sorted(ordered(x) for x in obj)
  9. else:
  10. return obj
  11. def col_print(title, array):
  12. print
  13. print
  14. print(title)
  15. indentwidth = 4
  16. indent = " " * indentwidth
  17. if not array:
  18. print(indent + "<None>")
  19. return
  20. padwidth = 2
  21. maxitemwidth = len(max(array, key=len))
  22. numCols = max(1, int((termwidth - indentwidth + padwidth) / (maxitemwidth + padwidth)))
  23. numRows = len(array) // numCols + 1
  24. pad = " " * padwidth
  25. for index in range(numRows):
  26. print(indent + pad.join(item.ljust(maxitemwidth) for item in array[index::numRows]))
  27. def waitForRawMessage(cmakeCommand):
  28. stdoutdata = ""
  29. payload = ""
  30. while not cmakeCommand.poll():
  31. stdoutdataLine = cmakeCommand.stdout.readline()
  32. if stdoutdataLine:
  33. stdoutdata += stdoutdataLine.decode('utf-8')
  34. else:
  35. break
  36. begin = stdoutdata.find("[== CMake Server ==[\n")
  37. end = stdoutdata.find("]== CMake Server ==]")
  38. if (begin != -1 and end != -1):
  39. begin += len("[== CMake Server ==[\n")
  40. payload = stdoutdata[begin:end]
  41. if print_communication:
  42. print("\nSERVER>", json.loads(payload), "\n")
  43. return json.loads(payload)
  44. def writeRawData(cmakeCommand, content):
  45. writeRawData.counter += 1
  46. payload = """
  47. [== CMake Server ==[
  48. %s
  49. ]== CMake Server ==]
  50. """ % content
  51. rn = ( writeRawData.counter % 2 ) == 0
  52. if rn:
  53. payload = payload.replace('\n', '\r\n')
  54. if print_communication:
  55. print("\nCLIENT>", content, "(Use \\r\\n:", rn, ")\n")
  56. cmakeCommand.stdin.write(payload.encode('utf-8'))
  57. cmakeCommand.stdin.flush()
  58. writeRawData.counter = 0
  59. def writePayload(cmakeCommand, obj):
  60. writeRawData(cmakeCommand, json.dumps(obj))
  61. def initProc(cmakeCommand):
  62. cmakeCommand = subprocess.Popen([cmakeCommand, "-E", "server"],
  63. stdin=subprocess.PIPE,
  64. stdout=subprocess.PIPE)
  65. packet = waitForRawMessage(cmakeCommand)
  66. if packet == None:
  67. print("Not in server mode")
  68. sys.exit(1)
  69. if packet['type'] != 'hello':
  70. print("No hello message")
  71. sys.exit(1)
  72. return cmakeCommand
  73. def waitForMessage(cmakeCommand, expected):
  74. data = ordered(expected)
  75. packet = ordered(waitForRawMessage(cmakeCommand))
  76. if packet != data:
  77. sys.exit(-1)
  78. return packet
  79. def waitForReply(cmakeCommand, originalType, cookie):
  80. packet = waitForRawMessage(cmakeCommand)
  81. if packet['cookie'] != cookie or packet['type'] != 'reply' or packet['inReplyTo'] != originalType:
  82. sys.exit(1)
  83. def waitForError(cmakeCommand, originalType, cookie, message):
  84. packet = waitForRawMessage(cmakeCommand)
  85. if packet['cookie'] != cookie or packet['type'] != 'error' or packet['inReplyTo'] != originalType or packet['errorMessage'] != message:
  86. sys.exit(1)
  87. def waitForProgress(cmakeCommand, originalType, cookie, current, message):
  88. packet = waitForRawMessage(cmakeCommand)
  89. if packet['cookie'] != cookie or packet['type'] != 'progress' or packet['inReplyTo'] != originalType or packet['progressCurrent'] != current or packet['progressMessage'] != message:
  90. sys.exit(1)
  91. def handshake(cmakeCommand, major, minor):
  92. version = { 'major': major }
  93. if minor >= 0:
  94. version['minor'] = minor
  95. writePayload(cmakeCommand, { 'type': 'handshake', 'protocolVersion': version, 'cookie': 'TEST_HANDSHAKE' })
  96. waitForReply(cmakeCommand, 'handshake', 'TEST_HANDSHAKE')