Rev 2: Refactor so that we can recurse and find files that need to be updated. in http://bzr.arbash-meinel.com/plugins/update_copyright

John Arbash Meinel john at arbash-meinel.com
Thu Jan 7 20:00:02 GMT 2010


At http://bzr.arbash-meinel.com/plugins/update_copyright

------------------------------------------------------------
revno: 2
revision-id: john at arbash-meinel.com-20100107195957-cjiwtx0iuhu0o5xf
parent: john at arbash-meinel.com-20100107194456-7chqyxethzihlcsx
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: update_copyright
timestamp: Thu 2010-01-07 13:59:57 -0600
message:
  Refactor so that we can recurse and find files that need to be updated.
-------------- next part --------------
=== modified file '__init__.py'
--- a/__init__.py	2010-01-07 19:44:56 +0000
+++ b/__init__.py	2010-01-07 19:59:57 +0000
@@ -46,18 +46,7 @@
             relpaths = None
         if relpaths is not None:
             relpaths = osutils.minimum_path_selection(relpaths)
-        tree.lock_write()
-        try:
-            basis_tree = tree.basis_tree()
-            basis_tree.lock_read()
-            try:
-                for relpath in relpaths:
-                    update_copyright.update_copyright(tree, basis_tree,
-                                                      relpath)
-            finally:
-                basis_tree.unlock()
-        finally:
-            tree.unlock()
+        update_copyright.update_tree_copyright(tree, relpaths)
 
 
 commands.register_command(cmd_update_copyright)

=== modified file 'test_update_copyright.py'
--- a/test_update_copyright.py	2010-01-07 19:44:56 +0000
+++ b/test_update_copyright.py	2010-01-07 19:59:57 +0000
@@ -94,6 +94,22 @@
             t.commit('fix copyright', timestamp=1231353865, timezone=0)) # 2009
         return t, rev_ids
 
+    def make_tree_with_dirs(self):
+        t, rev_ids = self.make_old_modified_tree()
+        self.build_tree(['dir/'])
+        self.build_tree_contents([('dir/file', 'Copyright 2008 Foo\r\n')])
+        t.add(['dir', 'dir/file'], ['dir-id', 'dfile-id'])
+        rev_ids.append(
+            t.commit('dir and file', timestamp=1199817865, timezone=0)) # 2008
+        self.build_tree(['dir/subdir/'])
+        self.build_tree_contents([
+            ('dir/file', 'Copyright 2008 Foo\r\nnew content\r\n'),
+            ('dir/subdir/foo', 'Copyright 2008 Bar\ncontent\n')])
+        t.add(['dir/subdir', 'dir/subdir/foo'], ['subdir-id', 'foo-id'])
+        rev_ids.append(
+            t.commit('mods', timestamp=1231353866, timezone=0)) # 2009
+        return t, rev_ids
+
     def test_get_years(self):
         t, rev_ids = self.make_old_modified_tree()
         self.assertEqual([2008],
@@ -123,6 +139,35 @@
         self.assertFileEqual('Copyright (c) 2008, 2009 Foo Bar\n'
                              'different content\n', 'file')
 
+    def test_update_tree_one(self):
+        t, rev_ids = self.make_old_modified_tree()
+        update_copyright.update_tree_copyright(t, ['file'])
+        year = datetime.datetime.now().year
+        self.assertFileEqual('Copyright (c) 2008, 2009, %d Foo Bar\n'
+                             'different content\n' % (year,), 'file')
+
+    def test_update_tree_all(self):
+        t, rev_ids = self.make_tree_with_dirs()
+        update_copyright.update_tree_copyright(t, None)
+        year = datetime.datetime.now().year
+        self.assertFileEqual('Copyright (c) 2008, 2009, %d Foo Bar\n'
+                             'different content\n' % (year,), 'file')
+        self.assertFileEqual('Copyright 2008, 2009, %d Foo\r\n'
+                             'new content\r\n' % (year,), 'dir/file')
+        self.assertFileEqual('Copyright 2009, %d Bar\n'
+                             'content\n' % (year,), 'dir/subdir/foo')
+
+    def test_update_tree_subdir(self):
+        t, rev_ids = self.make_tree_with_dirs()
+        update_copyright.update_tree_copyright(t, ['dir/subdir'])
+        year = datetime.datetime.now().year
+        self.assertFileEqual('Copyright (c) 2008 Foo Bar\n'
+                             'different content\n', 'file')
+        self.assertFileEqual('Copyright 2008 Foo\r\n'
+                             'new content\r\n', 'dir/file')
+        self.assertFileEqual('Copyright 2009, %d Bar\n'
+                             'content\n' % (year,), 'dir/subdir/foo')
+
     def test_blackbox(self):
         t, rev_ids = self.make_old_modified_tree()
         # We need to unlock so that run_bzr can grab a lock

=== modified file 'update_copyright.py'
--- a/update_copyright.py	2010-01-07 19:44:56 +0000
+++ b/update_copyright.py	2010-01-07 19:59:57 +0000
@@ -52,6 +52,23 @@
     return ', '.join(map(str, sorted(years)))
 
 
+def update_tree_copyright(tree, relpaths):
+    """Update all of the paths specified in the tree."""
+    tree.lock_write()
+    try:
+        basis_tree = tree.basis_tree()
+        basis_tree.lock_read()
+        try:
+            file_ids = tree.paths2ids(relpaths, trees=[basis_tree])
+            for path, ie in tree.iter_entries_by_dir(file_ids):
+                if ie.kind == 'file':
+                    update_copyright(tree, basis_tree, path)
+        finally:
+            basis_tree.unlock()
+    finally:
+        tree.unlock()
+
+
 def update_copyright(tree, basis_tree, filename):
     """Update the copyright line for a given file."""
     file_id = tree.path2id(filename)



More information about the bazaar-commits mailing list