Rev 3962: (jml) Refactor 'cmd_serve' to make it easier to extend. in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Tue Jan 27 17:55:36 GMT 2009


At file:///home/pqm/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 3962
revision-id: pqm at pqm.ubuntu.com-20090127175532-221guyk1hom2fzli
parent: pqm at pqm.ubuntu.com-20090127171757-9pgye6upofwicmsc
parent: john at arbash-meinel.com-20090127161617-paqmej2501xnuktf
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Tue 2009-01-27 17:55:32 +0000
message:
  (jml) Refactor 'cmd_serve' to make it easier to extend.
modified:
  NEWS                           NEWS-20050323055033-4e00b5db738777ff
  bzrlib/builtins.py             builtins.py-20050830033751-fc01482b9ca23183
  bzrlib/tests/blackbox/test_breakin.py test_breakin.py-20070424043903-qyy6zm4pj3h4sbp3-1
  bzrlib/tests/blackbox/test_serve.py test_serve.py-20060913064329-8t2pvmsikl4s3xhl-1
    ------------------------------------------------------------
    revno: 3955.1.8
    revision-id: john at arbash-meinel.com-20090127161617-paqmej2501xnuktf
    parent: jml at canonical.com-20090123224833-cqsppcpqokk84wu3
    parent: pqm at pqm.ubuntu.com-20090126181248-yl5ctbxc3y6nu9m4
    committer: John Arbash Meinel <john at arbash-meinel.com>
    branch nick: jam-integration
    timestamp: Tue 2009-01-27 10:16:17 -0600
    message:
      Merge bzr.dev 3960, resolve NEWS
    added:
      bzrlib/tests/branch_implementations/test_iter_merge_sorted_revisions.py test_merge_sorted_re-20090121004847-to3gvjwigstu93eh-1
    modified:
      NEWS                           NEWS-20050323055033-4e00b5db738777ff
      bzrlib/branch.py               branch.py-20050309040759-e4baf4e0d046576e
      bzrlib/osutils.py              osutils.py-20050309040759-eeaff12fbf77ac86
      bzrlib/progress.py             progress.py-20050610070202-df9faaab791964c0
      bzrlib/remote.py               remote.py-20060720103555-yeeg2x51vn0rbtdp-1
      bzrlib/tests/branch_implementations/__init__.py __init__.py-20060123013057-b12a52c3f361daf4
      bzrlib/tests/test_osutils.py   test_osutils.py-20051201224856-e48ee24c12182989
      bzrlib/tests/test_progress.py  test_progress.py-20060308160359-978c397bc79b7fda
      bzrlib/tests/test_ui.py        test_ui.py-20051130162854-458e667a7414af09
      bzrlib/transport/http/__init__.py http_transport.py-20050711212304-506c5fd1059ace96
      bzrlib/transport/http/_pycurl.py pycurlhttp.py-20060110060940-4e2a705911af77a6
      bzrlib/transport/http/_urllib.py _urlgrabber.py-20060113083826-0bbf7d992fbf090c
      bzrlib/transport/http/response.py _response.py-20060613154423-a2ci7hd4iw5c7fnt-1
      bzrlib/ui/__init__.py          ui.py-20050824083933-8cf663c763ba53a9
      bzrlib/ui/text.py              text.py-20051130153916-2e438cffc8afc478
      doc/developers/api-versioning.txt apiversioning.txt-20070626065626-iiihgmhgkv91uphz-1
      doc/developers/plugin-api.txt  pluginapi.txt-20080229110225-q2j5y4agqhlkjn0s-1
    ------------------------------------------------------------
    revno: 3955.1.7
    revision-id: jml at canonical.com-20090123224833-cqsppcpqokk84wu3
    parent: jml at canonical.com-20090123223622-vx2zuxzgebj0p30r
    committer: Jonathan Lange <jml at canonical.com>
    branch nick: refactor-serve
    timestamp: Fri 2009-01-23 20:48:33 -0200
    message:
      Fix some tests that assumed the port was on stderr rather than stdout.
    modified:
      bzrlib/tests/blackbox/test_breakin.py test_breakin.py-20070424043903-qyy6zm4pj3h4sbp3-1
      bzrlib/tests/blackbox/test_serve.py test_serve.py-20060913064329-8t2pvmsikl4s3xhl-1
    ------------------------------------------------------------
    revno: 3955.1.6
    revision-id: jml at canonical.com-20090123223622-vx2zuxzgebj0p30r
    parent: jml at canonical.com-20090123215439-mjdg4g3bzrt0db1r
    committer: Jonathan Lange <jml at canonical.com>
    branch nick: refactor-serve
    timestamp: Fri 2009-01-23 20:36:22 -0200
    message:
      Fix up a couple of dumb errors.
    modified:
      bzrlib/builtins.py             builtins.py-20050830033751-fc01482b9ca23183
    ------------------------------------------------------------
    revno: 3955.1.5
    revision-id: jml at canonical.com-20090123215439-mjdg4g3bzrt0db1r
    parent: jml at canonical.com-20090123215216-561di9mu3dtysuft
    committer: Jonathan Lange <jml at canonical.com>
    branch nick: refactor-serve
    timestamp: Fri 2009-01-23 19:54:39 -0200
    message:
      NEWS update and copyright update.
    modified:
      NEWS                           NEWS-20050323055033-4e00b5db738777ff
      bzrlib/builtins.py             builtins.py-20050830033751-fc01482b9ca23183
    ------------------------------------------------------------
    revno: 3955.1.4
    revision-id: jml at canonical.com-20090123215216-561di9mu3dtysuft
    parent: jml at canonical.com-20090123214639-mpgyfh5pmh3gom2a
    committer: Jonathan Lange <jml at canonical.com>
    branch nick: refactor-serve
    timestamp: Fri 2009-01-23 19:52:16 -0200
    message:
      Docstrings and extraction of method to get the smart server.
    modified:
      bzrlib/builtins.py             builtins.py-20050830033751-fc01482b9ca23183
    ------------------------------------------------------------
    revno: 3955.1.3
    revision-id: jml at canonical.com-20090123214639-mpgyfh5pmh3gom2a
    parent: jml at canonical.com-20090123214506-l7ck1ykttxhjjk9i
    committer: Jonathan Lange <jml at canonical.com>
    branch nick: refactor-serve
    timestamp: Fri 2009-01-23 19:46:39 -0200
    message:
      Rename run_server to run_smart_server.
    modified:
      bzrlib/builtins.py             builtins.py-20050830033751-fc01482b9ca23183
    ------------------------------------------------------------
    revno: 3955.1.2
    revision-id: jml at canonical.com-20090123214506-l7ck1ykttxhjjk9i
    parent: jml at canonical.com-20090123213217-2f2rt7abpv816zan
    committer: Jonathan Lange <jml at canonical.com>
    branch nick: refactor-serve
    timestamp: Fri 2009-01-23 19:45:06 -0200
    message:
      Extract the port-getting logic. Use note() rather than print()
    modified:
      bzrlib/builtins.py             builtins.py-20050830033751-fc01482b9ca23183
    ------------------------------------------------------------
    revno: 3955.1.1
    revision-id: jml at canonical.com-20090123213217-2f2rt7abpv816zan
    parent: pqm at pqm.ubuntu.com-20090123103145-yvo3icrif75vkt20
    committer: Jonathan Lange <jml at canonical.com>
    branch nick: refactor-serve
    timestamp: Fri 2009-01-23 19:32:17 -0200
    message:
      Extract the bit of run() that runs the server.
    modified:
      bzrlib/builtins.py             builtins.py-20050830033751-fc01482b9ca23183
