libroot/posix/stdio: Remove unused portions.
[haiku.git] / build / scripts / bootstrap_client.py
blob9ac12f7b19a225d81fd1ffc07f62b363d19bc47d
1 #!/usr/bin/env python
3 # Usage: bootstrap_client.py <address>[:port] <command> ...
5 # <command> and the following arguments are concatenated (separated by a space)
6 # and passed to a shell on the server.
8 import os
9 import select
10 import socket
11 import sys
14 port = 4242
15 bufferSize = 4 * 1024
17 # interpret command line args
18 if len(sys.argv) < 3:
19 sys.exit('Usage: ' + sys.argv[0] + ' <address>[:<port>] <command>')
21 address = sys.argv[1]
22 portIndex = address.find(':')
23 if portIndex >= 0:
24 port = int(address[portIndex + 1:])
25 address = address[:portIndex]
27 commandToRun = " ".join(sys.argv[2:])
29 # create sockets and connect to server
30 controlConnection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
31 stdioConnection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
32 stderrConnection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
34 try:
35 controlConnection.connect((address, port))
36 stdioConnection.connect((address, port))
37 stderrConnection.connect((address, port))
38 except socket.error, msg:
39 sys.exit('Failed to connect to %s port %d: %s' % (address, port, msg[1]))
41 # send command length and command
42 controlConnection.send("%08d" % len(commandToRun))
43 controlConnection.send(commandToRun)
45 # I/O loop. We quit when all sockets have been closed.
46 exitCode = ''
47 connections = [controlConnection, stdioConnection, stderrConnection, sys.stdin]
49 while connections and (len(connections) > 1 or not sys.stdin in connections):
50 (readable, writable, exceptions) = select.select(connections, [],
51 connections)
53 if sys.stdin in readable:
54 data = sys.stdin.readline(bufferSize)
55 if data:
56 stdioConnection.send(data)
57 else:
58 connections.remove(sys.stdin)
59 stdioConnection.shutdown(socket.SHUT_WR)
61 if stdioConnection in readable:
62 data = stdioConnection.recv(bufferSize)
63 if data:
64 sys.stdout.write(data)
65 else:
66 connections.remove(stdioConnection)
68 if stderrConnection in readable:
69 data = stderrConnection.recv(bufferSize)
70 if data:
71 sys.stderr.write(data)
72 else:
73 connections.remove(stderrConnection)
75 if controlConnection in readable:
76 data = controlConnection.recv(bufferSize)
77 if data:
78 exitCode += data
79 else:
80 connections.remove(controlConnection)
82 # either an exit code has been sent or we consider this an error
83 if exitCode:
84 sys.exit(int(exitCode))
85 sys.exit(1)