Debian Codegraph @

debian/fzp2copyright

raw
1 #!/usr/bin/python3
2
3 import os, sys, xml.dom.minidom, re, os.path
4
5 def findAllFzp(dirList=["bins","contrib","core","obsolete"]):
6 result=[]
7 for d in dirList:
8 for root, dirs, files in os.walk(d):
9 for file in files:
10 if file.endswith(".fzp"):
11 result.append(os.path.join(root, file))
12 result.sort()
13 return result
14
15 class Author:
16 def __init__(self,name="", year=""):
17 self.name=name.strip()
18 self.year=year.strip()
19
20 def __str__(self):
21 template="{year} {name}"
22 return template.format(**self.__dict__)
23
24 def __eq__(self, other):
25 return self.name==other.name and self.year==other.year
26
27 def __lt__(self, other):
28 return self.name < other.name or self.year < other.year
29
30 yearpattern=re.compile(r".*(\d\d\d\d).*")
31 def fzp2authorDict(paths):
32 result={}
33 i=0
34 for p in paths:
35 try:
36 msg="file not parsed"
37 doc=xml.dom.minidom.parse(open(p, "rt", encoding="utf-8"))
38 msg="date not parsed"
39 date=doc.getElementsByTagName("date")[0]
40 msg="author's name not parsed"
41 name=doc.getElementsByTagName("author")[0]
42 msg="year not parsed"
43 year=yearpattern.match(date.firstChild.nodeValue).group(1)
44 sys.stdout.write("{}\r".format(i))
45 i+=1
46 author=Author(name.firstChild.nodeValue, year)
47 # add paths into the dictionary. In order to be able
48 # to make space separated path lists, the space characters
49 # are replaced by the '?' wildcards.
50 if str(author) in result:
51 result[str(author)].append(p.replace(" ","?"))
52 else:
53 result[str(author)]=[p.replace(" ","?")]
54 except Exception as e:
55 sys.stderr.write("{}\n".format(e))
56 sys.stderr.write("Error, malformed XML file: {} ({})\n".format(p, msg))
57 sys.stdout.write("\n")
58 return result
59
60 def authorDict2copyright(d):
61 result=""
62 authors=sorted(d.keys())
63 for a in authors:
64 result+="""
65
66 Files: {}
67 Copyright: {}
68 License: CC-BY-SA-3.0
69 """.format(" ".join(d[a]), a)
70 return result
71
72 if __name__=="__main__":
73 fzps=findAllFzp()
74 dictAuthor=fzp2authorDict(fzps)
75 copyrightFile=os.path.abspath(os.path.join(os.path.dirname(__file__),
76 "copyright"))
77 with open(copyrightFile, "wt", encoding="utf-8") as outfile, \
78 open(copyrightFile+".in", "rt", encoding="utf-8") as infile:
79 lines=infile.readlines()
80 for l in lines:
81 if "#end authors of parts" not in l:
82 outfile.write(l)
83 else:
84 outfile.write(authorDict2copyright(dictAuthor))
85 outfile.write("Comment: "+l)
86 print("wrote", copyrightFile)