2015-11-25 05:35:17 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2015-11-25 05:35:32 +08:00
|
|
|
"""
|
|
|
|
SWIG generation server. Listens for connections from swig generation clients
|
|
|
|
and runs swig in the requested fashion, sending back the results.
|
|
|
|
"""
|
|
|
|
|
2015-11-25 05:35:17 +08:00
|
|
|
# Future imports
|
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2015-11-25 05:35:32 +08:00
|
|
|
# Python modules
|
|
|
|
import argparse
|
2015-12-01 06:31:13 +08:00
|
|
|
import io
|
2015-11-25 05:35:32 +08:00
|
|
|
import logging
|
|
|
|
import os
|
2015-11-25 05:35:40 +08:00
|
|
|
import select
|
2015-11-25 05:35:50 +08:00
|
|
|
import shutil
|
2015-11-25 05:35:32 +08:00
|
|
|
import socket
|
|
|
|
import struct
|
|
|
|
import sys
|
2015-11-25 05:35:50 +08:00
|
|
|
import tempfile
|
2015-11-25 05:35:32 +08:00
|
|
|
import traceback
|
|
|
|
|
|
|
|
# LLDB modules
|
|
|
|
import use_lldb_suite
|
2015-12-01 06:31:13 +08:00
|
|
|
from lldbsuite.support import fs
|
2015-11-25 05:35:32 +08:00
|
|
|
from lldbsuite.support import sockutil
|
|
|
|
|
|
|
|
# package imports
|
|
|
|
from . import local
|
2015-12-01 06:31:13 +08:00
|
|
|
from . import remote
|
2015-11-25 05:35:32 +08:00
|
|
|
|
|
|
|
default_port = 8537
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-12-03 03:00:52 +08:00
|
|
|
def add_subparser_args(parser):
|
2015-11-25 05:35:32 +08:00
|
|
|
parser.add_argument(
|
|
|
|
"--port",
|
|
|
|
action="store",
|
|
|
|
default=default_port,
|
|
|
|
help=("The local port to bind to"))
|
|
|
|
|
2015-12-01 06:31:13 +08:00
|
|
|
parser.add_argument(
|
|
|
|
"--swig-executable",
|
|
|
|
action="store",
|
|
|
|
default=fs.find_executable("swig"),
|
|
|
|
dest="swig_executable")
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-12-03 03:00:52 +08:00
|
|
|
def finalize_subparser_options(options):
|
|
|
|
pass
|
2015-11-25 05:35:32 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-11-25 05:35:32 +08:00
|
|
|
def initialize_listening_socket(options):
|
|
|
|
logging.debug("Creating socket...")
|
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
|
|
|
|
logging.info("Binding to ip address '', port {}".format(options.port))
|
|
|
|
s.bind(('', options.port))
|
|
|
|
|
|
|
|
logging.debug("Putting socket in listen mode...")
|
|
|
|
s.listen()
|
|
|
|
return s
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-11-25 05:35:32 +08:00
|
|
|
def accept_once(sock, options):
|
|
|
|
logging.debug("Waiting for connection...")
|
2015-11-25 05:35:40 +08:00
|
|
|
while True:
|
|
|
|
rlist, wlist, xlist = select.select([sock], [], [], 0.5)
|
|
|
|
if not rlist:
|
|
|
|
continue
|
|
|
|
|
|
|
|
client, addr = sock.accept()
|
|
|
|
logging.info("Received connection from {}".format(addr))
|
|
|
|
data_size = struct.unpack("!I", sockutil.recvall(client, 4))[0]
|
|
|
|
logging.debug("Expecting {} bytes of data from client"
|
|
|
|
.format(data_size))
|
|
|
|
data = sockutil.recvall(client, data_size)
|
|
|
|
logging.info("Received {} bytes of data from client"
|
|
|
|
.format(len(data)))
|
|
|
|
|
2015-11-25 05:35:50 +08:00
|
|
|
pack_location = None
|
|
|
|
try:
|
2015-12-01 06:31:24 +08:00
|
|
|
tempfolder = os.path.join(tempfile.gettempdir(), "swig-bot")
|
|
|
|
os.makedirs(tempfolder, exist_ok=True)
|
|
|
|
|
|
|
|
pack_location = tempfile.mkdtemp(dir=tempfolder)
|
|
|
|
logging.debug("Extracting archive to {}".format(pack_location))
|
|
|
|
|
|
|
|
local.unpack_archive(pack_location, data)
|
2015-11-25 05:35:50 +08:00
|
|
|
logging.debug("Successfully unpacked archive...")
|
|
|
|
|
2015-12-01 06:31:13 +08:00
|
|
|
config_file = os.path.normpath(os.path.join(pack_location,
|
|
|
|
"config.json"))
|
|
|
|
parsed_config = remote.parse_config(io.open(config_file))
|
|
|
|
config = local.LocalConfig()
|
|
|
|
config.languages = parsed_config["languages"]
|
|
|
|
config.swig_executable = options.swig_executable
|
|
|
|
config.src_root = pack_location
|
|
|
|
config.target_dir = os.path.normpath(
|
|
|
|
os.path.join(config.src_root, "output"))
|
|
|
|
logging.info(
|
|
|
|
"Running swig. languages={}, swig={}, src_root={}, target={}"
|
|
|
|
.format(config.languages, config.swig_executable,
|
|
|
|
config.src_root, config.target_dir))
|
|
|
|
|
2015-12-01 06:31:24 +08:00
|
|
|
status = local.generate(config)
|
|
|
|
logging.debug("Finished running swig. Packaging up files {}"
|
|
|
|
.format(os.listdir(config.target_dir)))
|
2015-12-01 06:31:13 +08:00
|
|
|
zip_data = io.BytesIO()
|
2015-12-01 06:31:24 +08:00
|
|
|
zip_file = local.pack_archive(zip_data, config.target_dir, None)
|
|
|
|
response_status = remote.serialize_response_status(status)
|
|
|
|
logging.debug("Sending response status {}".format(response_status))
|
|
|
|
logging.info("(swig output) -> swig_output.json")
|
|
|
|
zip_file.writestr("swig_output.json", response_status)
|
|
|
|
|
2015-12-01 06:31:13 +08:00
|
|
|
zip_file.close()
|
|
|
|
response_data = zip_data.getvalue()
|
|
|
|
logging.info("Sending {} byte response".format(len(response_data)))
|
|
|
|
client.sendall(struct.pack("!I", len(response_data)))
|
|
|
|
client.sendall(response_data)
|
2015-11-25 05:35:50 +08:00
|
|
|
finally:
|
|
|
|
if pack_location is not None:
|
|
|
|
logging.debug("Removing temporary folder {}"
|
|
|
|
.format(pack_location))
|
|
|
|
shutil.rmtree(pack_location)
|
2015-11-25 05:35:32 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-11-25 05:35:32 +08:00
|
|
|
def accept_loop(sock, options):
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
accept_once(sock, options)
|
|
|
|
except Exception as e:
|
|
|
|
error = traceback.format_exc()
|
|
|
|
logging.error("An error occurred while processing the connection.")
|
|
|
|
logging.error(error)
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-12-03 03:00:52 +08:00
|
|
|
def run(options):
|
2015-12-01 06:31:13 +08:00
|
|
|
print(options)
|
2015-11-25 05:35:32 +08:00
|
|
|
sock = initialize_listening_socket(options)
|
|
|
|
accept_loop(sock, options)
|
|
|
|
return options
|