Skip to content

Commit 26dbee2

Browse files
merge default
2 parents e4fb943 + 7f0de44 commit 26dbee2

File tree

1 file changed

+55
-47
lines changed

1 file changed

+55
-47
lines changed

encopyright.py

Lines changed: 55 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@
2020
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2121

2222
import os, time, sys
23-
import bzrlib.branch
24-
import bzrlib.log
23+
import py
2524

2625
header_template = """\
27-
# Copyright 2000-%s Michael Hudson-Doyle <[email protected]>%s
26+
# Copyright 2000-%(lastyear)s Michael Hudson-Doyle <[email protected]>%(others)s
2827
#
2928
# All Rights Reserved
3029
#
@@ -46,64 +45,69 @@
4645

4746
author_template = "\n#%s%%s"%(' '*(header_template.index("Michael")+1),)
4847

49-
branch, path = bzrlib.branch.Branch.open_containing(sys.argv[0])
50-
rev_tree = branch.basis_tree()
51-
branch.lock_read()
52-
53-
def process(thing):
54-
if os.path.isdir(thing):
55-
for subthing in os.listdir(thing):
56-
process(os.path.join(thing, subthing))
57-
elif os.path.isfile(thing):
58-
if thing[-3:] == '.py':
59-
process_file(thing)
60-
else:
61-
print "W `%s' not file or directory"%(thing,)
48+
6249

6350
author_map = {
6451
u'mwh': None,
52+
u'micahel': None,
6553
u'Michael Hudson <[email protected]>': None,
6654
u'arigo': u"Armin Rigo",
6755
u'antocuni': u'Antonio Cuni',
56+
u'anto': u'Antonio Cuni',
6857
u'bob': u'Bob Ippolito',
6958
u'fijal': u'Maciek Fijalkowski',
7059
u'agaynor': u'Alex Gaynor',
7160
u'hpk': u'Holger Krekel',
61+
u'Ronny': u'Ronny Pfannschmidt',
62+
u'amauryfa': u"Amaury Forgeot d'Arc",
7263
}
7364

74-
def process_file(file):
75-
ilines = open(file).readlines()
76-
file_id = rev_tree.path2id(file)
77-
rev_ids = [rev_id for (revno, rev_id, what)
78-
in bzrlib.log.find_touching_revisions(branch, file_id)]
79-
revs = branch.repository.get_revisions(rev_ids)
80-
revs = sorted(revs, key=lambda x:x.timestamp)
81-
modified_year = None
82-
for rev in reversed(revs):
83-
if 'encopyright' not in rev.message:
84-
modified_year = time.gmtime(rev.timestamp)[0]
85-
break
65+
66+
def author_revs(path):
67+
proc = py.std.subprocess.Popen([
68+
'hg','log', str(path),
69+
'--template', '{author|user} {date}\n',
70+
'-r', 'not keyword("encopyright")',
71+
], stdout=py.std.subprocess.PIPE)
72+
output, _ = proc.communicate()
73+
lines = output.splitlines()
74+
for line in lines:
75+
try:
76+
name, date = line.split(None, 1)
77+
except ValueError:
78+
pass
79+
else:
80+
if '-' in date:
81+
date = date.split('-')[0]
82+
yield name, float(date)
83+
84+
85+
def process(path):
86+
ilines = path.readlines()
87+
revs = sorted(author_revs(path), key=lambda x:x[1])
88+
modified_year = time.gmtime(revs[-1][1])[0]
8689
if not modified_year:
87-
print 'E: no sensible modified_year found for %s' % file,
90+
print 'E: no sensible modified_year found for', path
8891
modified_year = time.gmtime(time.time())[0]
89-
authors = set()
90-
for rev in revs:
91-
authors.update(rev.get_apparent_authors())
9292
extra_authors = []
93+
authors = set(rev[0] for rev in revs)
9394
for a in authors:
9495
if a not in author_map:
95-
print 'E: need real name for %r' % a
96+
print 'E: need real name for', a
9697
ea = author_map.get(a)
9798
if ea:
9899
extra_authors.append(ea)
99100
extra_authors.sort()
100-
header = header_template % (modified_year, ''.join([author_template%ea for ea in extra_authors]))
101+
header = header_template % {
102+
'lastyear': modified_year,
103+
'others': ''.join([author_template%ea for ea in extra_authors])
104+
}
101105
header_lines = header.splitlines()
102106
prelines = []
103107
old_copyright = []
104108

105109
if not ilines:
106-
print "W ignoring empty file `%s'"%(file,)
110+
print "W ignoring empty file", path
107111
return
108112

109113
i = 0
@@ -123,8 +127,8 @@ def process_file(file):
123127
if abs(len(old_copyright) - len(header_lines)) < 2 + len(extra_authors):
124128
for x, y in zip(old_copyright, header_lines):
125129
if x[:-1] != y:
126-
print "C change needed in", file
127-
ofile = open(file, "w")
130+
print "C change needed in", path
131+
ofile = path.open("w")
128132
for l in prelines:
129133
ofile.write(l)
130134
ofile.write(header + "\n")
@@ -133,17 +137,21 @@ def process_file(file):
133137
ofile.close()
134138
break
135139
else:
136-
print "M no change needed in", file
140+
print "M no change needed in", path
137141
else:
138142
print "A no (c) in", file
139-
ofile = open(file, "w")
140-
for l in prelines:
141-
ofile.write(l)
142-
ofile.write(header + "\n\n")
143-
for l in ilines[len(prelines):]:
144-
ofile.write(l)
145-
ofile.close()
146-
143+
with path.open("w") as ofile:
144+
for l in prelines:
145+
ofile.write(l)
146+
ofile.write(header + "\n\n")
147+
for l in ilines[len(prelines):]:
148+
ofile.write(l)
149+
147150

148151
for thing in sys.argv[1:]:
149-
process(thing)
152+
path = py.path.local(thing)
153+
if path.check(dir=1):
154+
for item in path.visit('*.py'):
155+
process(item)
156+
elif path.check(file=1, ext='py'):
157+
process(path)

0 commit comments

Comments
 (0)