Class: Licensee::ProjectFiles::LicenseFile
- Inherits:
-
ProjectFile
- Object
- ProjectFile
- Licensee::ProjectFiles::LicenseFile
- Includes:
- ContentHelper
- Defined in:
- lib/licensee/project_files/license_file.rb
Direct Known Subclasses
Constant Summary collapse
- PREFERRED_EXT =
List of extensions to give preference to
%w[md markdown txt html].freeze
- PREFERRED_EXT_REGEX =
/\.#{Regexp.union(PREFERRED_EXT)}\z/
- LICENSE_EXT_REGEX =
Regex to match any extension except .spdx or .header
%r{\.(?!spdx|header)[^./]+\z}i
- OTHER_EXT_REGEX =
Regex to match any extension except a few unlikely as license texts with complex filenames
%r{\.(?!xml|go|gemspec)[^./]+\z}i
- ANY_EXT_REGEX =
Regex to match any extension
%r{\.[^./]+\z}i
- LICENSE_REGEX =
Regex to match, LICENSE, LICENCE, unlicense, etc.
/(un)?licen[sc]e/i
- COPYING_REGEX =
Regex to match COPYING, COPYRIGHT, etc.
/copy(ing|right)/i
- OFL_REGEX =
Regex to match OFL.
/ofl/i
- PATENTS_REGEX =
BSD + PATENTS patent file
/patents/i
- FILENAME_REGEXES =
Hash of Regex => score with which to score potential license files
{ /\A#{LICENSE_REGEX}\z/ => 1.00, # LICENSE /\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}#{LICENSE_EXT_REGEX}\z/ => 0.80, # LICENSE.textile /\A#{COPYING_REGEX}#{ANY_EXT_REGEX}\z/ => 0.75, # COPYING.textile /\A#{LICENSE_REGEX}[-_][^.]*#{OTHER_EXT_REGEX}?\z/ => 0.70, # LICENSE-MIT /\A#{COPYING_REGEX}[-_][^.]*#{OTHER_EXT_REGEX}?\z/ => 0.65, # COPYING-MIT /\A\w+[-_]#{LICENSE_REGEX}[^.]*#{OTHER_EXT_REGEX}?\z/ => 0.60, # MIT-LICENSE-MIT /\A\w+[-_]#{COPYING_REGEX}[^.]*#{OTHER_EXT_REGEX}?\z/ => 0.55, # MIT-COPYING /\A#{OFL_REGEX}#{PREFERRED_EXT_REGEX}/ => 0.50, # OFL.md /\A#{OFL_REGEX}#{OTHER_EXT_REGEX}/ => 0.45, # OFL.textile /\A#{OFL_REGEX}\z/ => 0.40, # OFL /\A#{PATENTS_REGEX}\z/ => 0.35, # PATENTS /\A#{PATENTS_REGEX}#{OTHER_EXT_REGEX}\z/ => 0.30, # PATENTS.txt // => 0.00 # Catch all }.freeze
- CC_FALSE_POSITIVE_REGEX =
CC-NC and CC-ND are not open source licenses and should not be detected as CC-BY or CC-BY-SA which are 98%+ similar
/ ^(creative\ commons\ )?Attribution-(NonCommercial|NoDerivatives) /xi
Constants included from ContentHelper
ContentHelper::DIGEST, ContentHelper::END_OF_TERMS_REGEX, ContentHelper::NORMALIZATIONS, ContentHelper::REGEXES, ContentHelper::START_REGEX, ContentHelper::STRIP_METHODS, ContentHelper::VARIETAL_WORDS
Constants inherited from ProjectFile
ProjectFile::ENCODING, ProjectFile::ENCODING_OPTIONS, ProjectFile::HASH_METHODS
Instance Attribute Summary
Attributes inherited from ProjectFile
Class Method Summary collapse
-
.lesser_gpl_score(filename) ⇒ Object
case-insensitive block to determine if the given file is LICENSE.lesser.
- .name_score(filename) ⇒ Object
Instance Method Summary collapse
- #attribution ⇒ Object
- #gpl? ⇒ Boolean
- #lgpl? ⇒ Boolean
- #license ⇒ Object
- #possible_matchers ⇒ Object
-
#potential_false_positive? ⇒ Boolean
Is this file likely to result in a creative commons false positive?.
Methods included from ContentHelper
const_missing, #content_hash, #content_normalized, #content_without_title_and_version, format_percent, #length, #length_delta, #similarity, title_regex, #wordset, wrap
Methods inherited from ProjectFile
#confidence, #content_hash, #content_normalized, #copyright?, #directory, #filename, #initialize, #matched_license, #matcher, #path_relative_to_root
Methods included from HashHelper
Constructor Details
This class inherits a constructor from Licensee::ProjectFiles::ProjectFile
Class Method Details
.lesser_gpl_score(filename) ⇒ Object
case-insensitive block to determine if the given file is LICENSE.lesser
98 99 100 |
# File 'lib/licensee/project_files/license_file.rb', line 98 def self.lesser_gpl_score(filename) filename.casecmp('copying.lesser').zero? ? 1 : 0 end |
.name_score(filename) ⇒ Object
93 94 95 |
# File 'lib/licensee/project_files/license_file.rb', line 93 def self.name_score(filename) FILENAME_REGEXES.find { |regex, _| filename =~ regex }[1] end |
Instance Method Details
#attribution ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/licensee/project_files/license_file.rb', line 64 def attribution @attribution ||= if copyright? || license.content&.include?('[fullname]') matches = Matchers::Copyright::REGEX .match(content_without_title_and_version) matches[0] if matches end end |
#gpl? ⇒ Boolean
81 82 83 |
# File 'lib/licensee/project_files/license_file.rb', line 81 def gpl? license&.gpl? end |
#lgpl? ⇒ Boolean
77 78 79 |
# File 'lib/licensee/project_files/license_file.rb', line 77 def lgpl? LicenseFile.lesser_gpl_score(filename) == 1 && license && license.lgpl? end |
#license ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/licensee/project_files/license_file.rb', line 85 def license if matcher&.match matcher.match else License.find('other') end end |
#possible_matchers ⇒ Object
60 61 62 |
# File 'lib/licensee/project_files/license_file.rb', line 60 def possible_matchers [Matchers::Copyright, Matchers::Exact, Matchers::Dice] end |
#potential_false_positive? ⇒ Boolean
Is this file likely to result in a creative commons false positive?
73 74 75 |
# File 'lib/licensee/project_files/license_file.rb', line 73 def potential_false_positive? content.strip =~ CC_FALSE_POSITIVE_REGEX end |