=== modified file 'NEWS'
--- a/NEWS	2009-01-27 16:16:13 +0000
+++ b/NEWS	2009-01-27 17:55:32 +0000
@@ -102,6 +102,9 @@
       efficient way of doing the mapping.
       (Ian Clatworthy)
 
+    * Refactor cmd_serve so that it's a little easier to build commands that
+      extend it, and perhaps even a bit easier to read.  (Jonathan Lange)
+
     * ``TreeDelta.show()`` now accepts a ``filter`` parameter allowing log
       formatters to retrict the output.
       (Vincent Ladeuil)

=== modified file 'bzrlib/builtins.py'
--- a/bzrlib/builtins.py	2009-01-15 05:18:22 +0000
+++ b/bzrlib/builtins.py	2009-01-23 22:36:22 +0000
@@ -1,4 +1,4 @@
-# Copyright (C) 2004, 2005, 2006, 2007, 2008 Canonical Ltd
+# Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Canonical Ltd
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -4080,9 +4080,70 @@
                 ),
         ]
 
-    def run(self, port=None, inet=False, directory=None, allow_writes=False):
+    def run_smart_server(self, smart_server):
+        """Run 'smart_server' forever, with no UI output at all."""
+        # For the duration of this server, no UI output is permitted. note
+        # that this may cause problems with blackbox tests. This should be
+        # changed with care though, as we dont want to use bandwidth sending
+        # progress over stderr to smart server clients!
         from bzrlib import lockdir
+        old_factory = ui.ui_factory
+        old_lockdir_timeout = lockdir._DEFAULT_TIMEOUT_SECONDS
+        try:
+            ui.ui_factory = ui.SilentUIFactory()
+            lockdir._DEFAULT_TIMEOUT_SECONDS = 0
+            smart_server.serve()
+        finally:
+            ui.ui_factory = old_factory
+            lockdir._DEFAULT_TIMEOUT_SECONDS = old_lockdir_timeout
+
+    def get_host_and_port(self, port):
+        """Return the host and port to run the smart server on.
+
+        If 'port' is None, the default host (`medium.BZR_DEFAULT_INTERFACE`)
+        and port (`medium.BZR_DEFAULT_PORT`) will be used.
+
+        If 'port' has a colon in it, the string before the colon will be
+        interpreted as the host.
+
+        :param port: A string of the port to run the server on.
+        :return: A tuple of (host, port), where 'host' is a host name or IP,
+            and port is an integer TCP/IP port.
+        """
+        from bzrlib.smart import medium
+        host = medium.BZR_DEFAULT_INTERFACE
+        if port is None:
+            port = medium.BZR_DEFAULT_PORT
+        else:
+            if ':' in port:
+                host, port = port.split(':')
+            port = int(port)
+        return host, port
+
+    def get_smart_server(self, transport, inet, port):
+        """Construct a smart server.
+
+        :param transport: The base transport from which branches will be
+            served.
+        :param inet: If True, serve over stdin and stdout. Used for running
+            from inet.
+        :param port: The port to listen on. By default, it's `
+            medium.BZR_DEFAULT_PORT`. See `get_host_and_port` for more
+            information.
+        :return: A smart server.
+        """
         from bzrlib.smart import medium, server
