4540
4540
takes_options = [
4542
4542
help='Serve on stdin/out for use from inetd or sshd.'),
4543
RegistryOption('protocol',
4544
help="Protocol to serve.",
4545
lazy_registry=('bzrlib.transport', 'transport_server_registry'),
4546
value_switches=True),
4544
4548
help='Listen for connections on nominated port of the form '
4545
4549
'[hostname:]portnumber. Passing 0 as the port number will '
4546
'result in a dynamically allocated port. The default port is '
4550
'result in a dynamically allocated port. The default port '
4551
'depends on the protocol.',
4549
4553
Option('directory',
4550
4554
help='Serve contents of this directory.',
4559
def run_smart_server(self, smart_server):
4560
"""Run 'smart_server' forever, with no UI output at all."""
4561
# For the duration of this server, no UI output is permitted. note
4562
# that this may cause problems with blackbox tests. This should be
4563
# changed with care though, as we dont want to use bandwidth sending
4564
# progress over stderr to smart server clients!
4565
from bzrlib import lockdir
4566
old_factory = ui.ui_factory
4567
old_lockdir_timeout = lockdir._DEFAULT_TIMEOUT_SECONDS
4569
ui.ui_factory = ui.SilentUIFactory()
4570
lockdir._DEFAULT_TIMEOUT_SECONDS = 0
4571
smart_server.serve()
4573
ui.ui_factory = old_factory
4574
lockdir._DEFAULT_TIMEOUT_SECONDS = old_lockdir_timeout
4576
4563
def get_host_and_port(self, port):
4577
4564
"""Return the host and port to run the smart server on.
4579
If 'port' is None, the default host (`medium.BZR_DEFAULT_INTERFACE`)
4580
and port (`medium.BZR_DEFAULT_PORT`) will be used.
4566
If 'port' is None, None will be returned for the host and port.
4582
4568
If 'port' has a colon in it, the string before the colon will be
4583
4569
interpreted as the host.
4586
4572
:return: A tuple of (host, port), where 'host' is a host name or IP,
4587
4573
and port is an integer TCP/IP port.
4589
from bzrlib.smart import medium
4590
host = medium.BZR_DEFAULT_INTERFACE
4592
port = medium.BZR_DEFAULT_PORT
4576
if port is not None:
4594
4577
if ':' in port:
4595
4578
host, port = port.split(':')
4596
4579
port = int(port)
4597
4580
return host, port
4599
def get_smart_server(self, transport, inet, port):
4600
"""Construct a smart server.
4602
:param transport: The base transport from which branches will be
4604
:param inet: If True, serve over stdin and stdout. Used for running
4606
:param port: The port to listen on. By default, it's `
4607
medium.BZR_DEFAULT_PORT`. See `get_host_and_port` for more
4609
:return: A smart server.
4611
from bzrlib.smart import medium, server
4613
smart_server = medium.SmartServerPipeStreamMedium(
4614
sys.stdin, sys.stdout, transport)
4616
host, port = self.get_host_and_port(port)
4617
smart_server = server.SmartTCPServer(
4618
transport, host=host, port=port)
4619
note('listening on port: %s' % smart_server.port)
4622
def run(self, port=None, inet=False, directory=None, allow_writes=False):
4623
from bzrlib.transport import get_transport
4624
from bzrlib.transport.chroot import ChrootServer
4582
def run(self, port=None, inet=False, directory=None, allow_writes=False,
4584
from bzrlib.transport import get_transport, transport_server_registry
4625
4585
if directory is None:
4626
4586
directory = os.getcwd()
4587
if protocol is None:
4588
protocol = transport_server_registry.get()
4589
host, port = self.get_host_and_port(port)
4627
4590
url = urlutils.local_path_to_url(directory)
4628
4591
if not allow_writes:
4629
4592
url = 'readonly+' + url
4630
chroot_server = ChrootServer(get_transport(url))
4631
chroot_server.setUp()
4632
t = get_transport(chroot_server.get_url())
4633
smart_server = self.get_smart_server(t, inet, port)
4634
self.run_smart_server(smart_server)
4593
transport = get_transport(url)
4594
protocol(transport, host, port, inet)
4637
4597
class cmd_join(Command):