1 |
To retain the behaviour of echangelog, update the copyrights on modified |
2 |
files (mostly ebuilds) when necessary. We also use this to update the |
3 |
ChangeLog's copyright. |
4 |
|
5 |
diff --git a/pym/repoman/utilities.py b/pym/repoman/utilities.py |
6 |
--- a/pym/repoman/utilities.py |
7 |
+++ b/pym/repoman/utilities.py |
8 |
@@ -523,9 +523,84 @@ |
9 |
|
10 |
return outvcs |
11 |
|
12 |
+def update_copyrights(pkgdir, files, year, pretend): |
13 |
+ """ |
14 |
+ Check list of files for Copyright statements, and update them to |
15 |
+ year. The patterns used for replacing copyrights are taken from |
16 |
+ echangelog. Only the first lines of each file that start with a |
17 |
+ hash ('#') are considered, until a line is found that doesn't start |
18 |
+ with a hash. |
19 |
+ patches and diffs (identified by .patch and .diff) are excluded |
20 |
+ """ |
21 |
+ |
22 |
+ for fn in files: |
23 |
+ if fn.endswith('.diff') or fn.endswith('.patch'): |
24 |
+ continue |
25 |
+ |
26 |
+ fn_path = os.path.join(pkgdir, fn) |
27 |
+ try: |
28 |
+ fn_hdl = io.open(_unicode_encode(fn_path, |
29 |
+ encoding=_encodings['fs'], errors='strict'), |
30 |
+ mode='r', encoding=_encodings['repo.content'], errors='replace') |
31 |
+ except EnvironmentError: |
32 |
+ continue |
33 |
+ |
34 |
+ orig_header = [] |
35 |
+ new_header = [] |
36 |
+ |
37 |
+ for line in fn_hdl: |
38 |
+ line_strip = line.strip() |
39 |
+ orig_header.append(line) |
40 |
+ if not line_strip or line_strip[:1] != '#': |
41 |
+ new_header.append(line) |
42 |
+ break |
43 |
+ |
44 |
+ # these two regexes are taken from |
45 |
+ # echangelog update_copyright() |
46 |
+ line = re.sub(r'^(# Copyright \d+) ', |
47 |
+ r'\1-%s ' % year, line) |
48 |
+ line = re.sub(r'^(# Copyright) \d\d\d\d-\d\d\d\d', |
49 |
+ r'\1 1999-%s' % year, line) |
50 |
+ new_header.append(line) |
51 |
+ |
52 |
+ difflines = 0 |
53 |
+ for line in difflib.unified_diff(orig_header, new_header, |
54 |
+ fromfile=fn_path, tofile=fn_path, n=0): |
55 |
+ util.writemsg_stdout(line, noiselevel=-1) |
56 |
+ difflines += 1 |
57 |
+ util.writemsg_stdout("\n", noiselevel=-1) |
58 |
+ |
59 |
+ # unified diff has three lines to start with |
60 |
+ if difflines > 3 and not pretend: |
61 |
+ # write new file with changed header |
62 |
+ f, fnnew_path = mkstemp() |
63 |
+ f = io.open(f, mode='w', encoding=_encodings['repo.content'], |
64 |
+ errors='backslashreplace') |
65 |
+ for line in new_header: |
66 |
+ f.write(line); |
67 |
+ for line in fn_hdl: |
68 |
+ f.write(line) |
69 |
+ f.close() |
70 |
+ try: |
71 |
+ fn_stat = os.stat(fn_path) |
72 |
+ except OSError: |
73 |
+ fn_stat = None |
74 |
+ |
75 |
+ shutil.move(fnnew_path, fn_path) |
76 |
+ |
77 |
+ if fn_stat is None: |
78 |
+ util.apply_permissions(fn_path, mode=0o644) |
79 |
+ else: |
80 |
+ util.apply_stat_permissions(fn_path, fn_stat) |
81 |
+ fn_hdl.close() |
82 |
+ |
83 |
def UpdateChangeLog(pkgdir, category, package, new, removed, changed, \ |
84 |
msg, pretend, repodir): |
85 |
- """ Write an entry to an existing ChangeLog, or create a new one. """ |
86 |
+ """ |
87 |
+ Write an entry to an existing ChangeLog, or create a new one. |
88 |
+ Updates copyright year on changed files, and updates the header of |
89 |
+ ChangeLog with the contents of skel.ChangeLog. |
90 |
+ """ |
91 |
|
92 |
# figure out who to write as |
93 |
if 'GENTOO_COMMITTER_NAME' in os.environ and \ |
94 |
@@ -548,6 +623,12 @@ |
95 |
logging.critical(err) |
96 |
return None |
97 |
|
98 |
+ year = time.strftime('%Y') |
99 |
+ date = time.strftime('%d %b %Y') |
100 |
+ |
101 |
+ # check modified files and the ChangeLog for copyright updates |
102 |
+ update_copyrights(pkgdir, new + changed + ['ChangeLog'], year, pretend) |
103 |
+ |
104 |
cl_path = os.path.join(pkgdir, 'ChangeLog') |
105 |
clold_lines = [] |
106 |
clnew_lines = [] |
107 |
@@ -578,7 +659,8 @@ |
108 |
# construct correct header first |
109 |
try: |
110 |
if clold_file is not None: |
111 |
- # retain header from old ChangeLog |
112 |
+ # retain header from old ChangeLog, its copyright has |
113 |
+ # already been updated by update_copyrights |
114 |
for line in clold_file: |
115 |
line_strip = line.strip() |
116 |
clold_lines.append(line) |
117 |
@@ -597,7 +679,7 @@ |
118 |
line = line.replace('<CATEGORY>', category) |
119 |
line = line.replace('<PACKAGE_NAME>', package) |
120 |
line = re.sub(r'^(# Copyright \d\d\d\d)-\d\d\d\d ', |
121 |
- r'\1-%s ' % time.strftime('%Y'), line) |
122 |
+ r'\1-%s ' % year, line) |
123 |
clnew_lines.append(line) |
124 |
clnew_lines.append(_unicode_decode('\n')) |
125 |
clskel_file.close() |
126 |
@@ -651,7 +733,7 @@ |
127 |
if clold_file is not None: |
128 |
# clold_lines may contain a saved non-header line |
129 |
# that we want to write first. |
130 |
- if clold_lines[-1].strip(): |
131 |
+ if len(clold_lines) > 0 and clold_lines[-1].strip(): |
132 |
f.write(clold_lines[-1]) |
133 |
|
134 |
for line in clold_file: |