Skip to content

Commit

Permalink
Merge branch 'master' into sim-scaled
Browse files Browse the repository at this point in the history
  • Loading branch information
mlinksva committed Nov 28, 2017
2 parents 0ca3587 + aab9f3d commit 316939d
Show file tree
Hide file tree
Showing 18 changed files with 463 additions and 189 deletions.
1 change: 1 addition & 0 deletions lib/licensee/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ module Matchers
autoload :NpmBower, 'licensee/matchers/npm_bower'
autoload :Package, 'licensee/matchers/package'
autoload :Reference, 'licensee/matchers/reference'
autoload :Spdx, 'licensee/matchers/spdx'
end
end
2 changes: 1 addition & 1 deletion lib/licensee/matchers/npm_bower.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class NpmBower < Licensee::Matchers::Package
# While we could parse the package.json or bower.json file, prefer
# a lenient regex for speed and security. Moar parsing moar problems.
LICENSE_REGEX = /
\s*[\"\']license[\"\']\s*\:\s*[\'\"]([a-z\-0-9\.]+)[\'\"],?\s*
\s*[\"\']license[\"\']\s*\:\s*[\'\"]([a-z\-0-9\.+ ()]+)[\'\"],?\s*
/ix

private
Expand Down
16 changes: 16 additions & 0 deletions lib/licensee/matchers/spdx.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Licensee
module Matchers
class Spdx < Licensee::Matchers::Package
# While we could parse the LICENSE.spdx file, prefer
# a lenient regex for speed and security. Moar parsing moar problems.
LICENSE_REGEX = /PackageLicenseDeclared:\s*([a-z\-0-9\. +()]+)\s*/i

private

def license_property
match = @file.content.match LICENSE_REGEX
match[1].downcase if match && match[1]
end
end
end
end
12 changes: 6 additions & 6 deletions lib/licensee/project_files/license_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class LicenseFile < Licensee::ProjectFiles::ProjectFile
PREFERRED_EXT = %w[md markdown txt].freeze
PREFERRED_EXT_REGEX = /\.#{Regexp.union(PREFERRED_EXT)}\z/

# Regex to match any extension
ANY_EXT_REGEX = %r{\.[^./]+\z}
# Regex to match any extension except .spdx
NONSPDX_EXT_REGEX = %r{\.(?!spdx)[^./]+\z}

# Regex to match, LICENSE, LICENCE, unlicense, etc.
LICENSE_REGEX = /(un)?licen[sc]e/i
Expand All @@ -28,17 +28,17 @@ class LicenseFile < Licensee::ProjectFiles::ProjectFile
/\A#{LICENSE_REGEX}#{PREFERRED_EXT_REGEX}\z/ => 0.95, # LICENSE.md
/\A#{COPYING_REGEX}\z/ => 0.90, # COPYING
/\A#{COPYING_REGEX}#{PREFERRED_EXT_REGEX}\z/ => 0.85, # COPYING.md
/\A#{LICENSE_REGEX}#{ANY_EXT_REGEX}\z/ => 0.80, # LICENSE.textile
/\A#{COPYING_REGEX}#{ANY_EXT_REGEX}\z/ => 0.75, # COPYING.textile
/\A#{LICENSE_REGEX}#{NONSPDX_EXT_REGEX}\z/ => 0.80, # LICENSE.textile
/\A#{COPYING_REGEX}#{NONSPDX_EXT_REGEX}\z/ => 0.75, # COPYING.textile
/\A#{LICENSE_REGEX}[-_]/ => 0.70, # LICENSE-MIT
/\A#{COPYING_REGEX}[-_]/ => 0.65, # COPYING-MIT
/[-_]#{LICENSE_REGEX}/ => 0.60, # MIT-LICENSE-MIT
/[-_]#{COPYING_REGEX}/ => 0.55, # MIT-COPYING
/\A#{OFL_REGEX}#{PREFERRED_EXT_REGEX}/ => 0.50, # OFL.md
/\A#{OFL_REGEX}#{ANY_EXT_REGEX}/ => 0.45, # OFL.textile
/\A#{OFL_REGEX}#{NONSPDX_EXT_REGEX}/ => 0.45, # OFL.textile
/\A#{OFL_REGEX}\z/ => 0.40, # OFL
/\A#{PATENTS_REGEX}\z/ => 0.35, # PATENTS
/\A#{PATENTS_REGEX}#{ANY_EXT_REGEX}\z/ => 0.30, # PATENTS.txt
/\A#{PATENTS_REGEX}#{NONSPDX_EXT_REGEX}\z/ => 0.30, # PATENTS.txt
// => 0.00 # Catch all
}.freeze

Expand Down
21 changes: 13 additions & 8 deletions lib/licensee/project_files/package_manager_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,26 @@ class PackageManagerFile < Licensee::ProjectFiles::ProjectFile

# Hash of Filename => [possible matchers]
FILENAMES_EXTENSIONS = {
'DESCRIPTION' => [Matchers::Cran],
'dist.ini' => [Matchers::DistZilla]
'DESCRIPTION' => [Matchers::Cran],
'dist.ini' => [Matchers::DistZilla],
'LICENSE.spdx' => [Matchers::Spdx]
}.freeze

FILENAMES_SCORES = {
'package.json' => 1.0,
'LICENSE.spdx' => 1.0,
'DESCRIPTION' => 0.9,
'dist.ini' => 0.8,
'bower.json' => 0.75
}.freeze

def possible_matchers
MATCHERS_EXTENSIONS[extension] || FILENAMES_EXTENSIONS[filename] || []
end

def self.name_score(filename)
return 1.0 if ['.gemspec', '.cabal'].include?(File.extname(filename))
return 1.0 if filename == 'package.json'
return 0.8 if filename == 'dist.ini'
return 0.9 if filename == 'DESCRIPTION'
return 0.75 if filename == 'bower.json'
0.0
return 1.0 if ['.gemspec', '.cabal'].include?(File.extname(filename))
FILENAMES_SCORES[filename] || 0.0
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/licensee/project_files/project_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def license
def copyright?
return false unless is_a?(LicenseFile)
return false unless matcher.is_a?(Matchers::Copyright)
filename =~ /\Acopyright(?:#{LicenseFile::ANY_EXT_REGEX})?\z/i
filename =~ /\Acopyright(?:#{LicenseFile::NONSPDX_EXT_REGEX})?\z/i
end
end
end
Expand Down
1 change: 1 addition & 0 deletions script/diff
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Example usage: cat LICENSE.md | be script/diff mit

require 'licensee'
require 'tmpdir'

if STDIN.tty? || ARGV[0].to_s.empty?
puts 'USAGE: [LICENSE_CONTENT] | script/diff [LICENSE_ID]'
Expand Down
13 changes: 11 additions & 2 deletions spec/licensee/matchers/npm_bower_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
let(:content) { '"license": "mit"' }
let(:file) { Licensee::ProjectFiles::LicenseFile.new(content, 'LICENSE.txt') }
let(:mit) { Licensee::License.find('mit') }
let(:no_license) { Licensee::License.find('no-license') }
let(:other) { Licensee::License.find('other') }

subject { described_class.new(file) }

it 'matches' do
Expand Down Expand Up @@ -42,7 +43,15 @@
let(:content) { "'license': 'foo'" }

it 'returns other' do
expect(subject.match).to eql(Licensee::License.find('other'))
expect(subject.match).to eql(other)
end
end

context 'a license expression' do
let(:content) { "'license': '(MIT OR Apache-2.0 OR AGPL-3.0+)'" }

it 'returns other' do
expect(subject.match).to eql(other)
end
end
end
41 changes: 41 additions & 0 deletions spec/licensee/matchers/spdx_matcher_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
RSpec.describe Licensee::Matchers::Spdx do
let(:content) { 'PackageLicenseDeclared: MIT' }
let(:file) do
Licensee::ProjectFiles::LicenseFile.new(content, 'LICENSE.spdx')
end
let(:mit) { Licensee::License.find('mit') }
let(:other) { Licensee::License.find('other') }
subject { described_class.new(file) }

it 'matches' do
expect(subject.match).to eql(mit)
end

it 'has a confidence' do
expect(subject.confidence).to eql(90)
end

context 'no license field' do
let(:content) { 'foo: bar' }

it 'returns nil' do
expect(subject.match).to be_nil
end
end

context 'an unknown license' do
let(:content) { 'PackageLicenseDeclared: xyz' }

it 'returns other' do
expect(subject.match).to eql(other)
end
end

context 'a license expression' do
let(:content) { 'PackageLicenseDeclared: (MIT OR Apache-2.0)' }

it 'returns other' do
expect(subject.match).to eql(other)
end
end
end
4 changes: 2 additions & 2 deletions spec/licensee/project_files/license_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@

context 'any extension regex' do
it 'matches .foo' do
expect(described_class::ANY_EXT_REGEX).to match('.foo')
expect(described_class::NONSPDX_EXT_REGEX).to match('.foo')
end

it 'does not match .md/foo' do
expect(described_class::ANY_EXT_REGEX).to_not match('.md/foo')
expect(described_class::NONSPDX_EXT_REGEX).to_not match('.md/foo')
end
end

Expand Down

0 comments on commit 316939d

Please sign in to comment.