From ef83d367c23aeadb7bf3b632b9c4bb7dc7869442 Mon Sep 17 00:00:00 2001 From: Guilhem Lavaux Date: Sat, 2 Mar 2013 18:36:08 -0500 Subject: [PATCH] Added script to add license information to the code --- build_tools/gather_sources.py | 91 +++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 build_tools/gather_sources.py diff --git a/build_tools/gather_sources.py b/build_tools/gather_sources.py new file mode 100644 index 0000000..3ff32f4 --- /dev/null +++ b/build_tools/gather_sources.py @@ -0,0 +1,91 @@ +import shutil +import tempfile +import re +from git import Repo,Tree,Blob + +def apply_license(license, relimit, filename): + header = re.sub(r'@FILENAME@', filename, license) + + f = file(filename) + lines = f.read() + f.close() + + lines = re.sub(relimit, '', lines) + lines = header + lines + + with tempfile.NamedTemporaryFile(delete=False) as tmp_sources: + tmp_sources.write(lines) + + shutil.move(tmp_sources.name, filename) + + +def apply_python_license(filename): + license="""#+ +# CosmoTool -- A cosmotool box for cosmology -- @FILENAME@ +# Copyright (C) 2007-2013 Guilhem Lavaux +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +#+ +""" + + print("Shell/Python file: %s" % filename) + relimit=r'^#\+\n(#.*\n)*#\+\n' + apply_license(license, relimit, filename) + + +def apply_cpp_license(filename): + license="""/*+ + CosmoTool -- A cosmotool box for cosmology -- @FILENAME@ + Copyright (C) 2007-2013 Guilhem Lavaux + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ++*/ +""" + relimit = r'^(?s)/\*\+.*\+\*/' + print("C++ file: %s" % filename) + apply_license(license, relimit, filename) + + +def analyze_tree(prefix, t): + for ename,entry in t.items(): + if ename == 'external' or ename == 'zobov': + continue + if type(entry) == Tree: + analyze_tree(prefix + "/" + ename, entry) + elif type(entry) == Blob: + if re.match(".*\.(sh|py|pyx)$",ename) != None: + fname=prefix+"/"+ename + apply_python_license(fname) + if re.match('.*\.(cpp|hpp|h)$', ename) != None: + fname=prefix+"/"+ename + apply_cpp_license(fname) + + +if __name__=="__main__": + repo = Repo(".") + assert repo.bare == False + t = repo.tree() + analyze_tree(".", t)