← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~henninge/launchpad/devel-update-copyright into lp:launchpad/devel

 

Henning Eggers has proposed merging lp:~henninge/launchpad/devel-update-copyright into lp:launchpad/devel.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers): code


Adds a little script that updates the year information in the copyright header of all changed files. This should make it easy to keep them up to date.

I extracted the code that finds changed files using bzr from lint.sh, which is used by 'make lint'.

No test, this is just a little helper script. Use "bzr diff" after a run to see that it worked.
-- 
https://code.launchpad.net/~henninge/launchpad/devel-update-copyright/+merge/36701
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~henninge/launchpad/devel-update-copyright into lp:launchpad/devel.
=== modified file 'buildout-templates/bin/lint.sh.in'
--- buildout-templates/bin/lint.sh.in	2010-07-17 21:42:06 +0000
+++ buildout-templates/bin/lint.sh.in	2010-09-27 11:57:50 +0000
@@ -1,6 +1,6 @@
 #!/bin/bash
 #
-# Copyright 2009 Canonical Ltd.  This software is licensed under the
+# Copyright 2009-2010 Canonical Ltd.  This software is licensed under the
 # GNU Affero General Public License version 3 (see the file LICENSE).
 #
 # Runs pocketlint on files changed from parent branch.
@@ -12,47 +12,9 @@
 [ -z "$utilitiesdir" ] && utilitiesdir=.
 
 
-bzr() {
-    # For pylint to operate properly, PYTHONPATH must point to the ./lib
-    # directory in the launchpad tree. This directory includes a bzrlib. When
-    # this script calls bzr, we want it to use the system bzrlib, not the one
-    # in the launchpad tree.
-    PYTHONPATH='' `which bzr` "$@"
-}
-
-
 if [ -z "$1" ]; then
-    # No command line argument provided, use the default logic.
-    bzr diff > /dev/null
-    diff_status=$?
-    if [ $diff_status -eq 0 ] ; then
-        # No uncommitted changes in the tree.
-        bzr status | grep "^Current thread:" > /dev/null
-        if [ $? -eq 0 ] ; then
-            # This is a loom, lint changes relative to the lower thread.
-            rev_option="-r thread:"
-        else
-            if test "$(bzr pipes | sed -n -e "/^\\*/q;p" | wc -l)" -gt 0; then
-                # This is a pipeline with at least one pipe before the
-                # current, lint changes relative to the previous pipe
-                rev_option="-r ancestor::prev"
-            else
-                # Lint changes relative to the parent.
-                rev=`bzr info | sed \
-                    '/parent branch:/!d; s/ *parent branch: /ancestor:/'`
-                rev_option="-r $rev"
-            fi
-        fi
-    elif [ $diff_status -eq 1 ] ; then
-        # Uncommitted changes in the tree, lint those changes.
-        rev_option=""
-    else
-        # bzr diff failed
-        exit 1
-    fi
-    # Extract filename from status line.  Strip the @ that mark symlinks.
-    files=`bzr st --short $rev_option |
-        sed -e '/^.[MN]/!d; s/.* //' -e 's/@$//'`
+    # No command line argument provided, lint all changed files.
+    files=$($utilitiesdir/find-changed-files.sh)
 else
     # Add newlines so grep filters out pyfiles correctly later.
     files=`echo $* | tr " " "\n"`

