~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/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:])