Skip to content

Instantly share code, notes, and snippets.

@gokhansolak
Created February 5, 2020 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gokhansolak/0544ba85f375e80a14e60131c9503ecf to your computer and use it in GitHub Desktop.
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
#! /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