|
| 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