Rev 5146: (robertc) Make Command.run safe to call directly. (Robert Collins) in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Mon Apr 12 04:09:48 BST 2010


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

------------------------------------------------------------
revno: 5146 [merge]
revision-id: pqm at pqm.ubuntu.com-20100412030946-4ahe008i86yibsmc
parent: pqm at pqm.ubuntu.com-20100412015454-q9kwf7uffn9s7a3j
parent: robertc at robertcollins.net-20100409035814-eqayfeknoncyoctr
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Mon 2010-04-12 04:09:46 +0100
message:
  (robertc) Make Command.run safe to call directly. (Robert Collins)
modified:
  NEWS                           NEWS-20050323055033-4e00b5db738777ff
  bzrlib/commands.py             bzr.py-20050309040720-d10f4714595cf8c3
  bzrlib/tests/commands/test_branch.py test_branch.py-20070520173042-ou3a796w3xn1y8ps-1
  bzrlib/tests/commands/test_cat.py test_cat.py-20070525170351-vg2apsfb5j413913-1
  bzrlib/tests/commands/test_checkout.py test_checkout.py-20070525151718-vm7ligd5px5dtmda-1
  bzrlib/tests/commands/test_commit.py test_commit.py-20070913161801-ydrx2k5gmv7k7eiu-1
  bzrlib/tests/commands/test_init.py test_init.py-20070514074921-audbcdd8o56dpame-1
  bzrlib/tests/commands/test_init_repository.py test_init_repository-20070525163812-87xw0678ky573l27-1
  bzrlib/tests/commands/test_merge.py test_merge.py-20070525163813-v8yfs5wu77hjsx0o-1
  bzrlib/tests/commands/test_missing.py test_missing.py-20070525171057-qr1z4sleurlp9b5v-1
  bzrlib/tests/commands/test_pull.py test_pull.py-20070525144918-cgmunk4ici2krjnd-1
  bzrlib/tests/commands/test_push.py test_push.py-20070525122003-gc1ob0ea0nueoqgj-1
  bzrlib/tests/commands/test_update.py test_update.py-20070910091045-8uyp8v73j926l1g2-1
=== modified file 'NEWS'
--- a/NEWS	2010-04-10 18:03:54 +0000
+++ b/NEWS	2010-04-12 03:09:46 +0000
@@ -60,6 +60,12 @@
 Internals
 *********
 
+* ``bzrlib.commands.Command.run_direct`` is no longer needed - the pre
+  2.1 method of calling run() to perform testing or direct use via the API
+  is now possible again. As part of this, the _operation attribute on
+  Command is now transient and only exists for the duration of ``run()``.
+  (Robert Collins)
+
 Testing
 *******
 

=== modified file 'bzrlib/commands.py'
--- a/bzrlib/commands.py	2010-04-01 03:52:41 +0000
+++ b/bzrlib/commands.py	2010-04-09 03:58:14 +0000
@@ -411,7 +411,7 @@
             warn("No help message set for %r" % self)
         # List of standard options directly supported
         self.supported_std_options = []
-        self._operation = cleanup.OperationWithCleanups(self.run)
+        self._setup_run()
 
     def add_cleanup(self, cleanup_func, *args, **kwargs):
         """Register a function to call after self.run returns or raises.
@@ -429,7 +429,9 @@
 
         This is useful for releasing expensive or contentious resources (such
         as write locks) before doing further work that does not require those
-        resources (such as writing results to self.outf).
+        resources (such as writing results to self.outf). Note though, that
+        as it releases all resources, this may release locks that the command
+        wants to hold, so use should be done with care.
         """
         self._operation.cleanup_now()
 
@@ -680,11 +682,30 @@
 
         self._setup_outf()
 
-        return self.run_direct(**all_cmd_args)
-
+        return self.run(**all_cmd_args)
+
+    def _setup_run(self):
+        """Wrap the defined run method on self with a cleanup.
+
+        This is called by __init__ to make the Command be able to be run
+        by just calling run(), as it could be before cleanups were added.
+
+        If a different form of cleanups are in use by your Command subclass,
+        you can override this method.
+        """
+        class_run = self.run
+        def run(*args, **kwargs):
+            self._operation = cleanup.OperationWithCleanups(class_run)
+            try:
+                return self._operation.run_simple(*args, **kwargs)
+            finally:
+                del self._operation
+        self.run = run
+
+    @deprecated_method(deprecated_in((2, 2, 0)))
     def run_direct(self, *args, **kwargs):
