Merge lp:~mbp/bzr/cleanup into lp:bzr

Proposed by Martin Pool on 2010-04-27
Status: Merged
Approved by: Martin Pool on 2010-06-15
Approved revision: no longer in the revision history of the source branch.
Merged at revision: 5295
Proposed branch: lp:~mbp/bzr/cleanup
Merge into: lp:bzr
Diff against target: 92 lines (+38/-14)
2 files modified
bzrlib/cleanup.py (+23/-14)
bzrlib/tests/test_cleanup.py (+15/-0)
To merge this branch: bzr merge lp:~mbp/bzr/cleanup
Reviewer Review Type Date Requested Status
Andrew Bennetts 2010-04-27 Approve on 2010-04-27
Review via email: mp+24200@code.launchpad.net

Commit Message

add ObjectWithCleanups

Description of the Change

This lets the cleanup code be used when the cleanups should be implicitly be done through some other operation on the object.

To post a comment you must log in.
lp:~mbp/bzr/cleanup updated on 2010-04-27
5187. By Canonical.com Patch Queue Manager <email address hidden> on 2010-04-27

(vila) Workaround ``Crypto.Random`` check leading to spurious test
 failures

Andrew Bennetts (spiv) wrote :

Hmm. I think this is fine, although it feels half-done to me, because many objects that want cleanup management (e.g. Command, Commit) also want a robust way to run a method and then the cleanups.

So I'm ok with this landing if you find it useful for something, but I anticipate that it will soon grow some equivalent to the run method of OperationWithCleanups too (or perhaps _do_with_cleanups will be promoted to a public function?). Also, the cleanup.py module docstring is pretty focused on the robust-run-with-cleanups use case, so perhaps if the scope is growing to more general cleanup-list management then the docstring should be updated to reflect that.

review: Approve
lp:~mbp/bzr/cleanup updated on 2010-04-30
5188. By Canonical.com Patch Queue Manager <email address hidden> on 2010-04-28

(mbp, for sbjesse) fix news typo

5189. By Canonical.com Patch Queue Manager <email address hidden> on 2010-04-28

(mbp) add ControlComponent base class and standard user_url etc

5190. By Canonical.com Patch Queue Manager <email address hidden> on 2010-04-28

(vila) Add --exclude-common-ancestry log option (Vincent Ladeuil)

5191. By Canonical.com Patch Queue Manager <email address hidden> on 2010-04-28

(vila) Don't mention --no-strict when we just issue the warning about
 unclean trees

5192. By Canonical.com Patch Queue Manager <email address hidden> on 2010-04-28

(lifeless) Wrap os.rename to get better errors on failure. (Martin Pool)

5193. By Canonical.com Patch Queue Manager <email address hidden> on 2010-04-29

(andrew) Add bzrlib.merge.PerFileMerger to make it easier to write
 simple merge_file_content hooks.

5194. By Canonical.com Patch Queue Manager <email address hidden> on 2010-04-30

