Last active
March 16, 2023 14:50
-
-
Save linjackson78/209810407015c4f60388e470347eca4a to your computer and use it in GitHub Desktop.
A python script to batch add copyright header to files.
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
| import argparse | |
| import os | |
| import re | |
| help = """For example, to insert content of copyright.txt into .h and .m files under src/ and headers/ dir, | |
| you may run: python copyright_header_tool.py copyright.txt ./src ./headers --pattern='.*\.[h,m]$' """ | |
| parser = argparse.ArgumentParser(description="File header tools.", | |
| epilog=help) | |
| parser.add_argument("header", nargs=1, type=argparse.FileType("rb"), help="save your expected file header here.") | |
| parser.add_argument("paths", nargs="+", | |
| help="files inside these dirs will be processed.") | |
| parser.add_argument("--pattern", required=True, | |
| help="regexp to filter file names.") | |
| args = parser.parse_args() | |
| pattern = re.compile(args.pattern) | |
| files_to_process = [] | |
| for path in args.paths: | |
| for root, dirs, files in os.walk(path): | |
| for f in files: | |
| if pattern.match(f) is not None: | |
| abs_path = os.path.join(root, f) | |
| files_to_process.append(abs_path) | |
| if len(files_to_process) == 0: | |
| print "No files to process." | |
| exit(0) | |
| for f in files_to_process: | |
| print f | |
| s = raw_input("Files above will be processed, continue? y/n: ") | |
| if s != "y": | |
| print "Canceled." | |
| exit(0) | |
| header = args.header[0].read() | |
| for p in files_to_process: | |
| with open(p, "r+b") as f: | |
| print u"Processing {}".format(p) | |
| content = f.read() | |
| f.seek(0, os.SEEK_SET) | |
| f.write(header) | |
| f.write(content) | |
| print "All done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
python copyright_header_tool.py -hfor help.