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.



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

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] || ''
  @children = options[:children] || []
  @parents = Set.new # will be figured out later by package manager
  @groups = options[:groups] || []

  ## APPROVAL
  @whitelisted = false
  @blacklisted = 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)

DESCRIPTION



61
62
63
# File 'lib/license_finder/package.rb', line 61

def authors
  @authors
end

#childrenObject (readonly)

DESCRIPTION



61
62
63
# File 'lib/license_finder/package.rb', line 61

def children
  @children
end

#descriptionObject (readonly)

DESCRIPTION



61
62
63
# File 'lib/license_finder/package.rb', line 61

def description
  @description
end

#groupsObject (readonly)

DESCRIPTION



61
62
63
# File 'lib/license_finder/package.rb', line 61

def groups
  @groups
end

#homepageObject (readonly)

DESCRIPTION



61
62
63
# File 'lib/license_finder/package.rb', line 61

def homepage
  @homepage
end

#install_pathObject (readonly)

checked in tests, otherwise private



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

def install_path
  @install_path
end

#license_names_from_specObject (readonly)

LICENSING



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

def license_names_from_spec
  @license_names_from_spec
end

#loggerObject (readonly)

Returns the value of attribute logger.



18
19
20
# File 'lib/license_finder/package.rb', line 18

def logger
  @logger
end

#manual_approvalObject (readonly)

Returns the value of attribute manual_approval.



98
99
100
# File 'lib/license_finder/package.rb', line 98

def manual_approval
  @manual_approval
end

#nameObject (readonly)

DESCRIPTION



61
62
63
# File 'lib/license_finder/package.rb', line 61

def name
  @name
end

#parentsObject (readonly)

DESCRIPTION



61
62
63
# File 'lib/license_finder/package.rb', line 61

def parents
  @parents
end

#summaryObject (readonly)

DESCRIPTION



61
62
63
# File 'lib/license_finder/package.rb', line 61

def summary
  @summary
end

#versionObject (readonly)

DESCRIPTION



61
62
63
# File 'lib/license_finder/package.rb', line 61

def version
  @version
end

Class Method Details

.license_names_from_standard_spec(spec) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/license_finder/package.rb', line 20

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



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

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

#activationsObject



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

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

#approved?Boolean

Returns:

  • (Boolean)


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

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

#approved_manually!(approval) ⇒ Object

APPROVAL



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

def approved_manually!(approval)
  @manual_approval = approval
end

#approved_manually?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/license_finder/package.rb', line 71

def approved_manually?
  !@manual_approval.nil?
end

#blacklisted!Object



90
91
92
# File 'lib/license_finder/package.rb', line 90

def blacklisted!
  @blacklisted = true
end

#blacklisted?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/license_finder/package.rb', line 94

def blacklisted?
  @blacklisted
end

#decide_on_license(license) ⇒ Object



135
136
137
# File 'lib/license_finder/package.rb', line 135

def decide_on_license(license)
  @decided_licenses << license
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#hashObject



112
113
114
# File 'lib/license_finder/package.rb', line 112

def hash
  [name, version].hash
end

#license_filesObject



145
146
147
# File 'lib/license_finder/package.rb', line 145

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

#licensesObject



121
122
123
# File 'lib/license_finder/package.rb', line 121

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

#licenses_from_specObject



139
140
141
142
143
# File 'lib/license_finder/package.rb', line 139

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

#licensingObject



131
132
133
# File 'lib/license_finder/package.rb', line 131

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

#log_activation(activation) ⇒ Object



157
158
159
160
161
162
163
164
165
166
# File 'lib/license_finder/package.rb', line 157

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)


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

def missing?
  @missing
end

#package_managerObject



149
150
151
# File 'lib/license_finder/package.rb', line 149

def package_manager
  'unknown'
end

#whitelisted!Object



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

def whitelisted!
  @whitelisted = true
end

#whitelisted?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/license_finder/package.rb', line 86

def whitelisted?
  @whitelisted
end