view licenser/licenses.py @ 1:cc5add25bf83

abstract License to its own class and do the work there
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 10 May 2010 11:24:02 -0700
parents b0665b243ccd
children b8d620fa1116
line wrap: on
line source

import os

from datetime import datetime

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 __call__(self, directory, **kw):
    for var in self.variables:
      if var not in kw:
        print 'Enter %s: ' % var,
        kw[var] = raw_input()
    self.pre(kw)
    self.interpolate(directory, kw)

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

  def interpolate(self, directory, variables):
    for file in self.files(directory):
      pass

  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' ]
                
  def pre(self, variables):
    variables['year'] = datetime.now().year