3
# Copyright 2010 Canonical Ltd. This software is licensed under the
4
# GNU Affero General Public License version 3 (see the file LICENSE).
6
"""Update the year in copyright notices.
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.
13
from datetime import date
16
from subprocess import (
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.")
29
def years_string(yearfrom):
30
"""Build the new years string."""
31
if int(yearfrom) >= CURRENT_YEAR:
33
return "%s-%d" % (yearfrom, CURRENT_YEAR)
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])
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)
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." %(
56
if not os.access(filename, os.W_OK):
57
print "Skipped: %s is not writeable." % filename
59
lines = file(filename).readlines()
60
changed = update_copyright(lines)
62
newfile = open(filename, 'w')
63
newfile.write(''.join(lines))
65
print "Updated: %s" % filename
67
print "Unchanged: %s" % filename
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()
76
def find_and_update():
77
"""Put it all together."""
78
filenames = find_changed_files()
80
update_files(filenames.split(' '))
82
if __name__ == "__main__":
86
update_files(sys.argv[1:])