Skip to Content.
Sympa Menu

idok-commit - [idok-commit] idok commit r704 - branches/opensource/python/license_adder

idok-commit AT lists.psi.ch

Subject: Commit emails of the iDok project

List archive

[idok-commit] idok commit r704 - branches/opensource/python/license_adder


Chronological Thread 
  • From: "Apache" <apache AT savannah.psi.ch>
  • To: idok-commit AT lists.psi.ch
  • Subject: [idok-commit] idok commit r704 - branches/opensource/python/license_adder
  • Date: Tue, 5 Feb 2008 14:24:40 +0100
  • List-archive: <https://lists.web.psi.ch/pipermail/idok-commit/>
  • List-id: Commit emails of the iDok project <idok-commit.lists.psi.ch>

Author: geus AT PSI.CH
Date: Tue Feb 5 14:24:39 2008
New Revision: 704

Log:
Implemented python script that adds/updates license header for all source
files in a directory tree

Added:
branches/opensource/python/license_adder/
branches/opensource/python/license_adder/license_adder.py

Added: branches/opensource/python/license_adder/license_adder.py
==============================================================================
--- (empty file)
+++ branches/opensource/python/license_adder/license_adder.py Tue Feb 5
14:24:39 2008
@@ -0,0 +1,138 @@
+import cStringIO, logging, re, sys, os
+
+"""
+Add to/update license headers in source files
+
+Supports *.sh, *.py and *.java files
+"""
+
+LICENSE = """Copyright (C) 2006-2008 iDok team.
+
+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; either version 2
+of the License, or (at your option) any later version.
+
+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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+"""
+
+LANG_INFO = {".java": ["/**",
+ " * ",
+ " */",
+ r"([/][*][*].*?\n[ ][*][/]\n)[^ ][^*]",
+ None],
+ ".py": ["##",
+ "## ",
+ "##",
+ r"\n*([#][#].*?\n[#][#]\n)[^#]",
+ r"[#][!].*\n"],
+ ".sh": ["##",
+ "## ",
+ "##",
+ r"\n*([#][#].*?\n[#][#]\n)[^#]",
+ r"[#][!].*\n"]
+ }
+
+def main():
+ "Main program"
+ configure_loggers()
+ base_dir = sys.argv[1]
+ for root, dirs, files in os.walk(base_dir):
+ for file in files:
+ name, ext = os.path.splitext(file)
+ if ext in LANG_INFO.keys():
+ path = os.path.join(root, file)
+ logging.debug("Processing file " + path)
+ update_license(path, ext)
+ if '.svn' in dirs:
+ dirs.remove('.svn') # don't visit .svn directories
+
+def configure_loggers():
+ "Configure logging framework."
+ # Root logger level
+ logging.getLogger().setLevel(logging.DEBUG)
+
+ # Define a Handler which writes INFO messages or higher to the sys.stderr
+ console = logging.StreamHandler()
+ console.setLevel(logging.INFO)
+ # Set a format which is simpler for console use
+ formatter = logging.Formatter('%(message)s')
+ # Tell the handler to use this format
+ console.setFormatter(formatter)
+ # Add the handler to the root logger
+ logging.getLogger().addHandler(console)
+
+ # Define a Handler which writes ALL messages to a file
+ logger = logging.FileHandler('license_adder.log', "w")
+ logger.setLevel(logging.DEBUG)
+ formatter = logging.Formatter('%(levelname)-8s %(message)s')
+ logger.setFormatter(formatter)
+ logging.getLogger().addHandler(logger)
+
+def update_license(path, ext):
+ buf = open(path).read()
+ writer = cStringIO.StringIO()
+ start = 0
+
+ # Keep first line?
+ first_line_pattern = LANG_INFO[ext][4]
+ if first_line_pattern is not None:
+ match = re.match(first_line_pattern, buf)
+ if match is not None:
+ logging.debug("Found header line: " + match.group(0))
+ start = match.end(0)
+ writer.write(match.group(0))
+
+ # Add/replace license header
+ header_pattern = LANG_INFO[ext][3]
+ match = re.match(header_pattern, buf[start:], re.DOTALL)
+ if match is not None:
+ logging.debug("Found existing license header:\n" + match.group(1))

+ writer.write(buf[start:start+match.start(1)]) # keep characters in
front of header
+ start += match.end(1)
+ writer.write(generate_header(ext))
+ else:
+ if start != 0:
+ writer.write("\n")
+ writer.write(generate_header(ext))
+ writer.write("\n")
+ writer.write(buf[start:])
+
+ # Write updated file
+ if buf != writer.getvalue():
+ logging.info("Updating " + path)
+ f = open(path, "w")
+ f.truncate(0)
+ f.write(writer.getvalue())
+ f.close()
+ else:
+ logging.debug("Skipped " + path)
+
+def generate_header(ext):
+ "Return license header as a string for files with extension ext"
+ first_prefix = LANG_INFO[ext][0]
+ middle_prefix = LANG_INFO[ext][1]
+ last_prefix = LANG_INFO[ext][2]
+ buf = cStringIO.StringIO()
+ buf.write(first_prefix);
+ buf.write("\n");
+
+ reader = cStringIO.StringIO(LICENSE)
+ for line in reader:
+ buf.write(middle_prefix)
+ buf.write(line)
+
+ buf.write(last_prefix);
+ buf.write("\n");
+ return buf.getvalue()
+
+if __name__ == "__main__":
+ main()



  • [idok-commit] idok commit r704 - branches/opensource/python/license_adder, Apache, 02/05/2008

Archive powered by MHonArc 2.6.19.

Top of Page