Created
June 5, 2020 17:18
-
-
Save nedtwigg/a13d780ef9ed49b63d95150852f38dbf to your computer and use it in GitHub Desktop.
Copied verbatim from http://0pointer.de/public/copyright.py on June 5 2020, to make sure it doesn't go away
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from subprocess import * | |
from sys import * | |
from datetime import * | |
def pretty_years(s): | |
l = list(s) | |
l.sort() | |
start = None | |
prev = None | |
r = [] | |
for x in l: | |
if prev is None: | |
start = x | |
prev = x | |
continue | |
if x == prev + 1: | |
prev = x | |
continue | |
if prev == start: | |
r.append("%i" % prev) | |
else: | |
r.append("%i-%i" % (start, prev)) | |
start = x | |
prev = x | |
if not prev is None: | |
if prev == start: | |
r.append("%i" % prev) | |
else: | |
r.append("%i-%i" % (start, prev)) | |
return ", ".join(r) | |
def order_by_year(a, b): | |
la = list(a[2]) | |
la.sort() | |
lb = list(b[2]) | |
lb.sort() | |
if la[0] < lb[0]: | |
return -1 | |
elif la[0] > lb[0]: | |
return 1 | |
else: | |
return 0 | |
for f in argv[1:]: | |
print "File: %s" % f | |
commits = [] | |
data = {} | |
for ln in Popen(["git", "blame", "--incremental", f], stdout=PIPE).stdout: | |
if ln.startswith("filename "): | |
if len(data) > 0: | |
commits.append(data) | |
data = {} | |
elif ln.startswith("author "): | |
data["author"] = ln[7:].strip() | |
elif ln.startswith("author-mail <"): | |
data["author-mail"] = ln[12:].strip() | |
elif ln.startswith("author-time "): | |
data["author-time"] = ln[11:].strip() | |
elif ln.startswith("author-tz "): | |
data["author-tz"] = ln[9:].strip() | |
by_author = {} | |
for c in commits: | |
try: | |
n = by_author[c["author"]] | |
except KeyError: | |
n = (c["author"], c["author-mail"], set()) | |
by_author[c["author"]] = n | |
# FIXME: Handle time zones properly | |
year = datetime.fromtimestamp(int(c["author-time"])).year | |
n[2].add(year) | |
for an, a in list(by_author.iteritems()): | |
for bn, b in list(by_author.iteritems()): | |
if a is b: | |
continue | |
if a[1] == b[1]: | |
a[2].update(b[2]) | |
if by_author.has_key(an) and by_author.has_key(bn): | |
del by_author[bn] | |
copyright = list(by_author.itervalues()) | |
copyright.sort(order_by_year) | |
for name, mail, years in copyright: | |
print "\tCopyright (c) %s %s" % (pretty_years(years), name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment