Last active
June 24, 2021 00:06
-
-
Save nfitzen/25336f5e3f67d4ed639d554761be3dd1 to your computer and use it in GitHub Desktop.
Adds copyright headers to all files matching a certain pattern.
This file contains 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
# Copyright (C) 2020 Nathaniel Fitzenrider <https://github.com/nfitzen> | |
# | |
# SPDX-License-Identifier: MIT | |
from glob import iglob | |
filematch = './**/*.mcfunction' | |
def update_file(filename: str, oldcr: str, newcr: str): | |
'''update_file(filename, str, str) -> open(filename).write() | |
Updates the file with a copyright header''' | |
with open(filename, 'r+') as file: | |
text = file.read() | |
if oldcr and text.startswith(oldcr): | |
text = text[len(oldcr):] | |
if not text.startswith(newcr): | |
text = newcr + text | |
file.seek(0) | |
file.truncate() | |
writeReturn = file.write(text) | |
return writeReturn | |
try: | |
oldcr = open('oldcr.txt').read() | |
except: | |
oldcr = None | |
copyright = open('copyright_text.txt').read() | |
for filename in iglob(filematch, recursive=True): | |
print('updating '+filename) | |
update_file(filename, oldcr, copyright) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment