Class: LicenseFinder::Package

Inherits:
Object
  • Object
show all
Defined in:
lib/license_finder/package.rb

Overview

Super-class that adapts data from different package management systems (gems, npm, pip, etc.) to a common interface.

Guidance on adding a new system

  • subclass Package, and initialize based on the data you receive from the package manager

  • if the package specs will report license names, pass :spec_licenses in the constructor options

  • if the package’s files can be searched for licenses pass :install_path in the constructor options

  • otherwise, override #licenses_from_spec or #license_files

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, version = nil, options = {}) ⇒ Package

Returns a new instance of Package.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/license_finder/package.rb', line 36

def initialize(name, version = nil, options = {})
  @logger = options[:logger] || Core.default_logger

  ## DESCRIPTION
  @name = name
  @version = version || ''
  @authors = options[:authors] || ''
  @summary = options[:summary] || ''
  @description = options[:description] || ''
  @homepage = options[:homepage] || ''
  @package_url = options[:package_url].to_s
  @children = options[:children] || []
  @parents = Set.new # will be figured out later by package manager
  @groups = options[:groups] || []

  ## APPROVAL
  @permitted = false
  @restricted = false
  @manual_approval = nil

  ## LICENSING
  @license_names_from_spec = options[:spec_licenses] || []
  @install_path = options[:install_path]
  @missing = options[:missing] || false
  @decided_licenses = Set.new
end

Instance Attribute Details

#authorsObject (readonly)

Returns the value of attribute authors.



67
68
69
# File 'lib/license_finder/package.rb', line 67

def authors
  @authors
end

#childrenObject (readonly)

Returns the value of attribute children.



67
68
69
# File 'lib/license_finder/package.rb', line 67

def children
  @children
end

#descriptionObject (readonly)

Returns the value of attribute description.



67
68
69
# File 'lib/license_finder/package.rb', line 67

def description
  @description
end

#groupsObject (readonly)

Returns the value of attribute groups.



67
68
69
# File 'lib/license_finder/package.rb', line 67

def groups
  @groups
end

#homepageObject

DESCRIPTION



65
66
67
# File 'lib/license_finder/package.rb', line 65

def homepage
  @homepage
end

#install_pathObject (readonly)

checked in tests, otherwise private



126
127
128
# File 'lib/license_finder/package.rb', line 126

def install_path
  @install_path
end

#license_names_from_specObject (readonly)

LICENSING



125
126
127
# File 'lib/license_finder/package.rb', line 125

def license_names_from_spec
  @license_names_from_spec
end

#loggerObject (readonly)

Returns the value of attribute logger.



21
22
23
# File 'lib/license_finder/package.rb', line 21

def logger
  @logger
end

#manual_approvalObject (readonly)

Returns the value of attribute manual_approval.



104
105
106
# File 'lib/license_finder/package.rb', line 104

def manual_approval
  @manual_approval
end

#nameObject (readonly)

Returns the value of attribute name.



67
68
69
# File 'lib/license_finder/package.rb', line 67

def name
  @name
end

#package_urlObject

DESCRIPTION



65
66
67
# File 'lib/license_finder/package.rb', line 65

def package_url
  @package_url
end

#parentsObject (readonly)

Returns the value of attribute parents.



67
68
69
# File 'lib/license_finder/package.rb', line 67

def parents
  @parents
end

#summaryObject (readonly)

Returns the value of attribute summary.



67
68
69
# File 'lib/license_finder/package.rb', line 67

def summary
  @summary
end

#versionObject (readonly)

Returns the value of attribute version.



67
68
69
# File 'lib/license_finder/package.rb', line 67

def version
  @version
end

Class Method Details

.license_names_from_standard_spec(spec) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/license_finder/package.rb', line 23

def self.license_names_from_standard_spec(spec)
  licenses = spec['licenses'] || [spec['license']].compact
  licenses = [licenses] unless licenses.is_a?(Array)
  licenses = licenses.flatten
  licenses.map do |license|
    if license.is_a? Hash
      license['type']
    else
      license
    end
  end
end

Instance Method Details

#<=>(other) ⇒ Object

EQUALITY



108
109
110
111
112
113
# File 'lib/license_finder/package.rb', line 108

def <=>(other)
  eq_name = name <=> other.name
  return eq_name unless eq_name.zero?

  version <=> other.version
end

#activationsObject



132
133
134
135
136
# File 'lib/license_finder/package.rb', line 132

def activations
  licensing.activations.tap do |activations|
    activations.each { |activation| log_activation activation }
  end
end

#approved?Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
# File 'lib/license_finder/package.rb', line 81

def approved?
  # Question: is `!restricted?` redundant?
  # DecisionApplier does not call `permitted!` or `approved_manually!`
  # if a Package has been restricted.
  (approved_manually? || permitted?) && !restricted?
end

#approved_manually!(approval) ⇒ Object

APPROVAL



73
74
75
# File 'lib/license_finder/package.rb', line 73

def approved_manually!(approval)
  @manual_approval = approval
end

#approved_manually?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/license_finder/package.rb', line 77

def approved_manually?
  !@manual_approval.nil?
end

#decide_on_license(license) ⇒ Object



142
143
144
# File 'lib/license_finder/package.rb', line 142

def decide_on_license(license)
  @decided_licenses << license
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/license_finder/package.rb', line 115

def eql?(other)
  name == other.name && version == other.version
end

#hashObject



119
120
121
# File 'lib/license_finder/package.rb', line 119

def hash
  [name, version].hash
end

#license_filesObject



152
153
154
# File 'lib/license_finder/package.rb', line 152

def license_files
  LicenseFiles.find(install_path, logger: logger)
end

#licensesObject



128
129
130
# File 'lib/license_finder/package.rb', line 128

def licenses
  @licenses ||= activations.map(&:license).sort_by(&:name).to_set
end

#licenses_from_specObject



146
147
148
149
150
# File 'lib/license_finder/package.rb', line 146

def licenses_from_spec
  license_names_from_spec
    .map { |name| License.find_by_name(name) }
    .to_set
end

#licensingObject



138
139
140
# File 'lib/license_finder/package.rb', line 138

def licensing
  Licensing.new(self, @decided_licenses, licenses_from_spec, license_files)
end

#log_activation(activation) ⇒ Object



168
169
170
171
172
173
174
175
176
177
# File 'lib/license_finder/package.rb', line 168

def log_activation(activation)
  preamble = format('package %s:', activation.package.name)
  if activation.sources.empty?
    logger.debug activation.package.class, format('%s no licenses found', preamble)
  else
    activation.sources.each do |source|
      logger.debug activation.package.class, format("%s found license '%s' %s", preamble, activation.license.name, source)
    end
  end
end

#missing?Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/license_finder/package.rb', line 164

def missing?
  @missing
end

#notice_filesObject



156
157
158
# File 'lib/license_finder/package.rb', line 156

def notice_files
  NoticeFiles.find(install_path, logger: logger)
end

#package_managerObject



160
161
162
# File 'lib/license_finder/package.rb', line 160

def package_manager
  'unknown'
end

#permitted!Object



88
89
90
# File 'lib/license_finder/package.rb', line 88

def permitted!
  @permitted = true
end

#permitted?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/license_finder/package.rb', line 92

def permitted?
  @permitted
end

#restricted!Object



96
97
98
# File 'lib/license_finder/package.rb', line 96

def restricted!
  @restricted = true
end

#restricted?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/license_finder/package.rb', line 100

def restricted?
  @restricted
end