Skip to content

Commit a55486a

Browse files
committed
Add a script to fix file headers
1 parent a20aba4 commit a55486a

3 files changed

Lines changed: 155 additions & 15 deletions

File tree

doc/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# Copyright 2012 Vincent Jacques vincent@vincent-jacques.net
44
# Copyright 2013 Vincent Jacques vincent@vincent-jacques.net
55

6+
################################################################################
7+
68
# PyGithub documentation build configuration file, created by
79
# sphinx-quickstart on Thu Sep 13 22:51:42 2012.
810
#

manage.sh

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,22 @@ function check {
1414
pep8 --ignore=E501 github setup.py || exit
1515
}
1616

17-
function check_copyright {
18-
for file in $(git ls-files | grep "py$")
19-
do
20-
git log "--format=format:# Copyright %ad %an %ae" --date=short -- $file |
21-
sed "s/\([0-9][0-9][0-9][0-9]\)-[0-9][0-9]-[0-9][0-9]/\1/g" | sort -u |
22-
while read copyright
23-
do
24-
if grep -n $file -e "^$copyright$" > /dev/null
25-
then
26-
echo > /dev/null
27-
else
28-
echo "$file should contain '$copyright'"
29-
fi
30-
done
31-
done
17+
function fix_headers {
18+
python scripts/fix_headers.py
19+
# for file in $(git ls-files | grep "py$")
20+
# do
21+
# git log "--format=format:# Copyright %ad %an %ae" --date=short -- $file |
22+
# sed "s/\([0-9][0-9][0-9][0-9]\)-[0-9][0-9]-[0-9][0-9]/\1/g" | sort -u |
23+
# while read copyright
24+
# do
25+
# if grep -n $file -e "^$copyright$" > /dev/null
26+
# then
27+
# echo > /dev/null
28+
# else
29+
# echo "$file should contain '$copyright'"
30+
# fi
31+
# done
32+
# done
3233
}
3334

3435
function test {

scripts/fix_headers.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
# Copyright
5+
6+
# This file is part of PyGithub. http://jacquev6.github.com/PyGithub/
7+
8+
# PyGithub is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License
9+
# as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
10+
11+
# PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
13+
14+
# You should have received a copy of the GNU Lesser General Public License along with PyGithub. If not, see <http://www.gnu.org/licenses/>.
15+
16+
import fnmatch
17+
import os
18+
import subprocess
19+
import itertools
20+
21+
22+
license = [
23+
"# This file is part of PyGithub. http://jacquev6.github.com/PyGithub/",
24+
"",
25+
"# PyGithub is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License",
26+
"# as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.",
27+
"",
28+
"# PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of",
29+
"# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.",
30+
"",
31+
"# You should have received a copy of the GNU Lesser General Public License along with PyGithub. If not, see <http://www.gnu.org/licenses/>.",
32+
]
33+
34+
35+
class PythonHeader:
36+
def fix(self, filename, lines):
37+
isExecutable = lines[0].startswith("#!")
38+
newLines = []
39+
40+
if isExecutable:
41+
newLines.append("#!/usr/bin/env python")
42+
newLines.append("# -*- coding: utf-8 -*-")
43+
newLines.append("")
44+
45+
# @todo add <> around mails
46+
# @todo add ############## Copyrights and license (add sections to the header)
47+
48+
for year, name in sorted(listContributors(filename)):
49+
newLines.append("# Copyright " + year + " " + name)
50+
newLines.append("")
51+
52+
for line in license:
53+
newLines.append(line)
54+
55+
bodyLines = list(itertools.dropwhile(self.lineCanBeHeader, lines))
56+
57+
if len(bodyLines) > 0:
58+
newLines.append("")
59+
if "import " not in bodyLines[0] and bodyLines[0] != '"""' and not bodyLines[0].startswith("##########"):
60+
newLines.append("")
61+
62+
newLines += bodyLines
63+
64+
return newLines
65+
66+
def lineCanBeHeader(self, line):
67+
return (len(line) == 0 or line[0] == "#") and not line.startswith("##########")
68+
69+
70+
class StandardHeader:
71+
def fix(self, filename, lines):
72+
newLines = []
73+
74+
for year, name in sorted(listContributors(filename)):
75+
newLines.append("# Copyright " + year + " " + name)
76+
newLines.append("")
77+
78+
for line in license:
79+
newLines.append(line)
80+
81+
bodyLines = list(itertools.dropwhile(self.lineCanBeHeader, lines))
82+
83+
if len(bodyLines) > 0:
84+
newLines.append("")
85+
newLines += bodyLines
86+
87+
return newLines
88+
89+
def lineCanBeHeader(self, line):
90+
return (len(line) == 0 or line[0] == "#") and not line.startswith("##########")
91+
92+
93+
def listContributors(filename):
94+
contributors = set()
95+
for line in subprocess.check_output(["git", "log", "--format=format:%ad %an %ae", "--date=short", "--", filename]).split("\n"):
96+
year = line[0:4]
97+
name = line[11:]
98+
contributors.add((year, name))
99+
return contributors
100+
101+
102+
def findHeadersAndFiles():
103+
for root, dirs, files in os.walk('.', topdown=True):
104+
if ".git" in dirs:
105+
dirs.remove(".git")
106+
107+
for filename in files:
108+
fullname = os.path.join(root, filename)
109+
if filename.endswith(".py"):
110+
yield (PythonHeader(), fullname)
111+
elif filename in ["COPYING", "COPYING.LESSER"]:
112+
pass
113+
elif filename.endswith(".rst") or filename.endswith(".md"):
114+
pass
115+
elif filename == ".gitignore":
116+
yield (StandardHeader(), fullname)
117+
elif "ReplayData" in fullname:
118+
pass
119+
else:
120+
print "Don't know what to do with", filename
121+
122+
123+
def main():
124+
for header, filename in findHeadersAndFiles():
125+
print "Analyzing", filename
126+
with open(filename) as f:
127+
lines = list(line.rstrip() for line in f)
128+
newLines = header.fix(filename, lines)
129+
if newLines != lines:
130+
print " => actually modifying", filename
131+
with open(filename, "w") as f:
132+
for line in newLines:
133+
f.write(line + "\n")
134+
135+
136+
if __name__ == "__main__":
137+
main()

0 commit comments

Comments
 (0)