Created
January 8, 2017 20:16
-
-
Save mkrautz/69e6dbe826dcdff502a343f59c48f75a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 | |
# | |
# Copyright 2005-2017 The Mumble Developers. All rights reserved. | |
# Use of this source code is governed by a BSD-style license | |
# that can be found in the LICENSE file at the root of the | |
# Mumble source tree or at <https://www.mumble.info/LICENSE>. | |
from __future__ import (unicode_literals, print_function, division) | |
import os | |
def licenseHeader(): | |
s = '' | |
with open('LICENSE.header', 'r') as f: | |
s = f.read() | |
return s | |
def splitLicenseHeader(): | |
hdr = licenseHeader() | |
splat = hdr.split(b'\n')[0:-1] | |
out = [] | |
for line in splat: | |
fixedLine = line.replace(b'// ', b'', 1) | |
out.append(fixedLine) | |
return out | |
def fileHasLicenseHeader(fn): | |
f = open(fn, 'r') | |
buf = f.read(4096) | |
splitHeaderLines = splitLicenseHeader() | |
if not splitHeaderLines[0].startswith(b'Copyright'): | |
raise Exception('Expected LICENSE.header to start with "Copyright".') | |
if not b'Copyright' in buf: | |
return False | |
for line in splitHeaderLines[1:]: | |
if not line in buf: | |
return False | |
return True | |
def updateLicenseHeader(fn): | |
hdr = licenseHeader() | |
splitHdr = splitLicenseHeader() | |
# Read the original file | |
with open(fn, 'rb') as f: | |
buf = f.read() | |
# Figure out which comment style | |
# the file uses. Also, try to figure | |
# out if it uses Unix-style line endings, | |
# or Windows-style line endings. | |
lastLine = splitHdr[-1] | |
lastLineIndex = buf.find(b' ' + lastLine) | |
if lastLineIndex < 0: | |
raise Exception('bad file') | |
commentToken = b'' | |
idx = lastLineIndex | |
while idx >= 0: | |
idx -= 1 | |
if buf[idx] == '\n': | |
break | |
commentToken = buf[idx] + commentToken | |
newline = '\n' | |
if buf[idx-1] == '\r': | |
newline = '\r\n' | |
# Construct the new header | |
newHeader = b'' | |
for line in splitHdr: | |
newHeader += commentToken + b' ' + line + newline | |
# Now, find the beginning of the license | |
# header in the file. | |
needle = commentToken + b' Copyright' | |
idx = lastLineIndex | |
while idx >= 0: | |
checkBuf = buf[idx:idx+len(needle)] | |
if checkBuf == needle: | |
break | |
idx -= 1 | |
if idx == -1: | |
raise Exception('could not find beginning of license header in {0}'.format(fn)) | |
headerBeginIndex = idx | |
# OK, we have the beginning. | |
# Now, find the full length of the | |
# existing header. (Inlcuding | |
# newline characters.) | |
idx = lastLineIndex | |
while True: | |
if buf[idx] == '\n' or buf[idx] == '\r': | |
break | |
idx += 1 | |
headerLength = byteAfterHeaderIdx = idx - 1 + len(newline) + 1 | |
# OK, let's re-write the file. | |
with open(fn, 'wb') as nf: | |
if headerBeginIndex > 0: | |
nf.write(buf[0:headerBeginIndex]) | |
nf.write(newHeader) | |
nf.write(buf[byteAfterHeaderIdx:]) | |
def main(): | |
target = '.' | |
for root, dirs, files in os.walk(target): | |
for fn in files: | |
absFn = os.path.join(root, fn) | |
# Skip git | |
if os.path.join(target, '.git', '') in absFn: | |
continue | |
# Skip LICENSE.header | |
if absFn == os.path.join(target, 'LICENSE.header'): | |
continue | |
hasHeader = fileHasLicenseHeader(absFn) | |
if hasHeader: | |
updateLicenseHeader(absFn) | |
print('Updated license header in file at {0}'.format(absFn)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment