Gentoo Archives: gentoo-portage-dev

From: Fabian Groffen <grobian@g.o>
To: gentoo-portage-dev@l.g.o
Subject: [gentoo-portage-dev] [PATCH] repoman: update copyright on modified files
Date: Thu, 20 Oct 2011 18:24:10
Message-Id: 2f204b2ec375bba6598f.1319135020@nut.cheops.ods.org
In Reply to: Re: [gentoo-portage-dev] [PATCH 3 of 3] repoman: update copyright on modified files by Zac Medico
1 To retain the behaviour of echangelog, update the copyrights on modified
2 files (mostly ebuilds) when necessary. Also update the ChangeLog's
3 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,77 @@
9
10 return outvcs
11
12 +def update_copyright(fn_path, year, pretend):
13 + """
14 + Check file for a Copyright statement, and update its year. The
15 + patterns used for replacing copyrights are taken from echangelog.
16 + Only the first lines of each file that start with a hash ('#') are
17 + considered, until a line is found that doesn't start with a hash.
18 + """
19 +
20 + try:
21 + fn_hdl = io.open(_unicode_encode(fn_path,
22 + encoding=_encodings['fs'], errors='strict'),
23 + mode='r', encoding=_encodings['repo.content'], errors='replace')
24 + except EnvironmentError:
25 + return
26 +
27 + orig_header = []
28 + new_header = []
29 +
30 + for line in fn_hdl:
31 + line_strip = line.strip()
32 + orig_header.append(line)
33 + if not line_strip or line_strip[:1] != '#':
34 + new_header.append(line)
35 + break
36 +
37 + # these two regexes are taken from
38 + # echangelog update_copyright()
39 + line = re.sub(r'^(# Copyright \d+) ',
40 + r'\1-%s ' % year, line)
41 + line = re.sub(r'^(# Copyright) \d\d\d\d-\d\d\d\d',
42 + r'\1 1999-%s' % year, line)
43 + new_header.append(line)
44 +
45 + difflines = 0
46 + for line in difflib.unified_diff(orig_header, new_header,
47 + fromfile=fn_path, tofile=fn_path, n=0):
48 + util.writemsg_stdout(line, noiselevel=-1)
49 + difflines += 1
50 + util.writemsg_stdout("\n", noiselevel=-1)
51 +
52 + # unified diff has three lines to start with
53 + if difflines > 3 and not pretend:
54 + # write new file with changed header
55 + f, fnnew_path = mkstemp()
56 + f = io.open(f, mode='w', encoding=_encodings['repo.content'],
57 + errors='backslashreplace')
58 + for line in new_header:
59 + f.write(line);
60 + for line in fn_hdl:
61 + f.write(line)
62 + f.close()
63 + try:
64 + fn_stat = os.stat(fn_path)
65 + except OSError:
66 + fn_stat = None
67 +
68 + shutil.move(fnnew_path, fn_path)
69 +
70 + if fn_stat is None:
71 + util.apply_permissions(fn_path, mode=0o644)
72 + else:
73 + util.apply_stat_permissions(fn_path, fn_stat)
74 + fn_hdl.close()
75 +
76 def UpdateChangeLog(pkgdir, category, package, new, removed, changed, \
77 msg, pretend, repodir):
78 - """ Write an entry to an existing ChangeLog, or create a new one. """
79 + """
80 + Write an entry to an existing ChangeLog, or create a new one.
81 + Updates copyright year on changed files, and updates the header of
82 + ChangeLog with the contents of skel.ChangeLog.
83 + """
84
85 # figure out who to write as
86 if 'GENTOO_COMMITTER_NAME' in os.environ and \
87 @@ -548,6 +616,16 @@
88 logging.critical(err)
89 return None
90
91 + year = time.strftime('%Y')
92 + date = time.strftime('%d %b %Y')
93 +
94 + # check modified files and the ChangeLog for copyright updates
95 + # patches and diffs (identified by .patch and .diff) are excluded
96 + for fn in new + changed:
97 + if fn.endswith('.diff') or fn.endswith('.patch'):
98 + continue
99 + update_copyright(os.path.join(pkgdir, fn), year, pretend)
100 +
101 cl_path = os.path.join(pkgdir, 'ChangeLog')
102 clold_lines = []
103 clnew_lines = []
104 @@ -584,8 +662,9 @@
105 clold_lines.append(line)
106 if line_strip[:1] != '#':
107 break
108 - if clskel_file is None:
109 - clnew_lines.append(line)
110 + line = re.sub(r'^(# Copyright) \d\d\d\d-\d\d\d\d',
111 + r'\1 1999-%s' % year, line)
112 + clnew_lines.append(line)
113 if not line_strip:
114 break
115 elif clskel_file is not None:
116 @@ -597,7 +676,7 @@
117 line = line.replace('<CATEGORY>', category)
118 line = line.replace('<PACKAGE_NAME>', package)
119 line = re.sub(r'^(# Copyright \d\d\d\d)-\d\d\d\d ',
120 - r'\1-%s ' % time.strftime('%Y'), line)
121 + r'\1-%s ' % year, line)
122 clnew_lines.append(line)
123 clnew_lines.append(_unicode_decode('\n'))
124 clskel_file.close()

Replies