-        """Call run directly with objects (without parsing an argv list)."""
-        return self._operation.run_simple(*args, **kwargs)
+        """Deprecated thunk from bzrlib 2.1."""
+        return self.run(*args, **kwargs)
 
     def run(self):
         """Actually run the command.
@@ -695,6 +716,17 @@
         Return 0 or None if the command was successful, or a non-zero
         shell error code if not.  It's OK for this method to allow
         an exception to raise up.
+
+        This method is automatically wrapped by Command.__init__ with a 
+        cleanup operation, stored as self._operation. This can be used
+        via self.add_cleanup to perform automatic cleanups at the end of
+        run().
+
+        The argument for run are assembled by introspection. So for instance,
+        if your command takes an argument files, you would declare::
+
+            def run(self, files=None):
+                pass
         """
         raise NotImplementedError('no implementation of command %r'
                                   % self.name())

=== modified file 'bzrlib/tests/commands/test_branch.py'
--- a/bzrlib/tests/commands/test_branch.py	2010-02-17 17:11:16 +0000
+++ b/bzrlib/tests/commands/test_branch.py	2010-04-09 03:58:14 +0000
@@ -28,16 +28,16 @@
 
     def test_branch_remote_local(self):
         cmd = cmd_branch()
-        cmd.run_direct(self.get_url('branch'), 'local')
+        cmd.run(self.get_url('branch'), 'local')
         self.assertEquals(1, len(self.connections))
 
     def test_branch_local_remote(self):
         cmd = cmd_branch()
-        cmd.run_direct('branch', self.get_url('remote'))
+        cmd.run('branch', self.get_url('remote'))
         self.assertEquals(1, len(self.connections))
 
     def test_branch_remote_remote(self):
         cmd = cmd_branch()
-        cmd.run_direct(self.get_url('branch'), self.get_url('remote'))
+        cmd.run(self.get_url('branch'), self.get_url('remote'))
         self.assertEquals(2, len(self.connections))
 

=== modified file 'bzrlib/tests/commands/test_cat.py'
--- a/bzrlib/tests/commands/test_cat.py	2010-02-23 07:43:11 +0000
+++ b/bzrlib/tests/commands/test_cat.py	2010-04-09 03:58:14 +0000
@@ -44,7 +44,7 @@
         self.start_logging_connections()
 
         cmd = cmd_cat()
-        cmd.run_direct(self.get_url('branch/foo'))
+        cmd.run(self.get_url('branch/foo'))
         self.assertEquals(1, len(self.connections))
         self.assertEquals('foo', self.outf.getvalue())
 

=== modified file 'bzrlib/tests/commands/test_checkout.py'
--- a/bzrlib/tests/commands/test_checkout.py	2010-02-17 17:11:16 +0000
+++ b/bzrlib/tests/commands/test_checkout.py	2010-04-09 03:58:14 +0000
@@ -27,7 +27,7 @@
         self.start_logging_connections()
 
         cmd = cmd_checkout()
-        cmd.run_direct(self.get_url('branch1'), 'local')
+        cmd.run(self.get_url('branch1'), 'local')
         self.assertEquals(1, len(self.connections))
 
     def test_checkout_lightweight(self):
@@ -36,6 +36,6 @@
         self.start_logging_connections()
 
         cmd = cmd_checkout()
-        cmd.run_direct(self.get_url('branch1'), 'local', lightweight=True)
+        cmd.run(self.get_url('branch1'), 'local', lightweight=True)
         self.assertEquals(1, len(self.connections))
 

=== modified file 'bzrlib/tests/commands/test_commit.py'
--- a/bzrlib/tests/commands/test_commit.py	2010-03-01 19:53:13 +0000
+++ b/bzrlib/tests/commands/test_commit.py	2010-04-09 03:58:14 +0000
@@ -42,7 +42,7 @@
         # commit do not provide a directory parameter, we have to change dir
         # manually
         os.chdir('local')
-        commit.run_direct(message=u'empty commit', unchanged=True)
+        commit.run(message=u'empty commit', unchanged=True)
         self.assertEquals(1, len(self.connections))
 
     def test_commit_both_modified(self):

=== modified file 'bzrlib/tests/commands/test_init.py'
--- a/bzrlib/tests/commands/test_init.py	2010-02-17 17:11:16 +0000
+++ b/bzrlib/tests/commands/test_init.py	2010-04-09 03:58:14 +0000
@@ -30,6 +30,6 @@
         cmd = cmd_init()
         # We don't care about the ouput but 'outf' should be defined
         cmd.outf = tests.StringIOWrapper()
-        cmd.run_direct(self.get_url())
+        cmd.run(self.get_url())
         self.assertEquals(1, len(self.connections))
 