+        if inet:
+            smart_server = medium.SmartServerPipeStreamMedium(
+                sys.stdin, sys.stdout, transport)
+        else:
+            host, port = self.get_host_and_port(port)
+            smart_server = server.SmartTCPServer(
+                transport, host=host, port=port)
+            note('listening on port: %s' % smart_server.port)
+        return smart_server
+
+    def run(self, port=None, inet=False, directory=None, allow_writes=False):
         from bzrlib.transport import get_transport
         from bzrlib.transport.chroot import ChrootServer
         if directory is None:
@@ -4093,33 +4154,8 @@
         chroot_server = ChrootServer(get_transport(url))
         chroot_server.setUp()
         t = get_transport(chroot_server.get_url())
-        if inet:
-            smart_server = medium.SmartServerPipeStreamMedium(
-                sys.stdin, sys.stdout, t)
-        else:
-            host = medium.BZR_DEFAULT_INTERFACE
-            if port is None:
-                port = medium.BZR_DEFAULT_PORT
-            else:
-                if ':' in port:
-                    host, port = port.split(':')
-                port = int(port)
-            smart_server = server.SmartTCPServer(t, host=host, port=port)
-            print 'listening on port: ', smart_server.port
-            sys.stdout.flush()
-        # for the duration of this server, no UI output is permitted.
-        # note that this may cause problems with blackbox tests. This should
-        # be changed with care though, as we dont want to use bandwidth sending
-        # progress over stderr to smart server clients!
-        old_factory = ui.ui_factory
-        old_lockdir_timeout = lockdir._DEFAULT_TIMEOUT_SECONDS
-        try:
-            ui.ui_factory = ui.SilentUIFactory()
-            lockdir._DEFAULT_TIMEOUT_SECONDS = 0
-            smart_server.serve()
-        finally:
-            ui.ui_factory = old_factory
-            lockdir._DEFAULT_TIMEOUT_SECONDS = old_lockdir_timeout
+        smart_server = self.get_smart_server(t, inet, port)
+        self.run_smart_server(smart_server)
 
 
 class cmd_join(Command):

=== modified file 'bzrlib/tests/blackbox/test_breakin.py'
--- a/bzrlib/tests/blackbox/test_breakin.py	2008-11-11 00:57:17 +0000
+++ b/bzrlib/tests/blackbox/test_breakin.py	2009-01-23 22:48:33 +0000
@@ -54,7 +54,7 @@
         proc = self.start_bzr_subprocess(self._test_process_args,
                 env_changes=dict(BZR_SIGQUIT_PDB=None))
         # wait for it to get started, and print the 'listening' line
-        proc.stdout.readline()
+        proc.stderr.readline()
         # first sigquit pops into debugger
         os.kill(proc.pid, signal.SIGQUIT)
         proc.stdin.write("q\n")
@@ -67,7 +67,7 @@
         proc = self.start_bzr_subprocess(self._test_process_args,
                 env_changes=dict(BZR_SIGQUIT_PDB=None))
         # wait for it to get started, and print the 'listening' line
-        proc.stdout.readline()
+        proc.stderr.readline()
         # break into the debugger
         os.kill(proc.pid, signal.SIGQUIT)
         # now send a second sigquit, which should cause it to exit.  That
@@ -93,7 +93,7 @@
         proc = self.start_bzr_subprocess(self._test_process_args,
                 env_changes=dict(BZR_SIGQUIT_PDB='0'))
         # wait for it to get started, and print the 'listening' line
-        proc.stdout.readline()
+        proc.stderr.readline()
         # first hit should just kill it
         os.kill(proc.pid, signal.SIGQUIT)
         proc.wait()

=== modified file 'bzrlib/tests/blackbox/test_serve.py'
--- a/bzrlib/tests/blackbox/test_serve.py	2008-05-19 11:43:41 +0000
+++ b/bzrlib/tests/blackbox/test_serve.py	2009-01-23 22:48:33 +0000
@@ -97,7 +97,7 @@
         args = ['serve', '--port', 'localhost:0']
         args.extend(extra_options)
         process = self.start_bzr_subprocess(args, skip_if_plan_to_signal=True)
-        port_line = process.stdout.readline()
+        port_line = process.stderr.readline()
         prefix = 'listening on port: '
         self.assertStartsWith(port_line, prefix)
         port = int(port_line[len(prefix):])




More information about the bazaar-commits mailing list