=== added file 'utilities/find-changed-files.sh'
--- utilities/find-changed-files.sh	1970-01-01 00:00:00 +0000
+++ utilities/find-changed-files.sh	2010-09-27 11:57:50 +0000
@@ -0,0 +1,45 @@
+#!/bin/bash
+#
+# Copyright 2009-2010 Canonical Ltd.  This software is licensed under the
+# GNU Affero General Public License version 3 (see the file LICENSE).
+#
+# Determine the changed files in Bazaar piplines, looms and plain branches.
+
+bzr() {
+    # PYTHONPATH may point to the ./lib directory in the launchpad tree. This
+    # directory includes a bzrlib. When this script calls bzr, we want it to
+    # use the system bzrlib, not the one in the launchpad tree.
+    PYTHONPATH='' `which bzr` "$@"
+}
+
+bzr diff > /dev/null
+diff_status=$?
+if [ $diff_status -eq 0 ] ; then
+    # No uncommitted changes in the tree.
+    bzr status | grep "^Current thread:" > /dev/null
+    if [ $? -eq 0 ] ; then
+        # This is a loom, lint changes relative to the lower thread.
+        rev_option="-r thread:"
+    elif [ "$(bzr pipes | sed -n -e "/^\\*/q;p" | wc -l)" -gt 0 ]; then
+        # This is a pipeline with at least one pipe before the
+        # current, lint changes relative to the previous pipe
+        rev_option="-r ancestor::prev"
+    else
+        # Lint changes relative to the parent.
+        rev=`bzr info | sed \
+            '/parent branch:/!d; s/ *parent branch: /ancestor:/'`
+        rev_option="-r $rev"
+    fi
+elif [ $diff_status -eq 1 ] ; then
+    # Uncommitted changes in the tree, return those files.
+    rev_option=""
+else
+    # bzr diff failed
+    exit 1
+fi
+# Extract filename from status line.  Strip the @ that mark symlinks.
+files=`bzr st --short $rev_option |
+    sed -e '/^.[MN]/!d; s/.* //' -e 's/@$//'`
+
+echo $files
+

=== added file 'utilities/update-copyright'
--- utilities/update-copyright	1970-01-01 00:00:00 +0000
+++ utilities/update-copyright	2010-09-27 11:57:50 +0000
@@ -0,0 +1,86 @@
+#!/usr/bin/python
+#
+# Copyright 2010 Canonical Ltd.  This software is licensed under the
+# GNU Affero General Public License version 3 (see the file LICENSE).
+
+"""Update the year in copyright notices.
+
+This simple script determines the changed files and updates the copyright
+notice to reflect the current year. Looks for the notice in the first three
+lines of the file and leaves the file unchanged if it finds none.
+"""
+
+from datetime import date
+import os
+import re
+from subprocess import (
+    PIPE,
+    Popen,
+    )
+import sys
+
+
+# This script lives in the 'utilites' directory.
+UTILITIES_DIR = os.path.dirname(__file__)
+CURRENT_YEAR = date.today().year
+copyright_pattern = re.compile(
+    "Copyright (?P<years>(?P<yearfrom>[0-9]{4})(-[0-9]{4})?) Canonical Ltd.")
+
+def years_string(yearfrom):
+    """Build the new years string."""
+    if int(yearfrom) >= CURRENT_YEAR:
+       return yearfrom
+    return "%s-%d" % (yearfrom, CURRENT_YEAR)
+
+def update_copyright(lines):
+    """Update the copyright notice in the given file lines."""
+    for line in range(min(len(lines), 3)):
+        match = copyright_pattern.search(lines[line])
+        if match is not None:
+            old_years = match.group('years')
+            new_years = years_string(match.group('yearfrom'))
+            if old_years != new_years:
+                lines[line] = lines[line].replace(old_years, new_years)
+                return True
+            return False
+    return False
+
+
+def update_files(filenames):
+    """Open the files with the given file names and update them."""
+    for filename in filenames:
+        if not os.path.isfile(filename):
+            print "Skipped: %s does not exist or is not a regular file." % filename
+            continue
+        if not os.access(filename, os.W_OK):
+            print "Skipped: %s is not writeable." % filename
+            continue
+        lines = file(filename).readlines()
+        changed = update_copyright(lines)
+        if changed:
+            newfile = open(filename, 'w')
+            newfile.write(''.join(lines))
+            newfile.close()
+            print "Updated: %s" % filename
+        else:
+            print "Unchanged: %s" % filename
+
+def find_changed_files():
+    """Use the find-changed-files.sh script."""
+    find_changed_files_cmd = [
+        os.path.join(UTILITIES_DIR, 'find-changed-files.sh')]
+    filenames = Popen(find_changed_files_cmd, stdout=PIPE).communicate()[0]
+    return filenames.strip()
+
+def find_and_update():
+    """Put it all together."""
+    filenames = find_changed_files()
+    if filenames != '':
+        update_files(filenames.split(' '))
+
+if __name__ == "__main__":
+    if len(sys.argv) < 2:
+        find_and_update()
+    else:
+        update_files(sys.argv[1:])
+