Skip to content

Commit

Permalink
add multiple license support to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
benbalter committed Jun 22, 2017
1 parent 84b450a commit 821bb22
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 36 deletions.
58 changes: 26 additions & 32 deletions bin/licensee
Original file line number Diff line number Diff line change
Expand Up @@ -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

if matched_file.license
puts "License: #{matched_file.license.meta['title']}"

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)}"
puts "Matched files: #{project.matched_files.map(&:filename)}"

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.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?
8 changes: 4 additions & 4 deletions lib/licensee/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def licenses

# 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
Expand All @@ -36,7 +36,7 @@ def matched_files

# 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
Expand All @@ -49,8 +49,8 @@ def license_files
# 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
Expand Down
72 changes: 72 additions & 0 deletions spec/licensee/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 821bb22

Please sign in to comment.