Created
February 5, 2020 15:21
-
-
Save gokhansolak/0544ba85f375e80a14e60131c9503ecf to your computer and use it in GitHub Desktop.
This program adds the given license notice at the beginning of all c, cpp, h, hpp files in a folder
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 | |
# This program adds the given license notice at the | |
# beginning of all c, cpp, h, hpp files in a folder | |
# | |
# usage: | |
# change to the directory that contains the code files | |
# python3 add_license_notice.py <license_file> <source_path> | |
# it will apply the changes too all files in that directory | |
import glob | |
import sys | |
if len(sys.argv) != 3: | |
print('Usage: \n\tpython3 add_license_notice.py <license_file> <source_path>') | |
exit() | |
code_files = [] | |
for ftype in ['c', 'cpp', 'h', 'hpp']: | |
code_files += glob.glob(sys.argv[2]+"/*."+ftype) | |
print("Files:\n"+str(code_files)) | |
with open (sys.argv[1], 'r' ) as f: | |
license = f.read() | |
print("License:\n"+license) | |
for fname in code_files: | |
with open (fname, 'r+' ) as f: | |
content = f.read() | |
content_new = license + '\n' + content | |
f.seek(0) | |
f.write(content_new) | |
f.truncate() | |
print("Completed.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment