view licenser/licenses.py @ 10:952eb98f6f6e

remove whitespace
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 22 Nov 2011 16:59:34 -0800
parents 0a01ed263c06
children a8ad01735f71
line wrap: on
line source

# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is mozilla.org code.
#
# The Initial Developer of the Original Code is
# Mozilla.org.
# Portions created by the Initial Developer are Copyright (C) 2010
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#     Jeff Hammel <jhammel@mozilla.com>     (Original author)
#
# Alternatively, the contents of this file may be used under the terms of
# either of the GNU General Public License Version 2 or later (the "GPL"),
# or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****

import os
from datetime import datetime
from string import Template

class License(object):
  """Abstract base class for a license"""

  variables = [] # required variables

  def __init__(self):
    if not os.path.isabs(self.template):
      self.template = os.path.join(os.path.dirname(__file__),
                                   'licenses',
                                   self.template)
    assert os.path.exists(self.template)

  def print_license(self):
    f = file(self.template)
    print f.read()
    f.close()

  def __call__(self, directory, **kw):
    variables = self.obtain_variables(**kw)
    self.interpolate(directory, variables)

  def obtain_variables(self, **kw):
    for var in self.variables:
      if var not in kw:
        print 'Enter %s: ' % var,
        kw[var] = raw_input()
    self.pre(kw)
    return kw

  def pre(self, variables):
    """do anything that needs to be done before interpolation"""

  def interpolate_file(self, _file, variables):

    # get the file lines
    f = file(_file)
    lines = [ i.rstrip() for i in f.readlines() ]
    f.close()
    f = file(_file, 'w')

    # print shebang if it exists
    if lines[0].startswith('#!'):
      shebang = lines.pop(0)
      print >> f, shebang
      print >> f

    # print the license
    g = file(self.template)
    for line in g.readlines():
      line = line.rstrip()
      _template = Template(line)
      print >> f, '# %s' % _template.substitute(**variables)
    g.close()

    # print the rest of the file
    for line in lines:
      print >> f, line.rstrip()
    f.close()


  def interpolate(self, directory, variables):
    for _file in self.files(directory):
      self.interpolate_file(_file, variables)

  def isempty(self, path):
    """
    determines if a python file is empty;  that is, contains only comments
    """
    for line in file(path, 'r').readlines():
        line = line.strip()
        if line and line[0] != '#':
            return False
    return True

  def files(self, directory):
    files = set()
    for dirpath, _, filenames in os.walk(directory):
        for f in filenames:
            if f.endswith('.py'): # could use os.path.splitext()
                path = os.path.join(dirpath, f)
                if not self.isempty(path):
                    files.add(path)
    return files


class MPL(License):
  """Mozilla Public License"""
  template = 'MPL' # could be implicit here
  variables = [ 'author', 'email' ]

  def pre(self, variables):
    variables['year'] = datetime.now().year