add multiple license support to cli

This commit is contained in:
Ben Balter 2017-06-22 12:00:58 -04:00
parent 84b450a7cc
commit 821bb22cf1
No known key found for this signature in database
GPG Key ID: DBB67C246AD356C4
3 changed files with 100 additions and 34 deletions

View File

@ -9,42 +9,36 @@ def format_percent(float)
end
project = Licensee.project(path, detect_packages: true, detect_readme: true)
license_file = project.license_file
matched_file = project.matched_file
if license_file
puts "License file: #{license_file.filename}"
puts "License hash: #{license_file.content_hash}"
puts "Attribution: #{license_file.attribution}" if license_file.attribution
if project.license
puts "License: #{project.license.name}"
elsif project.licenses
puts "Licenses: #{project.licenses.map(&:name)}"
else
puts "License: Not detected"
end
unless matched_file
puts 'License: Not detected'
exit 1
end
puts "Matched files: #{project.matched_files.map(&:filename)}"
if matched_file.license
puts "License: #{matched_file.license.meta['title']}"
project.matched_files.each do |matched_file|
puts "#{matched_file.filename}:"
puts " Content Hash: #{matched_file.content_hash}" if matched_file.respond_to?(:content_hash)
puts " Attribution: #{matched_file.attribution}" if matched_file.respond_to?(:attribution)
puts " License: #{matched_file.license.name}" if matched_file.license
puts " Confidence: #{format_percent(matched_file.confidence)}"
puts " Method: #{matched_file.matcher.class}" if matched_file.matcher
if matched_file.confidence
puts "Confidence: #{format_percent(matched_file.confidence)}"
end
puts "Method: #{matched_file.matcher.class}" if matched_file.matcher
exit 0
end
if matched_file.is_a?(Licensee::Project::LicenseFile)
matcher = Licensee::Matchers::Dice.new(matched_file)
licenses = matcher.licenses_by_similiarity
unless licenses.empty?
puts
puts "Here's the closest licenses:"
licenses[0...3].each do |license, similarity|
spdx_id = license.meta['spdx-id']
puts "* #{spdx_id} similarity: #{format_percent(similarity)}"
if matched_file.is_a?(Licensee::Project::LicenseFile) && matched_file.confidence != 100
matcher = Licensee::Matchers::Dice.new(matched_file)
licenses = matcher.licenses_by_similiarity
unless licenses.empty?
puts " Closest licenses:"
licenses[0...3].each do |license, similarity|
spdx_id = license.meta['spdx-id']
puts " * #{spdx_id} similarity: #{format_percent(similarity)}"
end
end
end
end
exit 1
exit !project.licenses.empty?

View File

@ -26,7 +26,7 @@ module Licensee
# Returns the ProjectFile used to determine the License
def matched_file
matched_files.first
matched_files.first if matched_files.count == 1 || lgpl?
end
# Returns an array of matches LicenseFiles
@ -36,7 +36,7 @@ module Licensee
# Returns the LicenseFile used to determine the License
def license_file
license_files.first
license_files.first if license_files.count == 1 || lgpl?
end
def license_files
@ -49,8 +49,8 @@ module Licensee
# Special case LGPL, which actually lives in LICENSE.lesser, per the
# license instructions. See https://git.io/viwyK
if files.first && files.first.license && files.first.license.gpl?
lesser = files.find { |l| l.lgpl? }
files.unshift(lesser) if lesser
lesser = files.find_index { |l| l.lgpl? }
files.unshift(files.delete_at(lesser)) if lesser
end
files

View File

@ -113,5 +113,77 @@
expect(subject.license).to eql(mit)
end
end
context "multiple licenses" do
let(:fixture) { 'multiple-license-files' }
it "returns nil for license" do
expect(subject.license).to be_nil
end
it "returns nil for matched_file" do
expect(subject.matched_file).to be_nil
end
it "returns nil for license_file" do
expect(subject.license_file).to be_nil
end
it "returns both licenses" do
expect(subject.licenses.count).to eql(2)
expect(subject.licenses.first).to eql(Licensee::License.find("mpl-2.0"))
expect(subject.licenses.last).to eql(mit)
end
it "returns both matched_files" do
expect(subject.matched_files.count).to eql(2)
expect(subject.matched_files.first.filename).to eql("LICENSE")
expect(subject.matched_files.last.filename).to eql("LICENSE.txt")
end
it "returns both license_files" do
expect(subject.license_files.count).to eql(2)
expect(subject.license_files.first.filename).to eql("LICENSE")
expect(subject.license_files.last.filename).to eql("LICENSE.txt")
end
end
context "lgpl" do
let(:gpl) { Licensee::License.find('gpl-3.0') }
let(:lgpl) { Licensee::License.find('lgpl-3.0') }
let(:fixture) { 'lgpl' }
it "license returns lgpl" do
expect(subject.license).to eql(lgpl)
end
it "matched_file returns copying.lesser" do
expect(subject.matched_file).to_not be_nil
expect(subject.matched_file.filename).to eql("COPYING.lesser")
end
it "license_file returns copying.lesser" do
expect(subject.license_file).to_not be_nil
expect(subject.license_file.filename).to eql("COPYING.lesser")
end
it "returns both licenses" do
expect(subject.licenses.count).to eql(2)
expect(subject.licenses.first).to eql(lgpl)
expect(subject.licenses.last).to eql(gpl)
end
it "returns both matched_files" do
expect(subject.matched_files.count).to eql(2)
expect(subject.matched_files.first.filename).to eql("COPYING.lesser")
expect(subject.matched_files.last.filename).to eql("LICENSE")
end
it "returns both license_files" do
expect(subject.license_files.count).to eql(2)
expect(subject.license_files.first.filename).to eql("COPYING.lesser")
expect(subject.license_files.last.filename).to eql("LICENSE")
end
end
end
end