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 |
||
23 |
# This script lives in the 'utilites' directory.
|
|
11636.1.5
by Henning Eggers
Speling |
24 |
UTILITIES_DIR = os.path.dirname(__file__) |
11636.1.2
by Henning Eggers
Created update-copyright script. |
25 |
CURRENT_YEAR = date.today().year |
26 |
copyright_pattern = re.compile( |
|
11636.1.3
by Henning Eggers
Fixed script |
27 |
"Copyright (?P<years>(?P<yearfrom>[0-9]{4})(-[0-9]{4})?) Canonical Ltd.") |
11636.1.2
by Henning Eggers
Created update-copyright script. |
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)): |
|
11636.1.3
by Henning Eggers
Fixed script |
38 |
match = copyright_pattern.search(lines[line]) |
11636.1.2
by Henning Eggers
Created update-copyright script. |
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): |
|
11636.1.6
by Henning Eggers
Long line. |
53 |
print "Skipped: %s does not exist or is not a regular file." %( |
54 |
filename) |
|
11636.1.2
by Henning Eggers
Created update-copyright script. |
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: |
|
11636.1.3
by Henning Eggers
Fixed script |
62 |
newfile = open(filename, 'w') |
11636.1.2
by Henning Eggers
Created update-copyright script. |
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 = [ |
|
11636.1.5
by Henning Eggers
Speling |
72 |
os.path.join(UTILITIES_DIR, 'find-changed-files.sh')] |
11636.1.2
by Henning Eggers
Created update-copyright script. |
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 |