diff options
author | Lars Wirzenius <liw@liw.fi> | 2013-04-24 20:54:33 +0100 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2013-04-24 20:54:33 +0100 |
commit | 41d0334857638016c9abb7a6d78c13ecaf2bf01b (patch) | |
tree | c10a2bd915654f7e5c95841ce9ea81c50df1d19c | |
parent | f61862c2daaae7acc07a8cfe1bb7d38d344c4a79 (diff) | |
parent | caeaeac50502940e5a372ce40c4fed7d87b62a72 (diff) | |
download | license-summary-41d0334857638016c9abb7a6d78c13ecaf2bf01b.tar.gz |
Merge branch 'check-mode'
-rwxr-xr-x | license-summary | 61 |
1 files changed, 45 insertions, 16 deletions
diff --git a/license-summary b/license-summary index 05ed6d1..90d07c8 100755 --- a/license-summary +++ b/license-summary @@ -23,6 +23,7 @@ import cliapp import re +import sys __version__ = '0.0' @@ -37,30 +38,58 @@ class LicenseSummary(cliapp.Application): '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<summary>\S.*\S)\s*=\*=') self.summaries = {} self.filenames = set() def cleanup(self): - for s in sorted(self.summaries.keys()): - ss = sorted(self.summaries[s]) - self.output.write('%s (%d):\n' % (s, len(ss))) - for filename in ss: - self.output.write('\t%s\n' % filename) - if filename in self.filenames: - self.filenames.remove(filename) - for pattern in self.settings['exclude']: - remove = [x for x in self.filenames if re.search(pattern, x)] - for filename in remove: - self.filenames.remove(filename) - if self.filenames: - self.output.write('no summary (%d):\n' % len(self.filenames)) - for filename in sorted(self.filenames): - self.output.write('\t%s\n' % filename) + 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): - self.filenames.add(filename) m = self.pat.search(line) if m: s = m.group('summary') |