Adjust update_copyright.py to handle Bazaar repos + minor fixes
[calibcant.git] / update_copyright.py
index 3e1e268bfccac094578beb21a57855aa0e5a23a5..d1caaa6e7c3ab5cc995174e27628950490eff97f 100755 (executable)
@@ -32,7 +32,6 @@ import email.utils
 import os
 import os.path
 import re
-import StringIO
 import sys
 import time
 
@@ -159,7 +158,6 @@ if PROJECT_INFO['vcs'] == 'Git':
         return stdout.rstrip('\n')
         
     def original_year(filename, year_hacks=YEAR_HACKS):
-        # shortdate filter: YEAR-MONTH-DAY
         output = git_cmd('log', '--follow',
                          '--format=format:%ad',  # Author date
                          '--date=short',         # YYYY-MM-DD
@@ -193,6 +191,10 @@ if PROJECT_INFO['vcs'] == 'Git':
 
 elif PROJECT_INFO['vcs'] == 'Mercurial':
 
+    import StringIO
+    import mercurial
+    import mercurial.dispatch
+
     def mercurial_cmd(*args):
         cwd = os.getcwd()
         stdout = sys.stdout
@@ -222,7 +224,8 @@ elif PROJECT_INFO['vcs'] == 'Mercurial':
         return years[0]
     
     def authors(filename, author_hacks=AUTHOR_HACKS):
-        output,error = mercurial_cmd('log', '-template', '{author}\n',
+        output,error = mercurial_cmd('log', '--follow',
+                                     '--template', '{author}\n',
                                      filename)
         ret = list(set(output.splitlines()))
         if splitpath(filename) in author_hacks:
@@ -230,8 +233,7 @@ elif PROJECT_INFO['vcs'] == 'Mercurial':
         return ret
     
     def authors_list(author_hacks=AUTHOR_HACKS):
-        output,error = mercurial_cmd('log', '--follow',
-                                     '--template', '{author}\n')
+        output,error = mercurial_cmd('log', '--template', '{author}\n')
         ret = list(set(output.splitlines()))
         for path,authors in author_hacks.items():
             ret.extend(authors)
@@ -244,7 +246,71 @@ elif PROJECT_INFO['vcs'] == 'Mercurial':
         return True
 
 elif PROJECT_INFO['vcs'] == 'Bazaar':
-    pass
+
+    import StringIO
+    import bzrlib
+    import bzrlib.builtins
+    import bzrlib.errors
+    import bzrlib.log
+
+    class LogFormatter (bzrlib.log.LogFormatter):
+        supports_merge_revisions = True
+        preferred_levels = 0
+        supports_deta = False
+        supports_tags = False
+        supports_diff = False
+
+        def log_revision(self, revision):
+            raise NotImplementedError
+
+    class YearLogFormatter (LogFormatter):
+        def log_revision(self, revision):
+            self.to_file.write(
+                time.strftime('%Y', time.gmtime(revision.rev.timestamp))
+                +'\n')
+
+    class AuthorLogFormatter (LogFormatter):
+        def log_revision(self, revision):
+            authors = revision.rev.get_apparent_authors()
+            self.to_file.write('\n'.join(authors)+'\n')
+
+    def original_year(filename, year_hacks=YEAR_HACKS):
+        cmd = bzrlib.builtins.cmd_log()
+        cmd.outf = StringIO.StringIO()
+        cmd.run(file_list=[filename], log_format=YearLogFormatter, levels=0)
+        years = [int(year) for year in set(cmd.outf.getvalue().splitlines())]
+        if splitpath(filename) in year_hacks:
+            years.append(year_hacks[splitpath(filename)])
+        years.sort()
+        return years[0]
+    
+    def authors(filename, author_hacks=AUTHOR_HACKS):
+        cmd = bzrlib.builtins.cmd_log()
+        cmd.outf = StringIO.StringIO()
+        cmd.run(file_list=[filename], log_format=AuthorLogFormatter, levels=0)
+        ret = list(set(cmd.outf.getvalue().splitlines()))
+        if splitpath(filename) in author_hacks:
+            ret.extend(author_hacks[splitpath(filename)])
+        return ret
+    
+    def authors_list(author_hacks=AUTHOR_HACKS):
+        cmd = bzrlib.builtins.cmd_log()
+        cmd.outf = StringIO.StringIO()
+        cmd.run(log_format=AuthorLogFormatter, levels=0)
+        output = cmd.outf.getvalue()
+        ret = list(set(cmd.outf.getvalue().splitlines()))
+        for path,authors in author_hacks.items():
+            ret.extend(authors)
+        return ret
+    
+    def is_versioned(filename):
+        cmd = bzrlib.builtins.cmd_log()
+        cmd.outf = StringIO.StringIO()
+        try:
+            cmd.run(file_list=[filename])
+        except bzrlib.errors.BzrCommandError:
+            return False
+        return True
 
 else:
     raise NotImplementedError('Unrecognized VCS: %(vcs)s' % PROJECT_INFO)
@@ -561,5 +627,3 @@ automatically.
 
     update_authors(dry_run=options.dry_run, verbose=options.verbose)
     update_files(files=args, dry_run=options.dry_run, verbose=options.verbose)
-
-#  LocalWords:  difflib