~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to utilities/update-copyright

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-12-22 04:55:30 UTC
  • mfrom: (14577.1.1 testfix)
  • Revision ID: launchpad@pqm.canonical.com-20111222045530-wki9iu6c0ysqqwkx
[r=wgrant][no-qa] Fix test_publisherconfig lpstorm import. Probably a
        silent conflict between megalint and apocalypse.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
#
 
3
# Copyright 2010 Canonical Ltd.  This software is licensed under the
 
4
# GNU Affero General Public License version 3 (see the file LICENSE).
 
5
 
 
6
"""Update the year in copyright notices.
 
7
 
 
8
This simple script determines the changed files and updates the copyright
 
9
notice to reflect the current year. Looks for the notice in the first three
 
10
lines of the file and leaves the file unchanged if it finds none.
 
11
"""
 
12
 
 
13
from datetime import date
 
14
import os
 
15
import re
 
16
from subprocess import (
 
17
    PIPE,
 
18
    Popen,
 
19
    )
 
20
import sys
 
21
 
 
22
 
 
23
# This script lives in the 'utilites' directory.
 
24
UTILITIES_DIR = os.path.dirname(__file__)
 
25
CURRENT_YEAR = date.today().year
 
26
copyright_pattern = re.compile(
 
27
    "Copyright (?P<years>(?P<yearfrom>[0-9]{4})(-[0-9]{4})?) Canonical Ltd.")
 
28
 
 
29
def years_string(yearfrom):
 
30
    """Build the new years string."""
 
31
    if int(yearfrom) >= CURRENT_YEAR:
 
32
       return yearfrom
 
33
    return "%s-%d" % (yearfrom, CURRENT_YEAR)
 
34
 
 
35
def update_copyright(lines):
 
36
    """Update the copyright notice in the given file lines."""
 
37
    for line in range(min(len(lines), 3)):
 
38
        match = copyright_pattern.search(lines[line])
 
39
        if match is not None:
 
40
            old_years = match.group('years')
 
41
            new_years = years_string(match.group('yearfrom'))
 
42
            if old_years != new_years:
 
43
                lines[line] = lines[line].replace(old_years, new_years)
 
44
                return True
 
45
            return False
 
46
    return False
 
47
 
 
48
 
 
49
def update_files(filenames):
 
50
    """Open the files with the given file names and update them."""
 
51
    for filename in filenames:
 
52
        if not os.path.isfile(filename):
 
53
            print "Skipped: %s does not exist or is not a regular file." %(
 
54
                filename)
 
55
            continue
 
56
        if not os.access(filename, os.W_OK):
 
57
            print "Skipped: %s is not writeable." % filename
 
58
            continue
 
59
        lines = file(filename).readlines()
 
60
        changed = update_copyright(lines)
 
61
        if changed:
 
62
            newfile = open(filename, 'w')
 
63
            newfile.write(''.join(lines))
 
64
            newfile.close()
 
65
            print "Updated: %s" % filename
 
66
        else:
 
67
            print "Unchanged: %s" % filename
 
68
 
 
69
def find_changed_files():
 
70
    """Use the find-changed-files.sh script."""
 
71
    find_changed_files_cmd = [
 
72
        os.path.join(UTILITIES_DIR, 'find-changed-files.sh')]
 
73
    filenames = Popen(find_changed_files_cmd, stdout=PIPE).communicate()[0]
 
74
    return filenames.strip()
 
75
 
 
76
def find_and_update():
 
77
    """Put it all together."""
 
78
    filenames = find_changed_files()
 
79
    if filenames != '':
 
80
        update_files(filenames.split(' '))
 
81
 
 
82
if __name__ == "__main__":
 
83
    if len(sys.argv) < 2:
 
84
        find_and_update()
 
85
    else:
 
86
        update_files(sys.argv[1:])
 
87