1
0
mirror of https://github.com/clementine-player/Clementine synced 2024-12-23 16:28:19 +01:00
Clementine-audio-player-Mac.../dist/copyright.py

158 lines
3.4 KiB
Python
Raw Normal View History

#!/usr/bin/python2
2014-11-02 19:32:48 +01:00
from subprocess import *
from sys import *
from os import rename, remove
from datetime import *
def pretty_years(s):
2014-11-02 19:32:48 +01:00
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
2014-11-02 19:32:48 +01:00
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):
2014-11-02 19:32:48 +01:00
la = list(a[2])
la.sort()
2014-11-02 19:32:48 +01:00
lb = list(b[2])
lb.sort()
if la[0] < lb[0]:
return -1
elif la[0] > lb[0]:
return 1
else:
return 0
def gen_copyrights(f):
commits = []
data = {}
copyrights = []
2014-11-02 19:32:48 +01:00
for ln in Popen(["git", "blame", "--incremental", f], stdout=PIPE).stdout:
2014-11-02 19:32:48 +01:00
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()
with open(f,'r') as fi:
fil = fi.readlines()
for i in fil:
if -1 < i.find("Original Author"):
da = i.split(" ")
print f
copyrights.append(" Copyright %s, %s %s\n" % (da[3].strip(), da[1], da[2]))
by_author = {}
2014-11-02 19:32:48 +01:00
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):
2014-11-02 19:32:48 +01:00
del by_author[bn]
copyright = list(by_author.itervalues())
copyright.sort(order_by_year)
for name, mail, years in copyright:
copyrights.append(" Copyright %s, %s %s\n" % (pretty_years(years), name, mail))
return copyrights
def change_file(filename):
content=[]
out=[]
extended=0
ends=0
with open(filename, "r") as fi:
content=fi.readlines()
copyrights=gen_copyrights(filename)
2014-11-02 19:32:48 +01:00
if -1 == content[0].find("/* This file is part of Clementine."):
2014-11-02 21:37:15 +01:00
print("File {} have no Clementine copyright info".format(filename))
2014-11-02 19:32:48 +01:00
return 0
for i in content:
if i.find("*/") != -1:
ends = 1
if i.find("Copyright ") != -1:
if not extended:
out.extend(copyrights)
extended = 1
2014-11-02 19:32:48 +01:00
if not ends:
continue
else:
out.append(i)
2014-11-02 19:32:48 +01:00
with open(filename+'_tmp', "w") as fi:
fi.writelines(out)
rename(filename+'_tmp', filename)
2014-11-02 19:32:48 +01:00
if __name__ == "__main__":
for files in argv[1:]:
change_file(files)