|
| 1 | +#! /usr/bin/python2 |
| 2 | +# coding=utf-8 |
| 3 | +import re |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | + |
| 7 | +# map from SVN log names to real names |
| 8 | +contributors = { |
| 9 | + 'anya' : 'Anya Helene Bagge', |
| 10 | + 'biz002' : 'Tero Hasu', |
| 11 | + 'eva' : 'Anya Helene Bagge' |
| 12 | +} |
| 13 | +# list of copyright owners that should have their years updated from the log |
| 14 | +autoYear = [] |
| 15 | +# list of copyright owners that should be added automatically (or when |
| 16 | +# a particular contributor is in the log) |
| 17 | +autoCopyright = { |
| 18 | + "University of Bergen" : "*", |
| 19 | + "Anya Helene Bagge" : "Anya Helene Bagge", |
| 20 | + "Tero Hasu" : "Tero Hasu" |
| 21 | +} |
| 22 | + |
| 23 | +standard_licenses = [("", {"GPLv3+", "EPLv1"})] |
| 24 | + |
| 25 | +# format for copyright lines |
| 26 | +fmt_copyright = "Copyright (c) %s %s\n" |
| 27 | +# format for contributor lines, when the contributor was found in the log |
| 28 | +fmt_contrib_from_log = "* %s\n" |
| 29 | +# format for contributor lines, when the contributor was only found in the previous header |
| 30 | +fmt_contrib_from_file = "+ %s\n" |
| 31 | +moreInfo_notice = "See the file COPYRIGHT for more information.\n" |
| 32 | +# GPLv3 licence notice |
| 33 | +gpl_notice = "This program is free software: you can redistribute it and/or modify\nit under the terms of the GNU General Public License as published by\nthe Free Software Foundation, either version 3 of the License, or\n(at your option) any later version. See http://www.gnu.org/licenses/\n" |
| 34 | +# EPL license notice |
| 35 | +epl_notice = "All rights reserved. This program and the accompanying materials\nare made available under the terms of the Eclipse Public License v1.0\nwhich accompanies this distribution, and is available at\nhttp://www.eclipse.org/legal/epl-v10.html\n" |
| 36 | +mit_notice = 'Permission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the "Software"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.' |
| 37 | + |
| 38 | +license_notices = { |
| 39 | + "GPLv3+" : gpl_notice, |
| 40 | + "EPLv1" : epl_notice, |
| 41 | + "MIT" : mit_notice |
| 42 | +} |
| 43 | + |
| 44 | +re_Empty = re.compile(r'^\s*$') # empty lines |
| 45 | +re_CommentStartLine = re.compile(r'^\s*/\*\s?(.*)$') # start of comment |
| 46 | +re_CommentEndLine = re.compile(r'^\s*(.*)\*/') # mid-comment line starting with a * |
| 47 | +re_CommentMidLine = re.compile(r'^\s*\*\s?(.*)$') # end of comment |
| 48 | +re_SvnLog = re.compile(r'^[^|]*\|\s*([^ ]*)\s*\|\s*([0-9][0-9][0-9][0-9])-') # svn log line |
| 49 | +re_eplLine = re.compile(r'.*(All rights reserved.\s*This program and the accompanying|Eclipse Public License|accompanies this distribution, and).*') # lines in an EPL notice |
| 50 | +re_eplIdLine = re.compile(r'.*(http://www.eclipse.org/legal/epl-v10.html).*') # lines identifying an EPLv1.0 notice |
| 51 | +re_gplLine = re.compile(r'.*(free software: you can redistribute it|GNU General Public License|any later version\.|http://www.gnu.org/licenses/).*') # lines in a GPL notice |
| 52 | +re_gplIdLine = re.compile(r'.*(Free Software Foundation, either version 3 of the License, or).*') # lines identifying a GPLv3+ notice |
| 53 | +re_mitLine = re.compile(r'.*(hereby granted, free of charge|software and associated documentation|Software without restriction|copy, modify, merge, publish, distribute, sublicense|and to permit persons to|do so, subject to the following conditions|substantial portions of the Software|WITHOUT WARRANTY OF ANY KIND|BUT NOT LIMITED TO THE WARRANTIES|PARTICULAR PURPOSE AND NONINFRINGEMENT|BE LIABLE FOR ANY CLAIM|WHETHER IN AN ACTION OF CONTRACT|THE SOFTWARE).*') |
| 54 | +re_moreInfoLine = re.compile(r'.*See.*for more info.*') |
| 55 | +re_copyright = re.compile(r'\s*Copyright\s*(\([cC]\)|©)\s*([0-9–, -]*)\s*(.*)$') # copyright lines |
| 56 | +re_contribs = re.compile(r'^\s*Contributor') # contributor heading |
| 57 | +re_contrib = re.compile(r'^\s*[*+]\s*(\w([\w\s.]|\S-\S)*)\s*') # individual contributor lines (only name is kept, the rest is stripped) |
| 58 | +re_decoration = re.compile(r'^\W*$') # contentless lines |
| 59 | + |
| 60 | + |
| 61 | +def autoLicenseFile(fileName): |
| 62 | + global currentFile |
| 63 | + currentFile = fileName |
| 64 | + (copy, code) = extractCopyrightComment(loadFile(fileName)) |
| 65 | + (contribs, first, last) = extractContributorsAndYears(getSvnLog(fileName)) |
| 66 | + (moreContribs, copyrights, oldLicenses, more) = decodeCopyright(copy) |
| 67 | + |
| 68 | + copyrightLines = [] |
| 69 | + for k in copyrights: |
| 70 | + y = copyrights[k] |
| 71 | + if k in autoYear: |
| 72 | + if first == last: |
| 73 | + y = first |
| 74 | + else: |
| 75 | + y = "%d-%d" % (first, last) |
| 76 | + if k not in autoCopyright: |
| 77 | + copyrightLines.append(fmt_copyright % (y, k)) |
| 78 | + for k in autoCopyright.keys(): |
| 79 | + if autoCopyright[k] == "*" or autoCopyright[k] in contribs: |
| 80 | + if first == last: |
| 81 | + y = first |
| 82 | + else: |
| 83 | + y = "%d-%d" % (first, last) |
| 84 | + copyrightLines.append(fmt_copyright % (y, k)) |
| 85 | + copyrightLines.sort() |
| 86 | + |
| 87 | + allContribs = list(contribs.union(moreContribs)) |
| 88 | + allContribs.sort() |
| 89 | + |
| 90 | + licenses = set() |
| 91 | + for (path,lics) in standard_licenses: |
| 92 | + if fileName.startswith(path): |
| 93 | + licenses = licenses.union(lics) |
| 94 | + |
| 95 | + if oldLicenses != set() and oldLicenses != licenses: |
| 96 | + error("Non-standard license: " + repr(oldLicenses)) |
| 97 | + licenses = oldLicenses |
| 98 | + licenses = list(licenses) |
| 99 | + licenses.sort() |
| 100 | + |
| 101 | + notice = "/**************************************************************************\n" |
| 102 | + if more != '': |
| 103 | + notice = "/*\n" + more + "*/" + notice |
| 104 | + for l in copyrightLines: |
| 105 | + notice = notice + l |
| 106 | + notice = notice + "\n" |
| 107 | + |
| 108 | + for l in licenses: |
| 109 | + notice = notice + license_notices[l] |
| 110 | + notice = notice + "\n" |
| 111 | + notice = notice + "\n" |
| 112 | + |
| 113 | + notice = notice + moreInfo_notice + "\n" |
| 114 | + |
| 115 | + notice = notice + "Contributors:\n" |
| 116 | + for c in allContribs: |
| 117 | + if c in contribs: |
| 118 | + notice = notice + fmt_contrib_from_log % c |
| 119 | + else: |
| 120 | + notice = notice + fmt_contrib_from_file % c |
| 121 | + |
| 122 | + notice = notice.replace("\n", "\n * ") |
| 123 | + notice = notice + "\n *************************************************************************/\n" |
| 124 | + |
| 125 | + writeFile(fileName, notice + code) |
| 126 | + |
| 127 | + |
| 128 | +def getSvnLog(fileName): |
| 129 | + log = subprocess.check_output(["svn", "log", "-q", fileName]) |
| 130 | + return log |
| 131 | + |
| 132 | +def extractContributorsAndYears(log): |
| 133 | + names = set() |
| 134 | + firstYear = 3000 |
| 135 | + lastYear = 0 |
| 136 | + for l in log.split('\n'): |
| 137 | + mo = re_SvnLog.match(l) |
| 138 | + if mo != None: |
| 139 | + name = mo.group(1) |
| 140 | + year = mo.group(2) |
| 141 | + if name == 'eva': |
| 142 | + print l |
| 143 | + firstYear = min(int(year), firstYear) |
| 144 | + lastYear = max(int(year), lastYear) |
| 145 | + if name in contributors: |
| 146 | + name = contributors[name] |
| 147 | + else: |
| 148 | + print "Unknown contributor: ", name |
| 149 | + names.add(name) |
| 150 | + |
| 151 | + return (names, firstYear, lastYear) |
| 152 | + |
| 153 | +def loadFile(fileName): |
| 154 | + f = open(fileName, "r") |
| 155 | + lines = f.readlines() |
| 156 | + f.close() |
| 157 | + return lines |
| 158 | + |
| 159 | +def writeFile(fileName, s): |
| 160 | + f = open(fileName, "w") |
| 161 | + f.write(s) |
| 162 | + f.close() |
| 163 | + |
| 164 | +def extractCopyrightComment(lines): |
| 165 | + mode = 's' |
| 166 | + cr = '' |
| 167 | + rest = '' |
| 168 | + for l in lines: |
| 169 | + if mode == 's': |
| 170 | + mo = re_CommentStartLine.match(l) |
| 171 | + if mo != None: |
| 172 | + if re_CommentEndLine.match(l): |
| 173 | + rest = rest + l |
| 174 | + else: |
| 175 | + cr = cr + mo.group(1) + "\n" |
| 176 | + mode = 'c' |
| 177 | + elif re_Empty.match(l) == None: |
| 178 | + rest = rest + l |
| 179 | + mode = '' |
| 180 | + elif mode == 'c': |
| 181 | + mo = re_CommentEndLine.match(l) |
| 182 | + if mo != None: |
| 183 | + cr = cr + mo.group(1) + "\n" |
| 184 | + mode = '' |
| 185 | + else: |
| 186 | + mo = re_CommentMidLine.match(l) |
| 187 | + if mo != None: |
| 188 | + cr = cr + mo.group(1) + "\n" |
| 189 | + else: |
| 190 | + cr = cr + l |
| 191 | + elif mode == '': |
| 192 | + rest = rest + l |
| 193 | + return (cr, rest) |
| 194 | + |
| 195 | + |
| 196 | +def decodeCopyright(copy): |
| 197 | + epl = False |
| 198 | + gpl = False |
| 199 | + contribs = set() |
| 200 | + copyrights = {} |
| 201 | + mode = 'n' |
| 202 | + licenses = set() |
| 203 | + more = '' |
| 204 | + |
| 205 | + for l in copy.split('\n'): |
| 206 | + mo = re_copyright.match(l) |
| 207 | + if mo != None: |
| 208 | + copyrights[mo.group(3).strip()] = mo.group(2).strip() |
| 209 | + mode = 'n' |
| 210 | + elif re_eplIdLine.match(l): |
| 211 | + licenses.add("EPLv1") |
| 212 | + mode = 'n' |
| 213 | + elif re_gplIdLine.match(l): |
| 214 | + licenses.add("GPLv3+") |
| 215 | + mode = 'n' |
| 216 | + elif re_gplIdLine.match(l): |
| 217 | + licenses.add("MIT") |
| 218 | + mode = 'n' |
| 219 | + elif re_eplLine.match(l) or re_gplLine.match(l) or re_mitLine.match(l) or re_moreInfoLine.match(l): |
| 220 | + mode = 'n' |
| 221 | + elif re_contribs.match(l): |
| 222 | + mode = 'c' |
| 223 | + elif mode == 'c': |
| 224 | + mo = re_contrib.match(l) |
| 225 | + if mo != None: |
| 226 | + contribs.add(mo.group(1).strip()) |
| 227 | + elif re_decoration.match(l) != None: |
| 228 | + mode = 'n' |
| 229 | + else: |
| 230 | + mode = 'n' |
| 231 | + error("Strange copyright line: " + l) |
| 232 | + more = more + l |
| 233 | + elif re_decoration.match(l) == None: |
| 234 | + error("Strange copyright line: " + l) |
| 235 | + more = more + l |
| 236 | + |
| 237 | + return (contribs, copyrights, licenses, more) |
| 238 | + |
| 239 | +currentFile = "" |
| 240 | +def error(s): |
| 241 | + print currentFile, ": ", s |
| 242 | + |
| 243 | + |
| 244 | +for f in sys.argv[1:]: |
| 245 | + print "Processing: ", f |
| 246 | + autoLicenseFile(f) |
0 commit comments