(Parth Malwankar) Define -p (for patch) as the short form of
 --show-diff in 'bzr commit' (#571461)

5195. By Canonical.com Patch Queue Manager <email address hidden> on 2010-04-30

(spiv) Properly handle param_name attribute for ListOption. (Martin von Gagern)

Vincent Ladeuil (vila) wrote :

@Martin: So, Approved or Work In Progress ?

Martin Pool (mbp) wrote :

It should probably wait until I'm sure it does something I specifically want. Thanks for keeping the queue down.

lp:~mbp/bzr/cleanup updated on 2010-06-15
5196. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-01

(lifeless) Improve the docstring for tree_ignores_add_patterns and remove a magic constant. (Alexander Belchenko)

5197. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-01

(Jelmer) Remove unused imports.

5198. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-03

(spiv) Improve docstring of bzrlib.osutils.relpath (Alexander Belchenko)

5199. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-03

(andrew) Merge lp:bzr/2.1 into lp:bzr, including fix for #528041.

5200. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-03

Update 'bzr bind' help to indicate what happens when no location is
 specified.

5201. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-04

(andrew) Allow repo.refresh_data() to be called in a write group for
 pack format repositories. (#574236)

5202. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-04

(John C Barstow) Bug #401605, document the BZR_LOG env var

5203. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-04

(garyvdm for gagern) Add --directory (-d) option to a number of
 commands.

5204. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-04

(garyvdm for gz) Include OverflowError in error types bencode
 translates,
 as Python 2.4 raises that rather than IndexError in some situations

5205. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-04

(Parth Malwankar) Bug #529930, aliases are stored in UTF-8 config,
 don't decode them in user_encoding.

5206. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-05

(lifeless) Docstring improvements for users of bzrlib. (Robert Collins)

5207. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-05

(doxxx) 'ignore' command avoids putting duplicate entry into
 .bzrignore

5208. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-05

(jbowtie) document additional bzr environment variables

5209. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-05

(garyvdm for amanica) Don't refuse to delete a directory with changed
 emigrated files. (Marius Kruger, Daniel Watkins, #129880)

5210. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-05

(parthm) heavyweight checkout now indicates that history is being copied.

5211. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-05

(garyvdm for gagern) Add a --authors switch to bzr log,
 allowing users to override the choice of authors for all built-in
 formats.

5212. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-05

(garyvdm for bialix) bzr clean-tree should not delete nested bzrdirs.

5213. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-05

bzr does not guess whoami anymore. (parthm, #549310)

5214. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-05

better unicode handling when commit message (-m) is same as unicode
 filename. (parthm, #563646)

5215. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-06

(vila, for jam) Remove zlib dependency

5216. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-07

(parthm, #181124) short options for commonly used ls long options.

5217. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-07

(parthm) trivial docstring fix to indicate when errors.NoWhoami is raised.

5218. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-07

(vila, for jelmer) Pass the colocated branch name along in more places,
 add extra tests.

5219. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-10

(andrew) Add more logging to stub_sftp.SocketListener to help
 diagnose intermittent test failures.

5220. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-10

(robertc) Fix lp: urls behind an https proxy.

5221. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-10

Support normal commit parameters in TreeTransform.commit

5222. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-11

(andrew) Remove pdb.set_trace() call in inventory.py.

5223. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-11

(lifeless) Provide a consistent interface to Tree, Branch,
 Repository where lock methods return an object with an unlock method to
 unlock the lock. This breaks the API for Branch,
 Repository on their lock_write methods. (Robert Collins)

5224. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-11

(lifeless) Fail to construct commands that have no help,
 rather than failing when help is first requested. (Robert Collins)

5225. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-11

(jam) Some chk_map and CHKInventory tweaks to reduce CPU overhead for
 full fetch.

5226. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-12

(andrew) Don't lazy_import errno in osutils.

5227. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-12

(mbp) (mbp) mention babune in testing guide;
 split out review guide (Martin Pool)

5228. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-13

(mbp) (mbp) better message when rename fails inside TreeTransform (Martin
 Pool)

5229. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-13

(abentley) CommitBuilder refuses to commit trees with no root.

5230. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-14

(lifeless) Make our selftest for dangling-lock detection in selftests more
 robust (fixes use with testtools 0.9.3). (Robert Collins)

5231. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-14

(vila,
 Martin [gz]) Make bt._rmtree_temp_dir more robust against non-ascii
 exceptions

5232. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-14

(andrew) Fix RemoteBranchConfig.set_user_option when passed a dict
 value. (#430382)

5233. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-14

(vila, Martin [gz]) More robust stream checking for subunit on windows

5234. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-14

(vila, mgz) Fix sftp homedir path handling on windows

5235. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-14

(vila, mgz) Blue babune for win32 !

5236. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-17

(lifeless) Changes some tests that compare paths to use a fancier method
 than just string comparison, fixing them on Windows. (Martin [gz])

5237. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-18

(lifeless) Unbreak a win32 test that has a test method and attribute with
 the same label. (Martin [gz])

5238. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-18

(lifeless) Explicitly close the file handle in WT.get_file_text. (Tim Penhey)

5239. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-18

(Martin <gz>) Some cleanups for the zlib.crc32 changes

5240. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-19

(lifeless) Replace the unmaintained bzr completion script with gagern's new
 one. (Martin von Gagern)

5241. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-19

(parthm) Estimated records to be fetched are now shown for fetch (2a only).

5242. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-20

(andrew) Fix AttributeError in RemoteBranch.lock_write after
 lock_read. (#582781)

5243. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-20

(lifeless) Merge 2.1 into trunk with a number of fixes including pyrex 0.9.9
 support. (Robert Collins)

5244. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-20

(lifeless) Use the production Launchpad instance for XML-RPC requests (and
 also changes existing bug references to remove .edge from them. (Martin [gz])

5245. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-21

(lifeless) Merge from 2.1,
 fix for closing fd's when a specific file is supplied to status/commit etc.
 (Robert Collins)

5246. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-21

(spiv) Treat pywintypes.error as a user error as per OSError,
 rather than an internal error (Martin [gz])

5247. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-21

(lifeless) Switch lp-propose back to using edge. See bug 583667 for details.
 (Robert Collins)

5248. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-21

(lifeless) Add a --approve to lp-propose for proposals that are
 self-approvable. (Robert Collins)

5249. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-21

(lifeless) Fix deprecation warning in bash_completion plugin. (Andrew
 Bennetts)

5250. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-21

(lifeless) Some cleanups to the readdir pyrex code for a little efficiency
 and to avoid compile warnings. (John A Meinel)

5251. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-23

(lifeless) Adjusted README.txt for bash_completion plugin,
 now that the plugin has been merged with the main tree. (Martin von Gagern)

5252. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-24

(mbp) Better docs on contributing, code reviews, and using bzrlib.

5253. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-24

(lifeless) Fix standalone code blocks in help. (John Szakmeister)

5254. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-24

Fix the type of the recently introduced null option - it should have been
 global, not standard.

5255. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-25

(lifeless) Generalise probing for executables used in selftest. (Martin von
 Gagern)

5256. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-26

(igc) User Reference in PDF

5257. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-26

(lifeless) Explicitly close various file objects used in bzrlib (Martin [gz])

5258. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-26

(lifeless) Fix the display of cmd aliases in the help topic for aliased
 commands. (Parth Malwankar)

5259. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-27

(lifeless) Add a gio based transport using gio+ as a prefix to get at the
 gio logic. (Mattias Eriksson)

5260. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-27

(lifeless) Make the installer on windows strip non-plugin docstrings.
 (Martin [gz])

5261. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-27

(mbp) doc: don't use try/except/finally (Martin Pool)

5262. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-27

(lifeless) Fix gio_transport to be python2.4 syntax compatible. (Robert
 Collins)

5263. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-27

(parthm) (parthm) added tip on explicit file closing to 'Portability Tips'
 section of code-style.txt (Parth Malwankar)

5264. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-27

(lifeless) Release 2.2b3. (Robert Collins)

5265. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-27

(lifeless) Open 2.2b4. (Robert Collins)

5266. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-28

(lifeless) Stop raising at runtime when a command has no help,
 instead have a test in the test suite that checks all known command objects.
 (Robert Collins)

5267. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-28

(Jelmer) This adds {split,
 join}_segment_parameters to urlutils - plumbing for colocated
 branches.

5268. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-28

(lifeless) Improve the release checklist. (Robert Collins)

5269. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-28

(vila) Fix gio-related test failures (Vincent Ladeuil)

5270. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-28

(parthm) NEWS entry added for gio transport support. (snaggen) (Mattias
 Eriksson)

5271. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-30

(Jelmer) Lazily load kerberos support.

5272. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-31

(mbp) test that kerberos isn't loaded (Martin Pool)

5273. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-31

(lifeless) Drop unmaintained zsh completion script. (Martin von Gagern)

5274. By Canonical.com Patch Queue Manager <email address hidden> on 2010-05-31

(lifeless) Add the regression tag to our bug handling docs. (Robert Collins)

5275. By Canonical.com Patch Queue Manager <email address hidden> on 2010-06-01

(Jelmer) Use launchpadlib rather than launchpadbugs in
 check-newsbugs.py.

5276. By Canonical.com Patch Queue Manager <email address hidden> on 2010-06-01

(lifeless) Explicitly removing ``--profile-imports`` option from parsed
 command-line arguments on Windows, because bzr script does the same. (bialix,
 #588277) (Alexander Belchenko)

5277. By Canonical.com Patch Queue Manager <email address hidden> on 2010-06-02

(lifeless) Tab-complete tags with names or at the edge of a revision range.
 Includes bzr-bash-completion 1.2.0. (Martin von Gagern)

5278. By Canonical.com Patch Queue Manager <email address hidden> on 2010-06-02

(andrew) Expand 'Cleanup methods' section of the coding style guide,
 plus fix some formatting nits.

5279. By Canonical.com Patch Queue Manager <email address hidden> on 2010-06-03

(mbp) avoid inaccurate use of "linux" or non-gnu-compliant "open source"
 (Martin Pool)

5280. By Canonical.com Patch Queue Manager <email address hidden> on 2010-06-04

(spiv) Use lazy imports in bzrilb.merge to minimise the startup cost of
 plugins like news_merge. (Andrew Bennetts)

5281. By Canonical.com Patch Queue Manager <email address hidden> on 2010-06-04

(mbp) 'bzr init' does not scan existing directory content anymore. (#501307)
 (Parth Malwankar)

5282. By Canonical.com Patch Queue Manager <email address hidden> on 2010-06-04

(mbp) remove-tree now refuses to run without --force if there are shelved
 changes. (#586639) (Matt Giuca)

5283. By Canonical.com Patch Queue Manager <email address hidden> on 2010-06-04

(mbp) add retrospective news summary for 2.2b1 (Martin Pool)

5284. By Canonical.com Patch Queue Manager <email address hidden> on 2010-06-07

(spiv) Add --directory option to 8 more commands. (Martin von Gagern)

5285. By Canonical.com Patch Queue Manager <email address hidden> on 2010-06-10

(vila) ``bzr log --exclude-common-ancestry`` works for linear
 ancestries

5286. By Canonical.com Patch Queue Manager <email address hidden> on 2010-06-11

(vila) Catch the wrong path descriptions in BZR_PLUGINS_AT

5287. By Canonical.com Patch Queue Manager <email address hidden> on 2010-06-11

(spiv) Fix trivial "was gc'd while locked" warning in
 test_lock_write_raises_in_lock_read. (Andrew Bennetts)

5288. By Canonical.com Patch Queue Manager <email address hidden> on 2010-06-11

(vila) BZR_PLUGINS_AT should import packages properly to handle
 relative imports

5289. By Canonical.com Patch Queue Manager <email address hidden> on 2010-06-11

(Jelmer) Remove a string exceptions.

5290. By Canonical.com Patch Queue Manager <email address hidden> on 2010-06-14

Document branch.get_config

5291. By Canonical.com Patch Queue Manager <email address hidden> on 2010-06-14

Fetch progress bar update now happens every 7 records instead of 71.
 Progress bar is better on slower connections.

5292. By Canonical.com Patch Queue Manager <email address hidden> on 2010-06-14

Improved ``bzrlib.urlutils`` to handle lp:foo/bar URLs.

5293. By Canonical.com Patch Queue Manager <email address hidden> on 2010-06-14

(lifeless) InterBranch improvements needed for bzr-loom support. (Robert
 Collins)

5294. By Canonical.com Patch Queue Manager <email address hidden> on 2010-06-15

(spiv) Fix 'bzr pull' when lp: URL explicitly defined in
 locations.conf or branch.conf. (Gordon Tyler, #534787)

Martin Pool (mbp) wrote :

I did find this useful later, so on the weight of spiv's earlier +Approved I'm going to send it up to pqm.

I agree that often one would want "do this and then clean up", but I think you can also have cases where cleanup_now will be called from eg a tearDown method, and it's somebody else's job to arrange for that to be called.

Martin Pool (mbp) wrote :

sent to pqm by email

lp:~mbp/bzr/cleanup updated on 2010-06-15
5295. By Canonical.com Patch Queue Manager <email address hidden> on 2010-06-15

(mbp) add ObjectWithCleanups (Martin Pool)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bzrlib/cleanup.py'
2--- bzrlib/cleanup.py 2010-03-03 22:59:21 +0000
3+++ bzrlib/cleanup.py 2010-04-27 07:33:23 +0000
4@@ -78,7 +78,28 @@
5 _run_cleanup(func, *args, **kwargs)
6
7
8-class OperationWithCleanups(object):
9+class ObjectWithCleanups(object):
10+ """A mixin for objects that hold a cleanup list.
11+
12+ Subclass or client code can call add_cleanup and then later `cleanup_now`.
13+ """
14+ def __init__(self):
15+ self.cleanups = deque()
16+
17+ def add_cleanup(self, cleanup_func, *args, **kwargs):
18+ """Add a cleanup to run.
19+
20+ Cleanups may be added at any time.
21+ Cleanups will be executed in LIFO order.
22+ """
23+ self.cleanups.appendleft((cleanup_func, args, kwargs))
24+
25+ def cleanup_now(self):
26+ _run_cleanups(self.cleanups)
27+ self.cleanups.clear()
28+
29+
30+class OperationWithCleanups(ObjectWithCleanups):
31 """A way to run some code with a dynamic cleanup list.
32
33 This provides a way to add cleanups while the function-with-cleanups is
34@@ -102,16 +123,8 @@
35 """
36
37 def __init__(self, func):
38+ super(OperationWithCleanups, self).__init__()
39 self.func = func
40- self.cleanups = deque()
41-
42- def add_cleanup(self, cleanup_func, *args, **kwargs):
43- """Add a cleanup to run.
44-
45- Cleanups may be added at any time before or during the execution of
46- self.func. Cleanups will be executed in LIFO order.
47- """
48- self.cleanups.appendleft((cleanup_func, args, kwargs))
49
50 def run(self, *args, **kwargs):
51 return _do_with_cleanups(
52@@ -121,10 +134,6 @@
53 return _do_with_cleanups(
54 self.cleanups, self.func, *args, **kwargs)
55
56- def cleanup_now(self):
57- _run_cleanups(self.cleanups)
58- self.cleanups.clear()
59-
60
61 def _do_with_cleanups(cleanup_funcs, func, *args, **kwargs):
62 """Run `func`, then call all the cleanup_funcs.
63
64=== modified file 'bzrlib/tests/test_cleanup.py'
65--- bzrlib/tests/test_cleanup.py 2010-03-03 22:59:21 +0000
66+++ bzrlib/tests/test_cleanup.py 2010-04-27 07:33:23 +0000
67@@ -20,6 +20,7 @@
68 from bzrlib.cleanup import (
69 _do_with_cleanups,
70 _run_cleanup,
71+ ObjectWithCleanups,
72 OperationWithCleanups,
73 )
74 from bzrlib.tests import TestCase
75@@ -276,3 +277,17 @@
76 [('func called', 'foo'), 'cleanup 1', 'cleanup 2', 'cleanup 3',
77 'cleanup 4'], call_log)
78
79+
80+class SampleWithCleanups(ObjectWithCleanups):
81+
82+ pass
83+
84+
85+class TestObjectWithCleanups(TestCase):
86+
87+ def test_object_with_cleanups(self):
88+ a = []
89+ s = SampleWithCleanups()
90+ s.add_cleanup(a.append, 42)
91+ s.cleanup_now()
92+ self.assertEqual(a, [42])