~launchpad-pqm/launchpad/devel

11636.1.2 by Henning Eggers
Created update-copyright script.
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
11636.1.3 by Henning Eggers
Fixed script
16
from subprocess import (
17
    PIPE,
18
    Popen,
19
    )
11636.1.2 by Henning Eggers
Created update-copyright script.
20
import sys
21
22
# This script lives in the 'utilites' directory.
11636.1.5 by Henning Eggers
Speling
23
UTILITIES_DIR = os.path.dirname(__file__)
11636.1.2 by Henning Eggers
Created update-copyright script.
24
CURRENT_YEAR = date.today().year
25
copyright_pattern = re.compile(
11636.1.3 by Henning Eggers
Fixed script
26
    "Copyright (?P<years>(?P<yearfrom>[0-9]{4})(-[0-9]{4})?) Canonical Ltd.")
11636.1.2 by Henning Eggers
Created update-copyright script.
27
28
def years_string(yearfrom):
29
    """Build the new years string."""
30
    if int(yearfrom) >= CURRENT_YEAR:
31
       return yearfrom
32
    return "%s-%d" % (yearfrom, CURRENT_YEAR)
33
34
def update_copyright(lines):
35
    """Update the copyright notice in the given file lines."""
36
    for line in range(min(len(lines), 3)):
11636.1.3 by Henning Eggers
Fixed script
37
        match = copyright_pattern.search(lines[line])
11636.1.2 by Henning Eggers
Created update-copyright script.
38
        if match is not None:
39
            old_years = match.group('years')
40
            new_years = years_string(match.group('yearfrom'))
41
            if old_years != new_years:
42
                lines[line] = lines[line].replace(old_years, new_years)
43
                return True
44
            return False
45
    return False
46
47
48
def update_files(filenames):
49
    """Open the files with the given file names and update them."""
50
    for filename in filenames:
51
        if not os.path.isfile(filename):
11636.1.6 by Henning Eggers
Long line.
52
            print "Skipped: %s does not exist or is not a regular file." %(
53
                filename)
11636.1.2 by Henning Eggers
Created update-copyright script.
54
            continue
55
        if not os.access(filename, os.W_OK):
56
            print "Skipped: %s is not writeable." % filename
57
            continue
58
        lines = file(filename).readlines()
59
        changed = update_copyright(lines)
60
        if changed:
11636.1.3 by Henning Eggers
Fixed script
61
            newfile = open(filename, 'w')
11636.1.2 by Henning Eggers
Created update-copyright script.
62
            newfile.write(''.join(lines))
63
            newfile.close()
64
            print "Updated: %s" % filename
65
        else:
66
            print "Unchanged: %s" % filename
67
68
def find_changed_files():
69
    """Use the find-changed-files.sh script."""
70
    find_changed_files_cmd = [
11636.1.5 by Henning Eggers
Speling
71
        os.path.join(UTILITIES_DIR, 'find-changed-files.sh')]
11636.1.2 by Henning Eggers
Created update-copyright script.
72
    filenames = Popen(find_changed_files_cmd, stdout=PIPE).communicate()[0]
73
    return filenames.strip()
74
75
def find_and_update():
76
    """Put it all together."""
77
    filenames = find_changed_files()
78
    if filenames != '':
79
        update_files(filenames.split(' '))
80
81
if __name__ == "__main__":
82
    if len(sys.argv) < 2:
83
        find_and_update()
84
    else:
85
        update_files(sys.argv[1:])
86