#!/usr/bin/python
#
# Copyright 2013 Lars Wirzenius
#
# 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 3 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, see .
#
# =*= License: GPL-3+ =*=
# sed -n ' "$@"
import cliapp
import re
import sys
__version__ = '0.0'
class LicenseSummary(cliapp.Application):
def add_settings(self):
self.settings.string_list(
['exclude', 'e'],
'exclude files matching REGEXP '
'from report of files without a license summary',
metavar='REGEXP')
self.settings.boolean(
['check'],
'fail if any files are missing a license summary, '
'or there are more than one summaries in use')
def setup(self):
self.pat = re.compile(r'=\*= [Ll]icen[cs]es?:\s*(?P\S.*\S)\s*=\*=')
self.summaries = {}
self.filenames = set()
def cleanup(self):
for summary in sorted(self.summaries.keys()):
self.report_group(summary, self.summaries[summary])
without_summary = self.filenames_without_summary()
if without_summary:
self.report_group('no summary', without_summary)
if self.settings['check']:
error = 0
if without_summary:
sys.stderr.write(
'ERROR: The following files have no license summary:\n')
for filename in sorted(without_summary):
sys.stderr.write(' %s\n' % filename)
error = 1
if len(self.summaries) > 1:
sys.stderr.write('ERROR: More than one summary in use\n')
sys.exit(error)
def report_group(self, summary, filenames):
self.output.write('%s (%d):\n' % (summary, len(filenames)))
for filename in sorted(filenames):
self.output.write('\t%s\n' % filename)
def filenames_without_summary(self):
return set(self.filenames).difference(self.filenames_with_summaries())
def filenames_with_summaries(self):
result = set()
for filenames in self.summaries.values():
result = result.union(set(filenames))
return result
def process_input(self, name):
self.filenames.add(name)
return cliapp.Application.process_input(self, name)
def process_input_line(self, filename, line):
m = self.pat.search(line)
if m:
s = m.group('summary')
self.summaries[s] = self.summaries.get(s, []) + [filename]
LicenseSummary(version=__version__).run()