xmlrpcclient.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #
  2. # BitBake XMLRPC Client Interface
  3. #
  4. # Copyright (C) 2006 - 2007 Michael 'Mickey' Lauer
  5. # Copyright (C) 2006 - 2008 Richard Purdie
  6. #
  7. # SPDX-License-Identifier: GPL-2.0-only
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License version 2 as
  11. # published by the Free Software Foundation.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. import os
  22. import sys
  23. import socket
  24. import http.client
  25. import xmlrpc.client
  26. import bb
  27. from bb.ui import uievent
  28. class BBTransport(xmlrpc.client.Transport):
  29. def __init__(self, timeout):
  30. self.timeout = timeout
  31. self.connection_token = None
  32. xmlrpc.client.Transport.__init__(self)
  33. # Modified from default to pass timeout to HTTPConnection
  34. def make_connection(self, host):
  35. #return an existing connection if possible. This allows
  36. #HTTP/1.1 keep-alive.
  37. if self._connection and host == self._connection[0]:
  38. return self._connection[1]
  39. # create a HTTP connection object from a host descriptor
  40. chost, self._extra_headers, x509 = self.get_host_info(host)
  41. #store the host argument along with the connection object
  42. self._connection = host, http.client.HTTPConnection(chost, timeout=self.timeout)
  43. return self._connection[1]
  44. def set_connection_token(self, token):
  45. self.connection_token = token
  46. def send_content(self, h, body):
  47. if self.connection_token:
  48. h.putheader("Bitbake-token", self.connection_token)
  49. xmlrpc.client.Transport.send_content(self, h, body)
  50. def _create_server(host, port, timeout = 60):
  51. t = BBTransport(timeout)
  52. s = xmlrpc.client.ServerProxy("http://%s:%d/" % (host, port), transport=t, allow_none=True, use_builtin_types=True)
  53. return s, t
  54. def check_connection(remote, timeout):
  55. try:
  56. host, port = remote.split(":")
  57. port = int(port)
  58. except Exception as e:
  59. bb.warn("Failed to read remote definition (%s)" % str(e))
  60. raise e
  61. server, _transport = _create_server(host, port, timeout)
  62. try:
  63. ret, err = server.runCommand(['getVariable', 'TOPDIR'])
  64. if err or not ret:
  65. return False
  66. except ConnectionError:
  67. return False
  68. return True
  69. class BitBakeXMLRPCServerConnection(object):
  70. def __init__(self, host, port, clientinfo=("localhost", 0), observer_only = False, featureset = None):
  71. self.connection, self.transport = _create_server(host, port)
  72. self.clientinfo = clientinfo
  73. self.observer_only = observer_only
  74. if featureset:
  75. self.featureset = featureset
  76. else:
  77. self.featureset = []
  78. self.events = uievent.BBUIEventQueue(self.connection, self.clientinfo)
  79. _, error = self.connection.runCommand(["setFeatures", self.featureset])
  80. if error:
  81. # disconnect the client, we can't make the setFeature work
  82. self.connection.removeClient()
  83. # no need to log it here, the error shall be sent to the client
  84. raise BaseException(error)
  85. def connect(self, token = None):
  86. if token is None:
  87. if self.observer_only:
  88. token = "observer"
  89. else:
  90. token = self.connection.addClient()
  91. if token is None:
  92. return None
  93. self.transport.set_connection_token(token)
  94. return self
  95. def removeClient(self):
  96. if not self.observer_only:
  97. self.connection.removeClient()
  98. def terminate(self):
  99. # Don't wait for server indefinitely
  100. socket.setdefaulttimeout(2)
  101. try:
  102. self.events.system_quit()
  103. except:
  104. pass
  105. try:
  106. self.connection.removeClient()
  107. except:
  108. pass
  109. def connectXMLRPC(remote, featureset, observer_only = False, token = None):
  110. # The format of "remote" must be "server:port"
  111. try:
  112. [host, port] = remote.split(":")
  113. port = int(port)
  114. except Exception as e:
  115. bb.warn("Failed to parse remote definition %s (%s)" % (remote, str(e)))
  116. raise e
  117. # We need our IP for the server connection. We get the IP
  118. # by trying to connect with the server
  119. try:
  120. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  121. s.connect((host, port))
  122. ip = s.getsockname()[0]
  123. s.close()
  124. except Exception as e:
  125. bb.warn("Could not create socket for %s:%s (%s)" % (host, port, str(e)))
  126. raise e
  127. try:
  128. connection = BitBakeXMLRPCServerConnection(host, port, (ip, 0), observer_only, featureset)
  129. return connection.connect(token)
  130. except Exception as e:
  131. bb.warn("Could not connect to server at %s:%s (%s)" % (host, port, str(e)))
  132. raise e