Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

WIP: Refactored remaining licenses and lookups

  • Loading branch information...
commit b493151af56606230ed1acb06c12255712ef45b6 1 parent 50dba03
Ian Lesperance authored
View
15 lib/license_finder/gem_spec_details.rb
@@ -35,12 +35,15 @@ def dependency
def determine_license
return @spec.license if @spec.license
- return 'MIT' if license_files.any?{|f| f.mit_license_body? || f.mit_license_header?}
- return 'Apache 2.0' if license_files.any?(&:apache_license_body?)
- return 'GPLv2' if license_files.any?(&:gplv2_license_body?)
- return 'ruby' if license_files.any?(&:ruby_license_body?)
- return 'LGPL' if license_files.any?(&:lgpl_license_body?)
- return 'ISC' if license_files.any?(&:isc_license_body?)
+
+ licenses = license_files.map(&:license)
+
+ return 'MIT' if licenses.include?(LicenseFinder::License::MIT)
+ return 'Apache 2.0' if licenses.include?(LicenseFinder::License::Apache)
+ return 'GPLv2' if licenses.include?(LicenseFinder::License::GPLv2)
+ return 'ruby' if licenses.include?(LicenseFinder::License::Ruby)
+ return 'LGPL' if licenses.include?(LicenseFinder::License::LGPL)
+ return 'ISC' if licenses.include?(LicenseFinder::License::ISC)
'other'
end
View
16 lib/license_finder/license.rb
@@ -10,6 +10,10 @@ class << self
def inherited(descendant)
LicenseFinder::License.all << descendant
end
+
+ def slug
+ name.gsub(/^.*::/, '').downcase
+ end
end
attr_reader :text
@@ -18,6 +22,18 @@ def initialize(text)
@text = text
end
+ def matches?
+ matches_body? || matches_header?
+ end
+
+ def matches_body?
+ false
+ end
+
+ def matches_header?
+ false
+ end
+
private
def on_single_line(text)
View
2  lib/license_finder/license/apache.rb
@@ -1,7 +1,7 @@
class LicenseFinder::License::Apache < LicenseFinder::License::Base
LICENSE_TEXT = (LicenseFinder::ROOT_PATH + 'templates/Apache-2.0-body').read
- def matches?
+ def matches_body?
!!on_single_line(text).index(on_single_line(LICENSE_TEXT))
end
end
View
2  lib/license_finder/license/gplv2.rb
@@ -1,7 +1,7 @@
class LicenseFinder::License::GPLv2 < LicenseFinder::License::Base
LICENSE_TEXT = (LicenseFinder::ROOT_PATH + 'templates/GPL-2.0-body').read
- def matches?
+ def matches_body?
!!on_single_line(text).index(on_single_line(LICENSE_TEXT))
end
end
View
7 lib/license_finder/license/isc.rb
@@ -0,0 +1,7 @@
+class LicenseFinder::License::ISC < LicenseFinder::License::Base
+ LICENSE_TEXT = (LicenseFinder::ROOT_PATH + 'templates/ISC-body').read
+
+ def matches_body?
+ !!on_single_line(text).index(on_single_line(LICENSE_TEXT))
+ end
+end
View
7 lib/license_finder/license/lgpl.rb
@@ -0,0 +1,7 @@
+class LicenseFinder::License::LGPL < LicenseFinder::License::Base
+ LICENSE_TEXT = (LicenseFinder::ROOT_PATH + 'templates/LGPL-body').read
+
+ def matches_body?
+ !!on_single_line(text).index(on_single_line(LICENSE_TEXT))
+ end
+end
View
7 lib/license_finder/license/mit.rb
@@ -5,8 +5,13 @@ class LicenseFinder::License::MIT < LicenseFinder::License::Base
ONE_LINER_REGEX = /is released under the MIT license/
URL_REGEX = %r{MIT Licence.*http://www.opensource.org/licenses/mit-license}
- def matches?
+ def matches_body?
!!cleaned_up(text).index(cleaned_up(LICENSE_TEXT)) ||
!!(on_single_line(text) =~ URL_REGEX)
end
+
+ def matches_header?
+ header = text.split("\n").first
+ header && ((header.strip =~ HEADER_REGEX) || !!(on_single_line(text) =~ ONE_LINER_REGEX))
+ end
end
View
7 lib/license_finder/license/ruby.rb
@@ -0,0 +1,7 @@
+class LicenseFinder::License::Ruby < LicenseFinder::License::Base
+ URL_REGEX = %r{http://www.ruby-lang.org/en/LICENSE.txt}
+
+ def matches_body?
+ !!(on_single_line(text) =~ URL_REGEX)
+ end
+end
View
64 lib/license_finder/license_file.rb
@@ -1,68 +1,34 @@
module LicenseFinder
class LicenseFile < FileParser
- MIT_LICENSE_TEXT = (LicenseFinder::ROOT_PATH + 'templates/MIT-body').read
- APACHE_LICENSE_TEXT = (LicenseFinder::ROOT_PATH + 'templates/Apache-2.0-body').read
- GPLv2_LICENSE_TEXT = (LicenseFinder::ROOT_PATH + 'templates/GPL-2.0-body').read
- LGPL_LICENSE_TEXT = (LicenseFinder::ROOT_PATH + 'templates/LGPL-body').read
- ISC_LICENSE_TEXT = (LicenseFinder::ROOT_PATH + 'templates/ISC-body').read
- MIT_HEADER_REGEX = /The MIT License/
MIT_DISCLAIMER_REGEX = /THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT\. IN NO EVENT SHALL ((\w+ ){2,8})BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE\./
- MIT_ONE_LINER_REGEX = /is released under the MIT license/
MIT_URL_REGEX = %r{MIT Licence.*http://www.opensource.org/licenses/mit-license}
- RUBY_URL_REGEX = %r{http://www.ruby-lang.org/en/LICENSE.txt}
- def body_type
- license = LicenseFinder::License.all.detect do |lic|
+ def license
+ LicenseFinder::License.all.detect do |lic|
lic.new(text).matches?
end
+ end
- if license
- license.name.gsub(/^.*::/, '').downcase
- elsif ruby_license_body?
- 'ruby'
- elsif lgpl_license_body?
- 'lgpl'
- elsif isc_license_body?
- 'isc'
- else
- 'other'
+ def body_type
+ license = LicenseFinder::License.all.detect do |lic|
+ lic.new(text).matches_body?
end
+
+ license && license.slug || 'other'
end
def header_type
- mit_license_header? ? 'mit' : 'other'
+ license = LicenseFinder::License.all.detect do |lic|
+ lic.new(text).matches_header?
+ end
+
+ license && license.slug || 'other'
end
def disclaimer_of_liability
mit_disclaimer_of_liability? ? "mit: #{@mit_authors}" : 'other'
end
- def mit_license_body?
- !!cleaned_up(text).index(cleaned_up(MIT_LICENSE_TEXT)) ||
- !!(on_single_line(text) =~ MIT_URL_REGEX)
- end
-
- def ruby_license_body?
- !!(on_single_line(text) =~ RUBY_URL_REGEX)
- end
-
- def apache_license_body?
- !!on_single_line(text).index(on_single_line(APACHE_LICENSE_TEXT))
- end
-
- def lgpl_license_body?
- !!on_single_line(text).index(on_single_line(LGPL_LICENSE_TEXT))
- end
-
- def isc_license_body?
- !!on_single_line(text).index(on_single_line(ISC_LICENSE_TEXT))
- end
-
- def mit_license_header?
- header = text.split("\n").first
- header && ((header.strip =~ MIT_HEADER_REGEX) || !!(on_single_line(text) =~ MIT_ONE_LINER_REGEX))
- end
-
def mit_disclaimer_of_liability?
if result = !!(on_single_line(text) =~ MIT_DISCLAIMER_REGEX)
@mit_authors = ($1 || '').strip
@@ -72,10 +38,6 @@ def mit_disclaimer_of_liability?
result
end
- def gplv2_license_body?
- !!on_single_line(text).index(on_single_line(GPLv2_LICENSE_TEXT))
- end
-
def to_hash
h = {
'file_name' => file_path,
View
21 spec/lib/license_finder/gem_spec_details_spec.rb
@@ -38,28 +38,33 @@ def license
its(:dependency_version) { should == '2.1.3' }
its(:install_path) { should == 'install/path' }
- describe "#gemspec licenses" do
+ describe "#determine_license" do
it "returns the license from the gemspec if provided" do
mock_gemspec = @mock_gemspec.new
stub(mock_gemspec).license { "MIT" }
LicenseFinder::GemSpecDetails.new(mock_gemspec).determine_license.should == "MIT"
end
- it "returns 'ruby' if if is a ruby license" do
+ it "returns the matched license if detected" do
mock_gemspec = @mock_gemspec.new
mock_license_file = LicenseFinder::LicenseFile.new('gem', 'gem/license/path')
- stub(mock_license_file).mit_license_body? { false }
- stub(mock_license_file).mit_license_header? { false }
- stub(mock_license_file).apache_license_body? { false }
- stub(mock_license_file).gplv2_license_body? { false }
+ stub(mock_license_file).license { LicenseFinder::License::Ruby }
- stub(mock_license_file).ruby_license_body? { true }
+ gemspec_details = LicenseFinder::GemSpecDetails.new(mock_gemspec)
+ stub(gemspec_details).license_files { [ mock_license_file ] }
+
+ gemspec_details.determine_license.should == "ruby"
+ end
+ it "returns 'other' otherwise" do
+ mock_gemspec = @mock_gemspec.new
+ mock_license_file = LicenseFinder::LicenseFile.new('gem', 'gem/license/path')
+ stub(mock_license_file).license { nil }
gemspec_details = LicenseFinder::GemSpecDetails.new(mock_gemspec)
stub(gemspec_details).license_files { [ mock_license_file ] }
- gemspec_details.determine_license.should == "ruby"
+ gemspec_details.determine_license.should == "other"
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.