Created
March 21, 2011 22:37
-
-
Save dokterbob/880378 to your computer and use it in GitHub Desktop.
Script for automatically prepending copyright notice to a file
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
#!/usr/bin/python | |
import sys | |
from optparse import OptionParser | |
def main(): | |
parser = OptionParser(usage="usage: %prog [options] <notice_filename> <code_filename>") | |
(options, args) = parser.parse_args() | |
if len(args) != 2: | |
parser.error('incorrect number of arguments') | |
notice_filename = args[0] | |
filename = args[1] | |
notice_file = open(notice_filename) | |
notice = notice_file.read() | |
notice_file.close() | |
code_file = open(filename, 'r+') | |
code = code_file.read() | |
if notice in code[:len(notice*2)]: | |
print 'Copyright notice already in file %s' % filename | |
sys.exit(0) | |
print 'Appending copyright notice to beginning of %s' % filename | |
code_file.seek(0) | |
split_first_line = code.split('\n', 1) | |
first_line = split_first_line[0] | |
if first_line[:2] == '#!': | |
print 'Shebang found, preserving' | |
code = split_first_line[1] | |
code_file.write(first_line+'\n') | |
code_file.write(notice) | |
code_file.write(code) | |
code_file.close() | |
sys.exit(0) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For example, to add the copyright notice in copyright-notice.txt to the beginning of every Python file in a git repository:
git filter-branch --tree-filter 'find . -name "*.py"
-exec copywriter.py copyright-notice.txt {} ;' HEAD