=== modified file 'bzrlib/tests/commands/test_init_repository.py'
--- a/bzrlib/tests/commands/test_init_repository.py	2010-02-17 17:11:16 +0000
+++ b/bzrlib/tests/commands/test_init_repository.py	2010-04-09 03:58:14 +0000
@@ -30,6 +30,6 @@
         cmd = cmd_init_repository()
         # We don't care about the ouput but 'outf' should be defined
         cmd.outf = tests.StringIOWrapper()
-        cmd.run_direct(self.get_url())
+        cmd.run(self.get_url())
         self.assertEquals(1, len(self.connections))
 

=== modified file 'bzrlib/tests/commands/test_merge.py'
--- a/bzrlib/tests/commands/test_merge.py	2010-02-17 17:11:16 +0000
+++ b/bzrlib/tests/commands/test_merge.py	2010-04-09 03:58:14 +0000
@@ -34,6 +34,5 @@
         cmd = cmd_merge()
         # We don't care about the ouput but 'outf' should be defined
         cmd.outf = StringIOWrapper()
-        cmd.run_direct(self.get_url('branch1'), directory='branch2')
+        cmd.run(self.get_url('branch1'), directory='branch2')
         self.assertEquals(1, len(self.connections))
-

=== modified file 'bzrlib/tests/commands/test_missing.py'
--- a/bzrlib/tests/commands/test_missing.py	2010-02-17 17:11:16 +0000
+++ b/bzrlib/tests/commands/test_missing.py	2010-04-09 03:58:14 +0000
@@ -33,6 +33,6 @@
         cmd = cmd_missing()
         # We don't care about the ouput but 'outf' should be defined
         cmd.outf = self.make_utf8_encoded_stringio()
-        cmd.run_direct(self.get_url('branch2'))
+        cmd.run(self.get_url('branch2'))
         self.assertEquals(1, len(self.connections))
 

=== modified file 'bzrlib/tests/commands/test_pull.py'
--- a/bzrlib/tests/commands/test_pull.py	2010-02-17 17:11:16 +0000
+++ b/bzrlib/tests/commands/test_pull.py	2010-04-09 03:58:14 +0000
@@ -35,7 +35,7 @@
         cmd = builtins.cmd_pull()
         # We don't care about the ouput but 'outf' should be defined
         cmd.outf = tests.StringIOWrapper()
-        cmd.run_direct(self.get_url('branch1'), directory='branch2')
+        cmd.run(self.get_url('branch1'), directory='branch2')
         self.assertEquals(1, len(self.connections))
 
     def test_pull_with_bound_branch(self):
@@ -53,6 +53,6 @@
         pull = builtins.cmd_pull()
         # We don't care about the ouput but 'outf' should be defined
         pull.outf = tests.StringIOWrapper()
-        pull.run_direct(self.get_url('remote'), directory='local')
+        pull.run(self.get_url('remote'), directory='local')
         self.assertEquals(1, len(self.connections))
 

=== modified file 'bzrlib/tests/commands/test_push.py'
--- a/bzrlib/tests/commands/test_push.py	2010-02-17 17:11:16 +0000
+++ b/bzrlib/tests/commands/test_push.py	2010-04-09 03:58:14 +0000
@@ -30,7 +30,7 @@
         cmd = cmd_push()
         # We don't care about the ouput but 'outf' should be defined
         cmd.outf = tests.StringIOWrapper()
-        cmd.run_direct(self.get_url('remote'), directory='branch')
+        cmd.run(self.get_url('remote'), directory='branch')
         self.assertEquals(1, len(self.connections))
 
     def test_push_onto_stacked(self):
@@ -41,6 +41,6 @@
 
         cmd = cmd_push()
         cmd.outf = tests.StringIOWrapper()
-        cmd.run_direct(self.get_url('remote'), directory='source',
+        cmd.run(self.get_url('remote'), directory='source',
                 stacked_on=self.get_url('base'))
         self.assertEqual(1, len(self.connections))

=== modified file 'bzrlib/tests/commands/test_update.py'
--- a/bzrlib/tests/commands/test_update.py	2010-02-17 17:11:16 +0000
+++ b/bzrlib/tests/commands/test_update.py	2010-04-09 03:58:14 +0000
@@ -40,6 +40,6 @@
         # update needs the encoding from outf to print URLs
         update.outf = tests.StringIOWrapper()
         # update calls it 'dir' where other commands calls it 'directory'
-        update.run_direct(dir='local')
+        update.run(dir='local')
         self.assertEquals(1, len(self.connections))
 




More information about the bazaar-commits mailing list