Created
January 15, 2019 18:10
-
-
Save mkrautz/00e255052018aad68f9e820fd152010c to your computer and use it in GitHub Desktop.
update-license-header.py
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', 'rb') as f: | |
s = f.read() | |
return s | |
def fileHasLicenseHeader(fn): | |
f = open(fn, 'rb') | |
buf = f.read(4096) | |
clue1 = b'Copyright' in buf | |
clue2 = b'The Mumble Developers. All rights reserved.' in buf | |
clue3 = b'Mumble source tree or at <https://www.mumble.info/LICENSE>.' in buf | |
return clue1 and clue2 and clue3 | |
def updateLicenseHeader(fn): | |
hdr = licenseHeader() | |
splitHdr = hdr.split(b'\n') | |
# Read the original file | |
with open(fn, 'rb') as f: | |
buf = f.read() | |
print(type(buf)) | |
# 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[-2] | |
lastLineNoComments = lastLine.replace(b'//', b'', 1) | |
lastLineIndex = buf.find(lastLineNoComments) | |
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: | |
if len(line) == 0: | |
continue | |
newHeader += line.replace(b'//', commentToken, 1) + newline | |
# Now, find the beginning of the license | |
# header in the file. | |
needle = commentToken + b' Copyright' | |
idx = lastLineIndex | |
while idx >= 0: | |
oi = buf[idx:idx+len(needle)] | |
if oi == 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 end. (Inlcuding | |
# newline characters.) | |
idx = lastLineIndex | |
while True: | |
if buf[idx] == '\r' and buf[idx+1] == '\n': | |
if newline != '\r\n': | |
raise Exception('found CRLF when searching for end-of-header but the file does not use CRLF') | |
break | |
if buf[idx] == '\n': | |
if newline != '\n': | |
raise Exception('found LF when searching for end-of-header but the file does not use LF') | |
break | |
idx += 1 | |
byteAfterHeaderIdx = idx + len(newline) | |
# 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:]) | |
return | |
def main(): | |
for root, dirs, files in os.walk('.'): | |
for fn in files: | |
absFn = os.path.join(root, fn) | |
# Skip git | |
if os.path.join('.git', '') in absFn: | |
continue | |
# Skip LICENSE.header | |
if absFn == os.path.join(root, 'LICENSE.header'): | |
continue | |
# Skip self | |
if absFn == os.path.join(root, 'scripts', 'update-license-header.py'): | |
continue | |
hasHeader = fileHasLicenseHeader(absFn) | |
if hasHeader: | |
updateLicenseHeader(absFn) | |
print('Updated license header in file at {0}'.format(absFn)) | |
continue | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment