Created
February 3, 2017 12:19
-
-
Save magnific0/0da89f2faf7d33b2a9165cd9602f2bb8 to your computer and use it in GitHub Desktop.
Tudat update all files to new (short) license
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
import os | |
import sys | |
import re | |
import fnmatch | |
import codecs | |
# Define copyright header for CMakeLists | |
newlic1 = "".join((" # Copyright (c) 2010-2017, Delft University of Technology\n", | |
" # All rigths reserved\n", | |
" #\n", | |
" # This file is part of the Tudat. Redistribution and use in source and\n", | |
" # binary forms, with or without modification, are permitted exclusively\n", | |
" # under the terms of the Modified BSD license. You should have received\n", | |
" # a copy of the license with this file. If not, please or visit:\n", | |
" # http://tudat.tudelft.nl/LICENSE.")) | |
# Adapt for cpp and h and matlab files | |
newlic2 = newlic1.replace("#","*") | |
newlic2 = "/" + newlic2[1:] | |
newlic3 = newlic1.replace("#","%") | |
topsection = "((\/\*)|(\s{0,2}#))\s{1,4}(Copy).*(Delft).*(\n\s{0,2}(\*|#).*)*" | |
# Generic regex expression for matching a section | |
# Ends section based on white comment line (\s{0,1}[\*|#]\s{1,4}(%s)\w*\n((.)*\n)*?(\s{0,1}[\*|#](\s*|\/)(?:\n)))' | |
regex_sec_generic = '(\s{0,1}(\*|#|%%)\s{1,4}(%s)\w{1,6}\s*\n)((.|\n)*?)(?=^(\s{0,1}(\*|#|%%)((\s{1,4}\w{5,10}\s*)|\/)){0,1}\s*$)' | |
# Specific regex for matching copyright section | |
regex_sec_copyright = '((\/\*)|(\s{0,2}(#|%)))\s{1,4}(Copy)(.|\n)*?(DAMAGE\.)' | |
# Specific implementation of changelog, notes and references sections, resp | |
regex_sec_changelog = regex_sec_generic % "[C|c][H|h][A|a][N|n]" | |
regex_sec_notes = regex_sec_generic % "[N|n][O|o][T|t][E|e]" | |
regex_sec_references = regex_sec_generic % "[R|r][E|e][F|f][E|e]|[B|b][I|i][B|b][L|l]|[C|c][I|i][T|t][A|a]|[S|s][O|o][U|u][R|r]" | |
for dname, dirs, files in os.walk("/home/jacco/Gits/tudatBundle/tudat/Tudat"): | |
for fname in files: | |
# dname = '/home/jacco/Gits/tudatBundle/tudat/Tudat/Astrodynamics/MissionSegments/UnitTests' | |
# fname = 'unitTestMathematicalShapeFunctions.cpp' | |
#print(fname) | |
fpath = os.path.join(dname, fname) | |
if fname.endswith(('.txt', '.example', '.cmake','.in', '%m', '.cpp', '.h')): | |
with codecs.open(fpath, 'r', 'utf-8') as f: | |
s = f.read() | |
s = s.encode('ascii', 'ignore').decode('ascii') | |
# Debug | |
# print("== CHANGELOG ==") | |
# c = re.search(regex_sec_changelog,s, re.M) | |
# print(len(c.group(0).split('\n'))) | |
# print(c.group(0)) | |
# print("== REFERENCES ==") | |
# c = re.search(regex_sec_references,s, re.M) | |
# print(c.group(0)) | |
# print(len(c.group(0).split('\n'))) | |
# print("== NOTES ==") | |
# c = re.search(regex_sec_notes,s, re.M) | |
# print(c.group(0)) | |
# print(len(c.group(0).split('\n'))) | |
# sys.exit() | |
# Delete the changelog | |
s = re.sub(regex_sec_changelog, "", s, flags=re.M) | |
#print(s) | |
#sys.exit() | |
# Match references and notes | |
c = re.search(regex_sec_notes, s, re.M) | |
if c is not None: | |
if len(c.group(0).split('\n')) <= 3: | |
s = re.sub(regex_sec_notes, "", s, flags=re.M) | |
c = re.search(regex_sec_references, s, re.M) | |
if c is not None: | |
if len(c.group(0).split('\n')) <= 3: | |
s = re.sub(regex_sec_references, "", s, flags=re.M) | |
# Replace the copyright | |
c = re.search(regex_sec_copyright, s) | |
if c is not None: | |
if re.match("\/\*",c.group(0)): | |
s = re.sub(regex_sec_copyright, newlic2, s) | |
elif re.match("\s{0,2}#",c.group(0)): | |
s = re.sub(regex_sec_copyright, newlic1, s) | |
elif re.match("\s{0,2}%",c.group(0)): | |
s = re.sub(regex_sec_copyright, newlic3, s) | |
else: | |
print("Couldn't determine comment type for %s" % fpath ) | |
with open(fpath, "w") as f: | |
f.write(s) | |
else: | |
print("Couldn't find match in %s" % fpath) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Features:
Regex build and tested with http://www.regexr.com. The section regex detects the beginning of a comment section (by the section heading) and matches everything in between till either the next section heading or end of comment block (using positive look ahead).