Skip to content

Instantly share code, notes, and snippets.

@swgillespie
Created March 1, 2016 22:12
Show Gist options
  • Save swgillespie/c4187a147de3e1d814c1 to your computer and use it in GitHub Desktop.
Save swgillespie/c4187a147de3e1d814c1 to your computer and use it in GitHub Desktop.
CoreCLR License Headers
import os
import glob
import sys
correct_header = r'''// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.'''
incorrect_header = r'''// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.'''
def main(root_folder):
glob_path = os.path.join(root_folder, '**/*.cs')
print("globbing on {}".format(glob_path))
for cs_file in glob.iglob('**/*.cs', recursive=True):
try:
with open(cs_file, 'r') as f:
text = f.read()
if correct_header in text and text.index(correct_header) == 0:
continue
with open(cs_file, 'w+') as f:
if correct_header in text:
print("{} had correct header, but not at the top of the file".format(cs_file))
text = text.replace(correct_header, '')
text = correct_header + '\n' + text
f.write(text)
continue
if incorrect_header in text:
print("Incorrect header detected for {}, replacing".format(cs_file))
text = text.replace(incorrect_header, '')
text = correct_header + '\n' + text
f.write(text)
continue
print("No header detected for {}, adding".format(cs_file))
text = correct_header + '\n' + text
f.write(text)
except Exception as e:
print("Non UTF-8 code detected for {}, skipping ({})".format(cs_file, e))
if __name__ == '__main__':
main(sys.argv[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment