Created
October 8, 2016 17:41
-
-
Save nathiss/5613ed38cb881967d5ffd9773350f827 to your computer and use it in GitHub Desktop.
Script for appending copyright info on beginning of the file(s).
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
| #!/usr/bin/env python3 | |
| # coding: utf-8 | |
| import argparse | |
| import os | |
| import subprocess | |
| def append_copyright(info, des): | |
| content = open(des, 'r').read() | |
| content = info + content | |
| open(des, 'w').write(content) | |
| # ---------------------------- | |
| # Parse command line arguments | |
| # ---------------------------- | |
| parser = argparse.ArgumentParser(prog='Copyright', | |
| description='Append copyright info on beginning of the file(s).', | |
| usage='%(prog)s [options] CP_SOURCE DESTINATION...') | |
| # Boolean options | |
| parser.add_argument('-r', '--recursive', | |
| action='store_true', help='Select files recursively.') | |
| parser.add_argument('-v', '--version', | |
| action='version', version='%(prog)s 1.0') | |
| # Needed arguments | |
| parser.add_argument('cp_source', type=str, | |
| help='File with copyright information.') | |
| parser.add_argument('destination', nargs='+', type=str, | |
| help='File(s) (or dir(s) if it\'s running recursively) where copyright information will be stored.') | |
| args = parser.parse_args() | |
| # ---------------------------- | |
| # Append copyright information | |
| # ---------------------------- | |
| copyright_info = open(args.cp_source, 'r').read() | |
| if args.recursive: | |
| for des_dir in args.destination: | |
| files = subprocess.check_output('find '+des_dir+' -type f', shell=True).splitlines() | |
| for des_file in files: | |
| append_copyright(copyright_info, des_file) | |
| else: | |
| for des_file in args.destination: | |
| append_copyright(copyright_info, des_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment