Rev 5142: ``bzrlib.commands.Command.run_direct`` is no longer needed - the pre in http://bazaar.launchpad.net/~lifeless/bzr/commands
Robert Collins
robertc at robertcollins.net
Fri Apr 9 04:58:24 BST 2010
At http://bazaar.launchpad.net/~lifeless/bzr/commands
------------------------------------------------------------
revno: 5142
revision-id: robertc at robertcollins.net-20100409035814-eqayfeknoncyoctr
parent: pqm at pqm.ubuntu.com-20100408084859-lbi26gvsu2rtz370
committer: Robert Collins <robertc at robertcollins.net>
branch nick: commands
timestamp: Fri 2010-04-09 13:58:14 +1000
message:
``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)
=== modified file 'NEWS'
--- a/NEWS 2010-04-08 08:48:59 +0000
+++ b/NEWS 2010-04-09 03:58:14 +0000
@@ -57,6 +57,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