Created
June 11, 2012 15:46
-
-
Save chipchilders/2910765 to your computer and use it in GitHub Desktop.
license text replacement script
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/env python | |
import os | |
import sys | |
def main(): | |
target_dir = sys.argv[len(sys.argv)-1] | |
for root, dirs, files in os.walk(target_dir): | |
for name in files: | |
filename = os.path.join(root, name) | |
known_type = False | |
if filename.endswith('.java'): | |
comment_chars = '//' | |
prog_header = None | |
license_header_file = '/Users/chip.childers/asfheader' | |
known_type = True | |
elif filename.endswith('.py'): | |
comment_chars = '#' | |
prog_header = '#!/usr/bin/env python\n' | |
license_header_file = '/Users/chip.childers/asfheaderhash' | |
known_type = True | |
elif filename.endswith('.sh'): | |
comment_chars = '#' | |
prog_header = '#!/usr/bin/env bash\n' | |
license_header_file = '/Users/chip.childers/asfheaderhash' | |
known_type = True | |
if known_type: | |
print 'changing: ' + filename | |
process_file(filename, comment_chars, license_header_file, prog_header) | |
else: | |
print 'skipping: ' + filename | |
def process_file(filename, comment_chars, license_header_file, prog_header): | |
file_to_read = open(filename, 'r') | |
file_lines = file_to_read.readlines() | |
file_to_read.close() | |
i=0 | |
if file_lines[0].startswith('#!'): | |
first_line_is_exec = True | |
else: | |
first_line_is_exec = False | |
for line in file_lines: | |
if not line.startswith(comment_chars): | |
break | |
i = i + 1 | |
header = open(license_header_file, 'r') | |
header_lines = header.readlines() | |
header.close() | |
del file_lines[0:i] | |
file_lines = header_lines + file_lines | |
if prog_header is not None and first_line_is_exec: | |
file_lines.insert(0, prog_header) | |
file_to_write = open(filename, 'w') | |
file_to_write.truncate() | |
file_to_write.writelines(file_lines) | |
file_to_write.close() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I just added an if statement and pointed to a local header file:
elif filename.endswith('.pot'):
comment_chars = '#'
prog_header = ''
license_header_file = '/Users/sebastiengoasguen/headerpot'
known_type = True