Rev 3: Build up stats while running. in http://bzr.arbash-meinel.com/plugins/update_copyright

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


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

------------------------------------------------------------
revno: 3
revision-id: john at arbash-meinel.com-20100107200850-e5155e13n2elnx2f
parent: john at arbash-meinel.com-20100107195957-cjiwtx0iuhu0o5xf
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: update_copyright
timestamp: Thu 2010-01-07 14:08:50 -0600
message:
  Build up stats while running.
-------------- next part --------------
=== modified file 'test_update_copyright.py'
--- a/test_update_copyright.py	2010-01-07 19:59:57 +0000
+++ b/test_update_copyright.py	2010-01-07 20:08:50 +0000
@@ -119,36 +119,59 @@
                          sorted(update_copyright.get_years(t.branch.repository,
                                 'file-id', rev_ids[1])))
 
+    def test_not_versioned(self):
+        t, rev_ids = self.make_old_modified_tree()
+        bt = t.basis_tree()
+        bt.lock_read()
+        self.addCleanup(bt.unlock)
+        self.assertEqual('not-versioned',
+                         update_copyright.update_copyright(t, bt, 'bar'))
+
     def test_update_copyright(self):
         t, rev_ids = self.make_old_modified_tree()
         bt = t.basis_tree()
         bt.lock_read()
         self.addCleanup(bt.unlock)
-        update_copyright.update_copyright(t, bt, 'file')
+        self.assertEqual('updated',
+                         update_copyright.update_copyright(t, bt, 'file'))
         year = datetime.datetime.now().year
         self.assertFileEqual('Copyright (c) 2008, 2009, %d Foo Bar\n'
                              'different content\n' % (year,), 'file')
 
+    def test_no_copyright(self):
+        t, rev_ids = self.make_old_modified_tree()
+        self.build_tree_contents([('file', 'No cc line\n')])
+        bt = t.basis_tree()
+        bt.lock_read()
+        self.addCleanup(bt.unlock)
+        self.assertEqual('no-copyright',
+                         update_copyright.update_copyright(t, bt, 'file'))
+        self.assertFileEqual('No cc line\n', 'file')
+
     def test_noop_update(self):
         t, rev_ids = self.make_old_modified_but_correct_tree()
         bt = t.basis_tree()
         bt.lock_read()
         self.addCleanup(bt.unlock)
-        update_copyright.update_copyright(t, bt, 'file')
+        self.assertEqual('copyright-correct',
+                         update_copyright.update_copyright(t, bt, 'file'))
         # Should not at *this* year, because the copyright is already correct
         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'])
+        res = 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')
+        self.assertEqual({'count': 1, 'updated': 1, 'no-copyright': 0,
+                          'copyright-correct': 0, 'not-versioned': 0},
+                         res)
 
     def test_update_tree_all(self):
         t, rev_ids = self.make_tree_with_dirs()
-        update_copyright.update_tree_copyright(t, None)
+        res = 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')
@@ -156,10 +179,13 @@
                              'new content\r\n' % (year,), 'dir/file')
         self.assertFileEqual('Copyright 2009, %d Bar\n'
                              'content\n' % (year,), 'dir/subdir/foo')
+        self.assertEqual({'count': 3, 'updated': 3, 'no-copyright': 0,
+                          'copyright-correct': 0, 'not-versioned': 0},
+                         res)
 
     def test_update_tree_subdir(self):
         t, rev_ids = self.make_tree_with_dirs()
-        update_copyright.update_tree_copyright(t, ['dir/subdir'])
+        res = 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')
@@ -167,6 +193,9 @@
                              'new content\r\n', 'dir/file')
         self.assertFileEqual('Copyright 2009, %d Bar\n'
                              'content\n' % (year,), 'dir/subdir/foo')
+        self.assertEqual({'count': 1, 'updated': 1, 'no-copyright': 0,
+                          'copyright-correct': 0, 'not-versioned': 0},
+                         res)
 
     def test_blackbox(self):
         t, rev_ids = self.make_old_modified_tree()

=== modified file 'update_copyright.py'
--- a/update_copyright.py	2010-01-07 19:59:57 +0000
+++ b/update_copyright.py	2010-01-07 20:08:50 +0000
@@ -54,6 +54,12 @@
 
 def update_tree_copyright(tree, relpaths):
     """Update all of the paths specified in the tree."""
+    result = {'not-versioned': 0,
+              'no-copyright': 0,
+              'copyright-correct': 0,
+              'updated': 0,
+              'count': 0,
+             }
     tree.lock_write()
     try:
         basis_tree = tree.basis_tree()
@@ -62,25 +68,28 @@
             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)
+                    result['count'] += 1
+                    action = update_copyright(tree, basis_tree, path)
+                    result[action] += 1
         finally:
             basis_tree.unlock()
     finally:
         tree.unlock()
+    return result
 
 
 def update_copyright(tree, basis_tree, filename):
     """Update the copyright line for a given file."""
     file_id = tree.path2id(filename)
     if file_id is None:
-        return # Not versioned
+        return 'not-versioned'
     this_year = datetime.datetime.now().year
     f = tree.get_file(file_id, path=filename)
     try:
         copyright_line = f.readline()
         m = copyright_re.match(copyright_line)
         if m is None:
-            return # No copyright line
+            return 'no-copyright'
         # Determine the actual dates that this file was modified
         # There really should be a better way to determine the revision of a
         # file in a tree
@@ -93,7 +102,7 @@
         new_copyright_line = '%s %s %s\n' % (
             m.group('prefix').rstrip(), format_years(years), m.group('suffix'))
         if copyright_line == new_copyright_line:
-            return # Copyright already correct
+            return 'copyright-correct'
         if this_year not in years:
             # We are modifying it today, so mark it as such
             years.add(this_year)
@@ -107,3 +116,4 @@
     #       all of the various content. This might also mess up
     #       'executable' bits, which TT would get correct
     tree.put_file_bytes_non_atomic(file_id, new_content)
+    return 'updated'



More information about the bazaar-commits mailing list